Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.Random;
- class InvalidFormatException extends Exception {
- public InvalidFormatException() {
- super("InvalidFormatException");
- }
- public InvalidFormatException(String message) {
- super(message);
- }
- }
- class InvalidNameException extends Exception {
- String name;
- public InvalidNameException(String name) {
- super("InvalidNameException");
- this.name = name;
- }
- }
- class MaximumSizeExceddedException extends Exception {
- public MaximumSizeExceddedException() {
- super("MaximumSizeExceddedException");
- }
- public MaximumSizeExceddedException(String message) {
- super(message);
- }
- }
- class InvalidNumberException extends Exception {
- public InvalidNumberException() {
- super("InvalidNumberException");
- }
- public InvalidNumberException(String message) {
- super(message);
- }
- }
- class Contact {
- String name;
- String[] phones;
- public Contact(String name, String ...phonenumber) throws InvalidNameException, MaximumSizeExceddedException, InvalidNumberException {
- setName(name);
- if (phonenumber.length > 5)
- throw new MaximumSizeExceddedException();
- phones = new String[phonenumber.length];
- for (int i=0; i<phonenumber.length; ++i)
- phones[i] = setPhone(phonenumber[i]);
- }
- public String getName() {
- return name;
- }
- public String[] getNumbers() {
- String [] copy = Arrays.copyOf(phones, phones.length);
- sort(copy);
- return copy;
- }
- public Contact(Contact c ){
- if (c.name == null)
- System.out.println("Hello");
- this.name = c.name;
- this.phones = new String[c.phones.length];
- for (int i=0; i<c.phones.length; ++i)
- this.phones[i] = c.phones[i];
- }
- public void addNumber (String number) throws InvalidNumberException, MaximumSizeExceddedException {
- if (phones.length ==5)
- throw new MaximumSizeExceddedException();
- else {
- String [] temp = new String[phones.length+1];
- int i;
- for (i=0; i<phones.length; ++i)
- temp[i] = phones[i];
- temp[i] = setPhone(number);
- phones = temp;
- }
- }
- private void sort(String [] list) {
- for (int i=0; i< list.length-1;++i) {
- for (int j=i; j<list.length; ++j) {
- if (list[i].compareTo(list[j]) > 0) {
- String temp = list[i];
- list[i] = list[j];
- list[j] = temp;
- }
- }
- }
- }
- private String setPhone(String phone ) throws InvalidNumberException{
- if (phone.length() != 9)
- throw new InvalidNumberException();
- for (int i=0; i<phone.length(); ++i) {
- if (!(Character.isDigit(phone.charAt(i))))
- throw new InvalidNumberException();
- }
- if (phone.charAt(0) != '0' || phone.charAt(1) != '7' || phone.charAt(2) == '3' ||
- phone.charAt(2) == '4' || phone.charAt(2) == '9')
- throw new InvalidNumberException();
- return phone;
- }
- private void setName(String name) throws InvalidNameException {
- if (name.length() < 4 || name.length() >10)
- throw new InvalidNameException(name);
- for (int i=0; i<name.length();++i) {
- char c = name.charAt(i);
- if (!(Character.isAlphabetic(c) || Character.isDigit(c)))
- throw new InvalidNameException(name);
- }
- this.name = name;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(name).append("\n").append(phones.length).append("\n");
- sort(phones);
- for (String phone : phones)
- sb.append(phone + "\n");
- return sb.toString();
- }
- }
- class PhoneBook {
- Contact [] contacts;
- int number;
- PhoneBook() {
- number = 0;
- contacts = new Contact[0];
- }
- public void addContact(Contact contact ) throws MaximumSizeExceddedException, InvalidNameException {
- if (number > 250)
- throw new MaximumSizeExceddedException();
- for (Contact c : contacts) {
- if (c.getName().equals(contact.getName()))
- throw new InvalidNameException(contact.getName());
- }
- Contact [] temp = new Contact[contacts.length+1];
- int i;
- for (i=0; i<contacts.length; ++i)
- temp[i] = contacts[i];
- temp[i] = contact;
- ++number;
- contacts = temp;
- }
- public Contact getContactForName(String name) {
- for (Contact contact : contacts) {
- if (contact.getName().equals(name))
- return contact;
- }
- return null;
- }
- public int numberOfContacts() {
- return number;
- }
- private void sort(Contact[] con) {
- Arrays.sort(con, new Comparator<Contact>() {
- @Override
- public int compare(Contact o1, Contact o2) {
- if (o1.getName().compareTo(o2.getName()) < 0)
- return -1;
- else if (o1.getName().compareTo(o2.getName()) > 0)
- return 1;
- return 0;
- }
- });
- }
- public Contact [] getContacts() {
- Contact [] copy = new Contact[contacts.length];
- for (int i=0; i<contacts.length; ++i)
- copy[i] = new Contact(contacts[i]);
- sort(copy);
- return copy;
- }
- public boolean removeContact(String name) {
- boolean find = false;
- Contact [] temp = new Contact[contacts.length-1];
- for (int i=0,j=0; j<contacts.length;) {
- if (name.equals(contacts[j].getName())){
- find = true;
- j++;
- continue;
- }
- temp[i] = contacts[j];
- ++i;
- ++j;
- }
- if (find) {
- contacts = temp;
- --number;
- }
- return find;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sort(contacts);
- for (Contact contact : contacts)
- sb.append(contact.toString()).append("\n");
- return sb.toString();
- }
- public static boolean saveAsTextFile(PhoneBook phonebook, String path) {
- boolean help = true;
- try {
- PrintWriter print = new PrintWriter(new FileOutputStream(path));
- print.println(phonebook);
- print.flush();
- }
- catch(FileNotFoundException e) {
- help = false;
- }
- return help;
- }
- public static PhoneBook loadFromTextFile(String path) throws FileNotFoundException, IOException, InvalidFormatException {
- try{
- BufferedReader reader = new BufferedReader(new FileReader(path));
- String line = reader.readLine();
- PhoneBook pbook = new PhoneBook();
- while ( line != null ) {
- String name = line;
- line = reader.readLine();
- int n = Integer.parseInt(line);
- String [] phone = new String[n];
- for (int i=0; i<n; ++i) {
- phone[i] = reader.readLine();
- }
- Contact c = new Contact(name,phone);
- pbook.addContact(c);
- line = reader.readLine();
- }
- return pbook;
- } catch (InvalidNameException e) {
- throw new InvalidFormatException();
- } catch (MaximumSizeExceddedException e) {
- throw new InvalidFormatException();
- } catch (InvalidNumberException e) {
- throw new InvalidFormatException();
- }
- }
- public Contact[] getContactsForNumber(String number_prefix) {
- Contact [] forn = new Contact[contacts.length];
- int k =0;
- for (int i=0; i<contacts.length; ++i) {
- for (int j=0; j<contacts[i].getNumbers().length; ++j) {
- if (contacts[i].getNumbers()[j].startsWith(number_prefix)) {
- forn[k] = new Contact(contacts[i]);
- ++k;
- break;
- }
- }
- }
- Contact [] help = new Contact[k];
- for (int i=0; i<k; ++i) {
- help[i] = new Contact(forn[i]);
- }
- return help;
- }
- }
- 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;
- }
- }
- 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