maria andrea erramouspe maria andrea erramouspe - 3 months ago 8
Java Question

Rewriting if elses into switch statement, i have wrote this code with if elses and need to rewrite as switch

I am working on the following piece of code for the following assignment, but i am having the hardest time to re write as a switch statement, please help me re write properly.

The ABC Community Hospital needs a program to compute and print the billing statement for each of its patient. The following table shows the fees for the various services:

The ABC Community Hospital

Room Charges per Day:
Private $550.0
Semi-private $350.0
Ward $105.00

Telephone $4.50

Television $7.50

Medication $275.00

Write a class called Hospital that accepts the following indicators:
• The patient’s name (First name and last name)
• An integer representing the number of days the patient is in the hospital.
• A single character representing the type of room the patient chooses. Room type (P/p= private, S/s means semi-private, and W/w means ward)

As part of the billing, note the following:
• If a patient chooses private room, then the cost for medication is twice what is shown on the billing chart; if the patient chooses semi-private, then the cost for medication is what is shown on the billing chart; and if the patient chooses ward, then the billing cost is half what is shown on the billing chart.
• A patient who chooses private room pays for television service and telephone service; a patient who chooses semi-private room pays for television service, and a patient who chooses the ward gets television and telephone services free.

import java.text.NumberFormat;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Andrea
*/
public class hospital {


private double roomCharges;
private double telephone;
private double television;
private double medication;
private static final NumberFormat nf = NumberFormat.getCurrencyInstance();
private double totalCharges;
private String header;
private String s;
char roomType;
int noOfdays;



public hospital() {
totalCharges = 0.0;
s = "";
header = "\tThe ABC Community Hospital\n"
+ "\t Patient Billing Statement\n\n";
}

hospital (int i, char p )
{
noOfdays = i;
roomType = p;
}

public void getBillingStatement() {
JTextArea b = new JTextArea("\tThe ABC Community Hospital\n\n"
+ "Room Charges:\n\tPrivate\t\t$550.0\n\tSemi-Private\t\t$350.0\n\tWard\t\t"
+ "$105.0\n\nTelephone\t\t\t$4.50\n\nTelevision\t\t\t$7.50\n\n"
+ "Medication\t\t\t$275.00", 14, 35);
JScrollPane p1 = new JScrollPane(b);
JOptionPane.showMessageDialog(null, p1, "Charges",
JOptionPane.INFORMATION_MESSAGE);

int days = Integer.parseInt(JOptionPane.showInputDialog("Days patient has been in the Hospital:"));
if (days > 0) {
s = s + "Number of days in hospital:\t\t" + days + "\n";
} else {
s = s + "Number of days in hospital must be positive!\n";
days = 0;
}

String room = JOptionPane.showInputDialog("Type of room: (P,S,W)");
switch (room.toUpperCase()) {
case "P":
if (room.equalsIgnoreCase("P"))
roomCharges = 550.0;
s = s + "Type of room:\t\t\tPrivate\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
break;
case "S":
if (room.equalsIgnoreCase("S"))
roomCharges = 350.0;
s = s + "Type of room:\t\t\tSemi-Private\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
break;
case "W" :
if (room.equalsIgnoreCase("W"))
roomCharges = 105.0;
s = s + "Type of room:\t\t\tWard\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
default:
roomCharges = 0.0;
s = s + "Please select a valid room type\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);

break;
}

String phone = JOptionPane.showInputDialog("Telephone: (Y/N)");
if (phone.equalsIgnoreCase("Y")) {
telephone = 4.50;
s = s + "Telephone:\t\t\t" + nf.format(telephone) + "\n";
totalCharges = totalCharges + telephone;
} else if (phone.equalsIgnoreCase("N")) {
telephone = 0.0;
s = s + "Telephone:\t\t\t" + nf.format(telephone) + "\n";
totalCharges = totalCharges + telephone;
} else {
telephone = 0.0;
s = s + "Invalid telephone option\n";
totalCharges = totalCharges + telephone;
}

String tv = JOptionPane.showInputDialog("Television: (Y/N)");
if (tv.equalsIgnoreCase("Y")) {
television = 7.50;
s = s + "Television:\t\t\t" + nf.format(television) + "\n";
totalCharges = totalCharges + television;
} else if (tv.equalsIgnoreCase("N")) {
television = 0.0;
s = s + "Television:\t\t\t" + nf.format(television) + "\n";
totalCharges = totalCharges + television;
} else {
television = 0.0;
s = s + "Invalid television option\n";
totalCharges = totalCharges + television;
}

JOptionPane.showMessageDialog(null, "Medication will be charged as well");
medication = 275.0;

s = s + "Medication:\t\t\t" + nf.format(medication) + "\n";
totalCharges = totalCharges + medication;

s = s + "Total Charges:\t\t\t" + nf.format(totalCharges) + "\n";
}

public static void main(String[] args) {

hospital i = new hospital(20, 's');
i.getBillingStatement();



}



}

Answer

You switch it (see what I did there?) by writing something like this:

switch(room.toUpperCase()) {
    case "P":
        //do stuff for P
        break;
    case "S":
        //do stuff for S
        break;
    .....
    default:
        //do what's in your "else" block
        break;
}

The switch statement decides which item to look at, then each case is for each outcome. If none of your cases match, then the code runs the default case. The breaks are pretty important too. If I understand correctly, without the breaks, your code will execute every case's code beneath and including the matching case. This can be handy if you want that implementation, but it looks like you probably don't here.

More info on switch-case can be found here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Comments