Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Address Book Using classes and inheritance, design an online address

ID: 3702861 • 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

Solution:

code:

AddressBook.java:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;

class Person {
   String firstName, lastName, address, phone;
   LocalDate birthDate;

   public Person(String firstName, String lastName, String address,
           String phone, LocalDate birthDate) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.address = address;
       this.phone = phone;
       this.birthDate = birthDate;
   }

   @Override
   public String toString() {
       return "Person [firstName=" + firstName + ", lastName=" + lastName
               + ", address=" + address + ", phone=" + phone + ", birthDate="
               + birthDate + "]";
   }
}

public class AddressBook {
   ArrayList<Person> listOfPersons;

   public AddressBook(String fileName) throws IOException {
       this.listOfPersons = new ArrayList<>();
       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

       formatter = formatter.withLocale(Locale.US);

       // read data using reader
       BufferedReader reader = new BufferedReader(new FileReader(fileName));
       String line;

       // while file has lines
       while ((line = reader.readLine()) != null) {
           // split token using one or more spaces
           String tokens[] = line.split(",");
           if (tokens.length == 5) {
               listOfPersons.add(new Person(tokens[0].trim(), tokens[1].trim(), tokens[2].trim(),
                       tokens[3].trim(), LocalDate.parse(tokens[4].trim(), formatter)));
           }
       }
       System.out.println("File has been read successfully.");
       // close reader
       reader.close();
   }

   public void sortByLastName() {
       Collections.sort(listOfPersons, new Comparator<Person>() {
           @Override
           public int compare(Person p1, Person p2) {
               return p1.lastName.compareTo(p2.lastName);
           }
       });
   }

   public Person searchByLastName(String lastName) {
       for (Person p : listOfPersons) {
           if (p.lastName.equalsIgnoreCase(lastName))
               return p;
       }
       return null;
   }

   public void birthDaysInCurrentMonth() {
       System.out.println("Birthdays falling in current Month:");
      
       for (Person p : listOfPersons) {
           if (p.birthDate.getMonth().equals(LocalDate.now().getMonth())) {
               System.out.println(p);
           }
       }
   }

   public void personsBetweenTwoLastNames(String last1, String last2) {
       System.out.println("Persons betweeen " + last1 + " & " + last2 + " :");
       for (Person p : listOfPersons) {
           if (p.lastName.compareTo(last1) >= 0
                   && p.lastName.compareTo(last2) <= 0)
               System.out.println(p);
       }
   }

   public void printAllInformation() {
       System.out.println("Person in AddressBook: ");
       for (Person p : listOfPersons) {
           System.out.println(p);
       }
   }

   public static void main(String[] args) throws IOException {
       AddressBook addressBook = new AddressBook("addressBook.txt");
       addressBook.printAllInformation();

       System.out.println(" ");
       addressBook.birthDaysInCurrentMonth();
      
       System.out.println(" ");
       addressBook.personsBetweenTwoLastNames("last1", "last3");
      
       System.out.println(" Searching for lastname: last4");
       System.out.println(addressBook.searchByLastName("last4"));
   }

}




addressBook.txt:
first1, last1, address1, phone1, 10-05-2017
first4, last4, address4, phone4, 13-04-2014
first2, last2, address2, phone2, 11-04-2016
first3, last3, address3, phone3, 12-10-2015
first5, last5, address5, phone5, 14-12-2013

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)