Advertisement
Guest User

PhoneBookUI.java

a guest
Apr 24th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class PhoneBookUI {
  5.  
  6.     private boolean run = true;
  7.     private Scanner reader;
  8.     private PhoneBook phonebook;
  9.  
  10.     public PhoneBookUI(PhoneBook phonebook) {
  11.         reader = new Scanner(System.in);
  12.         this.phonebook = phonebook;
  13.     }
  14.  
  15.     public void start() {
  16.         printMenu();
  17.         while (run) {
  18.             commandPrompt();
  19.         }
  20.     }
  21.  
  22.     public void printMenu() {
  23.         System.out.println(""
  24.                 + "phone search\n"
  25.                 + "available operations:\n"
  26.                 + " 1 add a number\n"
  27.                 + " 2 search for a number\n"
  28.                 + " 3 search for a person by phone number\n"
  29.                 + " 4 add an address\n"
  30.                 + " 5 search for personal information\n"
  31.                 + " 6 delete personal information\n"
  32.                 + " 7 filtered listing\n"
  33.                 + " x quit\n");
  34.     }
  35.  
  36.     public void commandPrompt() {
  37.         System.out.print("command: ");
  38.         String commandEntered = reader.nextLine();
  39.         if (commandEntered.equals("x")) {
  40.             commandEntered = "8";
  41.         }
  42.  
  43.         switch (Integer.parseInt(commandEntered)) {
  44.             case 1:
  45.                 addNumber();
  46.                 break;
  47.             case 2:
  48.                 searchForNumberByName();
  49.                 break;
  50.             case 3:
  51.                 searchForNameByNumber();
  52.                 break;
  53.             case 4:
  54.                 addAddress();
  55.                 break;
  56.             case 5:
  57.                 searchForInfoByName();
  58.                 break;
  59.             case 6:
  60.                 deletePerson();
  61.                 break;
  62.             case 7:
  63.                 searchNameAndAddressByTerm();
  64.                 break;
  65.             case 8:
  66.                 quit();
  67.         }
  68.     }
  69.  
  70.     public void addNumber() {
  71.         System.out.print("whose number: ");
  72.         String name = reader.nextLine();
  73.         System.out.print("number: ");
  74.         phonebook.addPhoneNumber(name, reader.nextLine());
  75.     }
  76.  
  77.     public void searchForNumberByName() {
  78.         System.out.print("whose number: ");
  79.         phonebook.printNumberByName(reader.nextLine());
  80.     }
  81.  
  82.     public void searchForNameByNumber() {
  83.         System.out.print("number: ");
  84.         phonebook.printNameByNumber(reader.nextLine());
  85.     }
  86.  
  87.     public void addAddress() {
  88.         System.out.print("whose address: ");
  89.         String name = reader.nextLine();
  90.         System.out.print("street: ");
  91.         String address = reader.nextLine();
  92.         System.out.print("city: ");
  93.         address += " " + reader.nextLine();
  94.         phonebook.addAddress(name, address);
  95.     }
  96.  
  97.     public void searchForInfoByName() {
  98.         System.out.print("whose information: ");
  99.         phonebook.printPersonInfoFromName(reader.nextLine());
  100.     }
  101.  
  102.     public void deletePerson() {
  103.         System.out.print("whose information: ");
  104.         phonebook.deletePerson(reader.nextLine());
  105.     }
  106.  
  107.     public void searchNameAndAddressByTerm() {
  108.         System.out.print("keyword (if empty, all listed): ");
  109.         phonebook.searchByTerm(reader.nextLine());
  110.     }
  111.  
  112.     private void quit() {
  113.         this.run = false;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement