Question

(JAVA) Using classes and inheritance, design an online address book to keep track of the names

(JAVA)

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

  • date.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

    Bash Bashfull
    2 8 1965
    Long Road
    New York
    New York
    01101
    212-782-8000
    FriendWelcome to the address book program Choose among the folloxing options: 1: To see if a person is in the address book 2: Print


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Address.java

/** Address class */
public class Address
{
/** private member variables of the class */
private String streetname;
private String city;
private String state;
private String zipcode;
  
/** parameterized constructor */
public Address(String n, String c, String s,String z){
streetname = n;
city = c;
state = s;
zipcode = z;
}
  
/** public getters for the member variables */
public String getStreetName(){
return streetname;
}
  
public String getCity(){
return city;
}
  
public String getState(){
return state;
}
  
public String getZipcode(){
return zipcode;
}
  
/** toString() method is overridden to return the full address when
this object gets printed*/
@Override
public String toString(){
String s = streetname + "\n";
s = s + city + ", " + state + " - " + zipcode;
return s;
}
}

=======================================================================================

Date.java


/** Date class */
public class Date
{
/** private member variables of the class */
private int day;
private int month;
private int year;
  
/** parameterised constructor */
public Date(int d, int m, int y){
day = d;
month = m;
year = y;
}
  
/** public getters for the member variables */
public int getDay(){
return day;
}
  
public int getMonth(){
return month;
}
  
public int getYear(){
return year;
}

/** toString() method is overridden to return the date of birth */
@Override
public String toString(){
String s = "Date of Birth: " + month + "-" + day + "-" + year;
return s;
}
  
}

====================================================================================

Person.java

/** Person class */
public class Person
{
private String firstname;
private String lastname;
  
public Person(String f, String l){
firstname = f;
lastname = l;
}
  
public String getFirstname(){
return firstname;
}
  
public String getLastname(){
return lastname;
}

}

=======================================================================================

ExtPerson.java


/** Class ExtPerson , a derived class of Person class */
public class ExtPerson extends Person
{
/** Member variables of ExtPerson class */
Address address;
Date dateofbirth;
String phonenumber;
String status;
  
/** parameterized constructor */
public ExtPerson(Address a, Date d, String p, String fn, String ln, String s){
super(fn,ln);
address = a;
dateofbirth = d;
phonenumber = p;
status = s;
}
  
/** getter methods for phonenumber and status */
String getPhonenumber(){
return phonenumber;
}
  
String getStatus(){
return status;
}
  
/** toString method is overridden to display the complete details of a person */
@Override
public String toString(){
String s = this.getFirstname() + " " + this.getLastname() + "\n";
s = s + dateofbirth.toString() + "\n";
s = s + "Phone Number: " + this.phonenumber + "\n";
s = s + "Person Type: " + this.status + "\n";
s = s + address.toString() + "\n";
return s;
}
}

============================================================================================

AddressBook.java

import java.util.*;
import java.io.*;

/** class Addressbook */
class AddressBook
{
/** Array of objects of type ExtPerson class */
ExtPerson person[] = new ExtPerson[500];
/** variable to know the exact number of ExtPerson objects created */
int index = -1;
  
/** method to load the address book from the file and add it to
* the person array
*/
public void loadAddressBook() throws IOException{
  
String line;
String blankline;
/** scanner object reads from the file */
Scanner sc = new Scanner(new File("data.txt"));
  
/** read every line from the line */
while(sc.hasNextLine()){
/** read the first line as a string and split it into
* firstname and lastname
*/
line = sc.nextLine();
String firstname = line.split(" ")[0];
String lastname = line.split(" ")[1];
  
/** read the second line as a string and split it into
* month day and year based on a delimiteer space.
* split method splits a whole string into substrings based on
* a delimiter and stores all the substrings in an array
*/
line = sc.nextLine();
int month = Integer.parseInt(line.split(" ")[0]);
int day = Integer.parseInt(line.split(" ")[1]);
int year = Integer.parseInt(line.split(" ")[2]);
/** create date object */
Date d = new Date(day, month, year);
  
/** read the subsequent lines for address and create address object */
String streetname = sc.nextLine();
String city = sc.nextLine();
String state = sc.nextLine();
String zipcode = sc.nextLine();
Address addr = new Address(streetname, city, state, zipcode);
  
/** read phonenumber and status and create an object of ExtPerson with
* all the details read from the file.
*/
String phonenumber = sc.nextLine();
String status = sc.nextLine();
ExtPerson ext = new ExtPerson(addr,d,phonenumber,firstname, lastname, status);
  
/** ignore the blank line after each record that is read from the line */
/** this blank line wont appear after last record and hence the below if statement
* is used to check if a line is there to be ignored.
*/
if(sc.hasNextLine())
blankline = sc.nextLine();
/** increment the index value and store the ExtPerson object to array */
index++;
person[index] = ext;
  
}
}
  
/** method to navigate through the array and display every record in an array location */
/** when the extperson object is printed,it internally calls the toString() method and print
* the details in required format
*/
public void displayAddressbook(){
for(int i=0;i<=index;i++){
ExtPerson extperson = person[i];
System.out.println(extperson);
}
}
  
/** method to search a record by its lastname and return true or false */
public boolean searchByLastname(String ln){
boolean found = false;
for(int i=0;i<=index;i++){
if(person[i].getLastname().equalsIgnoreCase(ln)){
found = true;
break;
}
}
return found;
}
  
/** method to search a record by its lastname and print the record */
public void printPersonByLastname(String ln){
for(int i=0;i<=index;i++){
System.out.println(person[i].getLastname());
if(person[i].getLastname().equalsIgnoreCase(ln)){
System.out.println(person[i]);
break;
}
}
}
  
/** method to search records by its month and print the record */
public void printPersonByBirthMonth(int month){
for(int i=0;i<=index;i++){
if(person[i].dateofbirth.getMonth() == month){
System.out.println(person[i].getFirstname() + " " + person[i].getLastname());
}
}
}
  
/** method to search records by person type and print the record */
public void printPersonByPersonType(String type){
for(int i=0;i<=index;i++){
if(person[i].status.equals(type)){
System.out.println(person[i].getFirstname() + " " + person[i].getLastname());
}
}
}
  
/** method to sort the address book */
public void sortAddressBook(){
int n = index;
for (int i = 0; i <= n-1; i++)
for (int j = 0; j <= n-i-1; j++)
if (person[j].getLastname().compareTo(person[j+1].getLastname())>0)
{
ExtPerson temp = person[j];
person[j] = person[j+1];
person[j+1] = temp;
}
}

/** main driver method to call all the above methods based on chosen menu option */
public static void main(String[] args) throws IOException
{
  
int choice;
Scanner sc;
AddressBook addressbook = new AddressBook();
addressbook.loadAddressBook();
String ln;
  
do
{
sc = new Scanner(System.in);
System.out.println();
System.out.println("Welcome to the address book program.");
System.out.println("Choose among the following options:");
System.out.println("1: To see if a person is in the address book");
System.out.println("2: Print the information of a person");
System.out.println("3: Print the names of persons having birthday in a particular month");
System.out.println("4: Print the names of persons having particular status");
System.out.println("5: Print the address book");
System.out.println("6: Sort and print the sorted address book");
System.out.println("7: Terminate the program");
  
System.out.print("Enter your choice :: ");
choice = sc.nextInt();
  

switch(choice)
{
case 1:
  
System.out.print("Enter the lastname of the person :: ");
ln= sc.next();
boolean found = addressbook.searchByLastname(ln);
if(found)
System.out.println(ln + " is in the address book");
else
System.out.println(ln + " is not in the address book");
break;
case 2:
System.out.print("Enter the lastname of the person :: ");
ln= sc.next();
System.out.println();
addressbook.printPersonByLastname(ln);
break;
case 3:
System.out.print("Enter the month number :: ");
int month= sc.nextInt();
System.out.println();
addressbook.printPersonByBirthMonth(month);
break;
case 4:
System.out.print("Enter person type Family, Friend, Business :: ");
String type= sc.next();
System.out.println();
addressbook.printPersonByPersonType(type);
break;
case 5:
System.out.println("Address Book");
System.out.println("************");
addressbook.displayAddressbook();
break;
case 6:
addressbook.sortAddressBook();
System.out.println("Sorted Address Book");
System.out.println("*******************");
addressbook.displayAddressbook();
break;
case 7:
System.out.println("End of Program !!!");
break;
default:
System.out.println("Enter valid choice");
  
}
sc.close();
}while(choice!=7);
}
}

===========================================================================================

Sample output:

Add a comment
Know the answer?
Add Answer to:
(JAVA) Using classes and inheritance, design an online address book to keep track of the names
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in C++: Using classes, design an online address book to keep track of the names f...

    Write a program in C++: Using classes, design an online address book to keep track of the names first and last, addresses, phone numbers, and dates of birth. The menu driven program should perform the following operations: Load the data into the address book from a file Write the data in the address book to a file Search for a person by last name or phone number (one function to do both) Add a new entry to the address book...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • Requirements Create an Address Book class in Java for general use with the following behaviors: 1....

    Requirements Create an Address Book class in Java for general use with the following behaviors: 1. Constructor: public Address Book Construct a new address book object. • A contact has four fields: first name, last name, email and phone. (There could be more information for a real contact. But these are sufficient for the assignment.) . The constructor reads from the disk to retrieve previously entered contacts. If previous contacts exist, the address book will be populated with those contacts...

  • Student.h Student.cpp Person.h Person.cpp In this assignment, you need to create a class to keep track...

    Student.h Student.cpp Person.h Person.cpp In this assignment, you need to create a class to keep track of books borrowed bya student. Each book has a name, Student (who borrowed it), and the date that was borrowed (see the .h file below). Use also need to use the Person class and the Student class included in the folder. Student class inherits from the Person class as was shown in the classroom Create a Date class (in the same project of the...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

  • language:python VELYIEW Design a program that you can use to keep information on your family or...

    language:python VELYIEW Design a program that you can use to keep information on your family or friends. You may view the video demonstration of the program located in this module to see how the program should function. Instructions Your program should have the following classes: Base Class - Contact (First Name, Last Name, and Phone Number) Sub Class - Family (First Name, Last Name, Phone Number, and Relationship le. cousin, uncle, aunt, etc.) Sub Class - Friends (First Name, Last...

  • Please implement the following problem in basic C++ code and include detailed comments so that I...

    Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance. // personType.h #include <string> using namespace std; class personType { public: virtual void print() const; void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); protected: string firstName; string lastName; }; // personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT