import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Toolkit;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;

public class CoffeeShop2 {
    DrinkItem theDrink;
    JButton coffeeButton;
    JButton teaButton;
    JButton creamButton;
    JButton sugarButton;
    JButton lemonButton;
    int creamUses = 0;
    int sugarUses = 0;
    int lemonUses = 0;
    
    public CoffeeShop2() {
        JFrame theFrame = new JFrame("Bill's Coffee Shop");
        Toolkit tk = Toolkit.getDefaultToolkit();
        int width = (int)(tk.getScreenSize().getWidth())/2;
        int height = (int)(tk.getScreenSize().getHeight())/2;
        theFrame.setSize(width,height);
        theFrame.setLocation(width/4,height/4);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.setLayout(new FlowLayout(FlowLayout.CENTER,200,30));
        JLabel drinkLabel = new JLabel("Choose your drink");
        JPanel drinkLabelPanel = new JPanel();
        drinkLabelPanel.add(drinkLabel);
        theFrame.add(drinkLabelPanel);
        coffeeButton = new JButton("Coffee $1.99");
        coffeeButton.addActionListener(new DrinkAction());
        teaButton = new JButton("Tea $1.49");
        teaButton.addActionListener(new DrinkAction());
        JPanel drinkPanel = new JPanel();
        drinkPanel.add(coffeeButton);
        drinkPanel.add(teaButton);
        theFrame.add(drinkPanel);
        JLabel additionLabel = new JLabel("Choose your addition. No more than a double addition is allowed.");
        JPanel additionLabelPanel = new JPanel();
        additionLabelPanel.add(additionLabel);
        theFrame.add(additionLabelPanel);
        creamButton = new JButton("Cream $0.50");
        creamButton.addActionListener(new CreamAction());
        sugarButton = new JButton("Sugar $0.75");
        sugarButton.addActionListener(new SugarAction());
        lemonButton = new JButton("Lemon $0.25");
        lemonButton.addActionListener(new LemonAction());
        JPanel addPanel = new JPanel();
        addPanel.add(creamButton);
        addPanel.add(sugarButton);
        addPanel.add(lemonButton);
        theFrame.add(addPanel);
        sugarButton.setEnabled(false);
        lemonButton.setEnabled(false);
        creamButton.setEnabled(false);
        JButton finishButton = new JButton("Finish");
        finishButton.setFont(new Font("TimesRoman",Font.BOLD,16));
        finishButton.addActionListener(new FinishAction());
        theFrame.add(finishButton);
        theFrame.setVisible(true);
    } // end constructor
    
    interface DrinkItem {
        public double getCost();
        public String getDescription();
    }// end DrinkItem
    
    class Drink implements DrinkItem {
        private String drinkType;
        private double drinkCost;
        
        public Drink(String type, double cost) {
            drinkType = type;
            drinkCost = cost;
        } // end Drink
        
        public double getCost() {
            return drinkCost;
        }
        
        public String getDescription() {
            return drinkType;
        }
    }// end Drink
    
    class Decorator implements DrinkItem {
        private DrinkItem drink;
        
        public Decorator(DrinkItem drink) {
            this.drink = drink;
        }
        public DrinkItem getDrinkItem() {
            return drink;
        }
        public double getCost() {
            return drink.getCost();
        }
        public String getDescription() {
            return drink.getDescription();
        }
    }// end Decorator
    
    class Addition extends Decorator {
        private String additionType;
        private double additionCost;
        
        public Addition(DrinkItem d, String type, double cost) {
            super(d);
            additionType = type;
            additionCost = cost;
        }
        public double getCost() {
            return getDrinkItem().getCost() + additionCost;
        }
        public String getDescription() {
            return getDrinkItem().getDescription() + " with " + additionType;
        }
    }// end Addition
    
    class DrinkAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String buttonLabel = e.getActionCommand();
            Scanner labelScanner = new Scanner(buttonLabel);
            labelScanner.useDelimiter("[ $]+");
            String name = labelScanner.next();
            double cost = labelScanner.nextDouble();
            theDrink = new Drink(name,cost);
            coffeeButton.setEnabled(false);
            teaButton.setEnabled(false);
            sugarButton.setEnabled(true);
            lemonButton.setEnabled(true);
            creamButton.setEnabled(true);
        } // end actionPerformed
    } //end DrinkAction
    
    class CreamAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String buttonLabel = e.getActionCommand();
            Scanner labelScanner = new Scanner(buttonLabel);
            labelScanner.useDelimiter("[ $]+");
            String name = labelScanner.next();
            double cost = labelScanner.nextDouble();
            theDrink = new Addition(theDrink,name,cost);
            creamUses++;
            if( creamUses >= 2 ) {
                creamButton.setEnabled(false);
            } // end if
        } // end actionPerformed
    } //CreamAction
    
    class SugarAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String buttonLabel = e.getActionCommand();
            Scanner labelScanner = new Scanner(buttonLabel);
            labelScanner.useDelimiter("[ $]+");
            String name = labelScanner.next();
            double cost = labelScanner.nextDouble();
            theDrink = new Addition(theDrink,name,cost);
            sugarUses++;
            if( sugarUses >= 2 ) {
                sugarButton.setEnabled(false);
            } // end if
        } // end actionPerformed
    } //SugarAction
    
    class LemonAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String buttonLabel = e.getActionCommand();
            Scanner labelScanner = new Scanner(buttonLabel);
            labelScanner.useDelimiter("[ $]+");
            String name = labelScanner.next();
            double cost = labelScanner.nextDouble();
            theDrink = new Addition(theDrink,name,cost);
            lemonUses++;
            if( lemonUses >= 2 ) {
                lemonButton.setEnabled(false);
            } // end if
        } // end actionPerformed
    } // LemonAction
    
    class FinishAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if( theDrink == null ) return;
            NumberFormat money = NumberFormat.getCurrencyInstance(Locale.US);
            creamUses = 0;
            sugarUses = 0;
            lemonUses = 0;
            String drinkDescription = theDrink.getDescription();
            drinkDescription = fixRepeats("cream",drinkDescription);
            drinkDescription = fixRepeats("sugar",drinkDescription);
            drinkDescription = fixRepeats("lemon",drinkDescription);
            JOptionPane.showMessageDialog(null,drinkDescription + "\n" + money.format(theDrink.getCost()));
            theDrink = null;
            coffeeButton.setEnabled(true);
            teaButton.setEnabled(true);
            sugarButton.setEnabled(false);
            lemonButton.setEnabled(false);
            creamButton.setEnabled(false);
        } // end actionPerformed
        
    } // end FinishAction
    
    String fixRepeats(String s1, String s2) {
        int first = s2.indexOf(s1);
        if( first == -1 ) return s2;
        int next = s2.indexOf("with "+s1,first+s1.length());
        if( next == -1 ) return s2;
        s2 = s2.substring(0,next) + s2.substring(next+s1.length()+5);
        s2 = s2.substring(0,first) + "double " + s2.substring(first);
        return s2;
    } // end fixRepeats
    
    public static void main(String[] args) {
        new CoffeeShop2();
    } // end main
    
} // end CoffeeShop2





