lameski

Imenik_stream

Aug 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import java.util.Collection;
  2. import java.util.Comparator;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.Set;
  8. import java.util.TreeMap;
  9. import java.util.TreeSet;
  10. import java.util.stream.Collector;
  11. import java.util.stream.Collectors;
  12.  
  13. public class PhoneBookTest {
  14.  
  15. public static void main(String[] args) {
  16. PhoneBook phoneBook = new PhoneBook();
  17. Scanner scanner = new Scanner(System.in);
  18. int n = scanner.nextInt();
  19. scanner.nextLine();
  20. for (int i = 0; i < n; ++i) {
  21. String line = scanner.nextLine();
  22. String[] parts = line.split(":");
  23. try {
  24. phoneBook.addContact(parts[0], parts[1]);
  25. } catch (DuplicateNumberException e) {
  26. System.out.println(e.getMessage());
  27. }
  28. }
  29. while (scanner.hasNextLine()) {
  30. String line = scanner.nextLine();
  31. System.out.println(line);
  32. String[] parts = line.split(":");
  33. if (parts[0].equals("NUM")) {
  34. phoneBook.contactsByNumber(parts[1]);
  35. } else {
  36. phoneBook.contactsByName(parts[1]);
  37. }
  38. }
  39. }
  40.  
  41. }
  42.  
  43. //
  44. class DuplicateNumberException extends Exception
  45. {
  46. DuplicateNumberException(String number)
  47. {
  48. super(String.format("Duplicate number: %s", number));
  49. }
  50. }
  51. class PhoneBook
  52. {
  53. TreeSet<Contact> contacts;
  54. TreeMap<String, TreeSet<Contact>> name_contacts;
  55. public PhoneBook() {
  56. // TODO Auto-generated constructor stub
  57. contacts = new TreeSet<>();
  58. name_contacts = new TreeMap<>();
  59. }
  60.  
  61. void addContact(String name, String number) throws DuplicateNumberException
  62. {
  63.  
  64. Contact contact = new Contact(name, number);
  65. if(contacts.contains(contact))
  66. throw new DuplicateNumberException(number);
  67. else
  68. contacts.add(contact);
  69.  
  70. if(!name_contacts.containsKey(name))
  71. {
  72. TreeSet<Contact> temp = new TreeSet<>();
  73. temp.add(contact)
  74. ;
  75. name_contacts.put(name, temp);
  76. }
  77. else
  78. {
  79. TreeSet<Contact> temp = name_contacts.get(name);
  80. temp.add(contact);
  81. name_contacts.put(name, temp);
  82. }
  83.  
  84. /*
  85. contacts.computeIfAbsent
  86. (number, val -> {
  87. TreeSet<Contact> conT = new TreeSet<Contact>(Comparator.comparing(Contact::getName).thenComparing(Contact::getNumber));;
  88. conT.add(new Contact(name, number));
  89. return conT;
  90. });
  91. contacts.computeIfPresent(number, (key ,value) -> {
  92. value.add(new Contact(name, number));
  93. return value;
  94. });
  95. name_contacts.compute
  96. (name, (key, val) -> {
  97. if(val==null)
  98. {
  99. val = new TreeSet<>(Comparator.comparing(Contact::getName)
  100. .thenComparing(Contact::getNumber));
  101. val.add(new Contact(name, number));
  102. }
  103. else
  104. val.add(new Contact(name, number));
  105. return val;
  106. });*/
  107.  
  108.  
  109. }
  110.  
  111. void contactsByNumber(String number)
  112. {
  113. if(!contacts.stream().anyMatch(x -> x.number.contains(number)))
  114. {
  115. System.out.println("NOT FOUND");
  116. return;
  117. }
  118. contacts.stream()
  119. .filter(x -> x.number.contains(number))
  120. //.sorted(Comparator.comparing(Contact::getName)
  121. // .thenComparing(Contact::getNumber))
  122. .forEach(System.out::println);
  123. }
  124. void contactsByName(String name)
  125. {
  126. if(!name_contacts.containsKey(name))
  127. {
  128. System.out.println("NOT FOUND");
  129. return;
  130. }
  131. name_contacts.get(name).stream()
  132. .forEach(System.out::println);
  133. }
  134. }
  135.  
  136. class Contact implements Comparable<Contact>
  137. {
  138. String name, number;
  139.  
  140. public Contact(String name, String number) {
  141. super();
  142. this.name = name;
  143. this.number = number;
  144. }
  145.  
  146. public String getName() {
  147. return name;
  148. }
  149.  
  150. public String getNumber() {
  151. return number;
  152. }
  153. @Override
  154. public String toString() {
  155. // TODO Auto-generated method stub
  156. return String.format("%s %s", name, number);
  157. }
  158.  
  159. @Override
  160. public int compareTo(Contact arg0) {
  161. // TODO Auto-generated method stub
  162. if(name.compareTo(arg0.name)==0)
  163. return number.compareTo(arg0.number);
  164. return name.compareTo(arg0.name);
  165. }
  166. }
Add Comment
Please, Sign In to add comment