HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple address book in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
booksimpleaddressjava

Problem

I've answered the following question as best as I could, but I need some help with my design and am wondering if I've taken the correct approach. I would appreciate it if anyone could please point out any mistakes I've made and any improvements I could make.

Question:

Please implement an address book that allows a user to store (between successive runs of the program) the name and phone numbers of their friends, with the following functionality:

  • To be able to display the list of friends and their corresponding phone numbers sorted by their name



  • Given another address book that may or may not contain the same friends, display the list of friends that are unique to each address book (the union of all the relative complements).



For example given:

Book1 = { "Bob", "Mary", "Jane" }

Book2 = { "Mary", "John", "Jane" }


The friends that are unique to each address book is:

Book1 \ Book2 = { "Bob", "John" }


It is important to provide a solution that highlights your skills in these areas.

  • It is also important that your solution highlights your knowledge of


and approach to Agile software development.

  • The simplest solution is often the best. It is recommended that no


more than 4 – 8 hours is spent on the problem, as a sufficient
working program can be achieved in that time period.

  • The application must run and be easy to build from source. It also


must be easy to execute for us to determine if the application meets
the above requirements.

Contact Class

```
import java.io.Serializable;
import java.util.Comparator;

public class Contact implements Serializable {

private static final long serialVersionUID = 1L;

private String name;
private String primaryPhoneNumber;

public Contact(String name, String primaryPhoneNumber) {
this.name = name;
this.primaryPhoneNumber = primaryPhoneNumber;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name =

Solution

Just a few comments:

-
Overall design is fine, you have separated the requirements and functions into a
correct number of different objects with clear responsibilities, but keeping it simple without overengineering.

-
Unit Test class: your main method should not reference the test methods. In fact you don't need a main method at all. You should use the Junit4 test runner. You can run like this: java org.junit.runner.JUnitCore AddressBookTest. This will guarantee to run all your test methods. Generally people are running their tests using Maven or the IDE, so really there is probably not much need to use the command line.

-
For return values I prefer to use Collection generally so that client code does not rely on any specific implementation of the data structure. As the code evolves, if I see that client code needs a more specific collection type, then I refactor to return the more specific type. For example: Collection getUniqueContacts(List addressBooks).

-
Maybe it simplifies things to have Contact implements Serializable, Comparable, which should be easy since you already built a Comparator.

-
I'm not sure I understand the code in getUniqueContacts(). Why not just this:

public static Set getUniqueContacts(List addressBooks) {
   Set unique = new TreeSet(); // TreeSet will sort by Contact
   for (AddressBook book : addressBooks) {
      unique.addAll(book);
   }
   return unique;
}


A FEW MORE COMMENTS

I thought of a couple additional basic points you might want to consider:

-
add a toString() method to the Contact and Address book classes, which gives a nice String representation of the object. I find I use this more and more, both because it makes it easy to see things in the debugger, and also because it makes logging statements easy.

-
Instead of getUniqueContacts, maybe a better method conceptually, is AddressBook merge(List addressBooks). Taking the point above, and this point, printing unique contacts would look like this:

**

System.out.println("Merged address book=\n" + AddressBook.merge(addressBooksList));

Code Snippets

public static Set<Contact> getUniqueContacts(List<AddressBook> addressBooks) {
   Set<Contact> unique = new TreeSet<Contact>(); // TreeSet will sort by Contact
   for (AddressBook book : addressBooks) {
      unique.addAll(book);
   }
   return unique;
}
System.out.println("Merged address book=\n" + AddressBook.merge(addressBooksList));

Context

StackExchange Code Review Q#30019, answer score: 5

Revisions (0)

No revisions yet.