Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.util.Arrays;
- class InvalidFormatException extends Exception {
- public InvalidFormatException() {
- super();
- }
- }
- class InvalidNameException extends Exception {
- public String name;
- public InvalidNameException() {
- super();
- }
- public InvalidNameException(String name) {
- super();
- this.name = name;
- }
- }
- class MaximumSizeExceddedException extends Exception {
- public MaximumSizeExceddedException() {
- super();
- }
- }
- class InvalidNumberException extends Exception {
- public InvalidNumberException() {
- super();
- }
- }
- class Contact implements Comparable<Contact> {
- private String Name;
- private List<String> Numbers;
- public Contact(String name, String... phonenumber) throws InvalidNameException, InvalidNumberException, MaximumSizeExceddedException {
- checkName(name);
- checkPhoneNumbers(phonenumber);
- this.Name = name;
- this.Numbers = new ArrayList<>();
- this.Numbers.addAll(Arrays.asList(phonenumber));
- }
- private void checkPhoneNumbers(String[] phonenumber) throws InvalidNumberException, MaximumSizeExceddedException {
- if (phonenumber.length > 5)
- throw new MaximumSizeExceddedException();
- for (int i = 0; i < phonenumber.length; i++) {
- checkPhoneNumberPrefix(phonenumber[i]);
- }
- }
- private void checkPhoneNumberPrefix(String s) throws InvalidNumberException {
- if (s.length() != 9)
- throw new InvalidNumberException();
- String prefix = s.substring(0, 3);
- if (!(prefix.equals("070") || (prefix.equals("071")) || (prefix.equals("072"))
- || (prefix.equals("075")) || (prefix.equals("076")) || (prefix.equals("077")) || (prefix.equals("078"))))
- throw new InvalidNumberException();
- for (int i = 0; i < s.length(); i++) {
- if (!Character.isDigit(s.charAt(i)))
- throw new InvalidNumberException();
- }
- }
- private void checkName(String name) throws InvalidNameException {
- if (name.length() < 5 || name.length() > 10)
- throw new InvalidNameException(name);
- for (int i = 0; i < name.length(); i++) {
- if (!Character.isLetterOrDigit(name.charAt(i)))
- throw new InvalidNameException(name);
- }
- }
- public String getName() {
- return this.Name;
- }
- public String[] getNumbers() {
- List<String> copy = new ArrayList<>(Numbers);
- Collections.sort(copy);
- return copy.stream().toArray(String[]::new);////////////////????????????
- }
- @Override
- public int compareTo(Contact o) {
- return this.getName().compareTo(o.getName());
- }
- public void addNumber(String phonenumber) throws InvalidNumberException, MaximumSizeExceddedException {
- checkPhoneNumbers(new String[] {phonenumber});
- this.Numbers.add(phonenumber);
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(getName()).append("\n");
- sb.append(Numbers.size()).append("\n");
- String[] getNumbersSorted = getNumbers();
- for (String s : getNumbersSorted) {
- sb.append(s).append("\n");
- }
- return sb.toString();
- }
- }
- class PhoneBook {
- private List<Contact> Contacts;
- private int NumberOfContacts;
- public PhoneBook() {
- this.Contacts = new ArrayList<>();
- this.NumberOfContacts = 0;
- }
- public void addContact(Contact contact) throws InvalidNameException, MaximumSizeExceddedException {
- if(Contacts.stream().anyMatch(i -> i.getName().equals(contact.getName())))
- throw new InvalidNameException(contact.getName());
- if(NumberOfContacts==250)
- throw new MaximumSizeExceddedException();
- Contacts.add(contact);
- NumberOfContacts++;
- }
- public Contact getContactForName(String name) {
- Contact c = null;
- for (int i = 0; i < Contacts.size(); i++) {
- if(Contacts.get(i).getName().equals(name))
- c = Contacts.get(i);
- }
- return c;
- // if(Contacts.stream().anyMatch(i -> i.getName().equals(name)))
- // return null;
- // return Contacts.stream().filter(i -> i.getName().equals(name)).findFirst().get();
- // return Contacts.stream()
- // .filter(i -> i.getName().equals(name))
- // .findFirst()
- // .get();
- }
- public int numberOfContacts() {
- return this.NumberOfContacts;
- }
- public Contact[] getContacts() {
- List<Contact> arrayCopy = new ArrayList<>(Contacts);
- arrayCopy.sort(Comparator.comparing(Contact::getName));
- return arrayCopy.stream().toArray(Contact[]::new);///////////????????
- }
- public boolean removeContact(String name) {
- int i;
- Contact c = null;
- for (i = 0; i < Contacts.size(); i++) {
- if(Contacts.get(i).getName().equals(name)) {
- c = Contacts.get(i);
- break;
- }
- }
- if(c==null)
- return false;
- Contacts.remove(i);
- NumberOfContacts--;
- return true;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- for(int i=0; i<getContacts().length; i++)
- if(getContacts()[i]!=null)
- sb.append(getContacts()[i]).append('\n');
- return sb.toString();
- }
- public static boolean saveAsTextFile(PhoneBook phonenook, String path) throws IOException {
- File f = new File(path);
- if (phonenook == null)
- return false;
- if (!f.exists())
- f.createNewFile();
- try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
- oos.writeObject(phonenook);
- oos.close();
- }
- return true;
- }
- public static PhoneBook loadFromTextFile(String path) throws IOException, ClassNotFoundException, InvalidFormatException {
- File f = new File(path);
- if (!f.exists())
- throw new IOException();
- if (!f.canWrite())
- throw new InvalidFormatException();
- PhoneBook p = null;
- try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
- p = (PhoneBook) ois.readObject();
- ois.close();
- }
- return p;
- }
- public Contact[] getContactsForNumber(String number_prefix) {
- List<Contact> fillIt = new ArrayList<>();
- for (int i = 0; i < Contacts.size(); i++) {
- Contact c = Contacts.get(i);
- for(String number : c.getNumbers()) {
- if(number.startsWith(number_prefix)) {
- fillIt.add(c);
- }
- }
- }
- fillIt.sort(Comparator.comparing(Contact::getName));
- return fillIt.stream().toArray(Contact[]::new);
- }
- }
- 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