Advertisement
Guest User

Untitled

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