Advertisement
DamSi

Untitled

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