Question

Write a contacts database program that presents the user with a menu that allows the user...

Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java)

  • Save a contact.
  • Search for a contact.
  • Print all contacts out to the screen.
  • Quit

If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt.
If the user selects the second option, the program prompts the user asking for the name of the contact. It then searches the contacts.txt for a matching name. If found, it displays the phone number on the screen. If not found, it will display an appropriate error message.
If the user selects the third option, the program displays all contacts stored in contacts.txt in a neat table.
The program is menu driven and will repeat presenting the menu and processing choices until the user selects the fourth option to quit.
If the user selects an invalid option, an appropriate error message should be displayed.
If the user selects to print all contacts to the screen with no stored contacts, an appropriate error message should be displayed.

You may use message dialogs or make it a purely console-based application

Hints:

  • in.nextLine().charAt(0); will retrieve the first character of a string read from from the keyboard for Scanner object named in.
  • f.exists() returns true if the file opened using File object f exists, false otherwise. Can be used to make sure you don't throw an exception related to the file not existing.
  • attached, you can find a copy of the contacts.txt file that gets created by my solution.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Note: the fileName used in the program, please feel free to update the fileName when you run the program in your system. Dont change it is my recommendation.

Thank You !!

===========================================================================

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class PhoneBook {

    private String name;
    private String phoneNumber;

    public PhoneBook(String name, String phoneNumber) {
        this.name = name;
        this.phoneNumber = phoneNumber;
    }

    public String getName() {
        return name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    @Override
    public String toString() {
        return "Name: " + name + ", Phone Number: " + phoneNumber;
    }

    public static void main(String[] args) {

        try {
            loadContacts();
            char choice = '\0';
            do {
                choice = printMenuGetChoice();
                if (choice == '1') {
                    saveAContact();
                } else if (choice == '2') {
                    searchContact();
                } else if (choice == '3') {
                    printAll();
                } else if (choice == '4') {
                    saveAllData();
                    System.out.println("Thanks. Bye!");
                } else {

                }
            } while (choice != '4');
        } catch (FileNotFoundException e) {
            System.out.println("Unable to read data from file: " + fileName);
        } catch (IOException e) {
            System.out.println("Unable to save records in file: " + fileName);
        }

    }

    private static void saveAllData() throws IOException {

        PrintWriter writer = new PrintWriter(new FileWriter(fileName));
        for (PhoneBook book : contacts) {
            writer.write(book.getName() + "," + book.getPhoneNumber() + "\r\n");
            writer.flush();
        }
        writer.close();
    }

    private static void searchContact() {

        System.out.print("Enter contact name: ");
        String name = in.nextLine();
        boolean flag = true;
        for (PhoneBook contact : contacts) {
            if (contact.getName().equalsIgnoreCase(name)) {
                System.out.println("Matching record found.");
                System.out.println("Contact Phone number: " + contact.getPhoneNumber());
                flag = false;
            }
        }
        if (flag) System.out.println("No records found.");

    }

    private static void saveAContact() {

        System.out.print("Enter contact name: ");
        String name = in.nextLine();
        System.out.print("Enter phone number: ");
        String phone = in.nextLine();
        PhoneBook contact = new PhoneBook(name, phone);
        contacts.add(contact);
        System.out.println("Contact saved successfully.");
    }

    private static void printAll() {
        System.out.println("Printing all contacts");
        System.out.println("=====================");
        for (PhoneBook contact : contacts)
            System.out.println(contact);
    }

    private static void loadContacts() throws FileNotFoundException {

        File file = new File(fileName);
        if (file.exists()) {

            Scanner fileScanner = new Scanner(file);
            while (fileScanner.hasNext()) {
                String tokens[] = fileScanner.nextLine().split(",");
                PhoneBook contact = new PhoneBook(tokens[0], tokens[1]);
                contacts.add(contact);
            }
            fileScanner.close();
        }

    }

    private static final Scanner in = new Scanner(System.in);
    private static ArrayList<PhoneBook> contacts = new ArrayList<PhoneBook>();
    private static final String fileName = "contacts.txt";

    private static char printMenuGetChoice() {

        System.out.println("Type [1] - Save a contact");
        System.out.println("Type [2] - Search for a contact");
        System.out.println("Type [3] - Print all contacts");
        System.out.println("Type [4] - Quit");
        System.out.print("Enter choice: ");
        return in.nextLine().charAt(0);
    }

}
Add a comment
Know the answer?
Add Answer to:
Write a contacts database program that presents the user with a menu that allows the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that displays a menu on the screen. The menu will give the user...

    Write a program that displays a menu on the screen. The menu will give the user four options - enter two integers, enter two decimal numbers (#.#), enter one integer and one decimal number, or quit. The inputs should be labelled a, b, c, or d, and you should enter in the letter to choose the appropriate option. Once the user selects the option, two numbers will be read in from the user. The numbers will be added together and...

  • This program is used to create a phonebook for storing contact information into a text file....

    This program is used to create a phonebook for storing contact information into a text file. The program begins by asking the user to provide a name for the file that will contain their phone book information. It will then ask the user to provide the names and numbers of their contacts. It should keep asking the user for contact information until they type in Done (case-sensitive). After the user types Done, store all contact information into the file name...

  • Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects....

    Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- rectangle 2 -- circle 3 -- triangle 4 -- quit If the user selects choice 1, the program should find the area of a rectangle. rectangle area = length * width If the user selects choice 2, the program should find the area of a circle. circle area = PI * radius * radius...

  • In C++ write a simple menu program that displays a menu for the user in an...

    In C++ write a simple menu program that displays a menu for the user in an object-oriented technique. The menu should keep showing up until the user chooses to quit. Also, there is a sub-menu inside a menu. The user should get at least a line displayed after he or she chooses that particular option meaning if the user presses 1 for the read actors from the file. The output can look like "reading actors from a file" and then...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • Design program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven...

    Design program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- square 2 -- circle 3 -- right triangle 4 -- quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the user...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • C++ program Write a C++ program to manage a hospital system, the system mainly uses file...

    C++ program Write a C++ program to manage a hospital system, the system mainly uses file handling to perform basic operations like how to add, edit, search, and delete record. Write the following functions: 1. hospital_menu: This function will display the following menu and prompt the user to enter her/his option. The system will keep showing the menu repeatedly until the user selects option 'e' from the menu. Arkansas Children Hospital a. Add new patient record b. Search record c....

  • language:python VELYIEW Design a program that you can use to keep information on your family or...

    language:python VELYIEW Design a program that you can use to keep information on your family or friends. You may view the video demonstration of the program located in this module to see how the program should function. Instructions Your program should have the following classes: Base Class - Contact (First Name, Last Name, and Phone Number) Sub Class - Family (First Name, Last Name, Phone Number, and Relationship le. cousin, uncle, aunt, etc.) Sub Class - Friends (First Name, Last...

  • In this assignment, you will create an application that holds a list of contact information. You ...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. MUST BE IN PYTHON FORMAT Create the folllowing objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT