Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 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. command6();
  54. } else if (command.equals("7")) {
  55. command7();
  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. if (this.map.containsKey(name)) {
  146. Person person = (Person) this.map.get(name);
  147.  
  148. person.setAddress(street, city);
  149. } else {
  150. this.map.put(name, new Person(name, street, city));
  151. }
  152.  
  153. }
  154.  
  155. public void command4() {
  156. addAddress();
  157. }
  158.  
  159. public void searchInformationInterface() {
  160. System.out.print("whose information: ");
  161. String name = this.scanner.nextLine();
  162.  
  163. searchInformation(name, true);
  164. }
  165.  
  166. public void searchInformation(String name, boolean isFromInterface) {
  167. if (this.map.containsKey(name)) {
  168. Person person = (Person) this.map.get(name);
  169.  
  170. if (!(person.isAddressEmpty())) {
  171. System.out.println(" address: " + person.getAddress());
  172. } else {
  173. System.out.println(" address unknown");
  174. }
  175.  
  176. if (!(person.getPhoneNumber().isEmpty() || person.getPhoneNumber() == null)) {
  177. System.out.println(" phone numbers:");
  178. for (int i = 0; i < person.getPhoneNumber().size(); i++) {
  179. System.out.println(" " + person.getPhoneNumber().get(i));
  180. }
  181. } else {
  182. System.out.println(" phone number not found");
  183. }
  184. } else if (isFromInterface) {
  185. System.out.println("not found");
  186. }
  187. }
  188.  
  189. public void command5() {
  190. searchInformationInterface();
  191. }
  192.  
  193. public void removeInformation() {
  194. System.out.print("whose information: ");
  195. String name = this.scanner.nextLine();
  196.  
  197. if (this.map.containsKey(name)) {
  198. this.map.remove(name);
  199. }
  200. }
  201.  
  202. public void command6() {
  203. removeInformation();
  204. }
  205.  
  206. public void filteredSearch() {
  207. System.out.print("keyword (if empty, all listed): ");
  208. String keyWord = this.scanner.nextLine();
  209. keyWord = keyWord.trim().toLowerCase();
  210.  
  211. List<String> listPeopleKeyWord = new ArrayList<String>();
  212. List<Person> list = new ArrayList<Person>(this.map.values());
  213.  
  214. for (Person person : list) {
  215. if (person.getName().contains(keyWord)) {
  216. listPeopleKeyWord.add(person.getName());
  217. } else if (!person.isAddressEmpty()) {
  218. if (person.getAddress().contains(keyWord)) {
  219. listPeopleKeyWord.add(person.getName());
  220. }
  221. }
  222. }
  223.  
  224. if (!listPeopleKeyWord.isEmpty()) {
  225. Collections.sort(listPeopleKeyWord);
  226.  
  227. for (String name : listPeopleKeyWord) {
  228. System.out.println(" " + name);
  229. searchInformation(name, false);
  230. }
  231. } else {
  232. System.out.println(" keyword not found");
  233. }
  234.  
  235. }
  236.  
  237. public void command7() {
  238. filteredSearch();
  239. }
  240. }
  241.  
  242. /*
  243. * To change this license header, choose License Headers in Project Properties.
  244. * To change this template file, choose Tools | Templates
  245. * and open the template in the editor.
  246. */
  247. package phonesearch;
  248.  
  249. import java.util.ArrayList;
  250. import java.util.List;
  251.  
  252. /**
  253. *
  254. * @author cajo
  255. */
  256. public class Person implements Comparable<Person> {
  257.  
  258. private String name;
  259. private String street;
  260. private String city;
  261. private String address;
  262. private List<String> phoneNumberList;
  263. private boolean isAddressEmpty;
  264.  
  265. public Person(String name, String phoneNumber, String street, String city) {
  266. this.name = name;
  267. this.street = street;
  268. this.city = city;
  269. this.phoneNumberList = new ArrayList<String>();
  270. this.phoneNumberList.add(phoneNumber);
  271. this.address = street + " " + city;
  272. this.isAddressEmpty = false;
  273. }
  274.  
  275. public Person(String name, String phoneNumber) {
  276. this.name = name;
  277. this.phoneNumberList = new ArrayList<String>();
  278. this.phoneNumberList.add(phoneNumber);
  279. this.isAddressEmpty = true;
  280. //this.street = "";
  281. //this.city = "";
  282. }
  283.  
  284. public Person(String name, String street, String city) {
  285. this.name = name;
  286. this.street = street;
  287. this.city = city;
  288. this.phoneNumberList = new ArrayList<String>();
  289. this.address = street + " " + city;
  290. this.isAddressEmpty = false;
  291. }
  292.  
  293. public String getName() {
  294. return this.name;
  295. }
  296.  
  297. public List getPhoneNumber() {
  298. return this.phoneNumberList;
  299. }
  300.  
  301. public String getAddress() {
  302. return this.address;
  303. }
  304.  
  305. public void setName(String name) {
  306. this.name = name;
  307. }
  308.  
  309. public void setPhoneNumber(String phoneNumber) {
  310. this.phoneNumberList.add(phoneNumber);
  311. }
  312.  
  313. public void addPhoneNumber(String phoneNumber) {
  314. setPhoneNumber(phoneNumber);
  315. }
  316.  
  317. public void setAddress(String street, String city) {
  318. setStreet(street);
  319. setCity(city);
  320. this.address = street + " " + city;
  321. this.isAddressEmpty = false;
  322. }
  323.  
  324. public void setStreet(String street) {
  325. this.street = street;
  326. }
  327.  
  328. public void setCity(String city) {
  329. this.city = city;
  330. }
  331.  
  332. public String getStreet() {
  333. return this.street;
  334. }
  335.  
  336. public String getCity() {
  337. return this.city;
  338. }
  339.  
  340. public boolean isAddressEmpty() {
  341. return this.isAddressEmpty;
  342. }
  343.  
  344. public int compareTo(Person person) {
  345. String alphabet = "abcdefghijklmnopqrstuvwxyz";
  346.  
  347. int i = 0;
  348. int x = 0;
  349. int charAt = -1;
  350.  
  351. while (i == x) {
  352. charAt++;
  353.  
  354. for (i = 0; i <= 26; i++) {
  355. if (this.getName().charAt(charAt) == alphabet.charAt(i)) {
  356. break;
  357. }
  358. }
  359.  
  360. for (x = 0; x <= 26; x++) {
  361. if (person.getName().charAt(charAt) == alphabet.charAt(x)) {
  362. break;
  363. }
  364. }
  365. }
  366.  
  367. return i - x;
  368. }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement