Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(java) Create an AddressBook class in Java for general use with the following behaviors: Constructor: public AddressBook Construct a new address book object. A contact

(java)

Create an AddressBook class in Java for general use with the following behaviors:

  1. Constructor: public AddressBook

Construct a new address book object.

  1. 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)
  2. The constructor reads from the disk to retrieve previously entered contacts. If previous contacts exist, the address book will be populated with those contacts retrieved from the disk. Otherwise, the address book will be empty.
  3. The constructor should notify the user if any disk I/O error occurs (IOException).

Note: Since the AddressBook class should not include the user interface, the way in which the user is notified should be a correspondingly thrown exception object. A System.out.println() is NOT appropriate for this method.

  1. Method: public void addContact(Contact newContact)

Add a new contact into the address book:

  1. Neither the first name nor the last name in newContact can be null or empty (NullPointerException and EmptyNameException, respectively).
  2. The method should notify the user if a contact with the same first name and last name is already in the address book (DuplicateContactException). Therefore, newContact will not be inserted.
  3. When a new contact is added into the address book, the content of the address book should be saved to the disk. The method should notify the user if any disk I/O error occurs (IOException). If a disk error does occur, the addition of the new contact should be cancelled, that is, even if the new contact was added into the address book, it should be removed from the address book upon an I/O error so that consistency is maintained between the contacts in memory and those on the disk.

3 Method: public Contact[] list()

Return all the contacts in the address book as an array of contacts.

-------------------

In your implementation, the contacts in the address book should be read from and written into a file named contacts.dat. When the address book is opened again, the contacts previously added should be loaded into the address book from the file.

Your code should provide specifications for all classes, constructors and methods based on the templates given in the lecture notes and the textbook (OVERVIEW, REQUIRES, MODIFIES, EFFECTS, etc.).

Note that the method headers provided above do not include any throws clause. If you decide a certain exception object may be thrown from the method, the header should be modified accordingly. Whenever an exception is needed, if there is no applicable built-in Java API exception, you need to define your own exception and decide whether the exception should be checked or unchecked.

Apparently, you need to define a record class called Contact with the following String fields: firstName, lastName, email and phone.

Hints

 
  1. Always write the specifications first. By completing a specification, you will be very clear on what to do. While writing the specification, be careful about the side effects of the methods, e.g., writing to the contact file. Pay attention to the use of exceptions and its interactions with the side effects.
 
  1. While you may choose either a Contact array or an ArrayList object as the rep for the AddressBook class, I recommend ArrayList since the number of the contacts is likely to change frequently.
 
  1. You may add private helper functions as needed.
 
  1. Since you are required to keep the contacts in a file (contacts.dat) on the disk, you may implement private helper functions that read and write the contacts in the file, respectively.
 
  1. Based on Hint 4, you may implement the following private instance variables and methods in your AddressBook class:
    1. private final String CONTACT_FILE_NAME = contacts.dat;
    2. private void readContacts()
      1. This method reads al the contacts from the file contacts.dat. You need to consider the possibiity that the file does not exist since this might be the first time that a user uses your program.
      2. Any IOException shoud be reported.
      3. Use a try-with-recourses statement to automaticaly close the file after reading.
      4. A good pace to call the method readContacts() is in the constructor of AddressBook, where al previous contacts should be loaded from contacts.dat.
 
  1. private void writeContacts()
    1. This method overwrites the fie contacts.dat with the current contacts in the address book.
    2. Any IOException shoud be reported.
    3. Use a try-with-recourses statement to automaticaly close the file after writing.
    4. The two good paces to call writeContacts() are in methods addContact and deeteContact so that insertion and deetion of contacts are promptly stored in the file.
 
  1. For efficiency purposes, you are recommended to make contacts.dat a binary file. If you choose to read and write Contact objects directly, you need to implement the Serializable Interface for the record class Contact.
 
  1. Be very careful in handling exceptions. Always think about and write down the specification first and then strictly follow the specification in the implementation. Note that there are several methods with side effects. It is important to indicate in a specification how side effects interact with exceptions.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

Students also viewed these Databases questions

Question

Who are the participants in securities lending?

Answered: 1 week ago