Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class InvalidNameException extends Exception {
- /**
- *
- */
- protected String name;
- private static final long serialVersionUID = 1L;
- public InvalidNameException() {
- super("Invalid Name");
- }
- public InvalidNameException(String name) {
- super();
- this.name = name;
- }
- }
- class InvalidNumberException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InvalidNumberException() {
- super("Invalid Number");
- }
- public InvalidNumberException(String message) {
- super(message);
- }
- }
- class MaximumSizeExceddedException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public MaximumSizeExceddedException() {
- super("Maximum size excedded");
- }
- public MaximumSizeExceddedException(String message) {
- super(message);
- }
- }
- class InvalidFormatException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InvalidFormatException() {
- super("Invalid Format");
- }
- public InvalidFormatException(String message) {
- super(message);
- }
- }
- class Contact {
- private String name;
- private ArrayList<String> phoneNumbers = new ArrayList<String>();
- private static final String[] VALID_PREFIX = {"070", "071", "072", "075", "076", "077", "078"};
- public Contact(String name, String... phoneNumbers) throws InvalidNameException, InvalidNumberException, MaximumSizeExceddedException {
- if (name.length() < 4 || name.length() > 10 || !containsLettersAndNumbers(name)) throw new InvalidNameException();
- if (!arePhonesValid(phoneNumbers)) throw new InvalidNumberException();
- if (phoneNumbers.length > 5) throw new MaximumSizeExceddedException();
- this.name = name;
- for (int i = 0; i < phoneNumbers.length; i++)
- this.phoneNumbers.add(phoneNumbers[i]);
- }
- public String getName() {
- return name;
- }
- public static boolean containsLettersAndNumbers(String s) {
- for (int i = 0; i < s.length(); i++)
- if (!(Character.isLetterOrDigit(s.charAt(i))))
- return false;
- return true;
- }
- public static boolean containsDigitsOnly(String s) {
- for (int i = 0; i < s.length(); i++)
- if (!(Character.isDigit(s.charAt(i))))
- return false;
- return true;
- }
- public boolean isValid(String phoneNumber) {
- if (phoneNumber.length() == 9 && containsDigitsOnly(phoneNumber)) {
- String prefix = phoneNumber.substring(0, 3);
- for (int i = 0; i < VALID_PREFIX.length; i++)
- if (prefix.compareTo(VALID_PREFIX[i]) == 0)
- return true;
- }
- return false;
- }
- public boolean arePhonesValid(String[] phoneNumbers) {
- for (String phoneNumber : phoneNumbers)
- if (!isValid(phoneNumber))
- return false;
- return true;
- }
- public boolean equals(Object o) {
- if (o == null) return false;
- if (getClass() != o.getClass()) return false;
- Contact that = (Contact) o;
- return this.name.equals(that.name) && this.phoneNumbers.size() == that.phoneNumbers.size()
- && this.phoneNumbers.equals(that.phoneNumbers);
- }
- public String[] getNumbers() {
- String[] temp = new String[phoneNumbers.size()];
- temp = phoneNumbers.toArray(temp);
- Arrays.sort(temp);
- return temp;
- }
- public void addNumber(String phoneNumber) throws MaximumSizeExceddedException, InvalidNumberException {
- if (isValid(phoneNumber)) {
- if (phoneNumbers.size() > 5) throw new MaximumSizeExceddedException();
- phoneNumbers.add(phoneNumber);
- } else {
- throw new InvalidNumberException();
- }
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(this.name + "\n" + Integer.toString(this.phoneNumbers.size()) + "\n");
- String[] lexiOrdered = new String[getNumbers().length];
- lexiOrdered = getNumbers();
- for (int i = 0; i < lexiOrdered.length; i++)
- sb.append(lexiOrdered[i] + "\n");
- return sb.toString();
- }
- public static Contact valueOf(String s) throws InvalidFormatException {
- String[] temp = s.split("\n");
- String[] phoneNumbers = new String[Integer.parseInt(temp[1])];
- for (int i = 0; i < phoneNumbers.length; i++)
- phoneNumbers[i] = temp[i + 2];
- try {
- return new Contact(temp[0], phoneNumbers);
- } catch (Exception e) {
- throw new InvalidFormatException();
- }
- }
- public int hashCode() {
- return phoneNumbers.hashCode();
- }
- }
- class PhoneBook {
- private ArrayList<Contact> contacts = new ArrayList<Contact>();
- public PhoneBook() {
- }
- public void addContact(Contact contact) throws MaximumSizeExceddedException, InvalidNameException {
- if (contacts.size() > 250) throw new MaximumSizeExceddedException();
- if (!contacts.contains(contact)) {
- contacts.add(contact);
- } else {
- throw new InvalidNameException(contact.getName());
- }
- }
- public Contact getContactForName(String name) {
- for (int i = 0; i < contacts.size(); i++)
- if (contacts.get(i).getName().equals(name))
- return contacts.get(i);
- return null;
- }
- public int numberOfContacts() {
- return contacts.size();
- }
- public Contact[] getContacts() {
- Contact[] contactsArray = new Contact[contacts.size()];
- contactsArray = contacts.toArray(contactsArray);
- Contact temp;
- for (int i = 0; i < contacts.size(); i++) {
- for (int j = 1; j < contacts.size() - i; j++) {
- if (contactsArray[j - 1].getName().compareTo(contactsArray[j].getName()) > 0) {
- temp = contactsArray[j - 1];
- contactsArray[j - 1] = contactsArray[j];
- contactsArray[j] = temp;
- }
- }
- }
- return contactsArray;
- }
- public boolean removeContact(String name) {
- boolean foundName = false;
- for (int i = 0; i < contacts.size(); i++) {
- if (contacts.get(i).getName().equals(name)) {
- foundName = true;
- contacts.remove(i);
- }
- }
- return foundName;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- Contact[] contactsToString = new Contact[getContacts().length];
- contactsToString = getContacts();
- for (int i = 0; i < contactsToString.length; i++)
- sb.append(contactsToString[i].toString() + "\n");
- return sb.toString();
- }
- public static boolean saveAsTextFile(PhoneBook phonebook, String path) throws IOException {
- PrintWriter out = null;
- try {
- out = new PrintWriter(new FileOutputStream(path));
- out.print(phonebook);
- out.close();
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- public static PhoneBook loadFromTextFile(String path) throws IOException, InvalidFormatException,
- MaximumSizeExceddedException, InvalidNameException, InvalidNumberException {
- int noOfContacts = 0;
- PhoneBook phoneBook = new PhoneBook();
- Scanner inputStream = null;
- inputStream = new Scanner(new FileInputStream(path));
- try {
- while (inputStream.hasNextLine()) {
- noOfContacts = 0;
- StringBuilder contact = new StringBuilder();
- contact.append(inputStream.nextLine() + "\n");
- noOfContacts = Integer.parseInt(inputStream.nextLine());
- contact.append(Integer.toString(noOfContacts) + "\n");
- for (int i = 0; i < noOfContacts; i++)
- contact.append(inputStream.nextLine() + "\n");
- phoneBook.addContact(Contact.valueOf(contact.toString()));
- inputStream.nextLine();
- }
- } catch (InvalidFormatException e) {
- throw new InvalidFormatException();
- }
- inputStream.close();
- return phoneBook;
- }
- public Contact[] getContactsForNumber(String numberPrefix) {
- boolean hasPrefix = false;
- ArrayList<Contact> contactsWithPrefix = new ArrayList<Contact>();
- for (int i = 0; i < contacts.size(); i++) {
- String[] numbers = contacts.get(i).getNumbers();
- hasPrefix = false;
- for (int j = 0; j < numbers.length; j++) {
- if (numbers[j].substring(0, 3).equals(numberPrefix)) {
- hasPrefix = true;
- break;
- }
- }
- if (hasPrefix) contactsWithPrefix.add(contacts.get(i));
- }
- Contact[] temp = new Contact[contactsWithPrefix.size()];
- temp = contactsWithPrefix.toArray(temp);
- return temp;
- }
- public boolean equals(Object o) {
- if (o == null) return false;
- if (getClass() != o.getClass()) return false;
- PhoneBook that = (PhoneBook) o;
- /*System.out.println("PRVA NIZAAAAAAAAAAAAaaaaaa");
- for (int i = 0; i < contacts1.length; i++)
- System.out.println(contacts1[i]);
- System.out.println("VTORA NIZAAAAAAAAAAAAaaaaaa");
- for (int i = 0; i < contacts2.length; i++)
- System.out.println(contacts2[i]);*/
- return this.contacts.equals(that.contacts);
- }
- }
- 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("AAAAAAAAAAAAAAA");
- System.out.println(Arrays.toString(phonebook.getContacts()));
- //System.out.println("AAAAAAAAAAAAAAA");
- System.out.println(phonebook.toString());
- //System.out.println("AAAAAAAAAAAAAAA");
- 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