views:

31

answers:

2

hi,

I am creating an api for a phonebook which has 3 different types of phone-numbers:FAX, HOME, WORK, CELL

I want to add a number to a specific type but do not want to classify FAX,HOME,WORK etc. as primitive types as I am considering that I could change it at a later date.

I was trying to use enums but am not clear how I can use it efficiently in this case.

The code I worked up till now for this part:

private enum NumberType{

FAX, WORK, HOME, CELL}

class PhoneNumber{

 PhoneNumber(int Number, NumberType type){

    this.number = Number;

    this.type = type;
}
..
getter and setter for above
..
}

But I still do not get how I could assign a specific phoneNumber to a specific NumberType.

I am still confused. I want to make it easy for someone to use this feature.

Please help.

Thank you.

A: 

You just have to create a new instance of PhoneNumber :

PhoneNumber homeNumber = new PhoneNumber(1234567890, NumberType.HOME);

I would also suggest to use a String for phone numbers :)

Colin Hebert
A: 

You can write a method

NumberType findPhoneNumberType(String phoneNumber)
{
      //logic to differentiate between various types of phone numbers.
      //advantage: you can also check for illegal phone number values.
}

You can call this method before calling the constructor you have mentioned.

NumberType nt = findPhoneNumberType(number);
PhoneNumber phoneNumber = new PhoneNumber(number, nt);

I would also suggest to use Strings rather than ints for phone numbers.

athena