Problem 1: Address Book Using classes and inheritance, design an online address
ID: 3703418 • Letter: P
Question
Problem 1: Address Book Using classes and inheritance, design an online address book to keep track of the names, addresses, phone numbers and birthdays of family members, friends and business associates. Your program should be able to handle a maximum of 500 entries Define the following classes: Class Address to store a street name, city, state and zip code. .Class Date to store the day, month and year. .Class Person to store a person's last name and first name Class ExtPerson that extends the class Person and uses the classes Address and Date to store the person's address and date of birth. The class ExtPerson also stores the phone number and the person's status (e.g., family, friend or business). Class AddressBook to store a list of all your contacts' information using the ExtPerson class. Hint: Use an array of ExtPerson objects). The program should perform the following operations: Load the data into the address book from an input file. A sample input is given to you in the file "data.txt" Search for a person by last name. Print the information of a given person. Print the names of the persons whose birthdays are in a given month. Print the names of the persons with a particular status, eg. family, friend, business. Sort the address book by last name in ascending order. For all the above classes: Define the appropriate constructors to construct the objects and initialize the data members Define the toString() methods to print the appropriate information. Define the appropriate methods to perform the needed operations.Explanation / Answer
//1.
package person;
public class Person {
private String lastNmae;
private String firstName;
public String getLastNmae() {
return lastNmae;
}
public void setLastNmae(String lastNmae) {
this.lastNmae = lastNmae;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String lastNmae, String firstName) {
super();
this.lastNmae = lastNmae;
this.firstName = firstName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastNmae == null) ? 0 : lastNmae.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastNmae == null) {
if (other.lastNmae != null)
return false;
} else if (!lastNmae.equals(other.lastNmae))
return false;
return true;
}
@Override
public String toString() {
return "Person [lastNmae=" + lastNmae + ", firstName=" + firstName + "]";
}
}
//2.
package person;
public class Address {
private String streetName;
private String city;
private String state;
private String zipcode;
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
@Override
public String toString() {
return "Address [streetName=" + streetName + ", city=" + city + ", state=" + state + ", zipcode=" + zipcode
+ "]";
}
public Address(String streetName, String city, String state, String zipcode) {
super();
this.streetName = streetName;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}
public Address() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + ((streetName == null) ? 0 : streetName.hashCode());
result = prime * result + ((zipcode == null) ? 0 : zipcode.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
if (streetName == null) {
if (other.streetName != null)
return false;
} else if (!streetName.equals(other.streetName))
return false;
if (zipcode == null) {
if (other.zipcode != null)
return false;
} else if (!zipcode.equals(other.zipcode))
return false;
return true;
}
}
//3.
package person;
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
public Date() {
super();
// TODO Auto-generated constructor stub
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "Date [day=" + day + ", month=" + month + ", year=" + year + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Date other = (Date) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
}
//4.
package person;
public class ExtPerson extends Person{
private Address address;
private Date date;
private String phone;
private String personStatus;
public ExtPerson() {
super();
// TODO Auto-generated constructor stub
}
public ExtPerson(String lastNmae, String firstName) {
super(lastNmae, firstName);
// TODO Auto-generated constructor stub
}
public ExtPerson(Address address, Date date, String phone, String personStatus) {
super();
this.address = address;
this.date = date;
this.phone = phone;
this.personStatus = personStatus;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPersonStatus() {
return personStatus;
}
public void setPersonStatus(String personStatus) {
this.personStatus = personStatus;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((personStatus == null) ? 0 : personStatus.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ExtPerson other = (ExtPerson) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (personStatus == null) {
if (other.personStatus != null)
return false;
} else if (!personStatus.equals(other.personStatus))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
@Override
public String toString() {
return "ExtPerson [address=" + address + ", date=" + date + ", phone=" + phone + ", personStatus="
+ personStatus + "]";
}
}
//5.
package person;
import java.util.ArrayList;
public class AddressBook {
ArrayList<ExtPerson> extPersonList;
public ArrayList<ExtPerson> getExtPersonList() {
return extPersonList;
}
public void setExtPersonList(ArrayList<ExtPerson> extPersonList) {
this.extPersonList = extPersonList;
}
@Override
public String toString() {
return "AddressBook [extPersonList=" + extPersonList + "]";
}
}
//6. data.txt
Shelly Malik
9 8 2000
Lincoln Drive
Omaha
Nebraska
68131
402-555-1212
Family
Donald Duck
10 6 1980
Disney Street
Orlando
Florida
11234
622-873-8920
Friend
Brave Balto
2 6 1975
Disney Road
Orlando
Florida
35672
415-782-5555
Business
//7.
package person;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class MainTestClass{
public static void main(String[] args) {
ArrayList<ExtPerson> al=new ArrayList<>();
AddressBook abook=new AddressBook();
try {
File file = new File("data.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append(" ");
}
fileReader.close();
System.out.println("Contents of file:");
String[] strObj=stringBuffer.toString().split(" ");
for(int i=0;i<strObj.length;i=i+9){
Person p=new Person();
String[] persns=strObj[i].split(" ");
p.setFirstName(persns[0]);
p.setLastName(persns[1]);
Date d=new Date();
String[] dates=strObj[i+1].split(" ");
d.setDay(Integer.parseInt(dates[0]));
d.setMonth(Integer.parseInt(dates[1]));
d.setYear(Integer.parseInt(dates[2]));
Address adr=new Address();
adr.setStreetName(strObj[i+2]);
adr.setCity(strObj[i+3]);
adr.setState(strObj[i+4]);
adr.setZipcode(strObj[i+5]);
ExtPerson ep=new ExtPerson(p,adr, d, strObj[i+6], strObj[i+7]);
al.add(ep);
// System.out.println(p.toString()+" "+d.toString()+" "+adr.toString());
}
abook.setExtPersonList(al);
System.out.println(abook.getExtPersonList());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Welcome to the Address Book program:");
int k=0;
do{
System.out.println("1: To see if a person is in the address book "+
"2: Print the information of a person "+
"3: Print the names of persons having birthday in a particular month "+
"4: Print the names of persons having a particular status "+
"5: Print the address book "+
"6: Sort and print the sorted address book "+
"7: Terminate the program ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
k=n;
switch(n)
{
case 1:
System.out.println("Please enter person first & last name:");
String lname=sc.next();
String fname=sc.next();
System.out.println("You enter: "+fname+" "+lname);
int flag=0;
for(ExtPerson exp:abook.getExtPersonList()){
System.out.println(exp.toString());
System.out.println(exp.getFirstName());
if(fname.equals(exp.getFirstName()) && lname.equals(exp.getLastName())){
flag=1;
System.out.println("Person "+fname+" found in address book.");
break;
}
}
if(flag==0) System.out.println(fname+" Not found in address book");
break;
case 2:
System.out.println("Please enter person first & last name:");
String lname1=sc.next();
String fname1=sc.next();
for(ExtPerson exp:abook.getExtPersonList()){
if(fname1.equals(exp.getFirstName()) && lname1.equals(exp.getLastName())){
System.out.println(exp.getFirstName());
System.out.println(exp.getLastName());
System.out.println(exp.getAddress());
System.out.println(exp.getPhone());
System.out.println(exp.getPersonStatus());
break;
}
}
break;
case 3:
String[] p=new String[abook.getExtPersonList().size()];
int count=0;
for(int i=0;i< abook.getExtPersonList().size()-1;i++){
ExtPerson exp=abook.getExtPersonList().get(i);
if(exp.getDate().getMonth()==abook.getExtPersonList().get(i).getDate().getMonth()){
String s1=abook.getExtPersonList().get(i).getFirstName();
String s2=abook.getExtPersonList().get(i).getLastName();
String name=s1+" "+s2;
p[count]=name; count++;
}
}
System.out.println("Persons having birthday on same month: ");
for(int i=0;i<p.length;i++){
System.out.println(p[i]);
}
break;
case 4: System.out.println("Please enter status:");
String s1=sc.next();
ArrayList<String> prsn=new ArrayList<String>();
for(ExtPerson exp:abook.getExtPersonList()){
if(exp.getPersonStatus().equals(s1)){
String name=exp.getFirstName()+""+exp.getLastName();
prsn.add(name);
}
}
System.out.println("Persons having particular status:");
if(prsn.size()>0){
for(int i=0;i<prsn.size();i++){
System.out.println(prsn.get(i));
}
}else{
System.out.println("No persons have:");
}
break;
case 5:
System.out.println(abook.toString());
break;
case 6:
System.out.println("Sorting address book based on street name: ");
Address[] bookArray=new Address[abook.getExtPersonList().size()];
for(int i=0;i<abook.getExtPersonList().size();i++){
bookArray[i]=abook.getExtPersonList().get(i).getAddress();
}
Arrays.sort(bookArray,new AddressComparator());
case 7: System.exit(0);
default: System.out.println("Entered wrong choice:");
System.exit(0);
}
} while(k <= 7);
}
}
// 8.
package person;
import java.util.Comparator;
public class AddressComparator implements Comparator<Address> {
@Override
public int compare(Address o1, Address o2) {
return (int) (o1.getCity().compareTo(o2.getCity()));
}
}