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

Create a Design Document showing a mockup of what the new application will look

ID: 3866814 • Letter: C

Question

Create a Design Document showing a mockup of what the new application will look like and include any details about how it will function. Assume you will present this document to the management of your company. 1. 2. Create a JAVA application to create and maintain a contact list. The contact list should include the contact name, address, city, state, zip and phone number. In addition, the contact list application must have the ability to Add, Delete, Update and Find contacts as needed. 3. The contact list application will display appropriate messages when a user successfully adds, updates or deletes a contact and will also display any error messages when a given user action fails. You must create a database of your choice to store the contact information. Submit all files and code* created in a compressed zip folder to your instructor using 4. 5. the eLearn process. *Please Note: Application must be tested, compiled and able to be executed by instructor.

Explanation / Answer

import java.io.*;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Scanner;

import java.util.Set;

//The Customer class is a template for every customer object

//that will be saved in a file called contacts.bin

//Each customer has name, sex, address, email, and phone

class Customer implements Serializable{

private String cusName;

private String cusSex;

private String cusAddress;

private String cusEmail;

private String cusPhone;

//constructor

Customer(String name, String sex,String address, String email,String phone){

cusName=name;

cusSex=sex;

cusAddress=address;

cusEmail=email;

cusPhone=phone;

}

//get customer name

public String getName(){

return cusName;

}

//get customer sex

public String getSex(){

return cusSex;

}

//get customer address

public String getAddress(){

return cusAddress;

}

//get customer email

public String getEmail(){

return cusEmail;

}

//get customer name

public String getPhone(){

return cusPhone;

}

//print customer information

public void printInfo(){

System.out.println("Name:"+cusName+", Sex:"+cusSex+", Adress:"+cusAddress+", Email:"+cusEmail+", Phone:"+cusPhone);

}

}

public class ContactList{

static HashMap<String,Customer> ls;

public static void main(String[] args){

ls=readList(); //read all contacts and place them in the list

int ch;

char con='y';

Scanner sc=new Scanner(System.in); //create scanner object to receive choice input

while(con=='y'){

showMenu(); //show menu

System.out.println("Enter your choice:");

ch=sc.nextInt();

switch(ch){

case 1:viewAll();break;

case 2:addToList();break;

case 3:deleteFromList();break;

case 4:searchByName();break;

case 5:generateHTML();break;

case 6:System.exit(0);break;

default: System.out.println("Invalid choice");

}

try{

//prompt for continuing the program

InputStreamReader isr=new InputStreamReader(System.in);

System.out.println("Press y to continue:");

con=(char)isr.read();

}catch(IOException ie){}

}

}

//The viewAll method displays all customers in the list

public static void viewAll(){

if(ls!=null){

Set<String> set=ls.keySet(); //get all customer names

//based these names all contacts can be retrieved

Iterator<String> iterator=set.iterator();

while(iterator.hasNext()){

Customer cu=ls.get(iterator.next());

cu.printInfo();

}

}

}

//The addToList method is able to add each customer to the list

public static void addToList(){

//If the ls null, allocate memory for it so it is ready to get the new item

if(ls==null) ls=new HashMap<String,Customer>();

try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter name:");

String name=br.readLine();

System.out.println("Enter sex:");

String sex=br.readLine();

System.out.println("Enter address:");

String address=br.readLine();

System.out.println("Enter email:");

String email=br.readLine();

System.out.println("Enter phone:");

String phone=br.readLine();

Customer st=new Customer(name,sex,address,email,phone);

ls.put(name,st); //add new customer to the list

writeIt(ls); //save the update list

}catch(IOException e){}

}

//The deleteFromList method is able to delete a contact when customer name

//is correctly input

public static void deleteFromList(){

if(ls!=null){

int si=ls.size(); //number of contacts in the list before a contact is removed

try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Customer name:");

String key=br.readLine();

ls.remove(key); //remove the contact

if(ls.size()<si){ //removing is successful

writeIt(ls);

System.out.println("The contact has been deleted.");

}

else

System.out.println("Wrong customer name");

}catch(IOException ie){}

}

}

//The searchByName method has code to find a contact by customer name in the list

public static void searchByName(){

if(ls!=null){

try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Search by name:");

String key=br.readLine();

Customer cu=ls.get(key);

if(cu!=null)

cu.printInfo();

else

System.out.println("Not found");

}catch(IOException ie){}

}

}

//Write the HashMap object representing the contact list to the file

public static void writeIt(HashMap<String,Customer> obj){

try{

FileOutputStream fos=new FileOutputStream("contacts.bin");

ObjectOutputStream oos=new ObjectOutputStream(fos);

oos.writeObject(obj);

oos.flush();

oos.close();

}catch(IOException ie){}

}

//The readList method has code to read all contacts from the file

public static HashMap<String,Customer> readList(){

HashMap<String,Customer> lObject=null;

try{

FileInputStream fis=new FileInputStream("contacts1.bin");

ObjectInputStream ois=new ObjectInputStream(fis);

lObject=(HashMap<String,Customer>)ois.readObject();

ois.close();

}catch(Exception ie){}

return lObject;

}

//generate contact list html file

public static void generateHTML(){

try{

FileWriter fw=new FileWriter("contacts.html");

BufferedWriter bw=new BufferedWriter(fw);

bw.write("<!Doctype html>");

bw.newLine();

bw.write("<html><head><title>Contacts List</title></head>");

bw.newLine();

bw.write("<body>");

bw.newLine();

bw.write("<p>Contacts List</p>");

bw.write("<table>");

bw.newLine();

bw.write("<tr><th>Name</th><th>Sex</th><th>Address</th><th>Email</th><th>Phone</th></tr>");

if(ls!=null){

Set<String> set=ls.keySet();

Iterator<String> iterator=set.iterator();

while(iterator.hasNext()){

Customer cu=ls.get(iterator.next());

bw.write("<tr><td>"+cu.getName()+"</td><td>"+cu.getSex()+"</td><td>"+cu.getAddress()+"</td><td><a href="mailto:"+cu.getEmail()+"">"+cu.getEmail()+"</a></td><td>"+cu.getPhone()+"</td></tr>");

bw.newLine();

}

}

bw.write("</table>");

bw.newLine();

bw.write("</body></html>");

bw.flush();

bw.close();

}catch(IOException ie){ie.printStackTrace();}

}

//This method display a list of choices

public static void showMenu(){

System.out.println("1. View all contacts");

System.out.println("2. Add to contacts list");

System.out.println("3. Remove from contacts list");

System.out.println("4. Find a contact");

System.out.println("5. Generate contact list html file");

System.out.println("6. Exit");

}

}