Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- //------------------------------------------------
- //EXCEPTIONS
- @SuppressWarnings("serial")
- class InvalidNumberException extends Exception {
- public InvalidNumberException (String number) {
- super("This is not a valid number: "+ number);
- }
- }
- @SuppressWarnings("serial")
- class MissingEssentialInformationException extends Exception {
- public MissingEssentialInformationException(String razlog){
- super(razlog);
- }
- }
- @SuppressWarnings("serial")
- class ContactAlreadyExsistsException extends Exception {
- public ContactAlreadyExsistsException(String name){
- super("Vekje postoi kontakt so ime '" + name + "' i preklopuvacki broj!");
- }
- }
- //END EXCEPTIONS
- //-------------------------------------------------
- //CLASS ADDRESS -----------------------------------
- class Address {
- String ulica;
- String broj;
- String grad;
- String drzava;
- public Address(){
- ulica = "";
- broj = "";
- grad = "";
- drzava = "";
- }
- public Address(String ulica, String broj, String grad, String drzava){
- this.ulica = ulica;
- this.broj = broj;
- this.grad = grad;
- this.drzava = drzava;
- }
- @Override
- public String toString(){
- return "Ulica: " + this.ulica + ", Broj: " + this.broj + ", Grad: " + this.grad + ", Drzava: " + this.drzava;
- }
- }
- //END CLASS ADDRESS -----------------------------------
- //CLASS PHONENUMBER -----------------------------------
- class PhoneNumber{
- String number;
- public PhoneNumber(){
- number = "";
- }
- public PhoneNumber(String number) throws InvalidNumberException{
- if(number.equals("")) this.number = "";
- else {
- for(int i=0;i<number.length();i++){
- if(!Character.isDigit(number.charAt(i)))
- throw new InvalidNumberException(number);
- }
- this.number = number;
- }
- }
- public String getPhoneNumber(){
- return this.number;
- }
- public void setPhoneNumber(String number) throws InvalidNumberException{
- for(int i=0;i<number.length();i++){
- if(!Character.isDigit(number.charAt(i)))
- throw new InvalidNumberException(number);
- }
- this.number = number;
- }
- @Override
- public String toString(){
- return this.number;
- }
- }
- //END CLASS PHONENUMBER -----------------------------------
- //CLASS CONTACT -----------------------------------
- class Contact{
- String ime;
- String prezime;
- Address adresa;
- PhoneNumber domasen;
- PhoneNumber mobilen;
- PhoneNumber drug;
- String email;
- public Contact(String ime, String prezime, Address adresa, PhoneNumber domasen, PhoneNumber mobilen, PhoneNumber drug, String email) throws MissingEssentialInformationException {
- this.ime = ime;
- this.prezime = prezime;
- if((this.ime.equals(""))&&(this.prezime.equals("")))
- throw new MissingEssentialInformationException("Mora da e specificirano bar ime ILI bar prezime");
- this.adresa = adresa;
- if(domasen == null)
- try {
- this.domasen = new PhoneNumber("");
- } catch (InvalidNumberException e) {
- System.out.println("OPA: " + e.getMessage());
- }
- else this.domasen = domasen;
- if(mobilen == null)
- try {
- this.mobilen = new PhoneNumber("");
- } catch (InvalidNumberException e) {
- System.out.println("OPA: " + e.getMessage());
- }
- else this.mobilen = mobilen;
- if(drug == null)
- try {
- this.drug = new PhoneNumber("");
- } catch (InvalidNumberException e) {
- System.out.println("OPA: " + e.getMessage());
- }
- else this.drug = drug;
- if((this.domasen.getPhoneNumber().equals(""))&&(this.mobilen.getPhoneNumber().equals(""))&&(this.drug.getPhoneNumber().equals("")))
- throw new MissingEssentialInformationException("Mora da e specificiran bar eden telefonski broj");
- this.email = email;
- }
- @Override
- public String toString(){
- String finalno = "";
- if(!(this.ime.equals(""))) finalno += "Ime: " + this.ime;
- if(!(this.prezime.equals(""))) finalno += "\nPrezime: " + this.prezime;
- finalno += "\nAdresa: " + this.adresa;
- if(!(this.domasen.getPhoneNumber().equals(""))) finalno += "\nDomasen: " + this.domasen;
- if(!(this.mobilen.getPhoneNumber().equals(""))) finalno += "\nMobilen: " + this.mobilen;
- if(!(this.drug.getPhoneNumber().equals(""))) finalno += "\nDrug: " + this.drug;
- if(!(this.email.equals(""))) finalno += "\nEmail: " + this.email;
- finalno += "\n ---------------------------";
- return finalno;
- }
- }
- //END CLASS CONTACT -----------------------------------
- //CLASS PHONEBOOK -----------------------------------
- class Phonebook {
- Contact [] kontakti;
- int brojNaKontakti;
- public Phonebook(){
- kontakti = new Contact[100];
- brojNaKontakti = 0;
- }
- public void sort(){
- for(int j = 0; j < brojNaKontakti; j++) {
- for(int i = j + 1; i < brojNaKontakti; i++) {
- String imeprezime1 = kontakti[i].prezime + kontakti[i].ime;
- String imeprezime2 = kontakti[j].prezime + kontakti[j].ime;
- if(imeprezime1.toLowerCase().compareTo(imeprezime2.toLowerCase()) < 0) {
- Contact t = kontakti[j];
- kontakti[j] = kontakti[i];
- kontakti[i] = t;
- }
- }
- }
- }
- public void addContact(Contact kontakt) throws ContactAlreadyExsistsException{
- boolean merged = false;
- for(int i=0;i<brojNaKontakti;i++){
- if((kontakti[i].ime.equals(kontakt.ime))&&(kontakti[i].prezime.equals(kontakt.prezime))&&
- (kontakti[i].adresa.equals(kontakt.adresa)))
- {
- if(!mergeContacts(kontakti[i], kontakt)) throw new ContactAlreadyExsistsException(kontakt.ime);
- merged = true;
- break;
- }
- }
- if(!merged){
- kontakti[brojNaKontakti] = kontakt;
- brojNaKontakti++;
- }
- }
- public boolean mergeContacts(Contact a, Contact b){
- if(!(a.domasen.getPhoneNumber().equals(""))&&!(b.domasen.getPhoneNumber().equals(""))) return false;
- if(!(a.mobilen.getPhoneNumber().equals(""))&&!(b.mobilen.getPhoneNumber().equals(""))) return false;
- if(!(a.drug.getPhoneNumber().equals(""))&&!(b.drug.getPhoneNumber().equals(""))) return false;
- if(a.domasen.getPhoneNumber().equals("")) a.domasen = b.domasen;
- if(a.mobilen.getPhoneNumber().equals("")) a.mobilen = b.mobilen;
- if(a.drug.getPhoneNumber().equals("")) a.drug = b.drug;
- return true;
- }
- public void display(){
- this.sort();
- for(int i=0;i<brojNaKontakti;i++){
- System.out.println(kontakti[i]);
- }
- System.out.println("----- Z END ------");
- }
- public Contact[] searchContactByName(String query){
- Contact [] rezultati = new Contact[100];
- int brojNaRezultati = 0;
- for(int i=0;i<this.brojNaKontakti;i++){
- if((kontakti[i].ime.toLowerCase().contains(query.toLowerCase()))||(kontakti[i].prezime.toLowerCase().contains(query.toLowerCase())))
- {
- rezultati[brojNaRezultati] = kontakti[i];
- brojNaRezultati++;
- }
- }
- Contact [] rezultatifinal = new Contact[brojNaRezultati];
- for(int i=0;i<brojNaRezultati;i++){
- rezultatifinal[i] = rezultati[i];
- }
- return rezultatifinal;
- }
- public Contact[] searchContactByNumber(String query){
- Contact [] rezultati = new Contact[100];
- int brojNaRezultati = 0;
- for(int i=0;i<this.brojNaKontakti;i++){
- if((kontakti[i].domasen.getPhoneNumber().equals(query))||(kontakti[i].mobilen.getPhoneNumber().equals(query))||(kontakti[i].drug.getPhoneNumber().equals(query)))
- {
- rezultati[brojNaRezultati] = kontakti[i];
- brojNaRezultati++;
- }
- }
- Contact [] rezultatifinal = new Contact[brojNaRezultati];
- for(int i=0;i<brojNaRezultati;i++){
- rezultatifinal[i] = rezultati[i];
- }
- return rezultatifinal;
- }
- public void removeContact(Contact a){
- for(int i=0;i<brojNaKontakti;i++){
- if((kontakti[i].ime.equals(a.ime))&&(kontakti[i].prezime.equals(a.prezime))&&
- (kontakti[i].adresa.equals(a.adresa)))
- {
- for(int j=i;j<brojNaKontakti-1;j++){
- kontakti[j] = kontakti[j+1];
- }
- kontakti[brojNaKontakti] = null;
- brojNaKontakti--;
- }
- }
- }
- public void saveToFile(String filename){
- PrintWriter printWriter = null ;
- try {
- printWriter = new PrintWriter (new FileWriter(filename));
- for (Contact kontakt : kontakti) {
- if(kontakt==null) break;
- printWriter.println(kontakt.ime+","+kontakt.prezime+","+kontakt.adresa.ulica+","+kontakt.adresa.broj+","+kontakt.adresa.grad+","+kontakt.adresa.drzava+","+kontakt.domasen.getPhoneNumber()+","+kontakt.mobilen.getPhoneNumber()+","+kontakt.drug.getPhoneNumber()+","+kontakt.email);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- printWriter.close();
- }
- }
- public void importFromFile(String filename) throws MissingEssentialInformationException, InvalidNumberException, ContactAlreadyExsistsException{
- Scanner scanner = new Scanner(System.in);
- BufferedReader fileReader = null;
- try {
- fileReader = new BufferedReader(new FileReader(filename));
- String line = null ;
- while ((line = fileReader.readLine())!= null) {
- String [] parts = line.split(",");
- String [] delovi = new String[10];
- int i;
- for(i = 0 ; i<parts.length;i++){
- delovi[i] = parts[i];
- }
- while(i<delovi.length){
- delovi[i] = "";
- i++;
- }
- Address adresa = new Address(delovi[2], delovi[3], delovi[4], delovi[5]);
- Contact kontakt = new Contact(delovi[0], delovi[1], adresa, new PhoneNumber(delovi[6]), new PhoneNumber(delovi[7]), new PhoneNumber(delovi[8]), delovi[9]);
- this.addContact(kontakt);
- }
- } catch (IOException e) {
- e.printStackTrace ();
- } finally {
- try {
- fileReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void loadFromFile(String filename) throws MissingEssentialInformationException, InvalidNumberException, ContactAlreadyExsistsException{
- Scanner scanner = new Scanner(System.in);
- BufferedReader fileReader = null;
- Phonebook tmpkniga = new Phonebook();
- try {
- fileReader = new BufferedReader(new FileReader(filename));
- String line = null ;
- while ((line = fileReader.readLine())!= null) {
- String [] parts = line.split(",");
- String [] delovi = new String[10];
- int i;
- for(i = 0 ; i<parts.length;i++){
- delovi[i] = parts[i];
- }
- while(i<delovi.length){
- delovi[i] = "";
- i++;
- }
- Address adresa = new Address(delovi[2], delovi[3], delovi[4], delovi[5]);
- Contact kontakt = new Contact(delovi[0], delovi[1], adresa, new PhoneNumber(delovi[6]), new PhoneNumber(delovi[7]), new PhoneNumber(delovi[8]), delovi[9]);
- tmpkniga.addContact(kontakt);
- }
- } catch (IOException e) {
- e.printStackTrace ();
- } finally {
- try {
- fileReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- tmpkniga.display();
- }
- }
- //END CLASS PHONEBOOK -----------------------------------
- public class PhonebookTest {
- public static void main(String[] args) {
- Phonebook kniga = new Phonebook();
- Address Kozle = new Address("Kozle", "51b", "Skopje", "Makedonija");
- Address Meckin = new Address("Meckin Kamen", "53", "Skopje", "Makedonija");
- try{
- /*Contact Gorjan = new Contact("Gorjan", "Jovanovski", Kozle, new PhoneNumber("38977751563"), null, null, "");
- Contact GorjanLos = new Contact("Gorjan", "Jovanovski", Kozle, new PhoneNumber("38977751563"), null, null, "");
- Contact GorjanDobar = new Contact("Aetre", "Jovanovski", Kozle, null, new PhoneNumber("023085631"), null, "");
- Contact Bojan = new Contact("Bojan", "Janevski", Meckin, null, new PhoneNumber("38977123456"), null, "");
- kniga.addContact(Gorjan);
- kniga.addContact(Bojan);
- //Dodadi poklopuvanje kontakt
- //kniga.addContact(GorjanLos);
- //Dodaj ist kontakt bez poklokuvanje
- kniga.addContact(GorjanDobar);
- //Samo izlistaj gi kontaktite od fajlot
- kniga.loadFromFile("fajl.txt");
- //Vnesi gi vo segasnata biblioteka kontaktite od fajlot
- kniga.importFromFile("fajl.txt");
- kniga.display();
- */
- }
- catch(Exception e){
- System.out.println("Opa Cupa, Exception: " + e.getMessage());
- }
- finally{
- }
- //Generiraj fajl.txt datoteka so kontaktite
- //kniga.saveToFile("fajl.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement