0
You want to create a program for the management of a telephone book. For each person are provided for the following personal information:
Surname
Name
Title
E-mail address (can not contain spaces and must contain the @ symbol)
Company
Position
For every person you can store the following telephone numbers (one for each category)
Home
Office
Mobile Phone.
You can also store a list of other phone numbers. For each of the other numbers, you must store over to the phone number a description of the number.

Here it is a homework I have to make for this evening in Java. My issue is how I can implement the various category: Home,Office,ecc...Which is the best solution to implement those category? maybe an enum?

Here it is my implementation:

import java.util.*;

public class Persona {

private String Name;
private String surname;
private String title;
private String mail_addr;
private String company
private String position;
private Phone homePhone;
private Phone officePhone;
private Phone mobilePhone;
private Collection<Phone> otherphonesList

public Persona(String n,String s,String t,String m,String c,String p,Phone hp,Phone of,Phone mp,Collection<Phone> otherphones)
{
    name=n;
    surname=s;
    title=t;
    mail_addr=m;
    company=c;
    position=p;
    homePhone=hp;
    officePhone=of;
    mobilePhone=mp;
    otherphonesList=new ArrayList<Phone>(otherphones);
}

public String getName()
{
    return name;
}

public String getSurname()
{
    return surname;
}

public String getTitle()
{
    return title;
}

public String getMail()
{
    return mail_addr;
}

public String getCompany()
{
    return company;
}

public String getPosition()
{
    return position;
}
}


public class Phone {

private String  phone;
private String description;

public Phone(String phone,String description)
{
    this.phone=phone;
    this.description=description;
}

public String getPhone()
{
    return phone;
}

public String getDescription()
{
    return description;
}


}
2
  • and don't forget the title field :) Commented Dec 6, 2011 at 12:55
  • 1
    And, please, follow Java Code Conventions. s/Name/name. Commented Dec 6, 2011 at 13:01

3 Answers 3

2

You can write a PhoneBook class with fields you need:

public class PhoneBook {
    private Phone homePhone;
    private Phone officePhone;
    private Phone mobilePhone;
    private List<Phone> otherPhones;
    ..getters/setters..
}

public class Phone {
    private String phone;
    private String description;
    ..getters/setters..
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tought to this solution...create a type phone
And use an enum for the categories. Note: For homework-type questions, it's usually better to give suggestions instead of complete solutions.
why use enum if I have already created the attributes homePhone,officePhone...?
@Mazzy you don't use separate attributes for homePhone, ... but a List<Phone> to store the numbers and an enum type in the Phone class to define the type of the instance (home, office,...)
@Matten the exercise ask me to create the list only for other numbers.the numbers of home,office,eccc must be simple attributes
|
1

enums are a good solution if:

  1. The number of items is fixed
  2. All the information of the item is fixed as well (no loading of external files/resources/etc).

In a real application, you probably need to display the category on a display. This includes translating the category into the user's language which means there is an external dependency.

In such a case, you would use the enum as a key for a factory that gives you the text for each entry in the enum. The factory decouples your constant enum from the variables in the real world (like different/changing translations in the UI).

Comments

1

You may have a look at the Map class to store the phone numbers and add accessors for home, office and mobile phone entries.

4 Comments

-1 for suggesting a deprecated class. Since Java 1.2, Map should be used.
@Aaron Digulla - of course you're right. I edited my post. As a C# developer Dictionary was the first class which came up in my mind :) Should have read the linked page more carefully, especially the bold part at the top...
And I can't undo the downvote because SO insists that the answer hasn't been edited since I downvoted it :-((( damn:-(
@Aaron Digulla now it should work. Thanks for hinting me to the Map object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.