This is a JAVA question. This has been asked many times on here but every progra
ID: 3671865 • Letter: T
Question
This is a JAVA question. This has been asked many times on here but every program does not work, or the output does not look like the one provided below. Can someone please help with a working program?
I have seen many others around on the internet, but none output like mine needs to look like. Please provide your own solution to the problem.
Thanks, Phil
--------------------------------------------------------------------------------------------
Dorm and Meal Plan Calculator
A University has the following dormitories:
Allen Hall: $1500 per semester
Pike Hall: $1600 per semester
Farthing Hall: $1200 per semester
University Hall: $1800 per semester
The university also offers the following meals plans:
7 meals per week : $560 per semester
14 meals per week: $1095 per semester
Unlimited meals: $1500 per semester
Create an application with the two combo boxes. One should hold the names of the dormitories, and the other should hold the meals plans. The user should select a dormitory and a meal plan, and the application should show the total charges for the semester.
---------------------------------------------------------------------------------------------------------
HERE IS THE EXPECTED OUTPUT
Dorm and Meal Plan Calculator Select a Dorm. University Suites Select a Meal Plan. Unlimited meals per week Calculate Charges Exit Message Total Charges per Semester: $3,300.00 Total charges per semester: $3.300.00 OKExplanation / Answer
import java.awt.BorderLayout
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame
{
private JPanel dormPanel;
private JComboBox dormBox;
private JPanel mealPanel;
private JComboBox mealBox;
private JPanel totalChargesPanel;
private JPanel selectedMealPanel;
private JPanel buttonPanel;
private JButton calcButton;
private JLabel label1;
private JTextField totalCharges;
private String[] dorm = { "Allen Hall: $1,500 per semester",
"Pike Hall: $1,600 per semester",
"Farthing Hall: $1,200 per semester",
"University Suites: $1,800 per semester"};
private String[] meal = { "7 meals per week: $650 per semester",
"14 meals per week: $1,095 per semester",
"Unlimited meals: $1,500 per semester"};
public Main()
{
super("Dormitory and Meal Plan");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildDormPanel();
buildMealPanel();
buildSelectedTotalChargesPanel();
buildbuttonPanel();
add(dormPanel, BorderLayout.WEST);
add(mealPanel, BorderLayout.EAST);
add(totalChargesPanel, BorderLayout.SOUTH);
add(buttonPanel, BorderLayout.NORTH);
pack();
setVisible(true);
}
// The buildDormPanel method builds the dorm panel.
private void buildDormPanel()
{
// Create the dorm panel.
dormPanel = new JPanel();
dormBox = new JComboBox(dorm);
dormBox.addActionListener(new ComboBoxListener());
dormPanel.add(dormBox);
}
private void buildMealPanel()
{
// Create the meal panel.
mealPanel = new JPanel();
mealBox = new JComboBox(meal);
// Register the action listener.
mealBox.addActionListener(new ComboBoxListener());
mealPanel.add(mealBox);
}
// The buttonPanel method builds the bottun panel.
private void buildButtonPanel()
{
// Create a panel.
buttonPanel = new JPanel();
// Create a button.
calcButton = new JButton("Calculate");
button.addActionListener(new ButtonListerner());
// Add the button to the panel.
buttonPanel.add(calcButton);
}
private void buildSelectedTotalChargesPanel()
{
// Create the totalChargesPanel for the label.
totalChargesPanel = new JPanel();
label1 = new JLabel("Total charges per semester: ");
// Create the totalCharges textfield.
totalCharges = new JTextField (25);
totalCharges.setEditable(false);
// Add the totalChargesPanel to the panel.
totalChargesPanel.add(label1);
totalChargesPanel.add(totalCharges);
}
/** Private inner class that handles the event when the user
selects the dorm and meal boxes.
*/
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Variables to hold the dorm, meal, and total charges.
String dorm = (String) dormBox.getSelectedItem();
String meal = (String) mealBox.getSelectedItem();
// Calculates the total.
totalCharges.setText(meal + dorm);
}
}
public static void main(String[] args)
{
new Main();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.