In: Computer Science
Answer the following in Java programming language
Create a Java Program that will import a file of contacts
(contacts.txt) into a database (Just their first name and 10-digit
phone number). The database should use the following class to
represent each entry:
public class contact {
String firstName;
String phoneNum;
}
Furthermore, your program should have two classes.
(1) DatabaseDirectory.java:
This class should declare ArrayList as a private
datafield to store objects into the database (theDatabase) with the
contacts names and phone numbers
private List
<contacts>theDatabase = new ArrayList(); (example from
textbook)
So now you can use this statement:
theDatabase.add(new contact("Mary Smith", "8007894562");
to add a new entry to your database.
This class should also hold the following
methods:
(1) Display All Contacts
- as name suggests, should display
all entries in database
(2) Search Contact by Name
- uses String name
- should return the contact or tell
user if the contact does not exist
(3) Add/Edit Contact
- uses String name and String number
- ask user if they want to add or
edit
- prompt user for name they want to
add or edit
- prompt user for phone
number
- display all info before asking
the user if they want to save
(4) Remove Contact
- uses String name
- search name of contact to
remove
- if contact exists, ask user if
they are sure they want to delete the contact
- remove contact
(2) Database.java:
This is a driver class that will display the main menu
with it's selections. This driver class should execute the methods
held in the DatabaseDirectory.java class. The database should
continue to show the main menu until the user prompts the program
to exit.
Your program should closely follow this same output:
Phone Number Database
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 2
Enter the name of the contact: Jennifer
Name: Jennifer
Phone #: 8001234567
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 1
----------------------------------------------------
Name Phone
Number
Jennifer 8001234567
Karen
8004567854
Lowell
5206689875
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 5
Thank you for using this database!
Any extra comments would be greatly beneficial for my understanding of this assignment, thank you in advance.
/*******************************Contact.java***************************/
/**
* @author
*
* class Contact
*
*/
public class Contact {
/*
* Instance variable to store first name and phone
number
*/
private String firstName;
private String phoneNumber;
/**
* default constructor to initialize instance variable
to default value
*/
public Contact() {
this.firstName = "";
this.phoneNumber = "";
}
/**
* Parameterized constructor to initialize instance
variable to given parameter
*
* @param firstName
* @param phoneNumber
*/
public Contact(String firstName, String phoneNumber)
{
super();
this.firstName = firstName;
this.phoneNumber =
phoneNumber;
}
// return first name
public String getFirstName() {
return firstName;
}
// set first name
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// return phone number
public String getPhoneNumber() {
return phoneNumber;
}
// set phone number
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber =
phoneNumber;
}
// to show the contact information
@Override
public String toString() {
return firstName + "\t\t" +
phoneNumber;
}
}
/****************************************DatabaseDirectory.java******************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
/**
* @author
*
* class DatabaseDirectory
*/
public class DatabaseDirectory {
private List<Contact> theDataBase;
/**
* Initialize database directory by reading
contacts.txt
*/
public DatabaseDirectory() {
this.theDataBase = new
ArrayList<Contact>();
readDataBase();
}
// read file by Scanner line by line
private void readDataBase() {
try {
Scanner scan =
new Scanner(new File("contacts.txt"));
// while
scanner has line
while
(scan.hasNextLine()) {
// save data in array using split according to
separator
String[] data = scan.nextLine().split(","); // I
use comma if space is separator you can use \\s+
String firstName = data[0]; // save in to
variables
String phoneNumber = data[1];
theDataBase.add(new Contact(firstName,
phoneNumber)); // add to database
}
scan.close();
} catch (FileNotFoundException e)
{
System.out.println(e);
}
}
// add or edit
public void addOrEditDatabase(Scanner scan) {
System.out.println("You want to
E)dit or A)dd: ");
char choice =
scan.nextLine().toUpperCase().charAt(0);
if (choice == 'E') {
System.out.print("Enter name: ");
String name =
scan.nextLine();
Contact contact1
= null;
for (Contact
contact : theDataBase) {
if (contact.getFirstName().equalsIgnoreCase(name)) {
contact1 = contact;
break;
}
}
if (contact1 != null) {
System.out.print("Enter phone number: ");
String phoneNumber = scan.nextLine();
System.out.println("Name: " + name);
System.out.println("Phone number: " +
phoneNumber);
System.out.print("Do you want to save Y)es or
N)o: ");
char option =
scan.nextLine().toUpperCase().charAt(0);
if (option == 'Y') {
contact1.setFirstName(name);
contact1.setPhoneNumber(phoneNumber);
}
} else {
System.out.println("Sorry this contact not
exits!");
}
} else if (choice == 'A') {
System.out.print("Enter name: ");
String name =
scan.nextLine();
System.out.print("Enter phone number: ");
String
phoneNumber = scan.nextLine();
System.out.println("Name: " + name);
System.out.println("Phone number: " + phoneNumber);
System.out.print("Do you want to save Y)es or N)o: ");
char option =
scan.nextLine().toUpperCase().charAt(0);
if (option == 'Y') {
theDataBase.add(new Contact(name,
phoneNumber));
}
}
}
// search contact
public Contact searchContact(Scanner scan) {
System.out.print("Enter name:
");
String name =
scan.nextLine();
Contact contact1 = null;
for (Contact contact : theDataBase)
{
if (contact.getFirstName().equalsIgnoreCase(name)) {
contact1 = contact;
break;
}
}
return contact1;
}
// remove contact
public void removeContact(Scanner scan) {
System.out.print("Enter name:
");
String name =
scan.nextLine();
Contact contact1 = null;
Iterator<Contact> itr = theDataBase.iterator();
while (itr.hasNext()) {
Contact
contact = itr.next();
if
(contact.getFirstName().equalsIgnoreCase(name)) {
contact1 = contact;
}
}
if (contact1 != null) {
System.out.print("Are you sure Y)es or N)o: ");
char option =
scan.nextLine().toUpperCase().charAt(0);
if (option == 'Y') {
theDataBase.remove(contact1);
}
} else {
System.out.println("No contact found with this name!");
}
}
// display contact
public void displayAllContacts() {
System.out.println("Name\t\tPhone Number");
for (Contact contact : theDataBase)
{
System.out.println(contact.toString());
}
}
}
/*****************************************Database.java*****************************/
import java.util.Scanner;
/**
* @author
*
* class Database
*/
public class Database {
public static void main(String[] args) {
// create scanner object
Scanner scan = new
Scanner(System.in);
// create database director
DatabaseDirectory db = new
DatabaseDirectory();
System.out.println("Phone Number
Database\r\n");
int choice = 0;
// do while loop till user not
choose exit
do {
// print
menu
System.out.println("----------------------------------------------------\r\n"
+ "Main Menu:\r\n"
+ "(1) Display All
Contacts\r\n" + "(2) Search Contact by Name\r\n" + "(3) Add or Edit
Contact\r\n"
+ "(4) Remove Contact\r\n" +
"(5) Exit Database\r\n"
+
"----------------------------------------------------");
System.out.print("Please enter choice: ");
choice =
scan.nextInt();
scan.nextLine();
// switch
case to select operation
switch (choice)
{
case 1:
db.displayAllContacts();
break;
case 2:
Contact cn = db.searchContact(scan);
System.out.println("Name: " +
cn.getFirstName());
System.out.println("Phone #: " +
cn.getPhoneNumber());
break;
case 3:
db.addOrEditDatabase(scan);
break;
case 4:
db.removeContact(scan);
break;
case 5:
System.out.println("Thank you for using this
database!");
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (choice != 5);
scan.close();// close
scanner
}
}
/*********************************output***********************/
Phone Number Database
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 2
Enter name: Jennifer
Name: Jennifer
Phone #: 8001234567
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 1
Name Phone Number
Jennifer 8001234567
Karen 8004567854
Lowell 5206689875
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 3
You want to E)dit or A)dd:
A
Enter name: JKS
Enter phone number: 43765874454
Name: JKS
Phone number: 43765874454
Do you want to save Y)es or N)o: Y
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 1
Name Phone Number
Jennifer 8001234567
Karen 8004567854
Lowell 5206689875
JKS 43765874454
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 4
Enter name: Karen
Are you sure Y)es or N)o: Y
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 1
Name Phone Number
Jennifer 8001234567
Lowell 5206689875
JKS 43765874454
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 3
You want to E)dit or A)dd:
E
Enter name: JKS
Enter phone number: 4687465846
Name: JKS
Phone number: 4687465846
Do you want to save Y)es or N)o: Y
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 1
Name Phone Number
Jennifer 8001234567
Lowell 5206689875
JKS 4687465846
----------------------------------------------------
Main Menu:
(1) Display All Contacts
(2) Search Contact by Name
(3) Add or Edit Contact
(4) Remove Contact
(5) Exit Database
----------------------------------------------------
Please enter choice: 5
Thank you for using this database!
Please let me know if you have any doubt or modify the answer, Thanks:)