Reputation: 27
I have an ArrayList
that holds contacts. Each contact is assigned an ID
by the user when created. How do I reference that ID
to print out the details of the contact. For example:
Contact ID = 1
. I want to call 1
from the arrayList
and print Jone Smith, 123 West St etc..
Here is how I am creating the ArrayList
:
Main Class:
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());}
}
}
}
}
Parent Class:
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());
}
}
Subclasses are Personal and Business they just override some of the parent class so don't know that they are of interest to this issue. If wanted I can post them though.
Upvotes: 0
Views: 2654
Reputation: 627
I would also recommend using a Map<String, Contact>
, but just in case you don't want to do that, here is how to do it with your code:
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());
}
Upvotes: 0
Reputation: 3658
I think it might be easier to use a Map
here. This is a datastructure that consists of key-value pairs. You can use your id as a key and the contact as a value. An example:
Map<String, Contact> contacts = new HashMap<String, Contact>();
contacts.put("anId", aContact);
contacts.put("anotherId", anotherContact);
...
contacts.get("anId"); // returns aContact
Iterating over it to print can be done like this:
for(String contactId : contacts.keySet(){
System.out.println(contacts.get(contactId).displayFullName());}
}
Upvotes: 1