Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Libraries
- import java.text.DecimalFormat;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class DormMealCaclGUI extends JFrame {
- // Create memebrs
- private DormPrices dorm; // To display dorm prices and selections
- private MealPlans meals; // To display meal plan prices and selections
- private JPanel buttonPanel; // Holds calculate and exit buttons
- private JButton calcButton; // To calculate costs
- private JButton exitButton; // To exit application window when done
- public DormMealCaclGUI(){
- // Set Title
- setTitle("Univsersity Dorm & Meal Plan Calculator");
- // Specify action for application close
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Create border layout manager
- setLayout(new BorderLayout());
- // Create custom panels
- dorm = new DormPrices();
- meals = new MealPlans();
- // Create button panel
- buildButtonPanel();
- // Add components to pane
- add(dorm, BorderLayout.WEST);
- add(meals, BorderLayout.EAST);
- add(buttonPanel, BorderLayout.SOUTH);
- // Pack contents and display
- pack();
- setVisible(true);
- } // end constructor
- // This construtor will build the button panel
- public void buildButtonPanel(){
- // Create panel
- buttonPanel = new JPanel();
- // Create Buttons
- calcButton = new JButton("Calculate");
- exitButton = new JButton("Exit");
- // Register action listeners
- calcButton.addActionListener(new CalcButtonListener());
- exitButton.addActionListener(new ExitButtonListener());
- // Add buttons to panel
- buttonPanel.add(calcButton);
- buttonPanel.add(exitButton);
- } // end build button panel constructor
- // Private inner class to handle calcButton action events
- private class CalcButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- int total = 0; // holds overall total for dorm and meal plan selections
- // Calculate total
- total = dorm.actionCalculate() + meals.getMealPlanCost();
- // Create DecimalFormat objetc
- DecimalFormat cost = new DecimalFormat("##00.00");
- // Display total
- JOptionPane.showMessageDialog(null, "Dorm & Meal Plan Total: $" + cost.format(total));
- } // end action performed method
- } // end calc button listener class
- // Private inner class to handle exitButton action events
- private class ExitButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- System.exit(0);
- } // end method
- }// end exit button listener class
- public static void main(String[] args) {
- new DormMealCaclGUI();
- } // end main method
- }// end class
- // Libraries
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class DormPrices extends JPanel {
- // Constant variables used for calculating cost
- public final int ALLEN_HALL = 1500;
- public final int PIKE_HALL = 1600;
- public final int FARTHING_HALL = 1200;
- public final int UNI_SUITES = 1800;
- // Create combo box to allow user to select dorm
- // user only allowed to select one dorm per calculation
- public final String [] dormNames = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites"};
- private JComboBox dorms; // Combo box for dorm options
- private JLabel label1; // label to display results
- private JTextField selectedDorm; // text field to hold selected dorm
- // Constructor to build panel
- public DormPrices(){
- // Set new layout to GridLayout manager with 4 rows and 1 column
- setLayout(new GridLayout(4,1));
- // Create combo box, lable and text field
- dorms = new JComboBox<String>(dormNames);
- label1 = new JLabel("Dorm Selected: ");
- selectedDorm = new JTextField(12);
- selectedDorm.setEditable(false);
- // Add border to panel
- setBorder(BorderFactory.createTitledBorder("Dorm Hall Prices"));
- // Create action listener
- dorms.addActionListener(new ComboBoxListener());
- // Add components to panel
- add(dorms);
- add(label1);
- add(selectedDorm);
- } // end dorm prices constructor
- // this inner privtae class will handle the event of an itme being selected from the combo box
- private class ComboBoxListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- // Get seleted dorm name
- String dormSelection = (String)dorms.getSelectedItem();
- // Display selection
- selectedDorm.setText(dormSelection);
- } // end method
- }// end class
- // this method will pass the total of the dorm selected
- public int actionCalculate(){
- // Variable
- int dormTotal = 0; // this holds an initial value for dorm hall cost
- if(selectedDorm.getText() == "Allen Hall")
- dormTotal = ALLEN_HALL;
- else if(selectedDorm.getText() == "Pike Hall")
- dormTotal = PIKE_HALL;
- else if(selectedDorm.getText() == "Farthing Hall")
- dormTotal = FARTHING_HALL;
- else if(selectedDorm.getText() == "University Suites")
- dormTotal = UNI_SUITES;
- return dormTotal;
- }
- } // end DormPrices clas
- // Libraries
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class MealPlans extends JPanel{
- public final int SEVEN_MEALS_PER_WEEK = 560;
- public final int FRTN_MEALS_PER_WEEK = 1095;
- public final int UNLIMITED_MEALS = 1500;
- // Create combo box to allow user to select dorm
- // user only allowed to select one dorm per calculation
- public final String [] mealPlan = {"7 Meals per week", "14 Meals per week", "Unlimited Meals"};
- private JComboBox meal; // Combo box for dorm options
- private JLabel label2; // label to display results
- private JTextField selectedMeal; // text field to hold selected dorm
- public MealPlans() {
- // Set new layout to GridLayout manager with 3 rows and 1 column
- setLayout(new GridLayout(3,1));
- // Create combo box, label and text field
- meal = new JComboBox<String>(mealPlan);
- label2 = new JLabel("Meal Plan Selected: ");
- selectedMeal = new JTextField(20);
- selectedMeal.setEditable(false);
- // Add border to panel
- setBorder(BorderFactory.createTitledBorder("Meal Plan Prices"));
- // Create action listener
- meal.addActionListener(new ComboBoxListener());
- // Add components to panel
- add(meal);
- add(label2);
- add(selectedMeal);
- }
- // this inner privtae class will handle the event of an item being selected from the combo box
- private class ComboBoxListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- // Get seleted meal plan name
- String mealSelection = (String)meal.getSelectedItem();
- // Display selection
- selectedMeal.setText(mealSelection);
- } // end method
- }// end class
- public int getMealPlanCost() {
- // Variable
- int mealTotal = 0; // this holds an initial value for meal plan cost
- if(selectedMeal.getText() == "7 Meals per week")
- mealTotal = SEVEN_MEALS_PER_WEEK;
- else if(selectedMeal.getText() == "14 Meals per week")
- mealTotal = FRTN_MEALS_PER_WEEK;
- else if(selectedMeal.getText() == "Unlimited Meals")
- mealTotal = UNLIMITED_MEALS;
- return mealTotal;
- } // end method
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement