File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Including Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Including "+" sign in Enum" Watch "Including "+" sign in Enum" New topic
Author

Including "+" sign in Enum

B Nirvan
Ranch Hand

Joined: Oct 20, 2010
Posts: 82
Hi,
I have an enum that represents blood group. Allowable values are (A+, A- , B+, B-, AB+, AB-, O+, O-). The problem is that I am not able to define the enum because of the special character "+". I tried to escape it using A\+, but that doesn't work. Also tried to put the values in quotes (single and double) and that doesn't work too. How should I define enums for these blood groups.

regards,
Nirvan.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Give them other names, with a String field for the label:
If needed you can add a static method that returns a blood type for a specific label; loop over the values() until you find one with the label. Return null or throw an exception if none is found.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You could maintain a HashMap<String, BloodType> for the reverse reference if you get stressed but I usually find it isn't worth the effort and pretty much do as Rob says.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Nobody working with blood ever writes A+ or A-. They only write Apos or Aneg.
B Nirvan
Ranch Hand

Joined: Oct 20, 2010
Posts: 82
Rob,
Your solution seems to be nice and simple. I will use it.

Campbell,
My client wants to search the donors based on the blood group. I think it is much simpler to select from a dropdown list of blood groups using A+ format rather than Apos. But I am welcome for any drawbacks in using the earlier.

regards,
Nirvan.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

So you are changing your model because the view wants that? So if the client decides that it wants yet another format you're going to change your model? I would separate the concerns using something like MVC.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
B Nirvan
Ranch Hand

Joined: Oct 20, 2010
Posts: 82
Am I changing the model ? I don't think so. I will be storing integer values for the model. I believe that I am be only changing the view. If client needs a different format, I don't have to change the values in the database. Just changing the Enum strings ("A+") to (Apos) will work. Or there is something that I am failing to comprehend.

regards,
Nirvan.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

If you're using integers in the database to represent these values, I'd suggest having a lookup table in the database mapping the integers to the descriptions.

As things stand if you were asked to change from A+ to A-positive, you'd have to recompile your code and deploy a new version. As opposed to just changing a single value in the database.
B Nirvan
Ranch Hand

Joined: Oct 20, 2010
Posts: 82
Matthew,
I agree with your suggestion. But I don't think the client will ask for a change in the display type of the blood groups. Still I will think about using database look up of the integer values.

Thanks very much to all of you.

regards,
Nirvan.
Daniel Sagayaraj
Ranch Hand

Joined: Mar 15, 2010
Posts: 32


==============


OutPut::

A+
A-
B+
B-
AB+
AB-
O+
O-



=========================


With this we can achieve the expected task in your project
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Please UseCodeTags next time. I've added them this time.

Two comments:
- surely you are not using int where boolean should be used? Both positive and negative should be booleans of course!
- your example now requires if statements within the loop to generate the entire list of blood types. That adds complexity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
The reason for writing Apos rather than A+ is to avoid confusion and possible mis-readings, which with blood groups might be fatal.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Nirvan Buddha wrote:Am I changing the model ? I don't think so. I will be storing integer values for the model. I believe that I am be only changing the view. If client needs a different format, I don't have to change the values in the database. Just changing the Enum strings ("A+") to (Apos) will work. Or there is something that I am failing to comprehend.

regards,
Nirvan.
I would extract the information from the model. An example where your model fails: Client 1 want A+ Client 2 wants Apos. Or what if he wants it in 2 different languages (not that A+ would be different in another language but you get the point).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Including "+" sign in Enum