Advertisement
mmayoub

School, 23.09.2017, Phone book class exercise

Sep 23rd, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. Contact.java
  2. =============
  3.  
  4. public class Contact {
  5.     private String firstName;
  6.     private String lastName;
  7.     private String phone;
  8.    
  9.     public Contact(String firstName, String lastName, String phone) {
  10.         this.firstName = firstName;
  11.         this.lastName = lastName;
  12.         this.phone = phone;
  13.     }
  14.  
  15.     public Contact(Contact c) {
  16.         this.firstName = c.firstName;
  17.         this.lastName = c.lastName;
  18.         this.phone = c.phone;
  19.     }
  20.  
  21.     public String getFirstName() {
  22.         return firstName;
  23.     }
  24.  
  25.     public void setFirstName(String firstName) {
  26.         this.firstName = firstName;
  27.     }
  28.  
  29.     public String getLastName() {
  30.         return lastName;
  31.     }
  32.  
  33.     public void setLastName(String lastName) {
  34.         this.lastName = lastName;
  35.     }
  36.  
  37.     public String getPhone() {
  38.         return phone;
  39.     }
  40.  
  41.     public void setPhone(String phone) {
  42.         this.phone = phone;
  43.     }
  44.  
  45.     @Override
  46.     public String toString() {
  47.         return "Contact [firstName=" + firstName + ", lastName=" + lastName + ", phone=" + phone + "]";
  48.     }
  49.    
  50.    
  51.    
  52.    
  53.  
  54. }
  55.  
  56. PhoneBook.java
  57. ==============
  58.  
  59. public class PhoneBook {
  60.     private Contact[] myContacts;
  61.     private int current;
  62.  
  63.     public PhoneBook(int size) {
  64.         myContacts = new Contact[size];
  65.         current = 0;
  66.     }
  67.  
  68.     public boolean addContact(Contact c) {
  69.         if (current < myContacts.length) {
  70.             myContacts[current] = new Contact(c);
  71.             current += 1;
  72.             return true;
  73.         }
  74.  
  75.         return false;
  76.     }
  77.  
  78.     public boolean addContact(String fName, String lName, String Phone) {
  79.         if (current < myContacts.length) {
  80.             myContacts[current] = new Contact(fName, lName, Phone);
  81.             current += 1;
  82.             return true;
  83.         }
  84.  
  85.         return false;
  86.     }
  87.  
  88.     public void removeContact(String fName, String lName) {
  89.         for (int i = 0; i < current; i += 1) {
  90.             if (myContacts[i].getFirstName().compareTo(fName) == 0
  91.                     && myContacts[i].getLastName().compareTo(lName) == 0) {
  92.  
  93.                 for (int j = i + 1; j < current; j += 1) {
  94.                     myContacts[j - 1] = myContacts[j];
  95.                 }
  96.  
  97.                 // myContacts[current]=null;
  98.                 current -= 1;
  99.             }
  100.         }
  101.     }
  102.  
  103.     public void removeContact(String phone) {
  104.         for (int i = 0; i < current; i += 1) {
  105.             if (myContacts[i].getPhone().compareTo(phone) == 0) {
  106.  
  107.                 for (int j = i + 1; j < current; j += 1) {
  108.                     myContacts[j - 1] = myContacts[j];
  109.                 }
  110.  
  111.                 // myContacts[current]=null;
  112.                 current -= 1;
  113.             }
  114.         }
  115.     }
  116.  
  117.     public void removeAll() {
  118.         current = 0;
  119.     }
  120.  
  121.     public Contact findFirstByName(String name) {
  122.         return findNextByName(name, 0);
  123.     }
  124.  
  125.     public Contact findNextByName(String name, int from) {
  126.         for (int i = from; i < current; i += 1) {
  127.             if (myContacts[i].getFirstName().compareTo(name) == 0 || myContacts[i].getLastName().compareTo(name) == 0) {
  128.                 return myContacts[i];
  129.             }
  130.         }
  131.  
  132.         return null;
  133.     }
  134.  
  135.     public Contact findByNumber(String phone) {
  136.         for (int i = 0; i < current; i += 1) {
  137.             if (myContacts[i].getPhone().compareTo(phone) == 0) {
  138.                 return myContacts[i];
  139.             }
  140.         }
  141.  
  142.         return null;
  143.     }
  144.  
  145.     @Override
  146.     public String toString() {
  147.         String res = "Empty Phone Book";
  148.  
  149.         for (int i = 0; i < current; i += 1) {
  150.             res += myContacts[i] + "\n";
  151.         }
  152.  
  153.         return res;
  154.     }
  155. }
  156.  
  157. Tester.java
  158. ===========
  159.  
  160. public class Tester {
  161.  
  162.     public static void main(String[] args) {
  163.         PhoneBook myBook = new PhoneBook(5);
  164.  
  165.         myBook.addContact("Anan", "Samnia", "123123123");
  166.         myBook.addContact("Siraj", "Sawaed", "123456789");
  167.         myBook.addContact("Lolo", "jojo", "951623847");
  168.  
  169.         System.out.println("My phone Book\n" + myBook);
  170.  
  171.         if (!myBook.addContact("Anas", "Sawaed", "111111111"))
  172.             System.out.println("Error adding");
  173.         else
  174.             System.out.println("My phone Book\n" + myBook);
  175.  
  176.         if (myBook.findFirstByName("yamir") == null)
  177.             System.out.println("Yamir not found");
  178.         else
  179.             System.out.println(myBook.findFirstByName("yamir"));
  180.  
  181.         if (myBook.findFirstByName("Lolo") == null)
  182.             System.out.println("Lolo not found");
  183.         else
  184.             System.out.println(myBook.findFirstByName("Lolo"));
  185.  
  186.         myBook.removeContact("951623847");
  187.         System.out.println("My phone Book\n" + myBook);
  188.  
  189.         myBook.removeAll();
  190.         System.out.println("My phone Book\n" + myBook);
  191.  
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement