Project overview: Create a java graphics program that displays an order menu and
ID: 3915481 • Letter: P
Question
Project overview:
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.
Explanation / Answer
////SOurceCode:
//Library files
import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JRadioButton;
//Class declaration
public class MyFrame {
//main class
public static void main(String[] args) {
JFrame frame = new JFrame("OrcderCalculator");//Create frame
frame.setSize(500, 500);//frame size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close calc
JPanel panel = new JPanel();//Object creation
frame.add(panel); //Adding panel to the frame
placeComponents(panel);
frame.setVisible(true);
}
//Protected Method
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");//Label
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()) //Check
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);
//size
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 select ..then it will change to true
if(cheesecheckbox.isSelected())
cheese=true;
if(beefcheckbox.isSelected())
cheese=true;
if(turkeycheckbox.isSelected())
cheese=true;
if(hamcheckbox.isSelected())
cheese=true;
JRadioButton option11 = new JRadioButton("None");
JRadioButton option12 = new JRadioButton("Regular");
JRadioButton option13 = new JRadioButton("Decaf");
JRadioButton option14 = new JRadioButton("Cappuchino");
ButtonGroup group1 = new ButtonGroup();
group1.add(option11);
group1.add(option12);
group1.add(option13);
group1.add(option14);
option11.setBounds(10,141,100,20);
panel.add(option11);
option11.setSelected(true);
option12.setBounds(110,141,100,20);
panel.add(option12);
option13.setBounds(210,141,100,20);
panel.add(option13);
option14.setBounds(310,141,100,20);
panel.add(option14);
if(option11.isSelected())
coffee="none";
else if(option12.isSelected())
coffee="regular";
else if(option13.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.JLabel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CalculateListener implements ActionListener{
private double subtot;
private double white_brad;
private double wheat_brad;
private double cheese;
private double beef;
private double turk;
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) {
subtot=0;
white_brad=0.99;
wheat_brad=0.99;
cheese=0.50;
beef=0.50;
turk=0.50;
ham=0.50;
regular=2.99;
capuccino=3.99;
decaf=4.99;
if(bread.equals("white"))
subtot+=white_brad;
else
subtot+=wheat_brad;
if(coffee.equals("none"))
subtot+=0;
else if(coffee.equals("regular"))
subtot+=regular;
else if(coffee.equals("decaf"))
subtot+=decaf;
else
subtot+=capuccino;
if(cheese2)
subtot+=cheese;
if(ham2)
subtot+=ham;
if(turkey2)
subtot+=turk;
if(beef2)
subtot+=beef;
}
@Override
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("Bill generator");
frame.setSize(550, 550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel subtotLabel = new JLabel("Subtotal: "+subtot+"$");
subtotLabel.setBounds(20,0,300,20);
panel.add(subtotLabel);
JLabel taxLabel = new JLabel("tax: "+0.17*subtot+"$");
taxLabel.setBounds(20,100,300,20);
panel.add(taxLabel);
JLabel totalLabel = new JLabel("total: "+1.17*subtot+"$");
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.ActionListener;
import java.awt.event.ActionEvent;
public class CloseListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//DO SOMETHING
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.