Creating a JAVA GUI Pizza Order Application

Asked 2 years ago, Updated 2 years ago, 129 views

The JAVA GUI pizza ordering application is in the process of course. I think I've done everything else, even if I'm immature I'm asking you this question because there's something I don't know about printing the price at the end. You should add SizePrice and ExtraPrice to TotalPrice so that it can be output to Price TextField Should I say arithmetic expression? I'm not sure about that part. I'd appreciate it if you could help me.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class WelcomePanel extends JPanel {

private JLabel message;
public WelcomePanel() {
    message = new JLabel ("Welcome to Java Pizza";
    add(message);
}

}

class TypePanel extends JPanel {

private JRadioButton combo, potato, bulgogi;
public TypePanel() {
    setLayout(new GridLayout(3,1));
    combo = new JRadioButton ("combo", true);
    potato = new JRadioButton ("potato");
    bulkogi = new JRadioButton ("Bulgogi");
    ButtonGroup Type = new ButtonGroup();
    Type.add(combo);
    Type.add(potato);
    Type.add(bulgogi);
    setBorder(BorderFactory.createTitledBorder("type"));
    add(combo);
    add(potato);
    add(bulgogi);
}

} class ToppingPanel extends JPanel implements ItemListener {

private JCheckBox greenpeper, cheese, peperoni, bacon;
int ExtraPrice = 0;
public ToppingPanel() {
    setLayout(new GridLayout(4,1));
    greenpepper = new JCheckBox ("pepper");
    cheese = new JCheckBox ("cheese");
    peperoni = new JCheckBox ("peperoni");
    bacon = new JCheckBox ("bacon");
    setBorder(BorderFactory.createTitledBorder("Additional Topping"));
    add(greenpeper);
    add(cheese);
    add(peperoni);
    add(bacon);
    greenpeper.addItemListener(this);
    cheese.addItemListener(this);
    peperoni.addItemListener(this);
    bacon.addItemListener(this);
}

@Override
public void itemStateChanged(ItemEvent topping) {

    if(greenpeper.isSelected() == true)
        ExtraPrice += 1000;
    if(cheese.isSelected() == true)
        ExtraPrice += 1000;
    if(peperoni.isSelected() == true)
        ExtraPrice += 1000;
    if(bacon.isSelected() == true)
        ExtraPrice += 1000;
    }

}

class SizePanel extends JPanel implements ActionListener {

private JRadioButton s, m, l;
int SizePrice;
public SizePanel() {
setLayout(new GridLayout(3,1));
s = new JRadioButton("Small",true);
m = new JRadioButton("Medium");
l = new JRadioButton("Large");
ButtonGroup Size = new ButtonGroup();
Size.add(s);
Size.add(m);
Size.add(l);
setBorder(BorderFactory.createTitledBorder("Size"));
add(s);
add(m);
add(l);
s.addActionListener(this);
m.addActionListener(this);
l.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent size) {
    if(s.isSelected() == true) {
        SizePrice = 12000;
    }
    if(m.isSelected() == true) {
        SizePrice = 15000;
    }
    if(l.isSelected() == true) {
        SizePrice = 18000;
    }
}

}

public class PizzaOrder extends JFrame {

private static final int ExtraPrice = 0;
private static final int SizePrice = 0;
private JButton order, cancel;
JTextField Price;
int TotalPrice;
WelcomePanel WP = new WelcomePanel();
TypePanel TP = new TypePanel();
ToppingPanel TpP = new ToppingPanel();
SizePanel SP = new SizePanel();

public PizzaOrder() {
    setSize(550,250);
    setTitle ("Order Pizza");

    JPanel J = new JPanel();
    order = new JButton ("Order");
    cancel = new JBButton ("Cancel");
    Price = new JTextField(10);
    J.add(order);
    J.add(cancel);
    J.add(Price);

    add(WP,BorderLayout.NORTH);
    add(TP,BorderLayout.WEST);
    add(TpP,BorderLayout.CENTER);
    add(SP,BorderLayout.EAST);
    add(J,BorderLayout.SOUTH);

    order.addActionListener(a -> {
        ## It's this part.

        Price.setText("" + TotalPrice);
    });

    cancel.addActionListener(b -> {
        TotalPrice = 0;
        Price.setText("" + TotalPrice);
    });
    setVisible(true);
}
public static void main(String[] args) {
    PizzaOrder PO = new PizzaOrder();
}

}

java gui order application

2022-09-21 15:15

1 Answers

Hello :-)

SizePrice can be found here:

class SizePanel extends JPanel implements ActionListener {
//...Optimized...
public int SizePrice;
//...Optimized...
}

ExtraPrice can be found here:

private JCheckBox greenpeper, cheese, peperoni, bacon;
//...Optimized...
public int ExtraPrice = 0;
//...Optimized...

}

I just need to add everything from the event method that needs to be printed out, right?

public class PizzaOrder extends JFrame {

// **You won't need to be a member here because you're going to import them from the member variable in the class above.
//private static final int ExtraPrice = 0;
//private static final int SizePrice = 0;
int TotalPrice;
//...Optimized...
public PizzaOrder() {
    // ...Optimized...
    order.addActionListener(a -> {
        // This part...
        this.TotalPrice = SizePanel.SizePrice+ToppingPanel.ExtraPrice;
        Price.setText("" + TotalPrice.toString());
    });
    // ...Optimized...
}

The advice you get here can put out the fire right now, but why don't you study a little more to make it "mine"?

I hope it was helpful.

Thank you.


2022-09-21 15:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.