Question

In: Computer Science

Answer the following in Java programming language Create a Java Program that will import a file...

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.

Solutions

Expert Solution

/*******************************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:)


Related Solutions

This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
Please answer the problem below in C programming language: Create a pointer activity source file -...
Please answer the problem below in C programming language: Create a pointer activity source file - cptr2.c - that takes two arguments, a number from 1 to 3, and a string sentence(s). Create variables for a character, an integer, a string pointer. Based on integer value you will use that number of string pointers. The string variable is a string pointer that has not been allocated.    Define pointers to those variables types without any initialization of those points to the...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books...
JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books in order to identify duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called duplicateTitles.out. When complete, the output file should contain all titles that are duplicated in the input file. Note that the duplicate titles should be written once, even though the input file may contain same titles multiple...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT