JAVA program In this assignment, you will design a class called AddressBook that
ID: 3736194 • Letter: J
Question
JAVA program
In this assignment, you will design a class called AddressBook that uses a Linked List to store nodes that are made up of Contacts. You must adapt the Node class , and adapt and add to the Linked List class(You cannot use java.util.LinkedList for this assignment).
1. Start by designing the Contact class that holds a person’s last name, first name, street name, and phone number:
public class Contact{
private String lastName;
private String firstName;
private String streetName;
private String phone;
//complete the rest of the class by adding appropriate constructors, get, set methods, toString, etc.
}
2. Re-design the Node class to hold Contact as its data.
3. Re-design the Linked List class so that the methods will work with Nodes with Contacts as its data.
4. Finally, you will create the AddressBook class. It has a LinkedList and methods to add a contact, display all contacts, search for a specific contact and display it, or search and delete a specific contact.
AddressBook:
When a contact is added, it should be added in order alphabetically to the list by last name (and then by first name if there are multiple contacts with the same last name). To add alphabetically look at the compareTo String method which compares Strings lexicographically (returns an int >0, <0, or 0 which indicates order).
Displaying all the contacts should be formatted to show results lined up (e.g., in columns). See the example output.
Search should let users search on name, address or phone number. Search will find any contact where any associated instance variable contains the target search string, and prints all matches to the console. For example, if "elmore" is the search target and name is the search category, then any contact where elmore is the first name or last name should be displayed. Similarly, if "1234" is the search target and phone is the search category, all contacts with 1234 as part of their phone number should be displayed. As well, if the user searches the Street "Robie", it should return all the contacts that live on Robie Street.
The delete method should allow the user to either choose a contact to delete from the full address book or from a search on name, address or phone number (e.g., enters Robie to get a list of all contacts who live on Robie). The user will be presented with a numbered list of contacts (either with all contacts from the address book or from the search). Then the user will select the contact from the list to be deleted.
Note: you will need to deal with the situation where a user enters in a Contact with all the same information as an existing Contact. In this case, you could not allow duplicates to be added or when a user deletes a contact that has duplicate entries you could delete them all. Make sure you note in your comments how you are dealing with this.
Your AddressBook class will look like the UML diagram below:
AddressBook
- list : LinkedList ()
+ AddressBook ( ) : //initializes the LinkedList
+ addContact (Contact) : void //adds the given Contact in alphabetical order
+ removeContact (Contact) : void //removes the given Contact
+ getList () : LinkedList //returns the LinkedList of Contacts
+ search (String, int) : void //enter search word and category (e.g., 1 for name, 2 for address, 3 for phone)
+ displayAllContacts ( ) : void //prints the address book
Note: you can add supporting methods in the AddressBook if needed (make sure you properly comment these).
Demo the AddressBook class:
public class AddressBookDemo{
public static void main(String[] args){
//rest of the code
}
}
You should write a demo program that implements the AddressBook. Your complete program will have the following classes:
Contact.java
LinkedList.java
AddressBook.java
AddressBookDemo.java
A short example of output is below. The demo program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and delete it, and quit. Your output should be nicely formatted. Below is an example screen dialog.
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Adams
Enter first name: John
Enter Street name: Vernon
Enter phone number: 555-1234
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Smith
Enter first name: Amy
Enter Street name: Robie
Enter phone number: 555-4567
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Brown
Enter first name: Adam
Enter Street name: Robie
Enter phone number: 555-9812
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 2
Name Street Number
Adams, John Vernon 555-1234
Brown, Adam Robie 555-9812
Smith, Amy Robie 555-4567
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 3
Enter 1 to search name, 2 to search address, 3 to search phone number: 3
Enter what to search: 555-9812
Name Street Number
Brown, Adam Robie 555-9812
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 3
Enter 1 to search name, 2 to search address, 3 to search phone number: 2
Enter what to search: Robie
Name Street Number
Brown, Adam Robie 555-9812
Smith, Amy Robie 555-4567
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 4
Enter 1 to search name, 2 to search address, 3 to search phone number, 4 to display all contacts: 2
Enter what to search: Robie
Name Street Number
1. Brown, Adam Robie 555-9812
2. Smith, Amy Robie 555-4567
Entry to delete: 2
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 2
Name Street Number
Adam, John Vernon 555-1234
Brown, Adam Robie 555-9812
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 5
Good-bye!
AddressBook
- list : LinkedList ()
+ AddressBook ( ) : //initializes the LinkedList
+ addContact (Contact) : void //adds the given Contact in alphabetical order
+ removeContact (Contact) : void //removes the given Contact
+ getList () : LinkedList //returns the LinkedList of Contacts
+ search (String, int) : void //enter search word and category (e.g., 1 for name, 2 for address, 3 for phone)
+ displayAllContacts ( ) : void //prints the address book
Explanation / Answer
//this is main java program for the addressbook
//AddressBook.java
import java.util.*;
public class AddressBook {
public static void main(String[] args) {
SLList newList = new SLList();
InputOutput newIO = new InputOutput();
Scanner in = new Scanner(System.in);
String fileName = "";
String options;
boolean done = false;
do {
System.out.println();
System.out.format("----------------------------------------------------------------------------------%n");
System.out.format("(A)dd (D)elete (E)mail Search (P)rint List (R)estore from database (S)earch (W)rite to Disk (Q)uit%n");
System.out.format("-----------------------------------------------------------------------------------%n");
System.out.format("Please Enter a command: ");
options = in.nextLine().toUpperCase();
switch (options) {
case "A":
System.out.println("Add an entry");
System.out.print("Enter a FIRST and LAST Name: ");
String name = in.nextLine();
System.out.print("Enter an EMAIL ADDRESS: ");
String email = in.nextLine();
System.out.print("Enter a PHONE NUMBER: ");
String phoneNumber = in.nextLine();
newList.add(name, email, phoneNumber);
break;
case "D":
System.out.print("Enter index number to delete: ");
int delete = in.nextInt();
newList.delete(delete);
break;
case "E":
System.out.print("Enter an E-mail to search for: ");
String sEmail = in.nextLine();
newList.emailSearch(sEmail);
break;
case "P":
System.out.println("Print AddressBook");
newList.printList();
break;
case "R":
System.out.println("Restore from disk");
if(fileName.equals("")){
fileName = "pages";
}
newIO.readFile(fileName);
break;
case "S":
System.out.print("Enter a Name to search for: ");
String sName = in.nextLine();
newList.nameSearch(sName);
break;
case "W":
System.out.println("Write to disk ");
if(fileName.equals("")){
System.out.print("Name of file: ");
fileName = in.nextLine();
}else{
System.out.print("Use existing file " + fileName + " ? (Y/N): ");
String yn = in.nextLine();
switch(yn.toLowerCase()){
case "y":
break;
case "n":
System.out.print("New file name: ");
fileName = in.nextLine();
}
}
newIO.writeFile(fileName, newList);
break;
case "Q":
System.out.println("Exiting!");
done = true;
break;
default:
System.out.println("Enter valid command");
}
} while (!done);
System.out.println("Thanks for using Addressbook");
}
}
-------------------------------------------------------------------------------------------------------------------------
//SLList.java
import java.io.Serializable;
/**
* create serialization so files can be written and read in byte form
* and allow readability of types
*/
@SuppressWarnings("serial")
public class SLList implements Serializable {
private SLNode head;
private int length;
// initialize the list
public SLList() {
head = null;
length = 0;
}
// create method to add name, email, and phone number in String form
public void add(String name, String email, String phoneNumber) {
SLNode current = head;
SLNode previous = null;
SLNode newNode = new SLNode();
// Set the data
newNode.setName(name);
newNode.setEmail(email);
newNode.setPhoneNumber(phoneNumber);
// if the head is empty, new entry automatically becomes the head
// and length is increased by one
if (isEmpty()) {
head = newNode;
length++;
} else {
// compare last names of entries for priority by first
// letter in last name
for (int i = 0; i < length; i++) {
String[] ourNames1 = current.getName().split(" ");
String[] ourNames2 = newNode.getName().split(" ");
int result = ourNames1[ourNames1.length-1].compareToIgnoreCase(ourNames2[ourNames2.length-1]);
// if last name entered goes before what's stored in
// the head, new entry becomes the head
if (result > 0) {
if (previous == null) {
newNode.setNext(current);
head = newNode;
length++;
break;
}
// this section cycles through the "body" of the nodes if
// it's not the tail
previous.setNext(newNode);
newNode.setNext(current);
length++;
break;
}
else // if the entry replaces node at the tail, this
// entry becomes the new tail
{
if (current.getNext() == null) {
current.setNext(newNode);
newNode.setNext(null);
length++;
break;
}
// System.out.println("Add after");
previous = current;
current = current.getNext();
}
}
}
}
public void printList () {
// start at the head and check if there's anything there
SLNode tempNode = head;
if (head == null) {
System.out.println("The list is empty!");
} else {
// go through list and print in order
for (int i = 0; i < length; i++) {
System.out.print("Index = " + (i+1) + " ");
System.out.println(tempNode);
tempNode = tempNode.getNext();
}
}
System.out.println();
}
// method to use if the linked list is empty
public boolean isEmpty() {
return (length == 0);
}
// method used to search through the names in the string
public void nameSearch(String name) {
SLNode current = head;
boolean empty = true;
// check to see if the list is empty
if (isEmpty()) {
System.out.println("The list is empty!");
}else {
// check to see if input matches anything in the string
for (int i = 0; i < length; i++) {
if (current.getName().toLowerCase().contains(name.toLowerCase())) {
System.out.print("Index = " + (i+1) + " ");
System.out.println(current);
empty = false;
}
// if first string doesn't have desired search value
// go to the next node
current = current.getNext();
}
// if statement to show if there are no matches found
if (empty){
System.out.println("No matches to that name!");
}
}
}
// create method to search through list for email addresses
public void emailSearch(String email) {
SLNode current = head;
boolean empty = true;
// check to see if list is empty
if (isEmpty()) {
System.out.println("The list is empty!");
}else {
// check through first in list to see if desired search
// matches first entry in list
for (int i = 0; i < length; i++) {
if (current.getEmail().toLowerCase().contains(email.toLowerCase())) {
System.out.print("Index = " + (i+1) + " ");
System.out.println(current);
empty = false;
}
// if nothing in current node, set to next and search
// again
current = current.getNext();
}
// if statement if no emails match the desired search
if (empty){
System.out.println("No matches to that email address!");
}
}
}
// create method to delete entries by index
public void delete (int index) {
SLNode current = head;
SLNode previous = null;
boolean empty = true;
// check to see if list is empty
if (isEmpty()) {
System.out.println("The list is empty!");
} else {
// iterates over and finds the index and deletes it
for (int i = 0; i < length; i++) {
if ((index-1) == i) {
if (previous == null) {
head = head.getNext();
length--;
empty = false;
break;
} else if (current.getNext() == null){
previous.setNext(null);
length--;
empty = false;
break;
} else {
previous.setNext(current.getNext());
length--;
empty = false;
break;
}
} else {
previous = current;
current = current.getNext();
}
}
if (empty){
System.out.println("Index Does Not Exist!");
} else {
// notification of successful deletion of index
System.out.println("Successfully deleted index " + index);
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------
//SLNode.java
import java.io.Serializable;
@SuppressWarnings("serial")
public class SLNode implements Serializable {
private String name;
private String email;
private String phoneNumber;
private SLNode next;
// create instance of SLNode and set next to 'null'
public SLNode() {
//data = -1;
next = null;
}
/**
* create setters and getters for name, email, phone number, and SLNode
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public SLNode getNext() {
return next;
}
public void setNext(SLNode next) {
this.next = next;
}
@Override
public String toString() {
return "Name = " + name + ", Email = " + email + ", Phone Number = "
+ phoneNumber;
}
}
---------------------------------------------------------------------------------------------------------------------------------
//InputOutput.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class InputOutput {
public void writeFile(String file, SLList list) {
// File magic happens here
ObjectOutputStream o = null;
try {
o = new ObjectOutputStream(new FileOutputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* try/catch block to verify file has been written/created
*/
try {
o.writeObject(list);
System.out.println("File Successfully Created!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* create a method to read the file once it's been written
*/
public SLList readFile(String file) {
// Read it back in
ObjectInputStream i = null;
SLList temp = null;
try {
i = new ObjectInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* try/catch to verify file has been restored
*/
try {
temp = (SLList) i.readObject();
//System.out.println(temp);
System.out.println("Successfully restored data from file!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
i.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
}
}
----------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.