Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PhoneBook;
- import unit4.collectionsLib.Node;
- public class PhoneBook {
- private int count;
- private Node<Contact> lst;
- public PhoneBook() {
- count = 0;
- lst = null;
- }
- public void addContact(String name, String phone) {
- Node<Contact> pos = lst;
- while(pos != null && !name.equals(pos.getInfo().getName())){
- pos = pos.getNext();
- }
- if (pos != null){
- pos.getInfo().setPhone(phone);
- }else{
- pos = lst;
- Node<Contact> prev = null;
- while(pos != null && name.compareTo(pos.getInfo().getName())>0){
- prev = pos;
- pos = pos.getNext();
- }
- Contact c = new Contact(name, phone);
- if(prev != null){
- Node<Contact> tmp = new Node<Contact>(c, pos);
- prev.setNext(tmp);
- }else{
- Node<Contact> tmp = new Node<Contact>(c, lst);
- lst=tmp;
- }
- count++;
- }
- }
- public void delContact(String name) {
- Node<Contact> pos = lst;
- Node<Contact> prev = null;
- while(pos != null){
- if(pos.getInfo().getName().equals(name)){
- prev.setNext(pos.getNext());
- pos = null;
- }else{
- prev = pos;
- pos = pos.getNext();
- }
- }
- }
- public String getPhone(String name) {
- Node<Contact> pos = lst;
- while(pos != null){
- if(pos.getInfo().getName().equals(name)){
- return pos.getInfo().getPhone();
- }else{
- pos = pos.getNext();
- }
- }
- return null;
- }
- public String[] getAllContactsNames(){
- String[] str = new String[countContacts()];
- Node<Contact> pos = lst;
- for(int i = 0; pos != null; i++){
- str[i] = pos.getInfo().getName();
- pos = pos.getNext();
- }
- return str;
- }
- public int countContacts(){
- count = 0;
- Node<Contact> pos = lst;
- while(pos != null){
- count++;
- pos = pos.getNext();
- }
- return count;
- }
- public String toString() {
- Node<Contact> pos = lst;
- String nodeString = "";
- while(pos != null){
- nodeString += "" + pos.getInfo().getName() + pos.getInfo().getPhone() + "\n";
- }
- return nodeString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement