patternjavaMinor
Conditional statements relating to hospital management
Viewed 0 times
hospitalrelatingstatementsconditionalmanagement
Problem
It works just fine, but is there any other way to write this shorter?
Nurses are only available to intensive care patients (room I) and TV's and telephones are only available to non intensive care patients (room D or room P). Also,
```
if (room.equals("I")) { // if (room.equals("I"))
roomCost = 395 * (double)days;
//System.out.println(roomCost);
TVCost = X_COST;
//System.out.println(TVCost);
phoneCost = X_COST;
//System.out.println(phoneCost);
if (nurse.equals("X"))
nurseCost = X_COST;
//System.out.println(nurseCost);
else if (room.equals("N"))
nurseCost = 250 * (double)days;
//System.out.println(nurseCost);
else
nurseCost = 275 * (double)days;
//System.out.println(nurseCost);
} // if (room.equals("I"))
else if (room.equals("P")) { // else if (room.equals("P"))
roomCost = 350 * (double)days;
//System.out.println(roomCost);
nurseCost = X_COST;
//System.out.println(nurseCost);
if (TV.equals("V")) { // if (TV.equals("V"))
TVCost = 40 * (double)days;
//System.out.println(TVCost);
if (phone.equals("T"))
phoneCost = 15 * (double)days;
//System.out.println(phoneCost);
else
phoneCost = X_COST;
//System.out.println(phoneCost);
} // if (TV.equals("V"))
else { // else !(TV.equals("V"))
TVCost = X_COST;
//System.out.println(TVCost);
if (phone.equals("T"))
phoneCost = 15 * (double)days;
//System.out.println(phoneCost);
else
phoneCost = X_COST;
//System.out.println(phoneCost);
} // else !(TV.equals("V"))
} // else if (room.equals("P"))
else { // else
roomCost = 310 * (double)days;
//System.out.println(roomCost);
nurseCost = X_COST;
//System.out.println(nurseCost);
if (TV.equals("V")) { // if (TV.equals("V"))
Nurses are only available to intensive care patients (room I) and TV's and telephones are only available to non intensive care patients (room D or room P). Also,
X_COST has a value of 0.```
if (room.equals("I")) { // if (room.equals("I"))
roomCost = 395 * (double)days;
//System.out.println(roomCost);
TVCost = X_COST;
//System.out.println(TVCost);
phoneCost = X_COST;
//System.out.println(phoneCost);
if (nurse.equals("X"))
nurseCost = X_COST;
//System.out.println(nurseCost);
else if (room.equals("N"))
nurseCost = 250 * (double)days;
//System.out.println(nurseCost);
else
nurseCost = 275 * (double)days;
//System.out.println(nurseCost);
} // if (room.equals("I"))
else if (room.equals("P")) { // else if (room.equals("P"))
roomCost = 350 * (double)days;
//System.out.println(roomCost);
nurseCost = X_COST;
//System.out.println(nurseCost);
if (TV.equals("V")) { // if (TV.equals("V"))
TVCost = 40 * (double)days;
//System.out.println(TVCost);
if (phone.equals("T"))
phoneCost = 15 * (double)days;
//System.out.println(phoneCost);
else
phoneCost = X_COST;
//System.out.println(phoneCost);
} // if (TV.equals("V"))
else { // else !(TV.equals("V"))
TVCost = X_COST;
//System.out.println(TVCost);
if (phone.equals("T"))
phoneCost = 15 * (double)days;
//System.out.println(phoneCost);
else
phoneCost = X_COST;
//System.out.println(phoneCost);
} // else !(TV.equals("V"))
} // else if (room.equals("P"))
else { // else
roomCost = 310 * (double)days;
//System.out.println(roomCost);
nurseCost = X_COST;
//System.out.println(nurseCost);
if (TV.equals("V")) { // if (TV.equals("V"))
Solution
Code like the following ...
... is copy-and-pasted into more than one type of room.
Instead of having a structure like ...
... I suspect it would be better to have a structure like ...
Also I don't understand this ...
... because that's in a place where you already decided that
if (phone.equals("T"))
phoneCost = 15 * (double)days;
//System.out.println(phoneCost);
else
phoneCost = X_COST;... is copy-and-pasted into more than one type of room.
Instead of having a structure like ...
if room type
calculate nurse cost
calculate phone cost
calculate TV cost
else if other room type
calculate nurse cost again
calculate phone cost again
calculate TV cost again... I suspect it would be better to have a structure like ...
// Calculate TV cost.
TVCost = (room.equals("I") || !TV.equals("V")) ? X_COST : 40 * (double)days;
// Calculate Phone cost
... etc ...
// Calculate nurse cost
... etc ...Also I don't understand this ...
else if (room.equals("N"))
nurseCost = 250 * (double)days;
//System.out.println(nurseCost);... because that's in a place where you already decided that
room.equals("I").Code Snippets
if (phone.equals("T"))
phoneCost = 15 * (double)days;
//System.out.println(phoneCost);
else
phoneCost = X_COST;if room type
calculate nurse cost
calculate phone cost
calculate TV cost
else if other room type
calculate nurse cost again
calculate phone cost again
calculate TV cost again// Calculate TV cost.
TVCost = (room.equals("I") || !TV.equals("V")) ? X_COST : 40 * (double)days;
// Calculate Phone cost
... etc ...
// Calculate nurse cost
... etc ...else if (room.equals("N"))
nurseCost = 250 * (double)days;
//System.out.println(nurseCost);Context
StackExchange Code Review Q#47301, answer score: 4
Revisions (0)
No revisions yet.