As discussed on this forum, I'm not looking for a quick answer, just an explanation on why what I have written is not working and a point in the right direction that will make this program work.
Problem: Purpose of this program is to create a GUI that presents to the user choices for ordering a pizza. The gui portion of the code works just fine.
Problem: When I press the 'calculate' button, nothing shows up on the result textfield box.
Problem: I can't figure out the necessary code that will clear all options for the 'clear' button.
Any guidance provided is much appreciated.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class Program2 extends JFrame implements ActionListener
{
//create 5 sections of frame organized by borderlayout
JPanel westPanel = new JPanel();
JPanel eastPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
//pizza size options
JRadioButton jrbSmall = new JRadioButton("S");
JRadioButton jrbMedium = new JRadioButton("M");
JRadioButton jrbLarge = new JRadioButton("L");
//pizza crust options
JRadioButton jrbOriginal = new JRadioButton("Original");
JRadioButton jrbThin = new JRadioButton("Thin");
JRadioButton jrbPan = new JRadioButton("Pan");
//Delivery or Pickup buttons
JRadioButton jrbDelivery = new JRadioButton("Delivery");
JRadioButton jrbPickup = new JRadioButton("Pick-up");
//Drink choices
JCheckBox jchkCoke = new JCheckBox("Coke");
JCheckBox jchkDCoke = new JCheckBox("Diet Coke");
JCheckBox jchkDrPepper = new JCheckBox("Dr Pepper");
//Topping choices
JCheckBox jchkCheese = new JCheckBox("Cheese");
JCheckBox jchkPepperoni = new JCheckBox("Pepperoni");
JCheckBox jchkSausage = new JCheckBox("Sausage");
JCheckBox jchkChicken = new JCheckBox("Chicken");
//Calculate, Clear, Text Field areas
JButton jbtCalculate = new JButton("Calculate");
JButton jbtClear = new JButton("Clear");
JLabel label = new JLabel("Total: ");
JTextField tfResult = new JTextField(8);
//variables used to calculate total cost
double drinkCost = 0.00;
int toppingQty = 0;
double totalCost = 0.00;
double pizzaSize = 0.00;
double toppingCost = 0.00;
double pickupType = 0.00;
public Program2()
{
//layout and description of west portion of frame
westPanel.setLayout(new GridLayout(2,3));
westPanel.setBorder(new TitledBorder("Size & Crust Type"));
//register listeners with buttons
jrbSmall.addActionListener(this);
jrbMedium.addActionListener(this);
jrbLarge.addActionListener(this);
//button group forces radio buttons to act like radio buttons
ButtonGroup drinkSize = new ButtonGroup();
drinkSize.add(jrbSmall);
drinkSize.add(jrbMedium);
drinkSize.add(jrbLarge);
//button group for pizza crust options
ButtonGroup pizzaCrust = new ButtonGroup();
pizzaCrust.add(jrbOriginal);
pizzaCrust.add(jrbThin);
pizzaCrust.add(jrbPan);
//adds all buttons to west panel
westPanel.add(jrbSmall);
westPanel.add(jrbMedium);
westPanel.add(jrbLarge);
westPanel.add(jrbOriginal);
westPanel.add(jrbThin);
westPanel.add(jrbPan);
//sets layout and desription for east portion
eastPanel.setLayout(new GridLayout(2,3));
eastPanel.setBorder(new TitledBorder("Beverage & Delivery"));
//creates button group for delivery choices then adds buttons to the panel
ButtonGroup deliveryType = new ButtonGroup();
deliveryType.add(jrbDelivery);
deliveryType.add(jrbPickup);
eastPanel.add(jchkCoke);
eastPanel.add(jchkDCoke);
eastPanel.add(jchkDrPepper);
eastPanel.add(jrbDelivery);
eastPanel.add(jrbPickup);
//sets layout and description for center portion
centerPanel.setLayout(new GridLayout(5,5));
centerPanel.setBorder(new TitledBorder("Toppings"));
//adds buttons to center portion of panel
centerPanel.add(jchkCheese);
centerPanel.add(jchkPepperoni);
centerPanel.add(jchkSausage);
centerPanel.add(jchkChicken);
northPanel.add(new JButton("Matt's Pizza Parlor"));
southPanel.add(jbtCalculate);
southPanel.add(jbtClear);
southPanel.add(label);
southPanel.add(tfResult);
//adds all portions of panel together
setLayout(new BorderLayout(5,5));
add(westPanel, BorderLayout.WEST);
add(eastPanel, BorderLayout.EAST);
add(centerPanel, BorderLayout.CENTER);
add(northPanel, BorderLayout.NORTH);
add(southPanel, BorderLayout.SOUTH);
}
//handles action event for each of the pizza options
//setSize<S,M,L> calls method and assigns correct price
//setPickup/setDelivery calls method and assigns correct price
//drinkCost keeps a rally total of total drinks chosen
//toppingQty keeps a rally total of toppings chosen then
//calls the toppingCost method to determine cost of toppings
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jrbSmall)
setSizeS();
else if (e.getSource() == jrbMedium)
setSizeM();
else if (e.getSource() == jrbLarge)
setSizeL();
if (e.getSource() == jrbPickup)
setPickup();
else if (e.getSource() == jrbDelivery)
setDelivery();
if (e.getSource() == jchkCoke)
drinkCost++;
else if (e.getSource() == jchkDCoke)
drinkCost++;
else if (e.getSource() == jchkDrPepper)
drinkCost++;
if (e.getSource() == jchkCheese)
toppingQty++;
else if (e.getSource() == jchkPepperoni)
toppingQty++;
else if (e.getSource() == jchkSausage)
toppingQty++;
else if (e.getSource() == jchkChicken)
toppingQty++;
toppingCost(toppingQty);
if (e.getSource() == jbtCalculate)
calculate(pizzaSize, toppingCost, pickupType, drinkCost);
}
public double setSizeS()
{
return pizzaSize = 5.00;
}
public double setSizeM()
{
return pizzaSize = 10.00;
}
public double setSizeL()
{
return pizzaSize = 12.00;
}
public double setPickup()
{
return pickupType = 0.0;
}
public double setDelivery()
{
return pickupType = 1.25;
}
public double toppingCost(int toppingQty)
{
if (toppingQty == 1)
toppingCost = 0.50;
else if (toppingQty <= 3)
toppingCost = 1.00;
else if (toppingQty > 3)
toppingCost = 1.50;
return toppingCost;
}
//method for determining total cost of pizza order by combining results from
//choices chosen in gui
public void calculate(double pizzaSize, double toppingCost, double pickupType, double drinkCost)
{
totalCost = pizzaSize + toppingCost + pickupType + drinkCost;
tfResult.setText(String.valueOf(totalCost));
}
//main method for creating frame info
public static void main(String[] args)
{
Program2 jm = new Program2();
//set the window title
jm.setTitle("JAVA ITP220 - Program 2");
//set the window location
jm.setLocationRelativeTo(null);
//specify what happens when the close button is clicked
jm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the window size
jm.setSize(800, 250);
//display the window
jm.setVisible(true);
}
}