我有一个ArrayList
来保存联系人。创建时,用户会为每个联系人分配ID
。如何引用ID
来打印联系人的详细信息。例如:
Contact ID = 1
。我想从1
拨打arrayList
并打印Jone Smith,123 West St等。
以下是我创建ArrayList
:
主类:
import java.util.ArrayList;
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
ArrayList<Contact> contacts = new ArrayList<Contact>();
Scanner input1 = new Scanner(System.in);
int type = 0;
while(type != 4){
System.out.println("Please select an option:");
System.out.println("Add a Personal Contact: Enter 1");
System.out.println("Add a Business Contact: Enter 2");
System.out.println("Display Contacts List: Enter 3");
System.out.println("4 to quit");
type = input1.nextInt();
if(type == 4){
System.out.println("Goodbye");
break;
}
if (type==1 || type==2){
Contact contact = null;
//ArrayList<Contact> contacts = new ArrayList<Contact>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter ContactId : ");
String contactId = input.nextLine();
System.out.println("Please enter First Name : ");
String firstName = input.nextLine();
System.out.println("Please enter Last Name : ");
String lastName = input.nextLine();
System.out.println("Please enter Address : ");
String address = input.nextLine();
System.out.println("Please enter Phone Number : ");
String phoneNumber = input.nextLine();
System.out.println("Please enter Email Address : ");
String emailAddress = input.nextLine();
if(type == 1){
System.out.println("Please enter Birthday: ");
String dateofBirth = input.nextLine();
Contact pcontact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
contacts.add(pcontact);
for (Contact showcontact: contacts){
System.out.println(showcontact.displayContact());}
}
else if(type == 2){
System.out.println("Please enter Job Title: ");
String jobTitle = input.nextLine();
System.out.println("Please enter Organization: ");
String organization = input.nextLine();
Contact bcontact = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
contacts.add(bcontact);
for (Contact showcontact: contacts){
System.out.println(showcontact.displayContact());}
}
}
if(type == 3){
//System.out.println(contacts);
for (Contact showcontact: contacts){
System.out.println(showcontact.displayFullName());}
}
}
}
}
父类:
public abstract class Contact {
String contactId;
String firstName;
String lastName;
String address;
String phoneNumber;
String emailAddress;
public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
{
this.contactId = contactId;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
this.emailAddress = emailAddress;
}
public void setContactId(String input){
this.contactId = input;
}
public String getContactId(){
return contactId;
}
public void setFirstName(String input){
this.firstName = input;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String input){
this.lastName = input;
}
public String getLastName(){
return lastName;
}
public void setAddress(String input){
this.address = input;
}
public String getAddress(){
return address;
}
public void setPhoneNumber(String input){
this.phoneNumber = input;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setEmailAddress(String input){
this.emailAddress = input;
}
public String getEmailAddress(){
return emailAddress;
}
@Override
public String toString(){
return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress: " + this.getAddress() + "\nPhone Number: " + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
}
public String displayFullName(){
System.out.println("Contact List:");
return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName());
}
public String displayContact(){
return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress :" + this.getAddress() + "\nPhone Number :" + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
}
}
子类是个人和商业,他们只是覆盖一些父类,所以不知道他们对这个问题感兴趣。如果需要,我可以发布它们。
答案 0 :(得分:1)
我认为在这里使用Map
可能更容易。这是一个由键值对组成的数据结构。您可以将您的ID用作密钥,将联系人用作值。一个例子:
Map<String, Contact> contacts = new HashMap<String, Contact>();
contacts.put("anId", aContact);
contacts.put("anotherId", anotherContact);
...
contacts.get("anId"); // returns aContact
迭代它进行打印可以这样做:
for(String contactId : contacts.keySet(){
System.out.println(contacts.get(contactId).displayFullName());}
}
答案 1 :(得分:0)
我还建议您使用Map<String, Contact>
,但万一您不想这样做,以下是如何使用您的代码:
System.out.println("Please enter ID of contact: ");
String soughtId = input.nextLine();
for (Contact showcontact: contacts)
{
if (showcontact.getContactId().equals(soughtId))
System.out.println(showcontact.displayContact());
}