im работает над школьным проектом. Мы должны создать список контактов.Infinte объекты в Arraylist (Java)
Итак, я создал класс, генерирующий объекты вроде.
package coursework_q2;
public class Agenda {
private String name;
private long phoneNumber;
private String email;
private String type;
public Agenda(){
name = "";
phoneNumber = 0;
email = "";
type = "";
}//end of constructor
@Override
public String toString() {
return "\nName: " + this.getName() +
", Number: " + this.getPhoneNumber() +
", Email: " + this.getEmail() +
", Type: " + this.getType();
}
//setters
public void setName(String n){
name = n;
}//end of setName
public void setPhoneNumber(long n){
phoneNumber = n;
}//end of setPhoneNumber
public void setEmail(String e){
email = e;
}
public void setType(int t){
if (t==1){
type = "Personal";
}else{
type = "Business";
}
}//end of setType
//getters
public String getName(){
return name;
}//end of getName
public long getPhoneNumber(){
return phoneNumber;
}//end of getPhoneNumber
public String getEmail(){
return email;
}//end of getEmail
public String getType(){
return type;
}//end of getType
}//end of class
И в моем другом классе я храню их в arraylist
.
Моя проблема в том, что я не знаю, как создать столько контактов, сколько захочет пользователь.
Например:
package testcourse;
импорта java.util.ArrayList; импорт java.util.Scanner;
общественного класса CourseWork_Q2 {
public static void main(String[] args) {
ArrayList<Agenda> contacts = new ArrayList();
Scanner in = new Scanner(System.in);
int check;
String contact;
String search;
Agenda a;
a = new Agenda();
int select=0;
int b=0;
do {
System.out.println("1. Add Contact\n2. Edit Contact\n3. Delete Contact\n4. Display All Contacts\n5. Quit");
System.out.print("Please use the numbers 1-5 to choose an option: ");
do{//input validation
check=0;
if(in.hasNextInt()){
select = in.nextInt();
if(1<=select && 5>=select){
check=1;
}else{
System.out.println("Invalid Input!\nPlease use the numbers 1-5 to choose an option: ");
System.out.println("1. Add Contact\n2. Edit Contact\n3. Delete Contact\n4. Display All Contacts\n5. Quit");
}//end of if
}else{
System.out.println("Invalid Input!\nPlease use the numbers 1-5 to choose an option: ");
System.out.println("1. Add Contact\n2. Edit Contact\n3. Delete Contact\n4. Display All Contacts\n5. Quit");
in.next();
}//end of if
}while(check==0);
switch(select){
case 1: System.out.println("Add Contact");
System.out.print("Please enter your contact name: ");
a.setName(in.next());
System.out.print("\nPlease enter your contact phone-number: ");
a.setPhoneNumber(in.nextLong());
System.out.print("\nPlease enter your contact email: ");
a.setEmail(in.next());
System.out.print("\nPlease choose your contact type: ");
System.out.println("\n1. Personal\n2. Business");
a.setType(in.nextInt());
contacts.add(a);
break;
case 2: System.out.println("Edit Contact");
System.out.println("Please enter the name of the contact you wish to edit: ");
search = in.next();
for(int i=0; i<contacts.size(); i++){
contact = (contacts.get(i)).toString();
if(contact.contains("Name: "+search)){
System.out.println(contacts.get(i));
System.out.println("Please edit the name");
a.setName(in.next());
}
}
break;
case 3: System.out.println("Delete Contact");
break;
case 4: System.out.println("Display All Contact");
for (int i=0; i<contacts.size(); i++){
System.out.println(contacts.get(i));
}
break;
case 5:
System.exit(5);
break;
}//end of switch
}while (!(check==5));
}//end of main
} // конец класса
и так далее ..
Как я могу сделать это автоматически с моим кодом? Таким образом, пользователь может создать столько контактов, сколько захочет?
Спасибо!
Используйте цикл, подобный 'while'. –
Да, но я не могу изменить переменную a1 на a2 и так далее. В результате, когда пользователи хотят редактировать, можно сказать, что 5-й контакт все они меняются на отредактированный. – iliasbrinos
Просьба показать ВСЕ соответствующий код. Как вопрос стоит, неясно. – OldProgrammer