Advertisement
Parasect

Untitled

Mar 20th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1.  
  2. package PhoneBook;
  3.  
  4. import unit4.collectionsLib.Node;
  5. public class PhoneBook {
  6.  
  7.     private int count;
  8.     private Node<Contact> lst;
  9.    
  10.     public PhoneBook() {
  11.         count = 0;
  12.         lst = null;
  13.     }
  14.  
  15.     public void addContact(String name, String phone) {
  16.         Node<Contact> pos = lst;
  17.         while(pos != null && !name.equals(pos.getInfo().getName())){
  18.             pos = pos.getNext();
  19.         }
  20.         if (pos != null){
  21.             pos.getInfo().setPhone(phone);
  22.         }else{
  23.             pos = lst;
  24.             Node<Contact> prev = null;
  25.             while(pos != null && name.compareTo(pos.getInfo().getName())>0){
  26.                 prev = pos;
  27.                 pos = pos.getNext();
  28.             }
  29.             Contact c = new Contact(name, phone);
  30.             if(prev != null){
  31.                 Node<Contact> tmp = new Node<Contact>(c, pos);
  32.                 prev.setNext(tmp);
  33.             }else{
  34.                 Node<Contact> tmp = new Node<Contact>(c, lst);
  35.                 lst=tmp;
  36.             }
  37.             count++;
  38.         }
  39.         }
  40.  
  41.     public void delContact(String name) {
  42.         Node<Contact> pos = lst;
  43.         Node<Contact> prev = null;
  44.         while(pos != null){
  45.             if(pos.getInfo().getName().equals(name)){
  46.                 prev.setNext(pos.getNext());
  47.                 pos = null;
  48.             }else{
  49.                 prev = pos;
  50.                 pos = pos.getNext();
  51.             }
  52.         }
  53.     }
  54.  
  55.     public String getPhone(String name) {
  56.         Node<Contact> pos = lst;
  57.         while(pos != null){
  58.             if(pos.getInfo().getName().equals(name)){
  59.                 return pos.getInfo().getPhone();
  60.             }else{
  61.                 pos = pos.getNext();
  62.             }
  63.         }
  64.         return null;
  65.     }
  66.  
  67.     public String[] getAllContactsNames(){
  68.         String[] str = new String[countContacts()];
  69.         Node<Contact> pos = lst;
  70.         for(int i = 0; pos != null; i++){
  71.             str[i] = pos.getInfo().getName();
  72.             pos = pos.getNext();
  73.         }
  74.         return str;
  75.     }
  76.    
  77.     public int countContacts(){
  78.         count = 0;
  79.         Node<Contact> pos = lst;
  80.         while(pos != null){
  81.             count++;
  82.             pos = pos.getNext();
  83.         }
  84.         return count;
  85.     }
  86.  
  87.     public String toString() {
  88.         Node<Contact> pos = lst;
  89.         String nodeString = "";
  90.         while(pos != null){
  91.             nodeString += "" + pos.getInfo().getName() + pos.getInfo().getPhone() + "\n";
  92.         }
  93.         return nodeString;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement