Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. class InvalidNameException extends Exception{
  7. //Ovoj isklucok se frla dokolku dolzinata na imeto ne e od 5 do 10 karakteri,
  8. //ili imeto sodrzi karakteri koi ne se bukvi ili brojki.
  9. String name;
  10. InvalidNameException(String name){
  11. this.name = name;
  12. }
  13. }
  14.  
  15. class MaximumSizeExceddedException extends Exception{
  16. //Ovoj isklucok se frla dokolku se napravi obid na dodavanje na element vo vekje polna niza.
  17. }
  18.  
  19. class InvalidNumberException extends Exception{
  20. //Ovoj isklucok se frla dokolku dolzinata na telefonskite broevi ne e 9, ili
  21. //dokolku prvite tri cifri od brojot ne se 070, 071, 072, 075, 076, 077 ili 078.
  22. String phonenumber;
  23. InvalidNumberException(String phonenumber){
  24. this.phonenumber = phonenumber;
  25. }
  26. }
  27.  
  28. class Contact{
  29. private String name;
  30. private String[] phonenumber;
  31.  
  32. public Contact(String name, String... phonenumber) throws InvalidNameException, MaximumSizeExceddedException, InvalidNumberException {
  33. int length = name.length();
  34. if(length < 5 || length > 10){
  35. throw new InvalidNameException(name);
  36. }
  37. for(int i = 0; i < length; i++){
  38. if(!Character.isAlphabetic(name.charAt(i))&&!Character.isDigit(name.charAt(i))){
  39. throw new InvalidNameException(name);
  40. }
  41. }
  42.  
  43. int num = phonenumber.length;
  44. if(num > 5){
  45. throw new MaximumSizeExceddedException();
  46. }
  47. this.phonenumber = new String[num];
  48. for(int i = 0; i<num; i++) {
  49. length = phonenumber[i].length();
  50. if (length != 9 || phonenumber[i].charAt(0) != '0' || phonenumber[i].charAt(1) != '7' || (phonenumber[i].charAt(2) != '0'&&phonenumber[i].charAt(2) != '1'&&phonenumber[i].charAt(2) != '2' && phonenumber[i].charAt(2) != '5' &&
  51. phonenumber[i].charAt(2) != '6' && phonenumber[i].charAt(2) != '7' && phonenumber[i].charAt(2) != '8')) {
  52. throw new InvalidNumberException(phonenumber[i]);
  53. }
  54. for(int j = 3; j<9; j++){
  55. if(!Character.isDigit(phonenumber[i].charAt(j))){
  56. throw new InvalidNumberException(phonenumber[i]);
  57. }
  58. }
  59. this.phonenumber[i] = phonenumber[i];
  60. }
  61. this.name = name;
  62. this.sortNumbers();
  63. }
  64.  
  65. public Contact(Contact c){
  66. name = c.name;
  67. int len = c.phonenumber.length;
  68. phonenumber = new String[len];
  69. for(int i = 0; i<len; i++){
  70. phonenumber[i] = c.phonenumber[i];
  71. }
  72. }
  73.  
  74. public String getName(){
  75. return name;
  76. }
  77.  
  78. private void sortNumbers(){
  79. int length = phonenumber.length;
  80. int[] numbers = new int[length];
  81. for(int i = 0; i<length; i++){
  82. numbers[i] = Integer.parseInt(phonenumber[i]);
  83. }
  84. for(int i = 0; i<length; i++){
  85. int min = numbers[i];
  86. int minIndex = i;
  87. for(int j = i+1; j<length; j++){
  88. if(min > numbers[j]){
  89. min = numbers[j];
  90. minIndex = j;
  91. }
  92. }
  93. if(i != minIndex){
  94. int tmp = numbers[i];
  95. numbers[i] = numbers[minIndex];
  96. numbers[minIndex] = tmp;
  97. String temp = phonenumber[i];
  98. phonenumber[i] = phonenumber[minIndex];
  99. phonenumber[minIndex] = temp;
  100. }
  101. }
  102. }
  103.  
  104. public String[] getNumbers(){
  105. int length = phonenumber.length;
  106. String[] copy = new String[length];
  107. for(int i = 0; i<length; i++){
  108. copy[i] = phonenumber[i];
  109. }
  110. return copy;
  111. }
  112.  
  113. public void addNumber(String phonenumber) throws MaximumSizeExceddedException, InvalidNumberException {
  114. if(this.phonenumber.length == 5){
  115. throw new MaximumSizeExceddedException();
  116. }
  117. if (phonenumber.length() != 9 || phonenumber.charAt(0) != '0' || phonenumber.charAt(1) != '7' || (phonenumber.charAt(2) != '0' &&
  118. phonenumber.charAt(2) != '1' && phonenumber.charAt(2) != '2' && phonenumber.charAt(2) != '5' &&
  119. phonenumber.charAt(2) != '6' && phonenumber.charAt(2) != '7' && phonenumber.charAt(2) != '8')) {
  120. throw new InvalidNumberException(phonenumber);
  121. }
  122. for(int i = 3; i<9; i++){
  123. if(!Character.isDigit(phonenumber.charAt(i))){
  124. throw new InvalidNumberException(phonenumber);
  125. }
  126. }
  127. String[] tmpPhonenumber = this.phonenumber;
  128. this.phonenumber = new String[this.phonenumber.length + 1];
  129. for(int i = 0, len = tmpPhonenumber.length; i < len; i++){
  130. this.phonenumber[i] = tmpPhonenumber[i];
  131. }
  132. this.phonenumber[tmpPhonenumber.length] = phonenumber;
  133. this.sortNumbers();
  134. }
  135.  
  136. public String toString(){
  137. StringBuilder s = new StringBuilder();
  138. int length = phonenumber.length;
  139. s.append(name);
  140. s.append("\n");
  141. s.append(length);
  142. s.append("\n");
  143. for(int i = 0; i<length; i++){
  144. s.append(phonenumber[i]);
  145. s.append("\n");
  146. }
  147. return s.toString();
  148. }
  149.  
  150. static Contact valueOf(String s){
  151. //TODO: make it
  152. return null;
  153. }
  154. }
  155.  
  156. class PhoneBook{
  157. private Contact[] contacts;
  158.  
  159. public PhoneBook(){
  160. contacts = new Contact[0];
  161. }
  162. public void addContact(Contact contact) throws MaximumSizeExceddedException, InvalidNameException {
  163. int length = contacts.length;
  164. if(length == 250){
  165. throw new MaximumSizeExceddedException();
  166. }
  167. for(int i = 0; i<length; i++){
  168. if(contacts[i].getName().equals(contact.getName())){
  169. throw new InvalidNameException(contact.getName());
  170. }
  171. }
  172. Contact[] tmp = contacts;
  173. contacts = new Contact[length + 1];
  174. for(int i = 0; i<length; i++){
  175. contacts[i] = new Contact(tmp[i]);
  176. }
  177. contacts[length] = new Contact(contact);
  178. this.sortContacts();
  179. }
  180.  
  181. public Contact getContactForName(String name){
  182. for(int i = 0, len = contacts.length; i<len; i++){
  183. if(contacts[i].getName().equals(name)){
  184. return contacts[i];
  185. }
  186. }
  187. return null;
  188. }
  189.  
  190. public int numberOfContacts(){
  191. return contacts.length;
  192. }
  193.  
  194.  
  195. private void sortContacts(){
  196. int length = contacts.length;
  197.  
  198. for(int i = 0; i<length; i++){
  199. String min = contacts[i].getName();
  200. int minIndex = i;
  201. for(int j = i+1; j<length; j++){
  202. if(min.compareTo(contacts[j].getName())>0){
  203. min = contacts[j].getName();
  204. minIndex = j;
  205. }
  206. }
  207. if(i != minIndex){
  208. Contact temp = contacts[i];
  209. contacts[i] = contacts[minIndex];
  210. contacts[minIndex] = temp;
  211. }
  212. }
  213. }
  214.  
  215. public Contact[] getContacts(){
  216. int length = contacts.length;
  217. Contact[] copy = new Contact[length];
  218. for(int i = 0; i<length; i++){
  219. copy[i] = new Contact(contacts[i]);
  220. }
  221. return copy;
  222. }
  223.  
  224. public boolean removeContact(String name){
  225. int i, len;
  226. for(i = 0, len = contacts.length; i<len; i++){
  227. if(contacts[i].getName().equals(name)){
  228. break;
  229. }
  230. }
  231. if(i == len) {
  232. return false;
  233. }
  234. else{
  235. Contact[] tmp = contacts;
  236. len--;
  237. contacts = new Contact[len];
  238. for(int j = 0; j < i; j++){
  239. contacts[j] = new Contact(tmp[j]);
  240. }
  241. for(int j = i; j<len; j++){
  242. contacts[j] = new Contact(tmp[j+1]);
  243. }
  244. return true;
  245. }
  246. }
  247.  
  248. public String toString(){
  249. StringBuilder s = new StringBuilder();
  250. for(int i = 0, len = contacts.length; i<len; i++){
  251. s.append(contacts[i].toString());
  252. s.append("\n");
  253. }
  254. return s.toString();
  255. }
  256.  
  257. static public boolean saveAsTextFile(PhoneBook phonebook,String path) throws IOException, FileNotFoundException {
  258. try {
  259. File tmp = new File(path);
  260. tmp.createNewFile();
  261. PrintWriter pw = new PrintWriter(tmp);
  262. pw.print(phonebook.toString());
  263. pw.flush();
  264. pw.close();
  265. }
  266. catch(FileNotFoundException e){
  267. return false;
  268. }
  269. return true;
  270. }
  271.  
  272. static public PhoneBook loadFromTextFile(String path) throws IOException, InvalidNumberException, InvalidNameException, MaximumSizeExceddedException {
  273. BufferedReader br = new BufferedReader(new FileReader(new File(path)));
  274. String current;
  275. PhoneBook r = new PhoneBook();
  276. while ((current = br.readLine()) != null) {
  277. String name = current;
  278. int num = Integer.parseInt(br.readLine());
  279. String[] phone = new String[num];
  280. for(int i = 0; i<num; i++){
  281. phone[i] = br.readLine();
  282. }
  283. Contact c = new Contact(name, phone);
  284. br.readLine();
  285. }
  286. return r;
  287. }
  288.  
  289. public Contact[] getContactsForNumber(String number_prefix){
  290. int length = contacts.length;
  291. int counter = 0;
  292. Contact[] c = new Contact[0];
  293. for(int i = 0; i<length; i++){
  294. String[] phonenumber = contacts[i].getNumbers();
  295. int len1 = number_prefix.length();
  296. int len2 = phonenumber.length;
  297. for(int j = 0; j<len2; j++){
  298. if(phonenumber[j].substring(0,len1).equals(number_prefix)){
  299. Contact[] tmp = c;
  300. c = new Contact[counter+1];
  301. for(int k = 0; k<counter; k++){
  302. c[k] = new Contact(tmp[k]);
  303. }
  304. c[counter] = new Contact(contacts[i]);
  305. counter++;
  306. break;
  307. }
  308. }
  309. }
  310. return c;
  311. }
  312. }
  313.  
  314. public class PhonebookTester {
  315.  
  316. public static void main(String[] args) throws Exception {
  317. Scanner jin = new Scanner(System.in);
  318. String line = jin.nextLine();
  319. switch( line ) {
  320. case "test_contact":
  321. testContact(jin);
  322. break;
  323. case "test_phonebook_exceptions":
  324. testPhonebookExceptions(jin);
  325. break;
  326. case "test_usage":
  327. testUsage(jin);
  328. break;
  329. case "test_file":
  330. testFile(jin);
  331. break;
  332. }
  333. }
  334.  
  335. private static void testFile(Scanner jin) throws Exception {
  336. PhoneBook phonebook = new PhoneBook();
  337. while ( jin.hasNextLine() )
  338. phonebook.addContact(new Contact(jin.nextLine(),jin.nextLine().split("\\s++")));
  339. String text_file = "phonebook.txt";
  340. PhoneBook.saveAsTextFile(phonebook, text_file);
  341. PhoneBook pb = PhoneBook.loadFromTextFile(text_file);
  342. if ( ! pb.equals(phonebook) ) System.out.println("Your file saving and loading doesn't seem to work right");
  343. else System.out.println("Your file saving and loading works great. Good job!");
  344. }
  345.  
  346. private static void testUsage(Scanner jin) throws Exception {
  347. PhoneBook phonebook = new PhoneBook();
  348. while ( jin.hasNextLine() ) {
  349. String command = jin.nextLine();
  350. switch ( command ) {
  351. case "add":
  352. phonebook.addContact(new Contact(jin.nextLine(),jin.nextLine().split("\\s++")));
  353. break;
  354. case "remove":
  355. phonebook.removeContact(jin.nextLine());
  356. break;
  357. case "print":
  358. System.out.println(phonebook.numberOfContacts());
  359. System.out.println(Arrays.toString(phonebook.getContacts()));
  360. System.out.println(phonebook.toString());
  361. break;
  362. case "get_name":
  363. System.out.println(phonebook.getContactForName(jin.nextLine()));
  364. break;
  365. case "get_number":
  366. System.out.println(Arrays.toString(phonebook.getContactsForNumber(jin.nextLine())));
  367. break;
  368. }
  369. }
  370. }
  371.  
  372. private static void testPhonebookExceptions(Scanner jin) {
  373. PhoneBook phonebook = new PhoneBook();
  374. boolean exception_thrown = false;
  375. try {
  376. while ( jin.hasNextLine() ) {
  377. phonebook.addContact(new Contact(jin.nextLine()));
  378. }
  379. }
  380. catch ( InvalidNameException e ) {
  381. System.out.println(e.name);
  382. exception_thrown = true;
  383. }
  384. catch ( Exception e ) {}
  385. if ( ! exception_thrown ) System.out.println("Your addContact method doesn't throw InvalidNameException");
  386. /*
  387. exception_thrown = false;
  388. try {
  389. phonebook.addContact(new Contact(jin.nextLine()));
  390. } catch ( MaximumSizeExceddedException e ) {
  391. exception_thrown = true;
  392. }
  393. catch ( Exception e ) {}
  394. if ( ! exception_thrown ) System.out.println("Your addContact method doesn't throw MaximumSizeExcededException");
  395. */
  396. }
  397.  
  398. private static void testContact(Scanner jin) throws Exception {
  399. boolean exception_thrown = true;
  400. String names_to_test[] = { "And\nrej","asd","AAAAAAAAAAAAAAAAAAAAAA","Ð?ндрејA123213","Andrej#","Andrej<3"};
  401. for ( String name : names_to_test ) {
  402. try {
  403. new Contact(name);
  404. exception_thrown = false;
  405. } catch (InvalidNameException e) {
  406. exception_thrown = true;
  407. }
  408. if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw an InvalidNameException");
  409. }
  410. String numbers_to_test[] = { "+071718028","number","078asdasdasd","070asdqwe","070a56798","07045678a","123456789","074456798","073456798","079456798" };
  411. for ( String number : numbers_to_test ) {
  412. try {
  413. new Contact("Andrej",number);
  414. exception_thrown = false;
  415. } catch (InvalidNumberException e) {
  416. exception_thrown = true;
  417. }
  418. if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw an InvalidNumberException");
  419. }
  420. String nums[] = new String[10];
  421. for ( int i = 0 ; i < nums.length ; ++i ) nums[i] = getRandomLegitNumber();
  422. try {
  423. new Contact("Andrej",nums);
  424. exception_thrown = false;
  425. } catch (MaximumSizeExceddedException e) {
  426. exception_thrown = true;
  427. }
  428. if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw a MaximumSizeExceddedException");
  429. Random rnd = new Random(5);
  430. Contact contact = new Contact("Andrej",getRandomLegitNumber(rnd),getRandomLegitNumber(rnd),getRandomLegitNumber(rnd));
  431. System.out.println(contact.getName());
  432. System.out.println(Arrays.toString(contact.getNumbers()));
  433. System.out.println(contact.toString());
  434. contact.addNumber(getRandomLegitNumber(rnd));
  435. System.out.println(Arrays.toString(contact.getNumbers()));
  436. System.out.println(contact.toString());
  437. contact.addNumber(getRandomLegitNumber(rnd));
  438. System.out.println(Arrays.toString(contact.getNumbers()));
  439. System.out.println(contact.toString());
  440. }
  441.  
  442. static String[] legit_prefixes = {"070","071","072","075","076","077","078"};
  443. static Random rnd = new Random();
  444.  
  445. private static String getRandomLegitNumber() {
  446. return getRandomLegitNumber(rnd);
  447. }
  448.  
  449. private static String getRandomLegitNumber(Random rnd) {
  450. StringBuilder sb = new StringBuilder(legit_prefixes[rnd.nextInt(legit_prefixes.length)]);
  451. for ( int i = 3 ; i < 9 ; ++i )
  452. sb.append(rnd.nextInt(10));
  453. return sb.toString();
  454. }
  455.  
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement