How to build his program? Lab 4 - Contact Database due Mon. 4/30 Write a program
ID: 3712915 • Letter: H
Question
How to build his program?
Lab 4 - Contact Database due Mon. 4/30 Write a program that implements a database system. The program will use an array of type contact to store contact information (maximum size 100). The program should be written using 2 class files. One class will implement a contact object and the second class will implement the database and include the main method. The contact class should include instance variables to store the contact's first name, last name, phone number, and email address. Add appropriate constructors, accessor and mutator methods, an input method, and overloaded << function. Your database class should include a method to display a menu that allows the client to add a contact, display all contacts, search for a contact by phone number, and delete a contact. You may add a method to edit a contact for extra credit. Input the existing contact information for 10 contacts into the database's array instance variable from a file (use an initialize method).
Explanation / Answer
Solution:
code:
File Name: MyContact.java
//Contact class
public class MyContact
{
//Declare contactName
private String contactName;
//Declare contactPhoneNum
private String contactPhoneNum;
//Declare contactEmail
private String contactEmail;
//Define method to set contact name
public void setName(String contactName)
{
//Set contactName
this.contactName = contactName;
}
//Define method to set the phone number
public void setPhoneNumber(String contactPhoneNum)
{
//Set the contact phone number
this.contactPhoneNum = contactPhoneNum;
}
//Define method to set the contact email
public void setEmail(String contactEmail)
{
//Set the contact email
this.contactEmail = contactEmail;
}
//Define method to get the contact name
public String getName()
{
//Return contact name
return contactName;
}
//Define method to get the contact phone number
public String getMyContactPhoneNumber()
{
//Return the contact phone number
return contactPhoneNum;
}
//Define method to get the contact email
public String getMyContactEmail()
{
//return the contact email
return contactEmail;
}
//Define method to represent contact details as string
public String toString()
{
//Return
return " Name:"+contactName+" Phone Number:"+ contactPhoneNum + " Email:"+contactEmail+" ";
}
}
File Name: MyAddressBook.java
//Import the needed files
import java.util.*;
import java.util.HashMap;
import java.io.*;
//class defines a address book
public class MyAddressBook
{
//main
public static void main(String[] args) throws Exception
{
//Declare fileToSave
String fileToSave = null;
//Define inpScaner
Scanner inpScaner = new Scanner(System.in);
//Decalre cnt and initialize it
int cnt=1;
//Declare map to create address book
HashMap< Integer,MyContact > myContactList = new HashMap< >();
//Call Function
displayProgramMenu();
//Get user choice
int myUserCh = inpScaner.nextInt();
//Loop
while(true)
{
//Switch statement
switch(myUserCh)
{
//if choice is 1, display all contact details
case 1:
//read new line
String m=inpScaner.nextLine();
//Loop to display all contact details
for(int kk=0;kk<cnt-1;kk++)
{
//Print the current contact info
System.out.println(myContactList.get(kk+1));
}
//Break
break;
//If choice is 2, add new contact
case 2:
//read new line
m=inpScaner.nextLine();
//Create new contact
MyContact newContact = new MyContact();
//Prompt for name
System.out.print("Enter name: ");
//Get name
newContact.setName(inpScaner.nextLine());
//Prompt for phone number
System.out.print("Enter phone number(xxx-xxx-xxxx): ");
//Get phone number
newContact.setPhoneNumber(inpScaner.nextLine());
//Prompt for email
System.out.print("Enter email: ");
//Get email address
newContact.setEmail(inpScaner.nextLine());
//Add newContact to address book
myContactList.put(cnt, newContact);
//Increment cnt by 1
cnt++;
//Break
break;
//If choice 3, delete contact detail
case 3:
//read new line
m=inpScaner.nextLine();
//Prompt for name
System.out.print("Enter name to delete from list: ");
//Read the name
String name=inpScaner.nextLine();
//Loop to check the address book
for(int kk=0;kk<cnt-1;kk++)
{
//Get current contact details
MyContact tp =(MyContact)myContactList.get(kk+1);
//Check for condition
if(tp.getName().equals(name))
{
//Remove current contact
for(int aa=kk+1; aa<cnt-1;aa++)
{
System.out.println(aa);
MyContact mp = (MyContact)myContactList.get(aa+1);
myContactList.put(aa, mp);
}
//Decrease cnt by 1
cnt--;
//Break
break;
}
}
//Break
break;
//If choice is 4, save the file
case 4:
//Prompt for file name
System.out.print("Enter file name to save: ");
//Get file name
fileToSave = inpScaner.next();
//Create print writer object
BufferedWriter outContactFile = new BufferedWriter (new FileWriter(fileToSave));
//Loop
for(int kk=0;kk<cnt-1;kk++)
{
//Get current contact details
MyContact tp =(MyContact)myContactList.get(kk+1);
//Write name
outContactFile.write(tp.getName());
//newline
outContactFile.newLine();
//Write phone number
outContactFile.write( tp.getMyContactPhoneNumber());
//newline
outContactFile.newLine();
//Write email
outContactFile.write(tp.getMyContactEmail());
//newline
outContactFile.newLine();
}
//Flush file
outContactFile.flush();
//Close file
outContactFile.close();
//Print message.
System.out.println("Saved");
//Exit
System.exit(0);
//Break
break;
//Default
default:
//Display
System.out.println("Invalid choice");
}
//Call Function
displayProgramMenu();
//Get choice
myUserCh = inpScaner.nextInt();
}
}
//Define method to display memu
public static void displayProgramMenu()
{
//Display
System.out.println("1 Display contact list");
System.out.println("2 Add Contact");
System.out.println("3 Delete contact");
System.out.println("4 Save contact list and Exit");
System.out.print("Your choice: ");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.