//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// program name : LJpizza
// programmer: John Healy
// date 4/12/04
// version 1.5
//  discription:  This program will allow a user to use a GUI to
//              select the toppings for a pizza and the size of the
//              pizza.  This program will show a running of items
//              selected.  The aplaction will also have a background 
//              color.  this progrma will have two buttons one for 
//              clear and for exiting the program.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.event.*;

public class LJpizza extends JFrame implements ItemListener, ActionListener
{
   GridBagLayout flow = new GridBagLayout();
   GridBagConstraints dem = new GridBagConstraints();
   JLabel WindowLabel = new JLabel("Little john's Pizza");
   JLabel toppingsLabel = new JLabel("Toppings 50¢ Each");
   JLabel costLabel = new JLabel("Cost ");
   JTextField outputLabel = new JTextField(6);
   JButton clearButton = new JButton("Clear");
   JButton endButton = new JButton("End");
   JComboBox pizzaSize = new JComboBox();
   JCheckBox checkBox1 = new JCheckBox("Extra cheese");
   JCheckBox checkBox2 = new JCheckBox("Sausage");
   JCheckBox checkBox3 = new JCheckBox("Pepperoni");
   JCheckBox checkBox4 = new JCheckBox("Mushrooms");
   JCheckBox checkBox5 = new JCheckBox("Black Olives");
   JCheckBox checkBox6 = new JCheckBox("Onions");

   double runningTotal = 0.0;
   double topingsCost = 0.50;
   double smallPizza = 10.00; 
   double mediumPizza = 12.50; 
   double largePizza = 15.00; 
   
   boolean smallboolean = false;
   boolean mediunboolean = false;
   boolean largeboolean = false;
   
   Color bgcolor = new Color(204,255,255); 
   
   NumberFormat money = NumberFormat.getCurrencyInstance();

public LJpizza()
{
   super("Little John's Pizza");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JPanel paneOne = new JPanel();
   paneOne.setBackground(bgcolor);
   paneOne.setLayout(flow);
   dem.fill = GridBagConstraints.BOTH;
   dem.insets = new Insets(5,5,5,5);
   dem.gridx = 0; dem.gridy = 0;
   paneOne.add(toppingsLabel,dem);
   dem.gridx = 0; dem.gridy = 2;
   paneOne.add(checkBox1,dem);
   checkBox1.setBackground(bgcolor);
   dem.gridx = 0; dem.gridy = 3;
   paneOne.add(checkBox2,dem);
   checkBox2.setBackground(bgcolor);
   dem.gridx = 0; dem.gridy = 4;
   paneOne.add(checkBox3,dem);
   checkBox3.setBackground(bgcolor);
   dem.gridx = 0; dem.gridy = 5;
   paneOne.add(checkBox4,dem);
   checkBox4.setBackground(bgcolor);
   dem.gridx = 0; dem.gridy = 6;
   paneOne.add(checkBox5,dem);
   checkBox5.setBackground(bgcolor);
   dem.gridx = 0; dem.gridy = 7;
   paneOne.add(checkBox6,dem);
   checkBox6.setBackground(bgcolor);
   dem.gridx = 3; dem.gridy = 0;
   paneOne.add(pizzaSize,dem);
   pizzaSize.setBackground(bgcolor);
   pizzaSize.addItem("Select");
   pizzaSize.addItem("Small");
   pizzaSize.addItem("Medium");
   pizzaSize.addItem("Large");
   dem.gridx = 3; dem.gridy = 3;
   paneOne.add(clearButton,dem);
   clearButton.setBackground(bgcolor);
   dem.gridx = 3; dem.gridy = 4;
   paneOne.add(endButton,dem);
   endButton.setBackground(bgcolor);
   dem.gridx = 3; dem.gridy = 5;
   paneOne.add(costLabel,dem);
   dem.gridx = 3; dem.gridy = 6;
   paneOne.add(outputLabel,dem);
   checkBox1.addItemListener(this);
   checkBox2.addItemListener(this);
   checkBox3.addItemListener(this);
   checkBox4.addItemListener(this);
   checkBox5.addItemListener(this);
   checkBox6.addItemListener(this);
   clearButton.addActionListener(this);
   endButton.addActionListener(this);
   pizzaSize.addItemListener(this);
   setContentPane(paneOne);
}  //end constructor

public static void main(String[] junkOne)
{
   LJpizza pizzaOrder = new LJpizza();
   pizzaOrder.setSize(300,300);
   pizzaOrder.setVisible(true);

}//end main
public void itemStateChanged(ItemEvent change)
{
   Object whatIsIt = change.getSource();
   String output;
   if(whatIsIt == checkBox1)
   {
      int choice = change.getStateChange();
      if(choice == ItemEvent.SELECTED)
      {
           runningTotal += topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
      else
      {
           runningTotal -= topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
    }  // end checkbox1
   if(whatIsIt == checkBox2)
   {
      int choice = change.getStateChange();
      if(choice == ItemEvent.SELECTED)
      {
           runningTotal += topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
      else
      {
           runningTotal -= topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
    }// end checkbox2
   if(whatIsIt == checkBox3)
    {
      int choice = change.getStateChange();
      if(choice == ItemEvent.SELECTED)
      {
           runningTotal += topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
      else
      {
           runningTotal -= topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
    } // end checkbox3
    if(whatIsIt == checkBox4)
    {
      int choice = change.getStateChange();
      if(choice == ItemEvent.SELECTED)
      {
           runningTotal += topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
      else
      {
           runningTotal -= topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
    } // end checkbox4
    if(whatIsIt == checkBox5)
    {
      int choice = change.getStateChange();
      if(choice == ItemEvent.SELECTED)
      {
           runningTotal += topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
      else
      {
           runningTotal -= topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
    } // end checkbox5
    if(whatIsIt == checkBox6)
    {
      int choice = change.getStateChange();
      if(choice == ItemEvent.SELECTED)
      {
           runningTotal += topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
      else
      {
           runningTotal -= topingsCost;
           outputLabel.setText(money.format(runningTotal));
      }
    } // end checkbox6
    if(whatIsIt == pizzaSize)
    {
       int size = pizzaSize.getSelectedIndex();
       switch (size)
       {  
       case 1: 
       {
            if(smallboolean == true) 
            {
                runningTotal -= smallPizza;  
                smallboolean = false;
            }
            if(mediunboolean == true)
            {
                runningTotal -= mediumPizza;
                mediunboolean = false;
            }
            if(largeboolean == true)
            {
                runningTotal -= largePizza;
                largeboolean = false;
            }
            runningTotal += smallPizza;
            smallboolean = true;
            outputLabel.setText(money.format(runningTotal));
            break;
       }// end case 1
       case 2:
       {
             if(smallboolean == true) 
            {
                runningTotal -= smallPizza;  
                smallboolean = false;
            }
            if(mediunboolean == true)
            {
                runningTotal -= mediumPizza;
                mediunboolean = false;
            }
            if(largeboolean == true)
            {
                runningTotal -= largePizza;
                largeboolean = false;
            }
            runningTotal += mediumPizza;
            mediunboolean = true;
            outputLabel.setText(money.format(runningTotal));   
            break;
       }// end case 2
       case 3:
       {
             if(smallboolean == true) 
            {
                runningTotal -= smallPizza;  
                smallboolean = false;
            }
            if(mediunboolean == true)
            {
                runningTotal -= mediumPizza;
                mediunboolean = false;
            }
            if(largeboolean == true)
            {
                runningTotal -= largePizza;
                largeboolean = false;
            }
            runningTotal += largePizza;
            largeboolean = true;
            outputLabel.setText(money.format(runningTotal));
            break;
        }//end case 3
       }// end switch
    } //end pizzaSize
}// end function  itemStateChanged
  public void actionPerformed(ActionEvent click)
  {
      Object whatIsIt = click.getSource(); 
      if (whatIsIt == endButton) 
          System.exit(0);
      if (whatIsIt == clearButton)
      {
          checkBox1.setSelected(false);
          checkBox2.setSelected(false);
          checkBox3.setSelected(false);
          checkBox4.setSelected(false);
          checkBox5.setSelected(false);
          checkBox6.setSelected(false);
          pizzaSize.setSelectedIndex(0);
          smallboolean = false;
          mediunboolean = false;
          largeboolean = false;
          
          runningTotal = 0;
          outputLabel.setText(money.format(runningTotal));
      }
  }
  
}  //end class LJpizza
