Question
Set up a try catch to handle exceptions for each input text box like suite, double room, single room, number of days etc. so it
Set up a try catch to handle exceptions for each input text box like suite, double room, single room, number of days etc. so it looks like the following:
HotelCheckOut.java:
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class HotelCheckOut extends JApplet implements ActionListener { private final double PRICE_SUITE = 499.00; private final double PRICE_DOUBLE = 159.00; private final double PRICE_SINGLE = 99.00; private JLabel name1, name2, room, quantity, days, currency, exchange, display, result; private JPanel mainpanel, leftpanel, rightpanel; private JTextField numSuite, numDoubleRoom, numSingleRoom, numDays, exchangeRate; private JCheckBox suite, doubleRoom, singleRoom; private JRadioButton usCurrency, cdnCurrency; private JButton calculate; private ButtonGroup group;
public void init() { // Set JApplet dimension setSize(400, 350); mainpanel = new JPanel(); leftpanel = new JPanel(); rightpanel = new JPanel(); name1 = new JLabel(" OKANAGAN"); name2 = new JLabel("HOTEL"); room = new JLabel("Room Type:"); quantity = new JLabel("Number of rooms:"); days = new JLabel("Number of days:"); currency = new JLabel("Currency for Payment:"); exchange = new JLabel("U.S. Exchange Rate:"); suite = new JCheckBox("Suite"); doubleRoom = new JCheckBox("Double Room"); singleRoom = new JCheckBox("Single Room"); usCurrency = new JRadioButton("U.S. Currency"); cdnCurrency = new JRadioButton("Cdn Currency", true); numSuite = new JTextField("0"); numDoubleRoom = new JTextField("0"); numSingleRoom = new JTextField("0"); numDays = new JTextField("1"); exchangeRate = new JTextField(""); calculate = new JButton("Check Out"); display = new JLabel("Total amount:"); result = new JLabel(" "); result.setBorder(BorderFactory.createEtchedBorder()); group = new ButtonGroup(); group.add(usCurrency); group.add(cdnCurrency); exchangeRate.setEditable(false); calculate.addActionListener(this); usCurrency.addActionListener(this); cdnCurrency.addActionListener(this); mainpanel.setLayout(new GridLayout(1, 2)); leftpanel.setLayout(new BoxLayout(leftpanel, BoxLayout.Y_AXIS)); rightpanel.setLayout(new BoxLayout(rightpanel, BoxLayout.Y_AXIS)); leftpanel.add(name1); leftpanel.add(Box.createRigidArea(new Dimension(0, 21))); leftpanel.add(room); leftpanel.add(Box.createRigidArea(new Dimension(0, 9))); leftpanel.add(suite); leftpanel.add(Box.createRigidArea(new Dimension(0, 8))); leftpanel.add(doubleRoom); leftpanel.add(Box.createRigidArea(new Dimension(0, 8))); leftpanel.add(singleRoom); leftpanel.add(Box.createRigidArea(new Dimension(0, 15))); leftpanel.add(days); leftpanel.add(Box.createRigidArea(new Dimension(0, 15))); leftpanel.add(currency); leftpanel.add(usCurrency); leftpanel.add(cdnCurrency); leftpanel.add(Box.createRigidArea(new Dimension(0, 18))); leftpanel.add(display); rightpanel.add(name2); rightpanel.add(Box.createRigidArea(new Dimension(0, 22))); rightpanel.add(quantity); rightpanel.add(Box.createRigidArea(new Dimension(0, 11))); rightpanel.add(numSuite); rightpanel.add(Box.createRigidArea(new Dimension(0, 10))); rightpanel.add(numDoubleRoom); rightpanel.add(Box.createRigidArea(new Dimension(0, 10))); rightpanel.add(numSingleRoom); rightpanel.add(Box.createRigidArea(new Dimension(0, 15))); rightpanel.add(numDays); rightpanel.add(Box.createRigidArea(new Dimension(0, 15))); rightpanel.add(exchange); rightpanel.add(Box.createRigidArea(new Dimension(0, 6))); rightpanel.add(exchangeRate); rightpanel.add(Box.createRigidArea(new Dimension(0, 40))); rightpanel.add(result); rightpanel.add(Box.createRigidArea(new Dimension(0, 15))); rightpanel.add(calculate); mainpanel.add(leftpanel); mainpanel.add(rightpanel); add(mainpanel); } // init
public void computePayment() { String msg = "";
try { // Total payment double payment = 0;
// Get total price of suite if (suite.isSelected()) { msg = "Invalid number of Suites"; payment += Integer.parseInt(numSuite.getText().trim()) * PRICE_SUITE; }
// Get total price of double room if (doubleRoom.isSelected()) { msg = "Invalid number of Double Rooms"; payment += Integer.parseInt(numDoubleRoom.getText().trim()) * PRICE_DOUBLE; }
// Get total price of single room if (singleRoom.isSelected()) { msg = "Invalid number of Single Rooms"; payment += Integer.parseInt(numSingleRoom.getText().trim()) * PRICE_SINGLE; }
// Get number of days msg = "Invalid number of days"; int ttlDays = Integer.parseInt(numDays.getText().trim());
// Update payment payment *= ttlDays;
// Check currency for payment if (usCurrency.isSelected()) { msg = "Invalid exchange rate";
// Get exchange rate payment /= Double.parseDouble(exchangeRate.getText().trim());
// Set payment on result label result.setText(String.format("USD %.2f", payment)); } else // Set payment on result label result.setText(String.format("CAD %.2f", payment));
} catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(this, msg, "Error!!!", JOptionPane.ERROR_MESSAGE, null); } }
public void actionPerformed(ActionEvent event) { // Get source button Object src = event.getSource();
if (src == calculate) // Calculate button is clicked computePayment(); else if (src == usCurrency) // Payment in USD // Enable exchangeRate textfield exchangeRate.setEditable(true); else if (src == cdnCurrency) { // Payment in CAD // Disable exchangeRate textfield exchangeRate.setEditable(false);
// Compute payment computePayment(); }
} // actionPerformed } // class HotelCheckOut
OKANAGAN HOTEL OKANAGAN HOTEL Room Type: Number of rooms: Room Type: Number of rooms: Suite Double Room Single Room Suite 2.1 Double Room Single Room Number of days: Currency for Payment: U.S. Exchange Rate: Number of days: Currency for PaymentU.S. Exchange Rate: U.S. Currency Cdn Currency y.S. Currency Cdn Currency Total amount: Input errors: Only integers allowed for the numbers of rooms and days Total amount Input errors: Only integers allowed for the numbers of rooms and days $0.0 CDN $1,316 Check Out Check Out OKANAGAN HOTEL OKANAGAN HOTEL Room Type Number of rooms: Room Type: Number of rooms: Suite Double Room Single Room Suite Double Room Single Room Number of days: Number of days: Currency for Payment U.S. Exchange Rate: Currency for Payment: U.S.Exchange Rate: U.S.Currency Cdn Currency 0.8 .ys. currency O Cdn Currency Total amount U.S.$1,052.80 Total amount Please enter the u.s exchange rate with an appropriate number! heck Out Check Out otalStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started