Hey, so I need to write a program that simulates a phonebook. The program should contain the options to lookup a person,delete a person,insert a person, and too check the size(amount of persons inside the phonebook) now we're given a class Person, and an interface Phonebook. We're susposed to implement the interface in our class and use the methods insert,delete,lookup in order to do these tasks. This is what I've got so far;
class PhoneBookList implements PhoneBook { private int size = 0; Node head = null; Person lookup(String name) { Node p = head; int pos = 0; while( p!=null) { int comparison = p.compareTo(name); if(comparison == 0) return name; else Out.println("Person does not exist"); return -1; } } boolean insert(Person person) { Node p = head; Node prev = null; while( p!=null) { Node n = new Node(person); n.next = p; if(prev == null) { head = n; }else{ prev.next = n; } size++; } boolean delete(String name) { Node p = head; Node prev = null; while( p!=null) { prev = p; p = p.next; int comparison = p.compareTo(name); if(comparison == 0) { prev.next = p.next; return true; }else { Out.println("The person does not exist"); return false; } size--; } } int size() { Out.println(size); } static final class Node { final Person person; Node next; Node(Person person) { this.person = person; } } } }
Now I'm not sure if I am doing this right, we are also suspossed to use this compareTo method to check if the person does exist,if yes ok if not give out an error.The same analogy applies for the methods delete and insert. Am I using these methods right and if not what am I doing wrong? Thanks!