import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Mon May 02 13:28:34 CEST 2005
 */

public class Coffee extends JFrame
{
   private int currentState = -1;
   private static final int
             initialState = 0,
             have50CentState = 1,
             coffeeNoSugarState = 2,
             cappuccinoNoSugarState = 3,
             coffeeWithSugarState = 4,
             cappuccinoWithSugarState = 5,
             makeBeverageState = 6;

   public Coffee()
   {
      initComponents();
      coin50Cent.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            {
               if (currentState == have50CentState)
                  toState(coffeeNoSugarState);
               else if (currentState == initialState)
                  toState(have50CentState);
            }
         }
      );
      coin1Euro.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            { toState(coffeeNoSugarState); }
         }
      );
      coinReturn.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            { toState(initialState); }
         }
      );
      selectCoffee.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            {
               if (currentState == cappuccinoNoSugarState)
                  toState(coffeeNoSugarState);
               else if (currentState == cappuccinoWithSugarState)
                  toState(coffeeWithSugarState);
            }
         }
      );
      selectCappuccino.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            {
               if (currentState == coffeeNoSugarState)
                  toState(cappuccinoNoSugarState);
               else if (currentState == coffeeWithSugarState)
                  toState(cappuccinoWithSugarState);
            }
         }
      );
      addSugar.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            {
               if (currentState == coffeeNoSugarState)
                  toState(coffeeWithSugarState);
               else if (currentState == coffeeWithSugarState)
                  toState(coffeeNoSugarState);
               else if (currentState == cappuccinoNoSugarState)
                  toState(cappuccinoWithSugarState);
               else if (currentState == cappuccinoWithSugarState)
                  toState(cappuccinoNoSugarState);
            }
         }
      );
      makeItSo.addActionListener(
         new ActionListener()
         {
            public void actionPerformed (ActionEvent e)
            {
               toState(makeBeverageState);
               Timer delay = new Timer(5000,
                  new ActionListener()
                  {
                     public void actionPerformed (ActionEvent e)
                     {
                        JOptionPane.showMessageDialog(Coffee.this, "Ihr Getränk ist bereit!", "Info", JOptionPane.ERROR_MESSAGE);
                        toState(initialState);
                     }
                  }
               );
               delay.setRepeats(false);
               delay.start();
            }
         }
      );
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      toState(initialState);
      pack();
      setVisible(true);
   }

   private void toState (int nextState)
   {
      currentState = nextState;
      switch (nextState)
      {
         case initialState:
            coin1Euro.setEnabled(true);
            coin50Cent.setEnabled(true);
            coinReturn.setEnabled(false);
            selectCoffee.setEnabled(false);
            selectCappuccino.setEnabled(false);
            addSugar.setEnabled(false);
            makeItSo.setEnabled(false);
            amountDisplay.setText("0.00");
            selectCoffee.setSelected(false);
            selectCappuccino.setSelected(false);
            addSugar.setSelected(false);
            break;
         case have50CentState:
            coin1Euro.setEnabled(false);
            coinReturn.setEnabled(true);
            amountDisplay.setText("0.50");
            break;
         case coffeeNoSugarState:
            coin50Cent.setEnabled(false);
            coin1Euro.setEnabled(false);
            coinReturn.setEnabled(true);
            selectCoffee.setEnabled(false);
            selectCappuccino.setEnabled(true);
            addSugar.setEnabled(true);
            makeItSo.setEnabled(true);
            amountDisplay.setText("1.00");
            selectCoffee.setSelected(true);
            selectCappuccino.setSelected(false);
            addSugar.setSelected(false);
            break;
         case cappuccinoNoSugarState:
            selectCoffee.setEnabled(true);
            selectCappuccino.setEnabled(false);
            selectCoffee.setSelected(false);
            selectCappuccino.setSelected(true);
            addSugar.setSelected(false);
            break;
         case coffeeWithSugarState:
            selectCoffee.setEnabled(false);
            selectCappuccino.setEnabled(true);
            selectCoffee.setSelected(true);
            selectCappuccino.setSelected(false);
            addSugar.setSelected(true);
            break;
         case cappuccinoWithSugarState:
            selectCoffee.setEnabled(true);
            selectCappuccino.setEnabled(false);
            selectCoffee.setSelected(false);
            selectCappuccino.setSelected(true);
            addSugar.setSelected(true);
            break;
         case makeBeverageState:
            coin1Euro.setEnabled(false);
            coin50Cent.setEnabled(false);
            coinReturn.setEnabled(false);
            selectCoffee.setEnabled(false);
            selectCappuccino.setEnabled(false);
            addSugar.setEnabled(false);
            makeItSo.setEnabled(false);
            amountDisplay.setText("Brüh");
            break;
      }
   }

   private void initComponents()
   {
      // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
      coin1Euro = new JButton();
      coin50Cent = new JButton();
      amountDisplay = new JLabel();
      coinReturn = new JButton();
      selectCoffee = new JRadioButton();
      addSugar = new JCheckBox();
      makeItSo = new JButton();
      selectCappuccino = new JRadioButton();
      GridBagConstraints gbc;

      //======== this ========
      setTitle("Kaffee-Automat");
      Container contentPane = getContentPane();
      contentPane.setLayout(new GridBagLayout());
      ((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0, 0, 0};
      ((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0};
      ((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
      ((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};

      //---- coin1Euro ----
      coin1Euro.setText("1 Euro");
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridwidth = 2;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets.bottom = 5;
      gbc.insets.right = 5;
      contentPane.add(coin1Euro, gbc);

      //---- coin50Cent ----
      coin50Cent.setText("50 Cent");
      gbc = new GridBagConstraints();
      gbc.gridx = 2;
      gbc.gridy = 0;
      gbc.gridwidth = 2;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets.bottom = 5;
      gbc.insets.right = 5;
      contentPane.add(coin50Cent, gbc);

      //---- amountDisplay ----
      amountDisplay.setFont(new Font("SansSerif", Font.PLAIN, 24));
      amountDisplay.setText("0.00");
      gbc = new GridBagConstraints();
      gbc.gridx = 4;
      gbc.gridy = 0;
      gbc.gridwidth = 2;
      gbc.gridheight = 2;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets.bottom = 5;
      contentPane.add(amountDisplay, gbc);

      //---- coinReturn ----
      coinReturn.setText("Geldr\u00fcckgabe");
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 1;
      gbc.gridwidth = 4;
      gbc.fill = GridBagConstraints.VERTICAL;
      gbc.insets.bottom = 5;
      gbc.insets.right = 5;
      contentPane.add(coinReturn, gbc);

      //---- selectCoffee ----
      selectCoffee.setSelected(true);
      selectCoffee.setText("Kaffee");
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 2;
      gbc.gridwidth = 2;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets.bottom = 5;
      gbc.insets.right = 5;
      contentPane.add(selectCoffee, gbc);

      //---- addSugar ----
      addSugar.setText("mit Zucker");
      gbc = new GridBagConstraints();
      gbc.gridx = 2;
      gbc.gridy = 2;
      gbc.gridwidth = 2;
      gbc.gridheight = 2;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets.right = 5;
      contentPane.add(addSugar, gbc);

      //---- makeItSo ----
      makeItSo.setText("Start");
      gbc = new GridBagConstraints();
      gbc.gridx = 4;
      gbc.gridy = 2;
      gbc.gridwidth = 2;
      gbc.gridheight = 2;
      gbc.fill = GridBagConstraints.BOTH;
      contentPane.add(makeItSo, gbc);

      //---- selectCappuccino ----
      selectCappuccino.setText("Cappuccino");
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 3;
      gbc.gridwidth = 2;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets.right = 5;
      contentPane.add(selectCappuccino, gbc);
      // JFormDesigner - End of component initialization  //GEN-END:initComponents
   }

   // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
   private JButton coin1Euro;
   private JButton coin50Cent;
   private JLabel amountDisplay;
   private JButton coinReturn;
   private JRadioButton selectCoffee;
   private JCheckBox addSugar;
   private JButton makeItSo;
   private JRadioButton selectCappuccino;
   // JFormDesigner - End of variables declaration  //GEN-END:variables
   
   public static void main (String[] args)
   { new Coffee(); }
}

