158k views
5 votes
Write an application for Lambert’s Vacation Rentals. Separate ButtonGroups have been declared to allow a client to select one of three locations, the number of bedrooms, and whether meals are included in the rental. The locations are parkside for $600 per week, poolside for $750 per week, or lakeside for $825 per week. The rentals have one, two, or three bedrooms and that each bedroom greater than one adds $75 to the base price. If meals are added, the price is $200 more per rental. After each user selection, update the texfield to display the current cost of the user's selections.

User Hozefa
by
4.7k points

1 Answer

6 votes

Answer:

see explaination

Step-by-step explanation:

/**************JVacationRental.java*****************/

import javax.swing.*;

import java.awt.event.*;

public class JVacationRental extends JFrame implements ActionListener

{

//variable declaration

private final int PARK_SIDE1= 600;

private final int POOL_SIDE1 = 750;

private final int LAKE_SIDE1 = 825;

private final int ONE_BEDROOM1= 75;

private final int TWO_BEDROOMS1 = 150;

private final int THREE_BEDROOMS1 = 225;

private final int MEALS 1= 200;

//setting labels

private JLabel jlblLocations1 = new JLabel("LOCATION: ");

private JLabel jlblBedrooms1 = new JLabel("BEDROOMS: ");

private JLabel jlblMeals1 = new JLabel("INCLUDE MEALS: ");

//setting 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 jbtnReset 1= new JButton("RESET ");

//button for total rent

private JLabel result1 = new JLabel("TOTAL RENT: $ 0.0");

//panel setting with the 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 the cost

private int locationRent1 = 0;

private int bedroomsRent1 = 0;

private int mealsCost1 = 0;

//constructor

public JVacationRental()

{

super("LAMBERT’S VACATION RENTALS ");

//adding frames for the 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);

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)

{

double totalRent1 = locationRent1 + bedroomsRent1 + mealsCost1;

result1.setText("TOTAL

RENT: $ " + totalRent1);

}

else if(source == jbtnReset1)

{

groupLocations.clearSelection();

groupBedrooms.clearSelection();

groupMeals.clearSelection();

result.setText("Total Rent: $ 0.0");

//setting 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

mealsCost 1= 0;

}

}

public static void main(String[] args)

{

JVacationRental frame = new JVacationRental();

frame.setSize(350, 250);

frame.setVisible(true);

}

}

User Troynt
by
4.8k points