Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package phonesearch;
  7.  
  8. import java.util.*;
  9.  
  10. /**
  11. *
  12. * @author cajo
  13. */
  14. public class PhoneSearch {
  15.  
  16. private Scanner scanner;
  17. private Map map;
  18. //private Map numberPersonMap;
  19.  
  20. public PhoneSearch(Scanner scanner) {
  21. this.scanner = scanner;
  22. this.map = new HashMap<String, Person>();
  23. //this.numberPersonMap = new HashMap<Person, String>();
  24. }
  25.  
  26. public void start() {
  27. System.out.println("phone search");
  28. System.out.println("available operations:");
  29. System.out.println(" 1 add a number");
  30. System.out.println(" 2 search for a number");
  31. System.out.println(" 3 search for a person by phone number");
  32. System.out.println(" 4 add an address");
  33. System.out.println(" 5 search for personal information");
  34. System.out.println(" 6 delete personal information");
  35. System.out.println(" 7 filtered listing");
  36. System.out.println(" x quit");
  37.  
  38. while (true) {
  39. System.out.println("");
  40. System.out.print("command: ");
  41. String command = this.scanner.nextLine();
  42. if (command.equals("1")) {
  43. command1();
  44. } else if (command.equals("2")) {
  45. command2();
  46. } else if (command.equals("3")) {
  47. command3();
  48. } else if (command.equals("4")) {
  49. command4();
  50. } else if (command.equals("5")) {
  51. command5();
  52. } else if (command.equals("6")) {
  53.  
  54. } else if (command.equals("7")) {
  55.  
  56. } else if (command.equals("x")) {
  57. break;
  58. } else {
  59. System.out.println("invalid command");
  60. }
  61. }
  62. }
  63.  
  64. public void addNumber() {
  65. System.out.print("whose number: ");
  66. String name = this.scanner.nextLine();
  67.  
  68. System.out.print("number: ");
  69. String number = this.scanner.nextLine();
  70.  
  71. if (!this.map.containsKey(name)) {
  72. this.map.put(name, new Person(name, number));
  73. } else {
  74. Person person = (Person) this.map.get(name);
  75. person.addPhoneNumber(number);
  76. }
  77.  
  78. }
  79.  
  80. public void command1() {
  81. addNumber();
  82. }
  83.  
  84. public void searchNumber() {
  85. System.out.print("whose number: ");
  86. String name = this.scanner.nextLine();
  87.  
  88. if (this.map.containsKey(name)) {
  89. Person object = (Person) this.map.get(name);
  90.  
  91. if (object.getPhoneNumber().isEmpty()) {
  92. System.out.println(" not found");
  93. } else {
  94. List<String> list = object.getPhoneNumber();
  95.  
  96. for (String number : list) {
  97. System.out.println(" " + number);
  98. }
  99. }
  100. } else {
  101. System.out.println("not found");
  102. }
  103. }
  104.  
  105. public void command2() {
  106. searchNumber();
  107. }
  108.  
  109. public void searchPerson() {
  110. System.out.print("number: ");
  111. String number = this.scanner.nextLine();
  112.  
  113. List<String> nameList = new ArrayList<String>(this.map.keySet());
  114. boolean wasFound = false;
  115.  
  116. for (String name : nameList) {
  117. Person person = (Person) this.map.get(name);
  118.  
  119. if (person.getPhoneNumber().contains(number)) {
  120. System.out.println(" " + person.getName());
  121. wasFound = true;
  122. break;
  123. }
  124. }
  125.  
  126. if (!wasFound) {
  127. System.out.println(" not found");
  128. }
  129. }
  130.  
  131. public void command3() {
  132. searchPerson();
  133. }
  134.  
  135. public void addAddress() {
  136. System.out.print("whose address: ");
  137. String name = this.scanner.nextLine();
  138.  
  139. System.out.print("street: ");
  140. String street = this.scanner.nextLine();
  141.  
  142. System.out.print("city: ");
  143. String city = this.scanner.nextLine();
  144.  
  145. Person person = (Person) this.map.get(name);
  146. person.setAddress(street + " " + city);
  147. }
  148.  
  149. public void command4() {
  150. addAddress();
  151. }
  152.  
  153. public void searchInformation() {
  154. System.out.print("whose information: ");
  155. String name = this.scanner.nextLine();
  156.  
  157. Person person = (Person) this.map.get(name);
  158.  
  159. if (person.getAddress().isEmpty() || person.getAddress() == null) {
  160. System.out.println(" adress unknown");
  161. } else {
  162. System.out.println(" adress: " + person.getAddress());
  163. }
  164.  
  165. if (person.getPhoneNumber().isEmpty() || person.getPhoneNumber() == null) {
  166. System.out.println(" phone number not found");
  167. } else {
  168. for (int i = 0; i < person.getPhoneNumber().size(); i++) {
  169. System.out.println(" " + person.getPhoneNumber().get(i));
  170. }
  171. }
  172. }
  173.  
  174. public void command5() {
  175. searchInformation();
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement