Write an application for Lambert’s Vacation Rentals. Use separate ButtonGroups t
ID: 3798294 • Letter: W
Question
Write an application for Lambert’s Vacation Rentals. Use separate ButtonGroups to allow a client to select one of three locations, the number of bedrooms, and whether meals are included in the rental. Assume that the locations are parkside for $600 per week, poolside for $750 per week, or lakeside for $825 per week. Assume that the rentals have one, two, or three bedrooms and that each bedroom over one adds $75 to the base price. Assume that if meals are added, the price is $200 more per rental. Save the file as JVacationRental.java.
Explanation / Answer
Follow the below code:
import javax.swing.*;
import java.awt.event.*;
public class JVacationRental extends JFrame implements ActionListener
{
private final int PARK_SIDE1= 600;
private final int POOL_SIDE1 = 750;
private final int LAKE_SIDE1 = 825;
private final int>
private final int TWO_BEDROOMS1 = 150;
private final int THREE_BEDROOMS1 = 225;
private final int MEALS_1= 200;
//labels
private JLabel jlblLocations1 = new JLabel("LOCATION: ");
private JLabel jlblBedrooms1 = new JLabel("BEDROOMS: ");
private JLabel jlblMeals1 = new JLabel("INCLUDE MEALS: ");
//Button variables and designing buttons
private JRadioButton jrbtnPark1 = new JRadioButton("PARK SIDE");
private JRadioButton jrbtnPool1= new JRadioButton("POOL SIDE");
private JRadioButton jrbtnLake1= new JRadioButton("LAKE SIDE");
private JRadioButton jrbtnOne1 = new JRadioButton("1");
private JRadioButton jrbtnTwo1 = new JRadioButton("2");
private JRadioButton jrbtnThree1 = new JRadioButton("3");
private JRadioButton jrbtnYes1 = new JRadioButton("YES");
private JRadioButton jrbtnNo1 = new JRadioButton("NO");
//Location bedroom and meals button
private ButtonGroup groupLocations1 = new ButtonGroup();
private ButtonGroup groupBedrooms1 = new ButtonGroup();
private ButtonGroup groupMeals1 = new ButtonGroup();
//Button to calculate and result
private JButton jbtnCalculate1 = new JButton(" CALCULATE TOTAL RENT");
private JButton jbtnReset1= new JButton("RESET ");
//Button for total rent
private JLabel result1 = new JLabel("TOTAL RENT: $ 0.0");
//Panel setting with corresponding variables
private JPanel jpnlLocations1 = new JPanel();
private JPanel jpnlBedrooms1 = new JPanel();
private JPanel jpnlMeals1 = new JPanel();
private JPanel jpnlButton1 = new JPanel();
private JPanel jpnlLabel1 = new JPanel();
private JPanel pane1l = new JPanel();
//Initializing cost
private int locationRent1 = 0;
private int bedroomsRent1 = 0;
private int mealsCost1 = 0;
//constructor
public JVacationRental()
{
super("LAMBERT’S VACATION RENTALS ");
//Adding frames for variables
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
groupLocations.add(jrbtnPark1);
groupLocations.add(jrbtnPool1);
groupLocations.add(jrbtnLake1);
groupBedrooms.add(jrbtnOne1);
groupBedrooms.add(jrbtnTwo1);
groupBedrooms.add(jrbtnThree1);
groupMeals.add(jrbtnYes1);
groupMeals.add(jrbtnNo1);
jpnlLocations.add(jlblLocations1);
jpnlLocations.add(jrbtnPark1);
jpnlLocations.add(jrbtnPool1);
jpnlLocations.add(jrbtnLake1);
jpnlBedrooms.add(jlblBedrooms1);
jpnlBedrooms.add(jrbtnOne1);
jpnlBedrooms.add(jrbtnTwo1);
jpnlBedrooms.add(jrbtnThree1);
jpnlMeals.add(jlblMeals1);
jpnlMeals.add(jrbtnYes1);
jpnlMeals.add(jrbtnNo1);
jpnlButton.add(jbtnCalculate1);
jpnlButton.add(jbtnReset1);
jpnlLabel.add(result1);
panel.add(jpnlLocations1);
panel.add(jpnlBedrooms1);
panel.add(jpnlMeals1);
panel.add(jpnlButton1);
panel.add(jpnlLabel1);
//Adding panel
add(panel1);
jrbtnPark1.addItemListener(new LocationsListener());
jrbtnPool1.addItemListener(new LocationsListener());
jrbtnLake1.addItemListener(new LocationsListener());
jrbtnOne1.addItemListener(new BedroomsListener());
jrbtnTwo1.addItemListener(new BedroomsListener());
jrbtnThree1.addItemListener(new)
BedroomsListener());
jrbtnYes1.addItemListener(new MealsListener());
jrbtnNo1.addItemListener(new MealsListener());
jbtnCalculate1.addActionListener(this);
jbtnReset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == jbtnCalculate)
{
//Calculating the rental value.
double totalRent1 = locationRent1 + bedroomsRent1 + mealsCost1;
//Displaying rental amount
result1.setText("TOTALRENT: $ " + totalRent1);
}
else if(source == jbtnReset1)
{
groupLocations.clearSelection();
groupBedrooms.clearSelection();
groupMeals.clearSelection();
result.setText("Total Rent: $ 0.0");
//Set initial value =0
locationRent1 = 0;
bedroomsRent1 = 0;
mealsCost1 = 0;
}
}
private class LocationsListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItem();
if(source == jrbtnPark1)
locationRent = PARK_SIDE1;
else if(source == jrbtnPool1)
locationRent = POOL_SIDE1;
else if(source == jrbtnLake1)
locationRent = LAKE_SIDE1;
else
locationRent1 = 0;
}
}
private class BedroomsListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItem();
if(source == jrbtnOne1)
bedroomsRent1 = ONE_BEDROOM1;
else if(source == jrbtnTwo1)
bedroomsRent1 = TWO_BEDROOMS1;
else if(source == jrbtnThree1)
bedroomsRent1 = THREE_BEDROOMS1;
else
bedroomsRent1 = 0;
}
}
private class MealsListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItem();
if(source == jrbtnYes1)
mealsCost1 = MEALS1;
else if(source == jrbtnNo1)
mealsCost1 = 0;
else
mealsCost1= 0;
}
}
// main()
public static void main(String[] args)
{
JVacationRental frame = new JVacationRental();
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.