Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare using enum and int

 
Clapa Danut
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy guys,
i have a question:
lets suppose i have a enum

now i have a int variable databaseCodeCar that can contains values between 1 and 3 acordingly with the enum class above;
i want to do something like

how i can do that?
i can do CarTypes(databaseCodeCar)(or any method to select a particular constant from that class everything else but naming it dirctly?) to select the cosntant from that enum class?

[Edit - added code tags - see UseCodeTags for details]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


However, what I'd do is add a static method to CarTypes to get a CarTypes instance from an int, and then I'd switch on that CarTypes object.

I'd also prefer the name CarType (singluar) over CarTypes (plural).
 
Clapa Danut
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:


figured out finally in the searching process
never used enum and didnt expected to be so easy

dingggggg ty for help
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

And welcome to the Ranch!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clapa Danut wrote: . . .
. . .
Do you see how I have enhanced your enum and you can get rid of those dreadful if-elses.
You can get a number called ordinal from an enum member, and you can also get an array of those members. So you can use the ordinal to search the array. It would appear that the ordinal is equal to codeCar - 1.You can also get an enum member from its name with the valueOf() method. For more details look in the Enum class, call the values() method of your enum, and look in the Java Tutorials.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you see how I have enhanced your enum and you can get rid of those dreadful if-elses.

Nice enhancement. Its reusable.
 
Clapa Danut
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Clapa Danut wrote: . . .
. . .
Do you see how I have enhanced your enum and you can get rid of those dreadful if-elses.
You can get a number called ordinal from an enum member, and you can also get an array of those members. So you can use the ordinal to search the array. It would appear that the ordinal is equal to codeCar - 1.You can also get an enum member from its name with the valueOf() method. For more details look in the Enum class, call the values() method of your enum, and look in the Java Tutorials.


Well beleve it or not that i tryed in the first time but i didnt figured out how to do this.
i have a variable int external lets suppose int external = 2;
basically what i want in this case is:
to indicate to CarTypes that i want the message from HONDA because my external int is 2.
something like CarType.getMessage(external == carCode 2).
to avoide if (external == CarCode.HONDA) printn ("BatMan");
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking. You can change the message with a Map
 
Clapa Danut
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking. You can change the message with a Map


So it isnt a straight solution as i thought. Thought at that but i beleved is wrong from the point of architectural view.

Thank you very much for help. You really solved me a problem
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking.

Can't figure out if these statements are sincere or not. Like any class, the attributes of an enum should be things that truly belong to it. Strings that are relevant to a CarType are legitimate attributes, but not, IMO, Strings that represent arbitrary reactions to encountering one. Those properly belong outside of the enum definition.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking.

Can't figure out if these statements are sincere or not. Like any class, the attributes of an enum should be things that truly belong to it. Strings that are relevant to a CarType are legitimate attributes, but not, IMO, Strings that represent arbitrary reactions to encountering one. Those properly belong outside of the enum definition.

Agree for what you have said. If they are sincere and related to the CarType then they truly belong to the CarType attribute.
And in case they dont belong to the CarType, the other approach of using a Map can be applied.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did mean it sincerely. And I showed examples of how those Strings can be inside the enum, ie belong to each CarType object, or outside the enum, ie simply associated with a CarType, which might be implemented with a Map.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic