I am making a GUI application on pizza Shop menu. it asks for name of customer a
ID: 3920902 • Letter: I
Question
I am making a GUI application on pizza Shop menu. it asks for name of customer and type of pizza as well as size of pizza. I did everything but i am not able to do the price part. Below is the screenshot of the price list. For regular pizza we can not check more than 2 toppings for the checkbox(i dont know how to do that) and no more that 3 toppings in feast. We have to display the total cost in scene 1(according to the code written below).
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setHgap(10);
root.setVgap(10);
Scene scene = new Scene(root,600,600);
Label title = new Label("Enter Details");
root.add(title, 0, 0, 2, 1);
title.setFont(Font.font(60));
TextField firstNameFld = new TextField();
TextField lastNameFld = new TextField();
Label firstNameLb1 = new Label("First Name: ");
Label lastNameLb1 = new Label("Last Name: ");
firstNameLb1.setLabelFor(firstNameFld);
lastNameLb1.setLabelFor(lastNameFld);
firstNameLb1.setMnemonicParsing(true);
lastNameLb1.setMnemonicParsing(true);
root.addRow(1, firstNameLb1, firstNameFld);
root.addRow(2, lastNameLb1, lastNameFld);
Button btn = new Button("Order Details");
root.add(btn, 0,14);
Label title1 = new Label("Choose the type of pizza you want");
root.add(title1, 0,3);
title1.setStyle("-fx-font-weight: bold;");
RadioButton radiobutton4 = new RadioButton("Regular");
RadioButton radiobutton5 = new RadioButton("Feast");
ToggleGroup radioGroup1 = new ToggleGroup();
radiobutton4.setToggleGroup(radioGroup1);
radiobutton5.setToggleGroup(radioGroup1);
VBox vbox1 = new VBox(radiobutton4, radiobutton5 );
root.add(vbox1, 0, 5, 2, 1 );
Label title2 = new Label("Choose the size of pizza you want");
root.add(title2, 0,7);
title2.setStyle("-fx-font-weight: bold;");
RadioButton radioButton1 = new RadioButton("small");
RadioButton radioButton2 = new RadioButton("medium");
RadioButton radioButton3 = new RadioButton("large");
ToggleGroup radioGroup = new ToggleGroup();
radioButton1.setToggleGroup(radioGroup);
radioButton2.setToggleGroup(radioGroup);
radioButton3.setToggleGroup(radioGroup);
VBox vbox2 = new VBox(radioButton1, radioButton2, radioButton3);
root.add(vbox2, 0, 8, 3, 2 );
Label title4 = new Label("Choose the toppings you would like to have on the pizza ");
root.add(title4, 0,10);
title4.setStyle("-fx-font-weight: bold;");
CheckBox cb1 = new CheckBox("Chicken");
CheckBox cb2 = new CheckBox("peppers");
CheckBox cb3 = new CheckBox("mushrooms");
HBox hbox = new HBox(cb1,cb2,cb3);
root.add(hbox, 0, 12, 2, 1 );
GridPane root2 = new GridPane();
root2.setAlignment(Pos.CENTER);
root2.setHgap(10);
root2.setVgap(10);
Scene scene1 = new Scene(root2,600,600);
Label outRadioBtn1 = new Label();
root2.add(outRadioBtn1, 0, 2);
Label outRadioBtn2 = new Label();
root2.add(outRadioBtn2, 0, 3);
Label nameLabel = new Label();
root2.add(nameLabel, 0, 1);
Label cbOut = new Label();
root2.add(cbOut, 0, 4);
GridPane root3 = new GridPane();
root3.setAlignment(Pos.CENTER);
root3.setHgap(10);
root3.setVgap(10);
Scene scene2= new Scene(root3,400,400);
GridPane root4 = new GridPane();
root4.setAlignment(Pos.CENTER);
root4.setHgap(10);
root4.setVgap(10);
Scene scene3= new Scene(root4,500,500);
Hyperlink hyperlink = new Hyperlink("Debit");
root3.add(hyperlink, 0, 1);
hyperlink.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
getHostServices().showDocument("http://interac.ca/en/interac-online-faq.html");
}
});
Hyperlink hyperlink1 = new Hyperlink("Credit");
root3.add(hyperlink1, 0, 3);
hyperlink1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
getHostServices().showDocument("https://www.paypal.com/ca/signin");
}
});
Hyperlink Cash = new Hyperlink("Cash");
root4.add(Cash, 0, 5);
Image image = new Image("file:src/application/cash.jpg");
ImageView iv = new ImageView();
Cash.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
iv.setImage(image);
root4.add(iv, 0, 1);
}
});
Label title3 = new Label("How would you like to pay for your order");
root2.add(title3, 0,6);
title3.setStyle("-fx-font-weight: bold;");
RadioButton radiobutton6 = new RadioButton("Cash");
RadioButton radiobutton7 = new RadioButton("Debit or Credit");
ToggleGroup radioGroup2 = new ToggleGroup();
radiobutton6.setToggleGroup(radioGroup2);
radiobutton7.setToggleGroup(radioGroup2);
VBox vbox3 = new VBox(radiobutton6, radiobutton7 );
root2.add(vbox3, 0, 7, 2, 1 );
Button btn1 = new Button("go back to previous scene");
root2.add(btn1, 0, 12);
Button btn2 = new Button("Submit");
root2.add(btn2, 0, 10);
Button btn3 = new Button("go back to previous scene");
root3.add(btn3, 0, 7);
Button btn4 = new Button("go back to previous scene");
root4.add(btn4, 0, 7);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(scene1);
if(radioButton1.isSelected()) {
outRadioBtn2.setText("Size of the Pizza : Small");
}
if(radioButton2.isSelected()) {
outRadioBtn2.setText("Size of the Pizza : Medium ");
}
if(radioButton3.isSelected()) {
outRadioBtn2.setText("Size of the Pizza : Large");
}
if(radiobutton4.isSelected()) {
outRadioBtn1.setText("Type of the Pizza : Regular ");
}
if(radiobutton5.isSelected()) {
outRadioBtn1.setText("Type of the Pizza : Feast");
}
nameLabel.setText("Name of the Customer : "+ firstNameFld.getText() +" " + lastNameFld.getText());
String Toppings = null;
if (cb1.isSelected()) {
Toppings = " Chicken ";
cbOut.setText(Toppings);
}
if (cb2.isSelected()) {
Toppings += " Peppers ";
cbOut.setText(Toppings);
}
if (cb3.isSelected()) {
Toppings += " Mushrooms ";
cbOut.setText(Toppings);
}
primaryStage.show();
}
});
btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(scene);
primaryStage.show();
}
});
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(radiobutton6.isSelected()) {
primaryStage.setScene(scene3);
}
if(radiobutton7.isSelected()) {
primaryStage.setScene(scene2);
}
primaryStage.show();
}
});
btn3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(scene1);
primaryStage.show();
}
});
btn4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(scene1);
primaryStage.show();
}
});
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is the price list.
Pizza 2 ToppingsRegular Feast 3 Toppings 2.5S/Topping Small (108) Small (105) 2.5S/Topping Medium (125) Medium (12S) Large (14S) Large (145) Total Cost of Pizza TypeofPizzaBaseCost NumberofToppings CostPerToppingExplanation / Answer
There are two types of programs for this :-
PIZZA GUI
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
public class PizzaCalculator extends JFrame implements ActionListener {
JPanel westPanel = new JPanel();
JPanel eastPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
// pizza size options
JRadioButton Small = new JRadioButton("Small");
JRadioButton Medium = new JRadioButton("Medium");
JRadioButton Large = new JRadioButton("Large");
// pizza topping selections
JCheckBox Sausage = new JCheckBox("Sausage");
JCheckBox Olives = new JCheckBox("Olives");
JCheckBox Pepperoni = new JCheckBox("Pepperoni");
JCheckBox Mushrooms = new JCheckBox("Mushrooms");
JCheckBox Salami = new JCheckBox("Salami");
JCheckBox Anchovies = new JCheckBox("Anchovies");
// calculate, clear, and total buttons
JButton Calculate = new JButton("Calculate");
JButton Exit = new JButton("Exit");
JLabel Total = new JLabel("Price: ");
JTextField TotalResult = new JTextField(8);
// variables used to calculate total
int TopQty = 0;
double Size = 0;
double TopCost = 0;
double TotalCost = 0;
public PizzaCalculator()
{
// layout north portion of frame
northPanel.setLayout(new GridLayout());
northPanel.setBorder(new TitledBorder("Size:"));
// register listeners with buttons
Small.addActionListener(this);
Medium.addActionListener(this);
Large.addActionListener(this);
Calculate.addActionListener(this);
ButtonGroup pizzaSize = new ButtonGroup();
pizzaSize.add(Small);
pizzaSize.add(Medium);
pizzaSize.add(Large);
// add buttons to north panel
northPanel.add(Small);
northPanel.add(Medium);
northPanel.add(Large);
centerPanel.setLayout(new GridLayout(5, 3));
centerPanel.setBorder(new TitledBorder("Toppings:"));
centerPanel.add(Sausage);
centerPanel.add(Olives);
centerPanel.add(Pepperoni);
centerPanel.add(Mushrooms);
centerPanel.add(Salami);
centerPanel.add(Anchovies);
southPanel.add(Total);
southPanel.add(TotalResult);
southPanel.add(Calculate);
southPanel.add(Exit);
// add all portions of panel
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
}
// handle action event for each pizza option
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == Small)
setSizeS();
else if (e.getSource() == Medium)
setSizeM();
else if (e.getSource() == Large)
setSizeL();
if (e.getSource() == Sausage)
TopQty++;
else if (e.getSource() == Olives)
TopQty++;
else if (e.getSource() == Pepperoni)
TopQty++;
else if (e.getSource() == Mushrooms)
TopQty++;
else if (e.getSource() == Salami)
TopQty++;
else if (e.getSource() == Anchovies)
TopQty++;
TopCost(TopQty);
if (e.getSource() == Calculate)
calculate(Size, TopCost);
}
public double setSizeS()
{
return Size = 6.99;
}
public double setSizeM()
{
return Size = 8.99;
}
public double setSizeL()
{
return Size = 10.99;
}
public double setTopCost()
{
if (TopQty == 1)
TopCost = 1.49;
else if (TopQty == 2)
TopCost = 1.49;
else if (TopQty == 3)
TopCost = 1.49;
else if (TopQty == 4)
TopCost = 0.99;
else if (TopQty == 5)
TopCost = 0.99;
else if (TopQty == 6)
TopCost = 0.99;
return TopCost;
}
public void calculate(double Size, double TopCost)
{
TotalCost = Size + TopCost;
TotalResult.setText(String.valueOf(TotalCost));
}
public static void main(String[] args)
{
PizzaCalculator PizzaCalc = new PizzaCalculator();
// set window title
PizzaCalc.setTitle("Pizza Calculator");
// set window location
PizzaCalc.setLocationRelativeTo(null);
// specify window location
PizzaCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set window size
PizzaCalc.setSize(300, 350);
// display window
PizzaCalc.setVisible(true);
}
}
2nd PIZZA GUI CODE
import java.text.NumberFormat;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
public class PizzaFrame extends JFrame
{
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
private JCheckBox pepperoniBox;
private JCheckBox anchoviesBox;
private ActionListener listener; // needs to update price
private JLabel priceArea; // doesn't need to be (big) text area
/** We want our frame to consist of 3 basic parts:
* A set of radio buttons for size of pizza
* a couple check boxes to choose toppings (pepperoni and/or anchovies)
* a section where the price of the pizza is to be shown.
* So that this method doesn't get too long, these 3 parts will be
* created in other methods.
*/
public PizzaFrame()
{
class DetectInput implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
updatePrice();
}
}
listener = new DetectInput();
JPanel sizePanel = createButtons();
JPanel toppingPanel = createBoxes();
// put first 2 panels into another panel to group them
JPanel centerPanel = new JPanel();
centerPanel.add(sizePanel);
centerPanel.add(toppingPanel);
JPanel pricePanel = createOutput();
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(pricePanel, BorderLayout.SOUTH);
pack();
}
/** createButtons - Radio buttons for the size panel.
*/
public JPanel createButtons()
{
// make 3 buttons, and attach the listener to them; make medium default
smallButton = new JRadioButton("Small");
mediumButton = new JRadioButton("Medium");
largeButton = new JRadioButton("Large");
smallButton.addActionListener(listener);
mediumButton.addActionListener(listener);
largeButton.addActionListener(listener);
mediumButton.setSelected(true);
// We need a button group because we want only 1 button to appear chosen
// at a time.
ButtonGroup group = new ButtonGroup();
group.add(smallButton);
group.add(mediumButton);
group.add(largeButton);
// make a panel for the radio buttons
JPanel radioButtonPanel = new JPanel();
radioButtonPanel.setLayout(new GridLayout(3,1));
// do we need a border?
radioButtonPanel.add(smallButton);
radioButtonPanel.add(mediumButton);
radioButtonPanel.add(largeButton);
return radioButtonPanel;
}
/** createBoxes - Check boxes for topping panel.
*/
public JPanel createBoxes()
{
// make 2 buttons for the toppings boxes
pepperoniBox = new JCheckBox("Pepperoni");
anchoviesBox = new JCheckBox("Anchovies");
pepperoniBox.addActionListener(listener);
anchoviesBox.addActionListener(listener);
JPanel boxPanel = new JPanel();
boxPanel.setLayout(new GridLayout(2,1));
boxPanel.add(pepperoniBox);
boxPanel.add(anchoviesBox);
// Do we want border around panel? Let's try it!
// JPanel's setBorder method takes a parameter that is an object of a
// class that implements the Border interface.
// The BorderFactory class has many static methods that can create
// borders for us.
boxPanel.setBorder(BorderFactory.createLineBorder(Color.green));
return boxPanel;
}
public JPanel createOutput()
{
JPanel pricePanel = new JPanel();
pricePanel.add(new JLabel("Your Price: "));
// initialize price and put it in panel too
priceArea = new JLabel();
updatePrice(); // find price of default pizza
pricePanel.add(priceArea);
return pricePanel;
}
public void updatePrice()
{
double price;
if (smallButton.isSelected())
price = 5.25;
else if (mediumButton.isSelected())
price = 6.50;
else // then it must be large
price = 7.75;
if (pepperoniBox.isSelected())
{
price += 0.60;
}
if (anchoviesBox.isSelected())
{
price += 0.30;
}
// format the price and put it in the priceArea label
NumberFormat money = NumberFormat.getCurrencyInstance();
String output = money.format(price);
priceArea.setText("" + output);
repaint();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.