Solved: create an application for an interactive program for a resort




Solved: create an application for an interactive program for a resort


Please follow step to create application:

Including JCheckBoxes in an Application

Next, you create an interactive program for a resort. The base price for a room is $200, and a guest can choose from several options. Reserving a room for a weekend night adds $100 to the price, including breakfast adds $20, and including a round of golf adds $75. A guest can select none, some, or all of these premium additions. Each time the user changes the option package, the price is recalculated.

  1. Open a new file, and then type the following first few lines of a Swing application that demonstrates the use of a JCheckBox. Note that the JResortCalculator class implements the ItemListener interface:

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JResortCalculator extends

JFrame implements ItemListener


  1. Declare the named constants that hold the base price for a resort room and the premium amounts for a weekend stay, including breakfast and a round of golf. Also include a variable that holds the total price for the stay, and initialize it to the value of the base price. Later, depending on the user’s selections, premium fees might be added to totalPrice, making it more than BASE_PRICE.

final int BASE_PRICE = 200; final int WEEKEND_PREMIUM = 100; final int BREAKFAST_PREMIUM = 20; final int GOLF_PREMIUM = 75; int totalPrice = BASE_PRICE;

  1. Declare three JCheckBox objects. Each is labeled with a String that contains a description of the option and the cost of the option. Each JCheckBox starts unchecked or deselected.

JCheckBox weekendBox = new JCheckBox (“Weekend premium $” + WEEKEND_PREMIUM, false);

JCheckBox breakfastBox = new JCheckBox(“Breakfast $” + BREAKFAST_PREMIUM, false);

JCheckBox golfBox = new JCheckBox (“Golf $” + GOLF_PREMIUM, false);

  1. Include JLabels to hold user instructions and information and a JTextField in which to display the total price:

JLabel resortLabel = new JLabel(“Resort Price Calculator”); JLabel priceLabel = new JLabel(“The price for your stay is”); JTextField totPrice = new JTextField(4); JLabel optionExplainLabel = new JLabel

(“Base price for a room is $” + BASE_PRICE + “.”); JLabel optionExplainLabel2 = new JLabel

(“Check the options you want.”);

  1. Begin the JResortCalculator class constructor. Include instructions to set the title by passing it to the JFrame parent class constructor, to set the default close operation, and to set the layout manager. Then add all the necessary components to the JFrame.

public JResortCalculator() {

super(“Resort Price Estimator”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(resortLabel); add(optionExplainLabel); add(optionExplainLabel2); add(weekendBox); add(breakfastBox); add(golfBox); add(priceLabel); add(totPrice);

  1. Continue the constructor by setting the text of the totPrice JTextField to display a dollar sign and the totalPrice value. Register the class as a listener for events generated by each of the three JCheckBoxes. Finally, add a closing curly brace for the constructor.

totPrice.setText(“$” + totalPrice); weekendBox.addItemListener(this); breakfastBox.addItemListener(this); golfBox.addItemListener(this);


  1. Begin the itemStateChanged() method that executes when the user selects or deselects a JCheckBox. Use the appropriate methods to determine which JCheckBox is the source of the current ItemEvent and whether the event was generated by selecting a JCheckBox or by deselecting one.

@Override public void itemStateChanged(ItemEvent event) {

Object source = event.getSource(); int select = event.getStateChange();

  1. Write a nested if statement that tests whether the source is equivalent to the weekendBox, breakfastBox, or, by default, the golfBox. In each case, depending on whether the item was selected or deselected, add or subtract the corresponding premium fee from the totalPrice. Display the total price in the JTextField, and add a closing curly brace for the method.

if(source == weekendBox) if(select == ItemEvent.SELECTED)

totalPrice += WEEKEND_PREMIUM; else

totalPrice -= WEEKEND_PREMIUM; else if(source == breakfastBox) {

if(select == ItemEvent.SELECTED) totalPrice += BREAKFAST_PREMIUM;

else totalPrice -= BREAKFAST_PREMIUM;

} else // if(source == golfBox) by default

if(select == ItemEvent.SELECTED) totalPrice += GOLF_PREMIUM;

else totalPrice -= GOLF_PREMIUM;

totPrice.setText(“$” + totalPrice);


  1. Add a main() method that creates an instance of the JFrame and sets its size and visibility. Then add a closing curly brace for the class.

public static void main(String[] args) {

JResortCalculator aFrame = new JResortCalculator(); final int WIDTH = 300; final int HEIGHT = 200; aFrame.setSize(WIDTH, HEIGHT); aFrame.setVisible(true);




Save the file as

error: Content is protected !!