The problem Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList.
Breaking it down public class PhoneBookEntry {
public PhoneBookEntry ( String name , String phoneNumber ) {
super ();
this . name = name ;
this . phoneNumber = phoneNumber ;
}
private String name ;
private String phoneNumber ;
public String getName () {
return name ;
}
public void setName ( String name ) {
this . name = name ;
}
public String getPhoneNumber () {
return phoneNumber ;
}
public void setPhoneNumber ( String phoneNumber ) {
this . phoneNumber = phoneNumber ;
}
@Override
public String toString () {
return "PhoneBookEntry [name=" + name + ", phoneNumber="
+ phoneNumber + "]" ;
}
}
public static void main ( String [] args ) {
PhoneBookArrayList bookArrayList = new PhoneBookArrayList ();
PhoneBookEntry entry1 = bookArrayList . new PhoneBookEntry ( "Jack" , "920-456-2345" );
PhoneBookEntry entry2 = bookArrayList . new PhoneBookEntry ( "Sam" , "868-344-2345" );
PhoneBookEntry entry3 = bookArrayList . new PhoneBookEntry ( "George" , "414-234-2345" );
PhoneBookEntry entry4 = bookArrayList . new PhoneBookEntry ( "Dimo" , "608-049-2345" );
PhoneBookEntry entry5 = bookArrayList . new PhoneBookEntry ( "Jenny" , "971-456-2345" );
List < PhoneBookEntry > phoneNumberEntries = new ArrayList <>();
phoneNumberEntries . add ( entry1 );
phoneNumberEntries . add ( entry2 );
phoneNumberEntries . add ( entry3 );
phoneNumberEntries . add ( entry4 );
phoneNumberEntries . add ( entry5 );
phoneNumberEntries . forEach ( number -> System . out . println ( number ));
}
Output PhoneBookEntry [name=Jack, phoneNumber=920-456-2345]
PhoneBookEntry [name=Sam, phoneNumber=868-344-2345]
PhoneBookEntry [name=George, phoneNumber=414-234-2345]
PhoneBookEntry [name=Dimo, phoneNumber=608-049-2345]
PhoneBookEntry [name=Jenny, phoneNumber=971-456-2345]
Phone book arraylist posted by Justin Musgrove on 09 June 2014
Tagged: java and java-exercises-beginner
Share on: Facebook Google+