QuestionQuestion

Create two classes, Contact and AddressBook.

Contact class represents an individual with following data fields:

First name, last name, address, city, state, zip, phone number, and email.

The Contact class should have the necessary constructors and setter/getter methods for these variables. It should also implement the toString() method which will return full contact information of the individual (i.e., Full name, address, phone number and email).

The AddressBook class represents an address book with multiple contacts. It will have an instance variable, which is an array of type Contact.(ArrayList can also be used)

The AddressBook class will have a constructor that accepts an integer parameter, which represents the size of the address book (i.e., number of contacts the address book can store).

The AddressBook should also have following methods:

public boolean importContacts (String fileName) : Reads contacts from a file specified in the parameter. You could assume that the file is in CSV format (i.e., comma separated). The order of fields in the file will be: first name, last name, address, city, state, zip, phone number, email. The method should return true if import is successful and false otherwise.

public boolean export (String outputFileName): Writes all contacts in the address book in a file specified in the parameter in CSV format. If the user-specified file already exists, the method will ask the user if it is OK to overwrite the contents of the file. The method will perform the export operation only if user confirms to overwrite the contents. If the file does not exist, no confirmation will be needed. The method should return true if export is successful and false otherwise.

public void search(String name) : Searches the contact(s) in the address book and displays contact(s) whose first name or last name exactly matches with the name specified in the parameter. The search will be case-insensitive. The method should display all information of the contacts including their position in the array.

public boolean add() : Adds a new contact to the address book. It should add the contact to the first empty position in the contact array. All the user information ( first name, last name, address, etc.) will be provided by the user via keyboard entry. The method should return true if the operation is successful or false otherwise. Add operation would fail only if the address book is full.

public boolean remove(int id) : Removes the contact in the user-specified position of the address book array. The method should display the full information of the contact and ask user if it is OK to remove this contact. It will remove the contact if the user confirms (by typing "yes"). The method should display an error message if the parameter is an invalid ID. The method should return true if the operation is successful or false otherwise.

public static void main(String[] args): Demonstrates the usage of the AddressBook class. Main method should get two parameters through args array, args[0] should hold the capacity of the address book object and args[1] should hold the file name of the address book file to import. The following piece of code should be in the beginning of the main method. The rest of the main method should test all the methods in the AddressBook class.

AddressBook myAddressBook;
if (args.length >= 1)
myAddressBook = new AddressBook(args[0]);
else
myAddressBook = new AddressBook(100);

if (args.length >= 2)
myAddressBook.importContacts(args[1]);

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

/*
* AddressBook class
*/
public class AddressBook {

    // Contact array
    private Contact[] contacts;
   
    // Constructor
    public AddressBook(int size) {
       this.contacts = new Contact[size];
    }
   
    // Constructor
    public AddressBook(String sizeString) {
       int size = Integer.parseInt(sizeString);
       this.contacts = new Contact[size];
    }
   
    // Private helper method to add Contact
    private boolean addContact(Contact contact) {
       for (int i = 0; i < contacts.length; i++) {
            if (contacts[i] == null) {
                contacts[i] = contact;
                return true;
            }
       }
       return false;
    }
   
    // Method to import Contacts from file
    public boolean importContacts(String fileName) {
       System.out.println("Importing contact data from " + fileName + "...");
       File file = new File(fileName);
       try {
            Scanner s = new Scanner(file);
            while (s.hasNextLine()) {
                String line = s.nextLine().trim();
                String[] params = line.split(",");
                if (params.length == 8) {
                   String firstName = params[0];
                   String lastName = params[1];
                   String address = params[2];
                   String city = params[3];
                   String state = params[4];
                   String zip = params[5];
                   String phone = params[6];
                   String email = params[7];
                   Contact contact = new Contact(firstName, lastName, address, city, state, zip, phone, email);
                   boolean result = addContact(contact);
                   if (result == false) {
                        return result;
                   }
                } else {
                   return false;
                }
            }
            s.close();
            System.out.println("Contact data imported successfully!");
            return true;
       } catch (FileNotFoundException e) {
            return false;
       }
    }
   
    // Method to export Contacts to file
    public boolean export(String outputFileName) {
       System.out.println("Exporting contact data to " + outputFileName + "...");
       File file = new File(outputFileName);
       if (file.exists()) {
            Scanner s = new Scanner(System...
$45.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats