• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Enum for Range Comparison  RSS feed

 
nitin pai
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?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!