Java: Create an interactive GUI program that allows the user to enter a rental t
ID: 3582393 • Letter: J
Question
Java:
Create an interactive GUI program that allows the user to enter a rental time in hours into a text field; if the value entered is not numeric, set the rental price to 0. Also allow the user to choose one equipment type to rent from
a group of seven choices.The chocies are: Jet Ski, Pontoon Boat, Rowboat, Canoe, Kayak, Beach Chair and Umbrella. The rental fee is $40 per hour for a jet ski or pontoon boat; $20 per hour for a rowboat, canoe, or kayak; and $7 per hour for a beach chair or umbrella. Let the user add an equipment lesson for an extra $5. Display a message that indicates all the details for the rental, including the total price.
Save the program as JSammysSeashore.java.
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JSammysSeashore extends JFrame implements ItemListener, ActionListener
{
FlowLayout flow = new FlowLayout();
JTextField rental_hrs = new JTextField(8);
JLabel rental_hrs_lbl = new JLabel("Enter the rental time (in Hrs.): "); // Label to display text
/* Check Box Options for Various Equipments */
JCheckBox opt_jetski = new JCheckBox("Jet Ski", false);
JCheckBox opt_pontoonboat = new JCheckBox("Pontoon Boat", false);
JCheckBox opt_rowboat = new JCheckBox("Row Boat", false);
JCheckBox opt_canoe = new JCheckBox("Canoe", false);
JCheckBox opt_kayak = new JCheckBox("Kayak", false);
JCheckBox opt_beach_chair = new JCheckBox("Beach Chair", false);
JCheckBox opt_umbrella = new JCheckBox("Umbrella", false);
/* Check Box Options Whether to Include Lesson or No Lesson */
JCheckBox opt_equipment_lesson = new JCheckBox("Lesson", false);
JCheckBox opt_equipment_no_lesson = new JCheckBox("No lesson", true);
JLabel lbl_title = new JLabel("Sammy's Seashore Rentals");
Font font1 = new Font("Ariel",Font.BOLD, 25);
Font font2 = new Font("Ariel",Font.PLAIN, 16);
JLabel lbl_total_pric = new JLabel("Total Price");
JLabel lbl1 = new JLabel("Select the rental equipment");
JLabel lbl2 = new JLabel("Want to include Lesson?");
JLabel total_price = new JLabel();
JLabel explainLabel = new JLabel();
int price_hrs = 0;
int price_lesson = 0;
int price_of_equipment = 0;
int price = 0;
int hours;
String output = "";
String str_equipment = "";
String str_time = "";
String str_lesson = "";
/* Displaying Main Menu */
public JSammysSeashore()
{
super("Sammys Seashore Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
ButtonGroup buttongroup_equipment = new ButtonGroup();
buttongroup_equipment.add(opt_jetski);
buttongroup_equipment.add(opt_pontoonboat);
buttongroup_equipment.add(opt_rowboat);
buttongroup_equipment.add(opt_canoe);
buttongroup_equipment.add(opt_kayak);
buttongroup_equipment.add(opt_beach_chair);
buttongroup_equipment.add(opt_umbrella);
ButtonGroup buttongroup_lesson_opt = new ButtonGroup();
buttongroup_lesson_opt.add(opt_equipment_lesson);
buttongroup_lesson_opt.add(opt_equipment_no_lesson);
add(lbl_title);
add(rental_hrs_lbl);
add(rental_hrs);
add(lbl1);
add(opt_jetski);
add(opt_pontoonboat);
add(opt_rowboat);
add(opt_canoe);
add(opt_kayak);
add(opt_beach_chair);
add(opt_umbrella);
lbl_title.setFont(font1);
lbl1.setFont(font2);
lbl2.setFont(font2);
total_price.setFont(font2);
add(lbl2);
add(opt_equipment_lesson);
add(opt_equipment_no_lesson);
add(lbl_total_pric);
add(total_price);
total_price.setText("$0");
add(explainLabel);
rental_hrs.addActionListener(this);
opt_jetski.addItemListener(this);
opt_pontoonboat.addItemListener(this);
opt_rowboat.addItemListener(this);
opt_kayak.addItemListener(this);
opt_canoe.addItemListener(this);
opt_beach_chair.addItemListener(this);
opt_umbrella.addItemListener(this);
opt_equipment_lesson.addItemListener(this);
opt_equipment_no_lesson.addItemListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == rental_hrs)
{
try
{
hours = Integer.parseInt(rental_hrs.getText()); // Convert Rental Hrs into Numeric
}
catch(Exception user_exception)
{
hours = 0;
rental_hrs.setText("Invalid Rental Hours");
}
price_hrs = hours * price_of_equipment; // Calculating Rental Equipment Price
price = price_hrs + price_lesson; // Calculating Total Price Including Lesson Proce
str_time = "- for " + hours + " hours - ";
output = "$" + price;
total_price.setText(output);
output = "Rental details: " + str_equipment + str_time + str_lesson; // For Displaying Rental Dtls
explainLabel.setText(output);
}
}
@Override
public void itemStateChanged(ItemEvent check_event)
{
Object source = check_event.getItem();
int opt_select = check_event.getStateChange();
final int JET_PONTOON_PRICE = 40; // Per hour rental fee of Jet Ski and Pontoon
final int ROW_CANOE_KAYAK_PRICE = 20; // Per hour rental fee of Row boat, Canoe, Kayak
final int BEACHCHAIR_UMBRELLA_PRICE = 7; // Per hour rental fee of Beach Chair, Umbrella
final int LESSON_PRICE = 5;
/* Calculating Price Based on Equipment Selected and The Output Text and Format to Display */
if(opt_select == ItemEvent.SELECTED)
{
if(source == opt_jetski)
{
str_equipment = "- price of jet ski @ $" + JET_PONTOON_PRICE + " per hour - " ;
price_of_equipment = JET_PONTOON_PRICE;
}
else
if(source == opt_pontoonboat)
{
str_equipment = "- price of pontoon boat @ $" + JET_PONTOON_PRICE + " per hour - " ;
price_of_equipment = JET_PONTOON_PRICE;
}
else
if(source == opt_rowboat)
{
str_equipment = " – price of row boat @ $" + ROW_CANOE_KAYAK_PRICE + " per hour - " ;
price_of_equipment = ROW_CANOE_KAYAK_PRICE;
}
else
if(source == opt_canoe)
{
str_equipment = "- price of canoe @ $" + ROW_CANOE_KAYAK_PRICE + " per hour - " ;
price_of_equipment = ROW_CANOE_KAYAK_PRICE;
}
else
if(source == opt_kayak)
{
str_equipment = " – price of kayak @ $" + ROW_CANOE_KAYAK_PRICE + " per hour - ";
price_of_equipment = ROW_CANOE_KAYAK_PRICE;
}
else
if(source == opt_beach_chair)
{
str_equipment = " – price of beach chair @ $" + BEACHCHAIR_UMBRELLA_PRICE + " per hour - " ;
price_of_equipment = BEACHCHAIR_UMBRELLA_PRICE;
}
else
if(source == opt_umbrella)
{
str_equipment = " – price of umbrella @ $" + BEACHCHAIR_UMBRELLA_PRICE + " per hour - " ;
price_of_equipment = BEACHCHAIR_UMBRELLA_PRICE;
}
else
if(source == opt_equipment_lesson)
{
str_lesson = " – price of lesson @ $" + LESSON_PRICE;
price_lesson = LESSON_PRICE;
}
else
{
str_lesson = " - no lesson";
price_lesson = 0;
}
price_hrs = hours * price_of_equipment;
price = price_hrs + price_lesson;
output = "$" + price;
total_price.setText(output);
output = "Rental equipment details: " + str_equipment + str_time + str_lesson;
explainLabel.setText(output);
}
}
/* Main Program */
public static void main(String[] args)
{
JSammysSeashore jsammyframe = new JSammysSeashore();
jsammyframe.setSize(400, 2oo);
jsammyframe.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.