Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- class Contact {
- private String name;
- private String[] numbers;
- public Contact(String n, String...number )throws InvalidNameException,InvalidNumberException,MaximumSizeExceddedException
- {
- if(n.length()<4&&n.length()>10) throw new InvalidNameException();
- for(int j=0;j<n.length();j++)
- if(!Character.isLetterOrDigit(n.charAt(j))) throw new InvalidNameException();
- this.name=n;
- String pom[]= new String[5];
- int i=0;
- for(String s:number){
- if(i>5) throw new MaximumSizeExceddedException();
- if(s.length()!=9) throw new InvalidNumberException();
- for(int z=0;z<s.length();z++)
- {
- if(!Character.isDigit(s.charAt(z))) throw new InvalidNumberException();
- }
- if(s.charAt(0)!='0' && s.charAt(1)!='7' && (s.charAt(2)!='0' ||
- s.charAt(2)!='1' || s.charAt(2)!='2' || s.charAt(2)!='5' ||
- s.charAt(2)!='6'||s.charAt(2)!='7'||s.charAt(2)!='8') ) throw new InvalidNumberException();
- pom[i]=s;
- i++;
- }
- sort(pom);
- numbers=new String[i];
- numbers=pom;
- }
- public String getName()
- {
- return name;
- }
- public String[] getNumbers()
- {
- String pom[]= new String[numbers.length];
- for(int i=0;i<numbers.length;i++)
- pom[i]=numbers[i];
- return pom;
- }
- public void addNumber(String number)throws InvalidNumberException,MaximumSizeExceddedException
- {
- if(numbers.length==5) throw new MaximumSizeExceddedException();
- if(number.length()!=9) throw new InvalidNumberException();
- for(int z=0;z<number.length();z++)
- {
- if(!Character.isDigit(number.charAt(z))) throw new InvalidNumberException();
- }
- if(number.charAt(0)!='0' && number.charAt(1)!='7' && (number.charAt(2)!='0' ||
- number.charAt(2)!='1' || number.charAt(2)!='2' || number.charAt(2)!='5' ||
- number.charAt(2)!='6'||number.charAt(2)!='7'||number.charAt(2)!='8') )
- throw new InvalidNumberException();
- String pom[]=new String[numbers.length+1];
- for(int i=0;i<numbers.length;i++)
- pom[i]=numbers[i];
- pom[numbers.length]=number;
- sort(pom);
- numbers=pom;
- }
- public String toString()
- {
- String pom="";
- pom+=name;
- pom+='\n';
- pom+=numbers.length;
- pom+='\n';
- for(int i=0;i<numbers.length;i++)
- {
- pom+=numbers[i]+"\n";
- }
- return pom;
- }
- /*public static Contact valueOf(String s)
- {
- }*/
- private void sort(String [] s)
- {
- String pom;
- for(int i=0;i<s.length-1;i++)
- {
- for(int j=i+1;j<s.length;j++)
- {
- if(s[i].compareTo(s[j])>0)
- {
- pom=s[i];
- s[i]=s[j];
- s[j]=pom;
- }
- }
- }
- }
- }
- class PhoneBook {
- private Contact niza[];
- public PhoneBook()
- {
- niza=new Contact[0];
- }
- public void addContact(Contact contact)throws MaximumSizeExceddedException,InvalidNameException
- {
- if(niza.length==250) throw new MaximumSizeExceddedException();
- for(int i=0;i<niza.length;i++)
- {
- if(niza[i].getName().equals(contact.getName()))throw new InvalidNameException("Veke postoi vakov kontakt");
- }
- Contact pom[]= new Contact[niza.length+1];
- for(int i=0;i<niza.length;i++)
- pom[i]=niza[i];
- pom[niza.length]=contact;
- sort(pom);
- niza=pom;
- }
- public Contact getContactForName(String name)
- {
- for(int i=0;i<niza.length;i++)
- {
- if(niza[i].getName().equals(name))
- return niza[i];
- }
- return null;
- }
- public int numberOfContacts()
- {
- return niza.length;
- }
- public Contact[] getContacts()
- {
- Contact [] pom= new Contact[niza.length];
- for(int i=0;i<niza.length;i++)
- pom[i]=niza[i];
- return pom;
- }
- public boolean removeContact(String name)
- {
- int i;
- for( i=0;i<niza.length;i++)
- {
- if(niza[i].getName().equals(name))
- break;
- }
- if(i==niza.length) return false;
- else{
- Contact [] pom= new Contact[niza.length-1];
- for(int j=0;j<i;j++)
- pom[j]=niza[j];
- for(int j=i;j<niza.length-1;j++)
- pom[j]=niza[j+1];
- niza=pom;
- }
- return true;
- }
- public String toString()
- {
- String pom="";
- for(int i=0;i<niza.length;i++)
- pom+=niza[i].toString();
- //Proveri dali treba da se stai uste eden red;
- return pom;
- }
- public static boolean saveAsTextFile(PhoneBook phonebook,String path )
- {
- try{
- PrintWriter write = new PrintWriter(path);
- write.print(phonebook.toString());
- write.flush();
- write.close();
- }
- catch(FileNotFoundException e)
- {
- e.getMessage();
- return false;
- }
- return true;
- }
- public static PhoneBook loadFromTextFile(String path)
- {
- PhoneBook imenik= new PhoneBook();
- Contact kontakt;
- try{
- BufferedReader br= new BufferedReader(new FileReader(path));
- while(br!=null)
- {
- String name=br.readLine();
- int numContacts= Integer.parseInt(br.readLine());
- String[] broevi= new String[numContacts];
- String broj="";
- int i=0;
- while(!broj.equals("\n"))
- {
- broj=br.readLine();
- broevi[i]=broj;
- i++;
- try{
- kontakt=new Contact(name,broevi);
- imenik.addContact(kontakt);
- }
- catch(InvalidNameException e)
- {
- System.out.println("Imeto ne e validno");
- }
- catch(InvalidNumberException e)
- {
- System.out.println("Brojot ne e validen");
- }
- catch(MaximumSizeExceddedException e)
- {
- System.out.println("Mn broevi");
- }
- }
- br.close();
- }
- }
- catch(IOException e)
- {
- System.out.printf("Datotekata so %s pateka nemoze da se otvori\n",path);
- }
- return imenik;
- }
- public Contact[] getContactsForNumber(String number_prefix)
- {
- String [] pomBroevi;
- Contact [] pom= new Contact[niza.length];
- boolean flag=false;
- int brojac=0;
- for(int i=0;i<niza.length;i++)
- {
- pomBroevi=niza[i].getNumbers();
- for(int j=0;j<pomBroevi.length;j++)
- {
- if(pomBroevi[j].contains(number_prefix))
- {
- flag=true;
- }
- }
- if(flag)
- {pom[brojac]=niza[i];
- brojac++;}
- }
- Contact[] vrati=new Contact[brojac];
- for(int i=0;i<brojac;i++)
- vrati[i]=pom[i];
- return vrati;
- }
- private void sort(Contact [] s)
- {
- Contact pom;
- for(int i=0;i<s.length-1;i++)
- {
- for(int j=i+1;j<s.length;j++)
- {
- if(s[i].getName().compareTo(s[j].getName())>0)
- {
- pom=s[i];
- s[i]=s[j];
- s[j]=pom;
- }
- }
- }
- }
- }
- class InvalidNumberException extends Exception{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InvalidNumberException()
- {
- super("Vnesovte nevaliden broj");
- }
- public InvalidNumberException(String s)
- {
- super(s);
- }
- }
- class InvalidNameException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public final String name;
- public InvalidNameException()
- {
- super("Imeto sto go vnesovte ne e validno");
- name="";
- }
- public InvalidNameException(String description)
- {
- super(description);
- name=description;
- }
- }
- class InvalidFormatException extends Exception{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InvalidFormatException()
- {
- }
- public InvalidFormatException(String s)
- {
- super(s);
- }
- }
- class MaximumSizeExceddedException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public MaximumSizeExceddedException()
- {
- super("Vnesovte poveke od 5 kontakti");
- }
- public MaximumSizeExceddedException(String s)
- {
- super(s);
- }
- }
- public class PhonebookTester {
- public static void main(String[] args) throws Exception {
- Scanner jin = new Scanner(System.in);
- String line = jin.nextLine();
- switch( line ) {
- case "test_contact":
- testContact(jin);
- break;
- case "test_phonebook_exceptions":
- testPhonebookExceptions(jin);
- break;
- case "test_usage":
- testUsage(jin);
- break;
- case "test_file":
- testFile(jin);
- break;
- }
- }
- private static void testFile(Scanner jin) throws Exception {
- PhoneBook phonebook = new PhoneBook();
- while ( jin.hasNextLine() )
- phonebook.addContact(new Contact(jin.nextLine(),jin.nextLine().split("\\s++")));
- String text_file = "phonebook.txt";
- PhoneBook.saveAsTextFile(phonebook,text_file);
- PhoneBook pb = PhoneBook.loadFromTextFile(text_file);
- if ( ! pb.equals(phonebook) ) System.out.println("Your file saving and loading doesn't seem to work right");
- else System.out.println("Your file saving and loading works great. Good job!");
- }
- private static void testUsage(Scanner jin) throws Exception {
- PhoneBook phonebook = new PhoneBook();
- while ( jin.hasNextLine() ) {
- String command = jin.nextLine();
- switch ( command ) {
- case "add":
- phonebook.addContact(new Contact(jin.nextLine(),jin.nextLine().split("\\s++")));
- break;
- case "remove":
- phonebook.removeContact(jin.nextLine());
- break;
- case "print":
- System.out.println(phonebook.numberOfContacts());
- System.out.println(Arrays.toString(phonebook.getContacts()));
- System.out.println(phonebook.toString());
- break;
- case "get_name":
- System.out.println(phonebook.getContactForName(jin.nextLine()));
- break;
- case "get_number":
- System.out.println(Arrays.toString(phonebook.getContactsForNumber(jin.nextLine())));
- break;
- }
- }
- }
- private static void testPhonebookExceptions(Scanner jin) {
- PhoneBook phonebook = new PhoneBook();
- boolean exception_thrown = false;
- try {
- while ( jin.hasNextLine() ) {
- phonebook.addContact(new Contact(jin.nextLine()));
- }
- }
- catch ( InvalidNameException e ) {
- System.out.println(e.name);
- exception_thrown = true;
- }
- catch ( Exception e ) {}
- if ( ! exception_thrown ) System.out.println("Your addContact method doesn't throw InvalidNameException");
- /*
- exception_thrown = false;
- try {
- phonebook.addContact(new Contact(jin.nextLine()));
- } catch ( MaximumSizeExceddedException e ) {
- exception_thrown = true;
- }
- catch ( Exception e ) {}
- if ( ! exception_thrown ) System.out.println("Your addContact method doesn't throw MaximumSizeExcededException");
- */
- }
- private static void testContact(Scanner jin) throws Exception {
- boolean exception_thrown = true;
- String names_to_test[] = { "And\nrej","asd","AAAAAAAAAAAAAAAAAAAAAA","�ндрејA123213","Andrej#","Andrej<3"};
- for ( String name : names_to_test ) {
- try {
- new Contact(name);
- exception_thrown = false;
- } catch (InvalidNameException e) {
- exception_thrown = true;
- }
- if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw an InvalidNameException");
- }
- String numbers_to_test[] = { "+071718028","number","078asdasdasd","070asdqwe","070a56798","07045678a","123456789","074456798","073456798","079456798" };
- for ( String number : numbers_to_test ) {
- try {
- new Contact("Andrej",number);
- exception_thrown = false;
- } catch (InvalidNumberException e) {
- exception_thrown = true;
- }
- if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw an InvalidNumberException");
- }
- String nums[] = new String[10];
- for ( int i = 0 ; i < nums.length ; ++i ) nums[i] = getRandomLegitNumber();
- try {
- new Contact("Andrej",nums);
- exception_thrown = false;
- } catch (MaximumSizeExceddedException e) {
- exception_thrown = true;
- }
- if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw a MaximumSizeExceddedException");
- Random rnd = new Random(5);
- Contact contact = new Contact("Andrej",getRandomLegitNumber(rnd),getRandomLegitNumber(rnd),getRandomLegitNumber(rnd));
- System.out.println(contact.getName());
- System.out.println(Arrays.toString(contact.getNumbers()));
- System.out.println(contact.toString());
- contact.addNumber(getRandomLegitNumber(rnd));
- System.out.println(Arrays.toString(contact.getNumbers()));
- System.out.println(contact.toString());
- contact.addNumber(getRandomLegitNumber(rnd));
- System.out.println(Arrays.toString(contact.getNumbers()));
- System.out.println(contact.toString());
- }
- static String[] legit_prefixes = {"070","071","072","075","076","077","078"};
- static Random rnd = new Random();
- private static String getRandomLegitNumber() {
- return getRandomLegitNumber(rnd);
- }
- private static String getRandomLegitNumber(Random rnd) {
- StringBuilder sb = new StringBuilder(legit_prefixes[rnd.nextInt(legit_prefixes.length)]);
- for ( int i = 3 ; i < 9 ; ++i )
- sb.append(rnd.nextInt(10));
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement