mercredi 3 août 2016

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;

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

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)");
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);
}
else 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);
}
else 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);
}
else
{
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);

}

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 String toString()
{
return header + s;
}
}

Aucun commentaire:

Enregistrer un commentaire