Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PhoneBook
- {
- Node<Phone> first;
- Node<Phone> last;
- public PhoneBook() // no costumer
- {
- first = null;
- last = null ;
- }
- public PhoneBook(Phone a) // Bank with only one consumer
- {
- Node <Phone> n = new Node<Phone> (a);
- first = n;
- last = first;
- }
- public void addContact(String name, String num)
- {
- Phone a = new Phone (name, num);
- Node<Phone> abc = new Node <Phone> (a);
- Node<Phone> p = first;
- //if there is something in list add to the last
- if (p != null)
- {
- Node <Phone> last = first;
- while (last.getNext() != null)
- {
- last = last.getNext();
- }
- last.setNext(abc);
- }
- //if there isnt something in list add to the beginning
- else
- {
- abc.setNext(first);
- first = abc;
- }
- }
- public void removeContact(Phone a)
- {
- Node<Phone> p = first;
- Node<Phone> save = first;
- int i = 0;
- while (!(p.getValue().getName().equals(a.getName())))
- {
- p = p.getNext();
- }
- p = first;
- if(i==0)
- {
- first = p.getNext();
- while(p != null)
- {
- p = p.getNext();
- }
- }
- else
- {
- while(save.getNext() != p)
- {
- save = save.getNext();
- }
- p = null;
- save.setNext(p);
- }
- }
- public String [] getAllContactsNames()
- {
- Node<Phone> p = first;
- int i = 0;
- while (p != null)
- {
- i++;
- p = p.getNext();
- }
- String [] s = new String [i];
- p = first;
- for(int j = 0; j < i; j++)
- {
- s[j] = p.getValue().getName();
- }
- return s;
- }
- void setPhone (String name, String phone)
- {
- Node <Phone> p = first;
- while (!(p.getValue().getName().equals(name)))
- {
- p = p.getNext();
- }
- p.getValue().setNum(phone);
- }
- public String toString()
- {
- String s = "";
- Node<Phone> p = first;
- while (p!=null)
- {
- s += "\n" + p.getValue().toString();
- p = p.getNext();
- }
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement