This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Enum for Range Comparison  RSS feed

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how to use Enums in this case:

I have to make a range comparison and I think Enums might be helpful here but not sure how to implement it.

I want something like this (functionally):

Enum STATUS{
STATUS1 (10,20),
STATUS2 (20,30)
}

while in the program i need a call as this:

int value = STATUS(12).getStatus(); //12 lies in the range 10 to 20

I should get the value as STATUS1. Is it possible to do so? Please show me how can this be done?
 
Marshal
Posts: 58415
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can be done, but it looks awkward. You would have to add a constructor to your enum members, with max and min in, then getMax and getMin methods, then find some way of checking against each member of the enumerated type.

Is that really what enumerated types were designed for? It doesn't look like good programming to me. Have you read the Java Tutorials? Or the links in this recent thread?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!