I am making an Address Book like application .
I have a contactType enum.
public enum ContactType
{
//just a cut down version
MOBILE,PHONE,FAX,EMAIL;
}
Then I have a Contact class
public class Contact
{
private ContactType contactType;
private String value;//e.g phone number or email
private String areaCode;//does not apply to email or mobile
//....getters and setters
}
Then I have a Person class
public class Person
{
private List<Contact>contacts;
//----Other attributes and getters and setters
}
I thought I have nailed very well :) but as it turns out probably not:( Because
I now needs to display a list of people(list of person) and in the table along with some other things (name etc)I have these columns:
Name|......|Phone|Mobile|Fax |Email
I now realised that I can not just loop through the list of people and display contact numbers since these contacts are in list inside every object in list of people.
In simple words as you might know by now already if I have a list of people e.g
List<Person>people ;
Then this is not a valid option
for(Person person: people)
{
//can not get phone number for example by going
person.getPhone();
}
but this is
for(Person person: people)
{
//but will have to get it from the list of contacts e.g
List<Contact>contacts = person.getContacts();
for(Contact contact:Contacts)
{
if (contact.getContactType.isPhone())
{
contact.getValue();
}
}
}
So I have now introduced a Rowitem Class
public class Rowitem {
private String phone;
private String fax;
private String mobile;
private String email;
....
}
and populating is like this
private void populateForDisplay(Rowitem item,List<Contact> contacts)
{
for (Contact contact : contacts)
{
if (contact.getType().isEmail()) {
item.setEmail(contact.getValue());
}
if (contact.getType().isFax()) {
item.setFax(contact.getValue());
}
if (contact.getType().isMobile()) {
item.setMobile(contact.getValue());
}
if (contact.getType().isPhone()) {
item.setPhone(contact.getValue());
}
}
}
While this approach works I just do not think it is right. Firstly I do not like the fact that I have to introduce this RowItem class and then all these if statements do not look right. How can I improve this?
The front end framework that I am using is JSF2 but probably not relevant here.
I need someone to review my approach rather than line by line code.
I have read https://codereview.stackexchange.com/faq and I think my question is with the rules if not then please guide me and I will move this.
Because not code problem I did not think will go to StackOverFlow