Question: The below code gives the same output everytime. Can i get a different solution than this? //Project// Create a java graphics program that displays an

The below code gives the same output everytime. Can i get a different solution than this?

//Project//

Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate Button is pressed. You can also combine the two functions in the same panel if you wish.

Project Requirements:

Develop a project that provides the functionality show in the images above.

The type of shop, items offered and cost is up to the designer. However each item must have a different cost and the final calculation must be correct.

The images display the minimum requirements, however the following is a list of those minimum requirements.

Welcome panel with the shop name and three item panels with panel labels.

At least one set of Checkboxes that allows multiple selection and one set of Radio buttons that are mutually exclusive.

A Panel that holds the calculate and exit button

A popup box or separate panel that displays the cost of the selections and the tax. Note the tax must be calculated after the items are selected.

Implementation Notes:

Create a project that is object oriented, therefore there should be several classes.

Below is an outline of possible structure of the program. This is a basic outline and does not address all the required details. You may want to think about your design first before reading the suggested structure below.

Create a separate class for each Panel except the button panel.

Create an Order Calculation class.

Extends JFrame.

Instance fields are objects of the Panel types.

Adds the Panels to the JFrame using a Boarder Layout

A method to create the Button Panel (Calculate and Exit)

Two inner ActionListener classes that handle the button actions.

A class that contains the main method and simply creates the Order Calculation object.The below code gives the same output everytime. Can i get a

//Solution//

import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton;

public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Order Calculator"); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(); frame.add(panel); placeComponents(panel);

frame.setVisible(true); }

private static void placeComponents(JPanel panel) {

String bread;String coffee; boolean cheese=false,ham=false,turkey=false,beef=false; panel.setLayout(null); JLabel welcomeLabel = new JLabel("Welcome to Johnny's Sandwich Shop"); welcomeLabel.setBounds(20,0,300,20); panel.add(welcomeLabel); JLabel breadLabel = new JLabel("bread"); breadLabel.setBounds(10,21,100,20); panel.add(breadLabel); JRadioButton option1 = new JRadioButton("White"); JRadioButton option2 = new JRadioButton("Wheat"); ButtonGroup group = new ButtonGroup(); group.add(option1); group.add(option2); option1.setBounds(10,41,100,20); panel.add(option1); option1.setSelected(true); option2.setBounds(110,41,100,20); panel.add(option2); if(option1.isSelected()) bread="white"; else bread="wheat"; JLabel meatLabel = new JLabel("meat/cheese"); meatLabel.setBounds(10,81,100,20); panel.add(meatLabel); JCheckBox cheesecheckbox = new JCheckBox("Cheese", true); JCheckBox beefcheckbox = new JCheckBox("Beef", false); JCheckBox turkeycheckbox = new JCheckBox("Turkey", false); JCheckBox hamcheckbox = new JCheckBox("Ham",false); cheesecheckbox.setBounds(10,101,100,20); panel.add(cheesecheckbox); beefcheckbox.setBounds(110,101,100,20); panel.add(beefcheckbox); turkeycheckbox.setBounds(210,101,100,20); panel.add(turkeycheckbox); hamcheckbox.setBounds(310,101,100,20); panel.add(hamcheckbox); JLabel coffeeLabel = new JLabel("coffee"); coffeeLabel.setBounds(10,121,100,20); panel.add(coffeeLabel); if(cheesecheckbox.isSelected()) cheese=true; if(beefcheckbox.isSelected()) cheese=true; if(turkeycheckbox.isSelected()) cheese=true; if(hamcheckbox.isSelected()) cheese=true; JRadioButton option21 = new JRadioButton("None"); JRadioButton option22 = new JRadioButton("Regular"); JRadioButton option23 = new JRadioButton("Decaf"); JRadioButton option24 = new JRadioButton("Cappuchino"); ButtonGroup group1 = new ButtonGroup(); group1.add(option21); group1.add(option22); group1.add(option23); group1.add(option24); option21.setBounds(10,141,100,20); panel.add(option21); option21.setSelected(true); option22.setBounds(110,141,100,20); panel.add(option22); option23.setBounds(210,141,100,20); panel.add(option23); option24.setBounds(310,141,100,20); panel.add(option24); if(option21.isSelected()) coffee="none"; else if(option22.isSelected()) coffee="regular"; else if(option23.isSelected()) coffee="decaf"; else coffee="cappuchino"; JButton close = new JButton("Close"); close.addActionListener(new CloseListener()); close.setBounds(10,181,100,20); panel.add(close); JButton calculate = new JButton("Calculate"); calculate.addActionListener(new CalculateListener(bread,coffee,cheese,beef,turkey,ham)); calculate.setBounds(110,181,100,20); panel.add(calculate);

}

} --------------------------------------------------------------------------------------------------------------------- import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

public class CalculateListener implements ActionListener{

private double subtotal; private double white_bread; private double wheat_bread; private double cheese; private double beef; private double turkey; private double ham; private double regular; private double capuccino; private double decaf; public CalculateListener(String bread, String coffee, boolean cheese2, boolean beef2, boolean turkey2, boolean ham2) { subtotal=0; white_bread=0.99; wheat_bread=0.99; cheese=0.50; beef=0.50; turkey=0.50; ham=0.50; regular=2.99; capuccino=3.99; decaf=4.99; if(bread.equals("white")) subtotal+=white_bread; else subtotal+=wheat_bread; if(coffee.equals("none")) subtotal+=0; else if(coffee.equals("regular")) subtotal+=regular; else if(coffee.equals("decaf")) subtotal+=decaf; else subtotal+=capuccino; if(cheese2) subtotal+=cheese; if(ham2) subtotal+=ham; if(turkey2) subtotal+=turkey; if(beef2) subtotal+=beef; }

@Override public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame("Bill generator"); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(); JLabel subtotalLabel = new JLabel("Subtotal: "+subtotal+"$"); subtotalLabel.setBounds(20,0,300,20); panel.add(subtotalLabel); JLabel taxLabel = new JLabel("tax: "+0.17*subtotal+"$"); taxLabel.setBounds(20,100,300,20); panel.add(taxLabel); JLabel totalLabel = new JLabel("total: "+1.17*subtotal+"$"); totalLabel.setBounds(20,200,300,20); panel.add(totalLabel); JButton close = new JButton("Close"); close.addActionListener(new CloseListener()); close.setBounds(20,300,100,20); panel.add(close); frame.add(panel); frame.setVisible(true); } } --------------------------------------------------------------------------------------------------------------------- import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

public class CloseListener implements ActionListener {

@Override public void actionPerformed(ActionEvent e) { //DO SOMETHING System.exit(0); }

}

Order Calculator Welcome to Johmy' Sandwich Shop BradscheeseCoffee WWhite Calculat Subotal: $4.75 Tax: $02R

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!