I have a nice problem - create a phone book - containing list of contacts. As a phonebook goes,
- Contacts are to be always sorted.(by name)
Can star mark certain contacts, so they have to be above all the rest.(the * contacts are ordered by the time of contact creation)
class PhoneBook{ //require an always sorted d.s TreeSet<Contact> contacts = new TreeSet<Contact>(); @Override public String toString() { return "PhoneBook [contacts=" + contacts + "]"; } public boolean addContact(Contact contact){ //validate before adding the contact. return contacts.add(contact); }
}
class Contact implements Comparable<Contact>{ String name; int phoneNo; Date timeAdded; boolean starContact; public Contact(String name, int phoneNo, Date timeAdded, boolean starContact) { super(); this.name = name; this.phoneNo = phoneNo; this.timeAdded = timeAdded; this.starContact = starContact; } @Override public int compareTo(Contact otherContact) { if(this.starContact && otherContact.starContact){ return this.timeAdded.before(otherContact.timeAdded)?-1:1; //impossible to add 2 contacts at the same time }else if(this.starContact){ return -1; }else if(otherContact.starContact){ return 1; }else{ //simple Contacts return this.name.compareTo(otherContact.name); } } @Override public String toString() { return "\nContact [name=" + name + ", timeAdded=" + timeAdded + ", starContact=" + starContact + "]"; } }
Test Code
public class MobilePhoneBookDemo {
/**
* @param args
*/
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Contact frnd1 = new Contact("Z",56,new Date(),false);
phoneBook.addContact(frnd1);
Contact frnd2 = new Contact("A",3,new Date(),false);
phoneBook.addContact(frnd2);
Contact frnd3 = new Contact("C",30,new Date(),false);
phoneBook.addContact(frnd3);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Contact ta = new Contact("Ta", 5, new Date(), true);
phoneBook.addContact(ta);
Contact ma = new Contact("Ma", 31, new Date(), true);
phoneBook.addContact(ma);
Contact baba = new Contact("Baba", 300, new Date(), true);
phoneBook.addContact(baba);
//change the priority later for one of my friends.
System.out.println(phoneBook);
frnd1.starContact = true;
System.out.println(phoneBook.contacts.contains(frnd1));
if(phoneBook.contacts.remove(frnd1)){
System.out.println("removed");
phoneBook.contacts.add(frnd1);
}
System.out.println(phoneBook);
}
}
Problems faced:
- The contains doesn't find the entry anymore, what's amiss? I did try and put an equals and a hashcode on Contact, apparently, if there is a Comparator/Comparable present, the compare* is only invoked.
- Is it fair to use a TreeSet here, or should any other datastructure be used? For eg. HashSet and then convert to a TreeSet?
- The contains() doesn't even compare for all entries in the map, it just compared against C,Ma and Ta entries. Why was that?
Questions priority according to order. I appreciate all the answers, but this is indeed a complete test case, so please try and run PhoneBook just once before providing an answer. Thanks a lot.