This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
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.
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.
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
posted
0
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.
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
posted
0
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.
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
posted
0
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.
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: 32830
4
posted
0
The reason for writing Apos rather than A+ is to avoid confusion and possible mis-readings, which with blood groups might be fatal.
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).