import javax.swing.*; import java.awt.*; import java.awt.event.*; /** The OrderC
ID: 3919346 • Letter: I
Question
import javax.swing.*; import java.awt.*; import java.awt.event.*; /** The OrderCalculatorGUI class creates the GUI for the Brandi's Bagel House application. */ public class OrderCalculatorGUI extends JFrame { private BagelPanel bagels; // Bagel panel private ToppingPanel toppings; // Topping panel private CoffeePanel coffee; // Coffee panel private GreetingPanel banner; // To display a greeting private JPanel buttonPanel; // To hold the buttons private JButton calcButton; // To calculate the cost private JButton exitButton; // To exit the application private final double TAX_RATE = 0.06; // Sales tax rate /** Constructor */ public OrderCalculatorGUI() { // Display a title. setTitle("Order Calculator"); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a BorderLayout manager. setLayout(new BorderLayout()); // Create the custom panels. banner = new GreetingPanel(); bagels = new BagelPanel(); toppings = new ToppingPanel(); coffee = new CoffeePanel(); // Create the button panel. buildButtonPanel(); // Add the components to the content pane. add(banner, BorderLayout.NORTH); add(bagels, BorderLayout.WEST); add(toppings, BorderLayout.CENTER); add(coffee, BorderLayout.EAST); add(buttonPanel, BorderLayout.SOUTH); // Pack the contents of the window and display it. pack(); /* * MAKE THE PANEL VISIBLE */ } /** The buildButtonPanel method builds the button panel. */ private void buildButtonPanel() { // Create a panel for the buttons. buttonPanel = new JPanel(); // Create the buttons. calcButton = new JButton("Calculate"); exitButton = new JButton("Exit"); // Register the action listeners. calcButton.addActionListener(new CalcButtonListener()); exitButton.addActionListener(new ExitButtonListener()); // Add the buttons to the button panel. buttonPanel.add(calcButton); buttonPanel.add(exitButton); } /** Private inner class that handles the event when the user clicks the Calculate button. */ /* * Create your inner class that computes the total do * Look to line 73 for your class name Here is body of the implemented * method that you have to code. Copy and paste this body into your class/method * BEGINING OF COPY // Variables to hold the subtotal, tax, and total double subtotal, tax, total; // Calculate the subtotal. subtotal = bagels.getBagelCost() + toppings.getToppingCost() + coffee.getCoffeeCost(); // Calculate the sales tax. tax = subtotal * TAX_RATE; // Calculate the total. total = subtotal + tax; // Display the charges. JOptionPane.showMessageDialog(null, String.format("Subtotal: $%,.2f " + "Tax: $%,.2f " + "Total: $%,.2f", subtotal, tax, total)); * END OF COPY */ /** Private inner class that handles the event when the user clicks the Exit button. */ /* * Create your inner class that cause the entire program/system to EXIT (hint) * Look to line 74 for your class name */ }
Explanation / Answer
===========Test.java==============
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
The OrderCalculatorGUI class creates the GUI for the
Brandi's Bagel House application.
*/
class OrderCalculatorGUI extends JFrame
{
private BagelPanel bagels; // Bagel panel
private ToppingPanel toppings; // Topping panel
private CoffeePanel coffee; // Coffee panel
private GreetingPanel banner; // To display a greeting
private JPanel buttonPanel; // To hold the buttons
private JButton calcButton; // To calculate the cost
private JButton exitButton; // To exit the application
private final double TAX_RATE = 0.06; // Sales tax rate
/**
Constructor
*/
public OrderCalculatorGUI()
{
// Display a title.
setTitle("Order Calculator");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a BorderLayout manager.
setLayout(new BorderLayout());
// Create the custom panels.
banner = new GreetingPanel();
bagels = new BagelPanel();
toppings = new ToppingPanel();
coffee = new CoffeePanel();
// Create the button panel.
buildButtonPanel();
// Add the components to the content pane.
add(banner, BorderLayout.NORTH);
add(bagels, BorderLayout.WEST);
add(toppings, BorderLayout.CENTER);
add(coffee, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);
// Pack the contents of the window and display it.
pack();
setVisible(true);
/*
* MAKE THE PANEL VISIBLE
*/
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// Variables to hold the subtotal, tax, and total
double subtotal, tax, total, st;
// Calculate the subtotal.
subtotal = bagels.getBagelCost() +
toppings.getToppingCost() +
coffee.getCoffeeCost();
// Calculate the sales tax.
tax = subtotal * TAX_RATE;
// Calculate the total.
total = subtotal + tax;
// Display the charges.
JOptionPane.showMessageDialog(null,
String.format("Subtotal: $%,.2f " +
"Tax: $%,.2f " +
"Total: $%,.2f",
subtotal, tax, total));
}
}
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
}
/**
The buildButtonPanel method builds the button panel.
*/
private void buildButtonPanel()
{
// Create a panel for the buttons.
buttonPanel = new JPanel();
// Create the buttons.
calcButton = new JButton("Calculate");
exitButton = new JButton("Exit");
// Register the action listeners.
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
// Add the buttons to the button panel.
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}
/**
Private inner class that handles the event when
the user clicks the Calculate button.
*/
/*
* Create your inner class that computes the total do
* Look to line 73 for your class name Here is body of the implemented
* method that you have to code. Copy and paste this body into your class/method
* BEGINING OF COPY
* END OF COPY
*/
/**
Private inner class that handles the event when
the user clicks the Exit button.
*/
/*
* Create your inner class that cause the entire program/system to EXIT (hint)
* Look to line 74 for your class name
*/
}
public class Test
{
public static void main(String[] args) {
System.out.println("main");
OrderCalculatorGUI ocg2=new OrderCalculatorGUI();
}
}
===========ToppingPanel.java==============
import java.awt.GridLayout;
import javax.swing.*;
public class ToppingPanel extends JPanel{
public final double CHEESE=0.50;
public final double honey=0.25;
public final double JELLY=0.75;
public final double BERRY=0.75;
private JCheckBox creamCheese;
private JCheckBox honey_1;
private JCheckBox peachJelly;
private JCheckBox blueberryJam;
public ToppingPanel(){
setLayout(new GridLayout(4,1));
creamCheese=new JCheckBox("Creame cheese");
honey_1=new JCheckBox("honey");
peachJelly=new JCheckBox("Peach jelly");
blueberryJam=new JCheckBox("Blueberry Jam");
setBorder(BorderFactory.createTitledBorder("Toppings"));
add(creamCheese);
add(honey_1);
add(peachJelly);
add(blueberryJam);
}
public double getToppingCost(){
double toppingCost=0.0;
if(creamCheese.isSelected()){
toppingCost+=CHEESE;
}
else if(honey_1.isSelected()){
toppingCost+=honey;
}
else if(peachJelly.isSelected()){
toppingCost+=JELLY;
}
else {
toppingCost+=BERRY;
}
return toppingCost;
}
}
===========GreetingPanel.java==============
import javax.swing.*;
public class GreetingPanel extends JPanel {
private JLabel greeting;
public GreetingPanel() {
greeting = new JLabel("Welcome tho Brandi's Bagel House");
add(greeting);
}
}
===========CoffeePanel.java==============
import java.awt.GridLayout;
import javax.swing.*;
public class CoffeePanel extends JPanel {
public final double NO_COFFEE = 0.0;
public final double REGULAR_COFFEE = 1.25;
public final double DECAF_COFFEE = 1.25;
public final double CAPPUCCINO = 2.00;
private JRadioButton noCoffee;
private JRadioButton regularCoffee;
private JRadioButton decafCoffee;
private JRadioButton cappuccino;
private ButtonGroup bg;
public CoffeePanel() {
setLayout(new GridLayout(4, 1));
noCoffee = new JRadioButton("None");
regularCoffee = new JRadioButton("Regular coffee", true);
decafCoffee = new JRadioButton("Decaf coffee");
cappuccino = new JRadioButton("Cappuccino");
bg = new ButtonGroup();
bg.add(cappuccino);
bg.add(decafCoffee);
bg.add(noCoffee);
bg.add(regularCoffee);
setBorder(BorderFactory.createTitledBorder("Coffee"));
add(noCoffee);
add(regularCoffee);
add(decafCoffee);
add(cappuccino);
}
public double getCoffeeCost() {
double coffeeCost = 0.0;
if (noCoffee.isSelected()) {
coffeeCost += NO_COFFEE;
} else if (regularCoffee.isSelected()) {
coffeeCost += REGULAR_COFFEE;
} else if (decafCoffee.isSelected()) {
coffeeCost += DECAF_COFFEE;
} else {
coffeeCost += CAPPUCCINO;
}
return coffeeCost;
}
}
===========ButtonPanel.java==============
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;
public class ButtonPanel extends JPanel {
private class Exiting implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
private class Calculating implements ActionListener {
public void actionPerformed(ActionEvent e) {
//if (e.getSource() == calButton) {
// System.out.println("Cong");
}
double subtotal, tax, total;
subtotal = bagels.getBagelCost() + toppings.getToppingCost()
+ coffee.getCoffeeCost();
tax = subtotal * TAX_RATE;
total = subtotal + tax;
DecimalFormat dollar = new DecimalFormat("0.00");
JOptionPane.showMessageDialog(null, "Subtotal: $"
+ dollar.format(subtotal) + " Tax: $" + dollar.format(tax)
+ " Total: $" + dollar.format(total));
}
}
private BagelPanel bagels;
private ToppingPanel toppings;
private CoffeePanel coffee;
private GreetingPanel banner;
private JButton calButton;
private JButton exitButton;
private final double TAX_RATE = 0.06;
public ButtonPanel() {
setLayout(new GridLayout(1, 2));
calButton = new JButton("Calculate");
exitButton = new JButton("Exit");
add(calButton);
add(exitButton);
calButton.addActionListener(new Calculating());
exitButton.addActionListener(new Exiting());
}
}
===========BagelPanel.java==============
import java.awt.GridLayout;
import javax.swing.*;
public class BagelPanel extends JPanel {
public final double RED_BAGEL = 1.25;
public final double RICE_BAGEL = 1.50;
private JRadioButton REDBagel;
private JRadioButton RICEBagel;
private ButtonGroup bg;
public BagelPanel() {
setLayout(new GridLayout(2, 1));
REDBagel = new JRadioButton("RED", true);
RICEBagel = new JRadioButton("RICE");
bg = new ButtonGroup();
bg.add(REDBagel);
bg.add(RICEBagel);
setBorder(BorderFactory.createTitledBorder("Bagel"));
add(RICEBagel);
add(REDBagel);
}
public double getBagelCost() {
double bagelCost = 0.0;
if (REDBagel.isSelected()) {
bagelCost = RED_BAGEL;
} else {
bagelCost = RICE_BAGEL;
}
return bagelCost;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.