Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- class InvalidNameException extends Exception{
- //Ovoj isklucok se frla dokolku dolzinata na imeto ne e od 5 do 10 karakteri,
- //ili imeto sodrzi karakteri koi ne se bukvi ili brojki.
- String name;
- InvalidNameException(String name){
- this.name = name;
- }
- }
- class MaximumSizeExceddedException extends Exception{
- //Ovoj isklucok se frla dokolku se napravi obid na dodavanje na element vo vekje polna niza.
- }
- class InvalidNumberException extends Exception{
- //Ovoj isklucok se frla dokolku dolzinata na telefonskite broevi ne e 9, ili
- //dokolku prvite tri cifri od brojot ne se 070, 071, 072, 075, 076, 077 ili 078.
- String phonenumber;
- InvalidNumberException(String phonenumber){
- this.phonenumber = phonenumber;
- }
- }
- class Contact{
- private String name;
- private String[] phonenumber;
- public Contact(String name, String... phonenumber) throws InvalidNameException, MaximumSizeExceddedException, InvalidNumberException {
- int length = name.length();
- if(length < 5 || length > 10){
- throw new InvalidNameException(name);
- }
- for(int i = 0; i < length; i++){
- if(!Character.isAlphabetic(name.charAt(i))&&!Character.isDigit(name.charAt(i))){
- throw new InvalidNameException(name);
- }
- }
- int num = phonenumber.length;
- if(num > 5){
- throw new MaximumSizeExceddedException();
- }
- this.phonenumber = new String[num];
- for(int i = 0; i<num; i++) {
- length = phonenumber[i].length();
- if (length != 9 || phonenumber[i].charAt(0) != '0' || phonenumber[i].charAt(1) != '7' || (phonenumber[i].charAt(2) != '0'&&phonenumber[i].charAt(2) != '1'&&phonenumber[i].charAt(2) != '2' && phonenumber[i].charAt(2) != '5' &&
- phonenumber[i].charAt(2) != '6' && phonenumber[i].charAt(2) != '7' && phonenumber[i].charAt(2) != '8')) {
- throw new InvalidNumberException(phonenumber[i]);
- }
- for(int j = 3; j<9; j++){
- if(!Character.isDigit(phonenumber[i].charAt(j))){
- throw new InvalidNumberException(phonenumber[i]);
- }
- }
- this.phonenumber[i] = phonenumber[i];
- }
- this.name = name;
- this.sortNumbers();
- }
- public Contact(Contact c){
- name = c.name;
- int len = c.phonenumber.length;
- phonenumber = new String[len];
- for(int i = 0; i<len; i++){
- phonenumber[i] = c.phonenumber[i];
- }
- }
- public String getName(){
- return name;
- }
- private void sortNumbers(){
- int length = phonenumber.length;
- int[] numbers = new int[length];
- for(int i = 0; i<length; i++){
- numbers[i] = Integer.parseInt(phonenumber[i]);
- }
- for(int i = 0; i<length; i++){
- int min = numbers[i];
- int minIndex = i;
- for(int j = i+1; j<length; j++){
- if(min > numbers[j]){
- min = numbers[j];
- minIndex = j;
- }
- }
- if(i != minIndex){
- int tmp = numbers[i];
- numbers[i] = numbers[minIndex];
- numbers[minIndex] = tmp;
- String temp = phonenumber[i];
- phonenumber[i] = phonenumber[minIndex];
- phonenumber[minIndex] = temp;
- }
- }
- }
- public String[] getNumbers(){
- int length = phonenumber.length;
- String[] copy = new String[length];
- for(int i = 0; i<length; i++){
- copy[i] = phonenumber[i];
- }
- return copy;
- }
- public void addNumber(String phonenumber) throws MaximumSizeExceddedException, InvalidNumberException {
- if(this.phonenumber.length == 5){
- throw new MaximumSizeExceddedException();
- }
- if (phonenumber.length() != 9 || phonenumber.charAt(0) != '0' || phonenumber.charAt(1) != '7' || (phonenumber.charAt(2) != '0' &&
- phonenumber.charAt(2) != '1' && phonenumber.charAt(2) != '2' && phonenumber.charAt(2) != '5' &&
- phonenumber.charAt(2) != '6' && phonenumber.charAt(2) != '7' && phonenumber.charAt(2) != '8')) {
- throw new InvalidNumberException(phonenumber);
- }
- for(int i = 3; i<9; i++){
- if(!Character.isDigit(phonenumber.charAt(i))){
- throw new InvalidNumberException(phonenumber);
- }
- }
- String[] tmpPhonenumber = this.phonenumber;
- this.phonenumber = new String[this.phonenumber.length + 1];
- for(int i = 0, len = tmpPhonenumber.length; i < len; i++){
- this.phonenumber[i] = tmpPhonenumber[i];
- }
- this.phonenumber[tmpPhonenumber.length] = phonenumber;
- this.sortNumbers();
- }
- public String toString(){
- StringBuilder s = new StringBuilder();
- int length = phonenumber.length;
- s.append(name);
- s.append("\n");
- s.append(length);
- s.append("\n");
- for(int i = 0; i<length; i++){
- s.append(phonenumber[i]);
- s.append("\n");
- }
- return s.toString();
- }
- static Contact valueOf(String s){
- //TODO: make it
- return null;
- }
- }
- class PhoneBook{
- private Contact[] contacts;
- public PhoneBook(){
- contacts = new Contact[0];
- }
- public void addContact(Contact contact) throws MaximumSizeExceddedException, InvalidNameException {
- int length = contacts.length;
- if(length == 250){
- throw new MaximumSizeExceddedException();
- }
- for(int i = 0; i<length; i++){
- if(contacts[i].getName().equals(contact.getName())){
- throw new InvalidNameException(contact.getName());
- }
- }
- Contact[] tmp = contacts;
- contacts = new Contact[length + 1];
- for(int i = 0; i<length; i++){
- contacts[i] = new Contact(tmp[i]);
- }
- contacts[length] = new Contact(contact);
- this.sortContacts();
- }
- public Contact getContactForName(String name){
- for(int i = 0, len = contacts.length; i<len; i++){
- if(contacts[i].getName().equals(name)){
- return contacts[i];
- }
- }
- return null;
- }
- public int numberOfContacts(){
- return contacts.length;
- }
- private void sortContacts(){
- int length = contacts.length;
- for(int i = 0; i<length; i++){
- String min = contacts[i].getName();
- int minIndex = i;
- for(int j = i+1; j<length; j++){
- if(min.compareTo(contacts[j].getName())>0){
- min = contacts[j].getName();
- minIndex = j;
- }
- }
- if(i != minIndex){
- Contact temp = contacts[i];
- contacts[i] = contacts[minIndex];
- contacts[minIndex] = temp;
- }
- }
- }
- public Contact[] getContacts(){
- int length = contacts.length;
- Contact[] copy = new Contact[length];
- for(int i = 0; i<length; i++){
- copy[i] = new Contact(contacts[i]);
- }
- return copy;
- }
- public boolean removeContact(String name){
- int i, len;
- for(i = 0, len = contacts.length; i<len; i++){
- if(contacts[i].getName().equals(name)){
- break;
- }
- }
- if(i == len) {
- return false;
- }
- else{
- Contact[] tmp = contacts;
- len--;
- contacts = new Contact[len];
- for(int j = 0; j < i; j++){
- contacts[j] = new Contact(tmp[j]);
- }
- for(int j = i; j<len; j++){
- contacts[j] = new Contact(tmp[j+1]);
- }
- return true;
- }
- }
- public String toString(){
- StringBuilder s = new StringBuilder();
- for(int i = 0, len = contacts.length; i<len; i++){
- s.append(contacts[i].toString());
- s.append("\n");
- }
- return s.toString();
- }
- static public boolean saveAsTextFile(PhoneBook phonebook,String path) throws IOException, FileNotFoundException {
- try {
- File tmp = new File(path);
- tmp.createNewFile();
- PrintWriter pw = new PrintWriter(tmp);
- pw.print(phonebook.toString());
- pw.flush();
- pw.close();
- }
- catch(FileNotFoundException e){
- return false;
- }
- return true;
- }
- static public PhoneBook loadFromTextFile(String path) throws IOException, InvalidNumberException, InvalidNameException, MaximumSizeExceddedException {
- BufferedReader br = new BufferedReader(new FileReader(new File(path)));
- String current;
- PhoneBook r = new PhoneBook();
- while ((current = br.readLine()) != null) {
- String name = current;
- int num = Integer.parseInt(br.readLine());
- String[] phone = new String[num];
- for(int i = 0; i<num; i++){
- phone[i] = br.readLine();
- }
- Contact c = new Contact(name, phone);
- br.readLine();
- }
- return r;
- }
- public Contact[] getContactsForNumber(String number_prefix){
- int length = contacts.length;
- int counter = 0;
- Contact[] c = new Contact[0];
- for(int i = 0; i<length; i++){
- String[] phonenumber = contacts[i].getNumbers();
- int len1 = number_prefix.length();
- int len2 = phonenumber.length;
- for(int j = 0; j<len2; j++){
- if(phonenumber[j].substring(0,len1).equals(number_prefix)){
- Contact[] tmp = c;
- c = new Contact[counter+1];
- for(int k = 0; k<counter; k++){
- c[k] = new Contact(tmp[k]);
- }
- c[counter] = new Contact(contacts[i]);
- counter++;
- break;
- }
- }
- }
- return c;
- }
- }
- 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