• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Including "+" sign in Enum

 
B Nirvan
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody working with blood ever writes A+ or A-. They only write Apos or Aneg.
 
B Nirvan
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
B Nirvan
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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


OutPut::

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



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


With this we can achieve the expected task in your project
 
Rob Spoor
Sheriff
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic