Create a JAVA program using classes, design an address book to keep track of the
ID: 3710997 • Letter: C
Question
Create a JAVA program using classes, design an address book to keep track of the names,
addresses, phone numbers, and birthdays of family members, close friends,
and certain business associates. Your program should be able to handle a
maximum of 500 entries.
a. Define the class Address that can store a street address, city, state, and
zip code. Use the appropriate methods to print and store the address.
Also, use constructors to automatically initialize the data members.
b. Define the class ExtPerson using the class Person , the class Date, and the class Address. Add a data member
to this class to classify the person as a family member, friend, or business
associate. Also, add a data member to store the phone number. Add
(or override) methods to print and store the appropriate information.
Use constructors to automatically initialize the data members.
c. Define the class AddressBook using previously defined classes. An object
of type AddressBook should be able to process a maximum of 500 entries.
The program should perform the following operations:
i. Load the data into the address book from a .txt file.
ii. Sort the address book by last name.
iii. Search for a person by last name.
iv. Print the address, phone number, and date of birth (if available) of a
given person.
v. Print the names of the people whose birthdays are in a given month or
between two given dates.
vi. Print the names of all the people between two last names.
Explanation / Answer
//Aplication
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Address
{
private String street,city,state,zipcode;
public Address()
{
street="";
city="";
state="";
zipcode="";
}
public Address(String street,String city,String state, String zipcode)
{
this.street=street;
this.city = city;
this.state=state;
this.zipcode=zipcode;
}
public void showAddress()
{
System.out.println("Street : "+street);
System.out.println("City : "+city);
System.out.println("State : "+state);
System.out.println("Zipcode : "+zipcode);
}
}
class Date
{
private String dob;
public Date()
{
}
public Date(String dob)
{
this.dob=dob;
}
public void setdob()
{
this.dob=dob;
}
public String getdob()
{
return this.dob;
}
public void showDate()
{
System.out.println("Date Of birth : "+this.dob);
}
}
class Person extends Address
{
private String firstName;
private String lastName;
private String type;
public Date dobj;
public Person()
{
}
public Person(String fistName,String lastName,String type,String dob,String street,String city,String state, String zipcode)
{
super(street,city,state,zipcode);
dobj=new Date(dob);
this.firstName = firstName;
this.lastName = lastName;
this.type =type;
}
public String getFirstName()
{
return this.firstName;
}
public String getLastName()
{
return this.lastName;
}
public String getPersonType()
{
return this.type;
}
public void showPerson()
{
System.out.println("FirstName : "+ this.firstName);
System.out.println("LastName : "+ lastName);
System.out.println("Type : "+ type);
}
}
class ExtPerson extends Person
{
public Address obj;
public Date dobj;
private String phoneno;
public ExtPerson()
{
}
public ExtPerson(String phoneno,String fname,String lname,String ptype,String dob,String street,String city,String state, String zipcode)
// public ExtPerson(String phoneno,String fname,String lname,String ptype,String dob)
{
super(fname,lname,ptype,dob,street,city,state,zipcode);
this.phoneno = phoneno;
}
public void addPhoneNumber(String phoneno)
{
this.phoneno = phoneno;
}
}
class AddressBook
{
public ExtPerson[] contents;
int addcount;
private static File file = new File("Addresses.txt");
public AddressBook()
{
addcount=0;
}
public void readAddressFile() {
String phoneno,fname,lname, ptype, dob,street, city, state, zipcode;
try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
String name = null;
while((name = reader.readLine()) != null) {
if(addcount > 500)
{
System.out.println("Records are not allowed more than 500");
break;
}
contents = new ExtPerson[addcount];
fname=reader.readLine();
lname=reader.readLine();
ptype=reader.readLine();
phoneno=reader.readLine();
dob=reader.readLine();
street=reader.readLine();
city=reader.readLine();
state=reader.readLine();
zipcode=reader.readLine();
ExtPerson person = new ExtPerson(phoneno,fname,lname,ptype,dob,street,city,state,zipcode);
contents[addcount]= person; //adds person to the list
addcount++;
}
}
catch ( IOException e) {
System.out.println(e);
}
}
public void lastnames() {
Scanner in = new Scanner(System.in);
System.out.print("Enter surname: ");
String surnameToFind = in.nextLine();
int matches = 0;
int scount = getEntries() ;
System.out.println("Last name 1 : "+contents[scount -1].getLastName());
System.out.println("Last name 1 : "+contents[scount -2].getLastName());
}
// to display address,name,dob
public void show(ExtPerson obj) //
{
obj.showPerson();
obj.showAddress();
obj.dobj.showDate();
}
public void searchBySurname() {
Scanner in = new Scanner(System.in);
System.out.print("Enter surname: ");
String surnameToFind = in.nextLine();
int matches = 0;
for(int i=0;i<getEntries();i++) {
if(contents[i].getLastName().equals(surnameToFind)) {
show(contents[i]);
matches++;
}
}
if(matches<=0) {
System.out.println("There is no person with this surname");
}
}
public int getEntries(){
return contents.length;
}
}
public class HelloWorld{
public static void main(String []args){
AddressBook obj=new AddressBook();
obj.readAddressFile(); //load file entires upto 500
obj.searchBySurname(); //search file name surname
obj.lastnames() ; //last 2 names
// we can show person details base person object
}
}