Complete the AddressBook class. Create a constructor for the class, which initia
ID: 3813481 • Letter: C
Question
Complete the AddressBook class.
Create a constructor for the class, which initializes the contacts field to an empty list.
Complete the addPerson method, which adds a Person object to the contacts list, unless they have the same surname and first name as a person already in the list – in which case, the method should not add them, but should instead print the message “could not add person”.
Complete the findPerson method, which searches for a person in the contacts list using their first name and surname. If a Person object with the specified first name and surname is found in the list, the method should return it; if not, the method should return null.
import java.util.ArrayList;
/**
* Manage an AddressBook of Person contacts
*
* @author (name)
* @version (date)
*/
public class AddressBook {
private ArrayList<Person> contacts;
// replace this line with your code for a constructor
/** Add the person p to the "contacts" list, unless they have the same
* surname and first name as a person already in the list, in which case
* do not add them, but instead print the error message "could not add person".
*
*/
public void addPerson(Person p) {
// replace this line with your code
}
/** Search for a person in the "contacts" list by first name and surname,
* and return the relevant Person object if one matches, or otherwise return null.
*/
public Person findPerson(String firstName, String surname) {
return null; // replace this line with your code
}
/**
* Find the most social person in the address book.
*/
public Person findMostSocial() {
return null; //replace this line with your code for task 3 (if any) here
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
/* Person class handles the person details */
class Person
{
private String surname;
private String firstname;
private String lastname;
private String phone;
public Person(String sname, String fname, String lname, String phon)
{
surname=sname;
firstname=fname;
lastname=lname;
phone=phon;
}
public String getSurname()
{
return surname;
}
public String getFirstname()
{
return firstname;
}
}
/* Manage an AddressBook of Person contacts */
public class AddressBook
{
private ArrayList<Person> contacts;
public AddressBook()
{
contacts = new ArrayList<Person>();
}
public void addPerson(Person p)
{
String sn=p.getSurname();
String fn=p.getFirstname();
if(findPerson(sn,fn)==null)
{
contacts.add(p);
System.out.println(" ==Successfully added=====");
}
else
System.out.println(" ==Could not add the Person===");
}
/** Search for a person in the "contacts" list by first name and surname,
* and return the relevant Person object if one matches, or otherwise return null.
*/
public Person findPerson(String surname, String firstname)
{
Person found=null;
for (int i=0;i<contacts.size();i++)
{
Person temp=contacts.get(i);
String name1=temp.getFirstname();
String surname1=temp.getSurname();
if (firstname.equals(name1) && surname.equals(surname1)){
found=temp;
break;
}
}
return found;
}
public static void main(String args[])
{
Person p1=new Person("janapana","soban","reddy","9999999999");
Person p2=new Person("thamada","srinivas","rao","88888888");
Person p3=new Person("janapana","soban","reddy","9999999999");
AddressBook book=new AddressBook();
book.addPerson(p1);
book.addPerson(p2);
book.addPerson(p3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.