Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PhoneBookUI {
- private boolean run = true;
- private Scanner reader;
- private PhoneBook phonebook;
- public PhoneBookUI(PhoneBook phonebook) {
- reader = new Scanner(System.in);
- this.phonebook = phonebook;
- }
- public void start() {
- printMenu();
- while (run) {
- commandPrompt();
- }
- }
- public void printMenu() {
- System.out.println(""
- + "phone search\n"
- + "available operations:\n"
- + " 1 add a number\n"
- + " 2 search for a number\n"
- + " 3 search for a person by phone number\n"
- + " 4 add an address\n"
- + " 5 search for personal information\n"
- + " 6 delete personal information\n"
- + " 7 filtered listing\n"
- + " x quit\n");
- }
- public void commandPrompt() {
- System.out.print("command: ");
- String commandEntered = reader.nextLine();
- if (commandEntered.equals("x")) {
- commandEntered = "8";
- }
- switch (Integer.parseInt(commandEntered)) {
- case 1:
- addNumber();
- break;
- case 2:
- searchForNumberByName();
- break;
- case 3:
- searchForNameByNumber();
- break;
- case 4:
- addAddress();
- break;
- case 5:
- searchForInfoByName();
- break;
- case 6:
- deletePerson();
- break;
- case 7:
- searchNameAndAddressByTerm();
- break;
- case 8:
- quit();
- }
- }
- public void addNumber() {
- System.out.print("whose number: ");
- String name = reader.nextLine();
- System.out.print("number: ");
- phonebook.addPhoneNumber(name, reader.nextLine());
- }
- public void searchForNumberByName() {
- System.out.print("whose number: ");
- phonebook.printNumberByName(reader.nextLine());
- }
- public void searchForNameByNumber() {
- System.out.print("number: ");
- phonebook.printNameByNumber(reader.nextLine());
- }
- public void addAddress() {
- System.out.print("whose address: ");
- String name = reader.nextLine();
- System.out.print("street: ");
- String address = reader.nextLine();
- System.out.print("city: ");
- address += " " + reader.nextLine();
- phonebook.addAddress(name, address);
- }
- public void searchForInfoByName() {
- System.out.print("whose information: ");
- phonebook.printPersonInfoFromName(reader.nextLine());
- }
- public void deletePerson() {
- System.out.print("whose information: ");
- phonebook.deletePerson(reader.nextLine());
- }
- public void searchNameAndAddressByTerm() {
- System.out.print("keyword (if empty, all listed): ");
- phonebook.searchByTerm(reader.nextLine());
- }
- private void quit() {
- this.run = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement