Question
(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:
- Constructor: public AddressBook
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)
- 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.
- 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.
- Method: public void addContact(Contact newContact)
Add a new contact into the address book:
- Neither the first name nor the last name in newContact can be null or empty (NullPointerException and EmptyNameException, respectively).
- 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.
- 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
- 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.
- 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.
- You may add private helper functions as needed.
- 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.
- Based on Hint 4, you may implement the following private instance variables and methods in your AddressBook class:
- private final String CONTACT_FILE_NAME = contacts.dat;
- private void readContacts()
- 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.
- Any IOException shoud be reported.
- Use a try-with-recourses statement to automaticaly close the file after reading.
- A good pace to call the method readContacts() is in the constructor of AddressBook, where al previous contacts should be loaded from contacts.dat.
- private void writeContacts()
- This method overwrites the fie contacts.dat with the current contacts in the address book.
- Any IOException shoud be reported.
- Use a try-with-recourses statement to automaticaly close the file after writing.
- 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.
- 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.
- 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started