Guest User

Untitled

a guest
May 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Map;
  3. import java.util.HashMap;
  4.  
  5. class AddressBook {
  6. private String name;
  7. private String phone;
  8.  
  9. public AddressBook(String name, String phone) {
  10. this.name = name;
  11. this.phone = phone;
  12. }
  13.  
  14. public String toString() {
  15. return String.format("%s(%s)", name, phone);
  16. }
  17. }
  18.  
  19. public class AddressBookManager {
  20. private Map<String, AddressBook> books = null;
  21.  
  22. public AddressBookManager() {
  23. //Implement to create books HashMap
  24. }
  25.  
  26. public void add(String name, String phone) {
  27. //Implement to create AddressBook and add into books
  28. }
  29.  
  30. public AddressBook remove(String name) {
  31. //Implement to remove AddressBook that has name as key
  32. //and return it
  33. }
  34.  
  35. public void printAll() {
  36. //Implement to printAll AddressBook
  37. }
  38.  
  39. public AddressBook findByName(String name) {
  40. //Implement to find book By Name
  41. AddressBook book = null;
  42. return book;
  43. }
  44.  
  45. public static void showMenu() {
  46. System.out.println("===== Menu =====");
  47. System.out.println("1. Add Address");
  48. System.out.println("2. PrintAll");
  49. System.out.println("3. Find By Name");
  50. System.out.println("4. Remove By Name");
  51. System.out.println("5. Quit");
  52. }
  53.  
  54. public static int getMenu(Scanner sc) {
  55. return Integer.parseInt(sc.nextLine().trim());
  56. }
  57.  
  58. public static void main(String...args) {
  59. Scanner sc = new Scanner(System.in);
  60. int count = 0;
  61.  
  62. AddressBookManager bookManager = new AddressBookManager();
  63.  
  64. while(true) {
  65. showMenu();
  66. int menu = getMenu(sc);
  67. if (menu == 1) {
  68. String name = sc.nextLine();
  69. String phone = sc.nextLine();
  70. bookManager.add(name, phone);
  71. } else if (menu == 2) {
  72. bookManager.printAll();
  73. } else if (menu == 3) {
  74. String name = sc.nextLine();
  75. AddressBook book = bookManager.findByName(name);
  76. if (book != null) {
  77. System.out.println("Find: " + book.toString());
  78. } else {
  79. System.out.println("There is no address : " + name);
  80. }
  81. } else if (menu == 4) {
  82. String name = sc.nextLine();
  83. AddressBook book = bookManager.remove(name);
  84. if (book != null) {
  85. System.out.println("Delete: " + book.toString());
  86. } else {
  87. System.out.println("There is no address : " + name);
  88. }
  89. } else if (menu == 5) {
  90. break;
  91. }
  92. }
  93. }
  94. }
Add Comment
Please, Sign In to add comment