Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.Collection;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. class PhoneBook {
  16. public PhoneBook() {
  17. this.phonebook = new HashMap<String, String>();
  18. }
  19.  
  20. void addNewContact(String name, String phone) {
  21. if (this.phonebook.containsKey(name)) {
  22. System.out.println("The contact already exists");
  23. } else {
  24. if (this.phonebook.get(name) == phone) {
  25. System.out.println("The phone number already exists");
  26. } else {
  27. this.phonebook.put(name, phone);
  28. }
  29. }
  30. }
  31.  
  32. void deleteContact(String name){
  33. if (this.phonebook.containsKey(name)){
  34. this.phonebook.remove(name);
  35. }
  36. else{
  37. System.out.println("Invalid name");
  38. }
  39. }
  40.  
  41. String getPhoneNumber(String name){
  42. if (this.phonebook.containsKey(name)){
  43. return this.phonebook.get(name);
  44. }
  45. return "Non existing contact";
  46. }
  47.  
  48. void print(){
  49. Map<String, String> sorted = new Map<String, String>() {
  50. @Override
  51. public int size() {
  52. return 0;
  53. }
  54.  
  55. @Override
  56. public boolean isEmpty() {
  57. return false;
  58. }
  59.  
  60. @Override
  61. public boolean containsKey(Object key) {
  62. return false;
  63. }
  64.  
  65. @Override
  66. public boolean containsValue(Object value) {
  67. return false;
  68. }
  69.  
  70. @Override
  71. public String get(Object key) {
  72. return null;
  73. }
  74.  
  75. @Override
  76. public String put(String key, String value) {
  77. return null;
  78. }
  79.  
  80. @Override
  81. public String remove(Object key) {
  82. return null;
  83. }
  84.  
  85. @Override
  86. public void putAll(Map<? extends String, ? extends String> m) {
  87.  
  88. }
  89.  
  90. @Override
  91. public void clear() {
  92.  
  93. }
  94.  
  95. @Override
  96. public Set<String> keySet() {
  97. return null;
  98. }
  99.  
  100. @Override
  101. public Collection<String> values() {
  102. return null;
  103. }
  104.  
  105. @Override
  106. public Set<Entry<String, String>> entrySet() {
  107. return null;
  108. }
  109. };
  110. this.phonebook.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> sorted.put(x.getKey(), x.getValue()));
  111.  
  112. sorted.forEach(((k,v) -> System.out.println(k + " " + v)));
  113.  
  114. }
  115.  
  116. void Call(String name){
  117. if (outgoing.containsKey(name)){
  118. outgoing.get(name) = + 1;
  119. }
  120. }
  121.  
  122. private
  123. HashMap<String, String> phonebook;
  124. HashMap<String, Integer> outgoing;
  125.  
  126.  
  127. }
  128.  
  129. public class Main {
  130.  
  131. public static void main(String[] args) throws IOException {
  132.  
  133. PhoneBook phoneBook = new PhoneBook();
  134. File file = new File("C:\\Users\\anton\\Desktop\\phonebook.txt");
  135. BufferedReader br = new BufferedReader(new FileReader(file));
  136.  
  137. String p1 = "^(\\+359)((87)|(88)|(89))[2-9][0-9]{6}$";
  138. String p2 = "^(0)((87)|(88)|(89))[2-9][0-9]{6}$";
  139. String p3 = "^(00359)((87)|(88)|(89))[2-9][0-9]{6}$";
  140.  
  141. String st;
  142. while ((st = br.readLine()) != null){
  143. String[] s = st.split("\\s+");
  144. if (s.length > 1){
  145. Pattern r = Pattern.compile(p1);
  146. Matcher m = r.matcher(s[1].trim());
  147. if (m.find()){
  148. phoneBook.addNewContact(s[0], s[1]);
  149. continue;
  150. }
  151. r = Pattern.compile(p2);
  152. m = r.matcher(s[1].trim());
  153. if (m.find()){
  154. phoneBook.addNewContact(s[0], s[1]);
  155. continue;
  156. }
  157. r = Pattern.compile(p3);
  158. m = r.matcher(s[1].trim());
  159. if (m.find()){
  160. phoneBook.addNewContact(s[0], s[1]);
  161. }
  162. }
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement