Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a Sketch of the Order Calculator window. The layout shown in the sketch c

ID: 3568388 • Letter: D

Question

Design a Sketch of the Order Calculator window.

The layout shown in the sketch can be achieved using a BorderLayout manager with the window's content pane. The label that displays "Welcome to Brandi's Bagel House" is in the north region, the radio buttons for the bagel types are in the west region, the check boxes for the toppings are in the center region, the radio buttons for the coffee selection are in the east region, and the Calculate and Exit buttons are in the south region. To construct this window we create the following specialized panel classes that inherit from JPanel:

CreetingsPanel. this panel contains the label that appears in the window's north region.

BagelPanel. this panel conains the radio buttons for the types of bagels.

ToppingsPanel. this panel contains the check boxes for the types of bagels.

CoffeePanel. this panel contains the radio buttons for the coffee selections.

(We will not create a specialized panel for the Calculate and Exit buttons. The reason is explained later.) After these classes have been created, we can create objects from them and add the objects to the correct regions of the window's content pane.

Order Calculator Welcome to Brandi's Bagel House Toppings- -Bagel-- -Coffee Coffee ! Cream cheese!! Butter ONone white Regular coffee ODecaf coffee Ocappuccino o whole wheat | | O Whole whePeach jelly Blueberry Jam Calculate Exit

Explanation / Answer

Java program to display order calculator window: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; /** 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(); setVisible(true); } /** 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. */ private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { // 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; // Create a DecimalFormat object to format output. DecimalFormat dollar = new DecimalFormat("0.00"); // Display the charges. JOptionPane.showMessageDialog(null, "Subtotal: $" + dollar.format(subtotal) + " " + "Tax: $" + dollar.format(tax) + " " + "Total: $" + dollar.format(total)); } } /** Private inner class that handles the event when the user clicks the Exit button. */ private class ExitButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } }