<
//*******************************************************//
// Pizza Project Authors: Chuck Armstrong, Nick Gesick. //
// //
//--------------Pizza Ordering Program-------------------//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class PizzaOrderingProgramPanel extends JFrame implements ActionListener, KeyListener
{
private JLabel size;
private JLabel type;
private JRadioButton small, medium, large;
private JRadioButton thin, thick, pan;
private JCheckBox anchovies, canadian_bacon, salami, pepperoni, sausage, mushroom, olive, onion, green_pepper;
private String smallSize, mediumSize, largeSize;
private String thinType, thickType, panType;
//---Pizza Options Pricing---//
private final double smallSizePrice = 5.00,
mediumSizePrice = 10.00,
largeSizePrice = 15.00,
thinTypePrice = 0.00,
thickTypePrice = 0.00,
panTypePrice = 1.50,
anchoviesToppingPrice = 1.00,
canadian_baconToppingPrice = 1.00,
salamiToppingPrice = 1.00,
pepperoniToppingPrice = 1.00,
sausageToppingPrice = 1.00,
mushroomToppingPrice = 1.00,
oliveToppingPrice = 1.00,
onionToppingPrice = 1.00,
green_pepperToppingPrice = 1.00,
textBoxPrice = 1.00;
//-------GUI Setup-------//
Panel northPanel,
sizePanel,
westPanel,
westTypePanel,
centerPanel,
centerToppingPanel,
eastPanel,
eastTextPanel,
southPricePanel,
southPanel;
Label titleLabel,
sizeLabel,
typeLabel,
textLabel,
amountLabel,
toppingLabel,
inputLabel,
outputLabel,
resultsLabel,
priceLabel,
priceDisplayLabel;
Button orderButton;
TextField pizzaText,
priceText;
ButtonGroup sizeBGroup;
JRadioButton smallJRadioButton,
mediumJRadioButton,
largeJRadioButton;
ButtonGroup typeCGroup;
JRadioButton thinJRadioButton,
thickJRadioButton,
panJRadioButton;
JCheckBox anchoviesJCheckBox,
canadian_baconJCheckBox,
salamiJCheckBox,
pepperoniJCheckBox,
sausageJCheckBox,
mushroomJCheckBox,
oliveJCheckBox,
onionJCheckBox,
green_pepperJCheckBox;
//--------------------------------------------------------------//
// Setup Panels with Radio Buttons, Checkboxes, & Text Field //
//--------------------------------------------------------------//
public PizzaOrderingProgramPanel()
{
setBackground(new Color(green));
setLayout(new BorderLayout(0,0));
northPanel = new Panel();
northPanel.setBackground(new Color(green));
northPanel.setLayout(new FlowLayout());
northSizePanel = new Panel();
northSizePanel.setLayout(new FlowLayout());
westTypePanel = new Panel();
westTypePanel.setBackground(new Color(green));
westTypePanel.setLayout(new FlowLayout());
centerPanel = new Panel();
centerToppingPanel = new Panel();
centerToppingPanel.setBackground(new Color(green));
centerToppingPanel.setLayout(new FlowLayout());
eastPanel = new Panel();
eastTextBoxPanel = new Panel();
eastTextBoxPanel.setBackground(new Color(green));
eastTextBoxPanel.setLayout (new Flowlayout());
southPanel = new Panel();
southPanel.setLayout(new FlowLayout());
southPricePanel = new Panel();
southPricePanel.setBackground(new Color(green));
southPricePanel.setLayout(new FlowLayout());
//---Radio Buttons---//
smallJRadioButton = new JRadioButton("Small", false);
regularJRadioButton = new JRadioButton("Regular", true);
largeJRadioButton = new JRadioButton("Large", false);
thinJRadioButton = new JRadioButton("Thin", false);
thickJRadioButton = new JRadioButton("Thick", true);
panJRadioButton = new JRadioButton("Pan", false);
sizeBGroup = new ButtonGroup();
sizeBGroup.add(smallJRadioButton);
sizeBGroup.add(regularJRadioButton);
sizeBGroup.add(largeJRadioButton);
typeCGroup = new ButtonGroup();
typeCGroup.add(thinJRadioButton);
typeCGroup.add(thickJRadioButton);
typeCGroup.add(panJRadioButton);
//---CheckBoxes---//
anchoviesJCheckBox = new JCheckBox("Anchovies",false);
canadian_baconJCheckBox = new JCheckBox("Canadian Bacon",false);
salamiJCheckBox = new JCheckBox("Salami",false);
pepperoniJCheckBox = new JCheckBox("Pepperoni",false);
sausageJCheckBox = new JCheckBox("Sausage",false);
mushroomJCheckBox = new JCheckBox("Mushroom",false);
oliveJCheckBox = new JCheckBox("Olive",false);
onionJCheckBox = new JCheckBox("Onion",false);
green-pepperJCheckBox = new JCheckBox("Green Pepper",false);
//---TextBox---//
pizzaText = new TextField("0",1);
pizzaText.setFont(new Font("Tahoma",Font.BOLD,12));
//---Ordering Button---//
orderButton = new Button("Order Now");
orderButton.setFont(new Font("Tahoma",Font.BOLD,14));
//---Setup Listeners---//
smallJRadioButton.addActionListener(this);
regularJRadioButton.addActionListener(this);
largeJRadioButton.addActionListener(this);
thinJRadioButton.addActionListener(this);
thickJRadioButton.addActionListener(this);
panJRadioButton.addActionListener(this);
anchoviesJCheckBox.addActionListener(this);
canadian_baconJCheckBox.addActionListener(this);
salamiJCheckBox.addActionListener(this);
pepperoniJCheckBox.addActionListener(this);
sausageJCheckBox.addActionListener(this);
mushroomJCheckBox.addActionListener(this);
oliveJCheckBox.addActionListener(this);
onionJCheckBox.addActionListener(this);
green_pepperJCheckBox.addActionListener(this);
pizzaTextBox.addKeyListener(this);
orderButton.addActionListener(this);
//---Setup Labels---//
titleLabel = new Label("");
titleLabel.setBackground(new Color(green));
titleLabel.setForeground(new Color(red));
titleLabel.setFont(new Font("Tahoma",Font.BOLD,40));
sizeLabel = new Label("Select a size for your custom-made pizza:");
sizeLabel.setForeground(new Color(red));
typeLabel.setFont(new Font("Tahoma",Font.BOLD,12));
typeLabel = new Label("Select a crust type for your custom-made pizza:");
typeLabel.setForeground(new Color(red));
typeLabel.setFont(new Font("Tahoma",Font.BOLD,12));
toppingLabel = new Label("Select one or more choices of the toppings below : ");
toppingLabel.setForeground(new Color(red));
toppingLabel.setFont(new Font("Tahoma",Font.BOLD,12));
textBoxLabel =new Label("Type in non-listed pizza topping:");
textBoxLabel.setForeground(new Color(red));
textBoxLabel.SetFont (new Font("Tahoma",Font.BOLD,12));
amountLabel = new Label("Amount: ");
amountLabel.setForeground(new Color(0,0,0));
amountLabel.setFont(new Font("Tahoma",Font.BOLD,12));
priceLabel = new Label("Total price of your order : ");
priceLabel.setForeground(new Color(red));
priceLabel.setFont(new Font("Tahoma",Font.BOLD,12));
priceDisplayLabel = new Label("$0.00 ");
priceDisplayLabel.setForeground(new Color(red));
priceDisplayLabel.setFont(new Font("Tahoma",Font.BOLD,30));
}
//---Nesting the Panels---//
public static void main (String[] args)
{
northPanel.add(sizeLabel);
northSizePanel.add(smallJRadioButton);
northSizePanel.add(regularJRadioButton);
northSizePanel.add(largeJRadioButton);
westTypePanel.add(typeLabel);
westTypePanel.add(thinJRadioButton);
westTypePanel.add(thickJRadioButton);
westTypePanel.add(panJRadioButton);
centerPanel.add(toppingLabel);
centerToppingPanel.add(anchoviesJCheckBox);
centerToppingPanel.add(canadian_baconJCheckBox);
centerToppingPanel.add(salamiJCheckBox);
centerToppingPanel.add(sausageJCheckBox);
centerToppingPanel.add(mushroomJCheckBox);
centerToppingPanel.add(oliveJCheckBox);
centerToppingPanel.add(onionJCheckBox);
centerToppingPanel.add(green_pepperJCheckBox);
eastTextBoxPanel.add(inputLabel);
eastTextBoxPanel.add(outputLabel);
eastTextBoxPanel.add(resultsLabel);
southPanel.add(southPricePanel);
southPricePanel.add(amountLabel);
southPricePanel.add(priceLabel);
southPricePanel.add(priceDisplayLabel);
southPricePanel.add(orderButton);
//---Setup Border---//
add(northPanel, BorderLayout.NORTH);
add(westpanel, BorderLayout.WEST);
add(centerPanel, BorderLayout.CENTER);
add(eastPanel, BorderLayout.EAST);
add(southPanel, BorderLayout.SOUTH);
pizzaText.selectAll();
}
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e)
{
//---If no text entered---//
try
{
String.parseString(pizzaText.getText());
}
catch (StringFormatException fe) {
pizzaText.setText("0");
}
refreshPrice();
}
//---Order Button Action---//
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == orderButton)
{
JOptionPane.showMessageDialog(this,
"Thank's for your " +
priceDisplayLabel.getText() + " payment." +
"\n\nYour custom pizza will be delivered to you shortly via owl!" +
"\nThank You for your Business! Happy Holidays!",
JOptionPane.INFORMATION_MESSAGE);
}
refreshPrice();
}
//---Text String Converted to Value---//
private void refreshPrice()
{
double price = 0;
double pizzaAmount = String.parseString(pizzaText.getText());
DecimalFormat moneyForm = (DecimalFormat)numberForm;
moneyForm.applyPattern("1.00");
//*******If Statements*******//
//---Radio Buttons Priced---//
if (smallJRadioButton.isSelected()) {
price+= smallPizzaPrice * pizzaAmount;
}
if (regularJRadioButton.isSelected()) {
price+= regularPizzaPrice * pizzaAmount;
}
if (largeJRadioButton.isSelected()) {
price+= largePizzaPrice * pizzaAmount;
}
if (thinJRadioButton.isSelected()) {
price+= thinPizzaPrice * pizzaAmount;
}
if (thickJRadioButton.isSelected()) {
price+= thickPizzaPrice * pizzaAmount;
}
if (panJRadioButton.isSelected()) {
price+= panPizzaPrice * pizzaAmount;
}
//---CheckBoxes priced---//
if (anchoviesJCheckBox.isSelected())
{
price+= anchoviesToppingPrice * pizzaAmount;
if (canadian_baconJCheckBox.isSelected())
{
price+= canadian_baconToppingPrice * pizzaAmount;
if (salamiJCheck.isSelected())
{
price+= salamiToppingPrice * pizzaAmount;
if (pepperoniJCheckBox.isSelected())
{
price+= pepperoniToppingPrice * pizzaAmount;
if (sausageJCheckBox.isSelected())
{
price+= sausageToppingPrice * pizzaAmount;
}
if (mushroomJCheckbox.isSelected())
{
price+= mushroomToppingPrice * pizzaAmount;
}
if (oliveJCheckBox.isSelected())
{
price+= oliveToppingPrice * pizzaAmount;
}
if (onionJCheckBox.isSelected()) {
price+= onionToppingPrice * pizzaAmount;
}
if (green_pepperJCheckBox.isSelected()) {
price+= green_pepperToppingPrice * pizzaAmount;
}
priceDisplayLabel.setText("$"+moneyForm.format(price));
}
}>