hi everyone, i am doing a pizza ordering Gui. i am stuck on 2 things, i am trying to get the receipt string to store the items that are checked. they are multiplying when they are displaying. the second is displaying the string in the jtext area,
any help woud be greatfully apreciated.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Pizza {
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
JPanel thirdPanel = new JPanel();
int topPrice;
ActionListener listener = new PriceListener();
ActionListener Listener = new ButtonListener();
JButton order, reset, cancel;
JTextArea ta;
JRadioButton small, medium, large, giant;
JCheckBox pepperoni, ham, onions, peppers, sweetcorn, chicken, lamb, meatballs, pineapple;
JCheckBox GarlicBread, Wedges, Fries, ChickenTenders, Coke, SevenUP;
String receipt, receipt1;
public class PriceListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double totalPrice = 0;
if (small.isSelected()) {
totalPrice = 6.00;
receipt += "Small\n";
}if (medium.isSelected()) {
totalPrice = 8.00;
receipt += "Medium\n";
}if (large.isSelected()) {
totalPrice = 9.00;
receipt += "Large\n";
}if (giant.isSelected()){
totalPrice = 10.00;
receipt = "Giant\n";
}
if (pepperoni.isSelected()) {
totalPrice += 0.50;
receipt += "Pepperoni\n";
}else if (ham.isSelected()) {
totalPrice += 0.40;
receipt += "Ham\n";
}if (onions.isSelected()) {
totalPrice += 0.40;
receipt += "Onions\n";
}if (peppers.isSelected()) {
totalPrice += 0.40;
receipt += "Peppers\n";
}if (sweetcorn.isSelected()) {
totalPrice += 0.40;
receipt += "Sweetcorn\n";
}if (chicken.isSelected()) {
totalPrice += 0.40;
receipt += "Chicken\n";
}if (lamb.isSelected()) {
totalPrice += 0.40;
receipt += "Lamb\n";
}if (meatballs.isSelected()) {
totalPrice += 0.40;
receipt += "Meetballs\n";
}if (pineapple.isSelected()) {
totalPrice += 0.40;
System.out.println("pineapple");
//receipt += "Pineapple\n";
}
if (GarlicBread.isSelected()) {
totalPrice += 0.50;
System.out.println("garlic bread");
//receipt += "Garlic Bread\n";
}if (Wedges.isSelected()) {
totalPrice += 0.40;
System.out.println("wedges");
//receipt += "Wedges\n";
}if (Fries.isSelected()) {
totalPrice += 0.40;
receipt += "Fries\n";
}if (ChickenTenders.isSelected()) {
totalPrice += 0.40;
receipt += "Chicken Tenders\n";
}if (Coke.isSelected()) {
totalPrice += 0.40;
receipt += "Bottle of Coke\n";
}if (SevenUP.isSelected()) {
totalPrice += 0.40;
receipt += "Bottle of 7UP\n";
}
}
}
public void addFrame() {
JFrame frame = new JFrame();
Container c = frame.getContentPane();
frame.setResizable(false);
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
c.setLayout(layout);
frame.add(firstPanel);
frame.add(secondPanel);
frame.add(thirdPanel);
frame.setSize(530,610);
frame.setLocationRelativeTo(null);
frame.setTitle("Make your order!");
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
secondPanel.setPreferredSize(new Dimension(250,500));
firstPanel.setPreferredSize(new Dimension(250,500));
firstPanel();
secondPanel();
thirdPanel();
borders();
}
public void borders(){
TitledBorder border1 = BorderFactory.createTitledBorder("Size and Toppings");
border1.setTitleJustification(TitledBorder.CENTER);
TitledBorder border2 = BorderFactory.createTitledBorder("Extras");
border2.setTitleJustification(TitledBorder.CENTER);
TitledBorder border3 = BorderFactory.createTitledBorder("Order");
border3.setTitleJustification(TitledBorder.CENTER);
firstPanel.setBorder(border1);
secondPanel.setBorder(border2);
thirdPanel.setBorder(border3);
}
public void firstPanel(){
JPanel p1= new JPanel();
JPanel p11 = new JPanel();
JPanel p12 = new JPanel();
p1.setPreferredSize(new Dimension(240,230));
GridLayout g = new GridLayout(3,3,5,5);
p12.setLayout(g);
JLabel option = new JLabel("Select your pizza size:");
option.setFont(new Font ("Ariel", Font.PLAIN, 14));
p11.add(option);
ButtonGroup sizeGroup = new ButtonGroup();
small = new JRadioButton("Small");
small.addActionListener(listener);
sizeGroup.add(small);
p12.add(small);
medium = new JRadioButton("Medium");
medium.addActionListener(listener);
sizeGroup.add(medium);
medium.setSelected(true);
p12.add(medium);
large = new JRadioButton("Large");
large.addActionListener(listener);
sizeGroup.add(large);
p12.add(large);
giant = new JRadioButton("Giant");
giant.addActionListener(listener);
sizeGroup.add(giant);
p12.add(giant);
p1.add(p11);
p1.add(p12);
firstPanel.add(p1);
//part 2
JPanel p2= new JPanel();
JPanel p21 = new JPanel();
JPanel p22 = new JPanel();
p2.setPreferredSize(new Dimension(240,230));
GridLayout g1 = new GridLayout(5,2);
p22.setLayout(g1);
JLabel choice = new JLabel("Select your toppings:");
choice.setFont(new Font ("Arial", Font.PLAIN, 14));
p21.add(choice);
pepperoni = new JCheckBox ("Pepperoni");
pepperoni.addActionListener(listener);
p22.add(pepperoni);
ham = new JCheckBox ("Ham");
ham.addActionListener(listener);
p22.add(ham);
onions = new JCheckBox ("Onions");
onions.addActionListener(listener);
p22.add(onions);
peppers = new JCheckBox ("Peppers");
peppers.addActionListener(listener);
p22.add(peppers);
sweetcorn = new JCheckBox ("Sweetcorn");
sweetcorn.addActionListener(listener);
p22.add(sweetcorn);
chicken = new JCheckBox ("Chicken");
chicken.addActionListener(listener);
p22.add(chicken);
lamb = new JCheckBox ("Lamb");
lamb.addActionListener(listener);
p22.add(lamb);
meatballs = new JCheckBox ("Meatballs");
meatballs.addActionListener(listener);
p22.add(meatballs);
pineapple = new JCheckBox ("Pineapple");
pineapple.addActionListener(listener);
p22.add(pineapple);
p2.add(p21);
p2.add(p22);
firstPanel.add(p2);
}
public void secondPanel(){
JPanel p1= new JPanel();
JPanel p11 = new JPanel();
JPanel p12 = new JPanel();
p1.setPreferredSize(new Dimension(240,120));
GridLayout g = new GridLayout(5,2);
p12.setLayout(g);
//JPanel p1 = new JPanel();
//JPanel p2 = new JPanel();
JLabel choice = new JLabel("Select your toppings : ");
choice.setFont(new Font ("Arial", Font.PLAIN, 14));
p11.add(choice);
GarlicBread = new JCheckBox ("Garlic Bread");
GarlicBread.addActionListener(listener);
//textArea.setText("Garlic Bread");
p12.add(GarlicBread);
Wedges = new JCheckBox ("Wedges");
Wedges.addActionListener(listener);
p12.add(Wedges);
Fries = new JCheckBox ("Fries");
Fries.addActionListener(listener);
p12.add(Fries);
ChickenTenders = new JCheckBox ("Chicken Tenders");
ChickenTenders.addActionListener(listener);
p12.add(ChickenTenders);
Coke = new JCheckBox ("2ltr Coke");
Coke.addActionListener(listener);
p12.add(Coke);
SevenUP = new JCheckBox ("2ltr 7UP");
SevenUP.addActionListener(listener);
p12.add(SevenUP);
p1.add(p11);
p1.add(p12);
secondPanel.add(p1);
JPanel p2= new JPanel();
JPanel p21 = new JPanel();
JPanel p22 = new JPanel();
p2.setPreferredSize(new Dimension(240,345));
JLabel receipt = new JLabel("You Receipt:");
receipt.setFont(new Font ("Arial", Font.PLAIN, 14));
p21.add(receipt);
ta = new JTextArea(240,325);
ta.setEditable(false);
p21.setPreferredSize(new Dimension(240,45));
p22.setPreferredSize(new Dimension(240,300));
p22.add(ta);
p2.add(p21);
p2.add(p22);
secondPanel.add(p2);
}
public void thirdPanel(){
FlowLayout g = new FlowLayout(FlowLayout.CENTER);
thirdPanel.setLayout(g);
thirdPanel.setPreferredSize(new Dimension(505,65));
order = new JButton("Order");
order.addActionListener(Listener);
reset = new JButton("Reset");
reset.addActionListener(Listener);
cancel = new JButton("Cancel");
cancel.addActionListener(Listener);
order.setSize(150, 50);
reset.setSize(150, 50);
cancel.setSize(150, 50);
thirdPanel.add(order);
thirdPanel.add(reset);
thirdPanel.add(cancel);
}
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
receipt1 = receipt;
System.out.println(receipt1);
}
}
/*public void actionPerformed(ActionEvent e) {
if (order.equals(e.getActionCommand())) {
ta.append(receipt);*/
//}}
public static void main(String[] args) {
// TODO Auto-generated method stub
Pizza app = new Pizza();
app.addFrame();
}
}