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

/**
 *
 * @author Bill Kraynek
 */
public class IceCreamShop2 {

    JButton dishButton = new JButton("Dish");
    JButton coneButton = new JButton("Cone");
    JButton vanillaButton = new JButton("Vanilla");
    JButton chocolateButton = new JButton("Chocolate");
    JButton strawberryButton = new JButton("Strawberry");
    JButton chocolateSyrupButton = new JButton("Chocolate Syrup");
    JButton whippedCreamButton = new JButton("Whipped Cream");
    JButton finishButton = new JButton("Finish");
    JButton closingButton = new JButton("Close");
    int scoops;
    boolean dish;
    ArrayList<Item> order = new ArrayList<Item>();
    ArrayList<ArrayList<Item>> items = new ArrayList<ArrayList<Item>>();
    JFrame theFrame;

    public IceCreamShop2() {
        theFrame = new JFrame("Bill's Ice Cream 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 / 2, height / 2);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.setLayout(new FlowLayout(FlowLayout.CENTER, width, 30));
        JLabel coneOrDishLable = new JLabel("Dish or Cone");
        theFrame.add(coneOrDishLable);
        DishOrConeAction dishOrConeAction = new DishOrConeAction();
        dishButton.addActionListener(dishOrConeAction);
        coneButton.addActionListener(dishOrConeAction);
        JPanel coneDishPanel = new JPanel();
        coneDishPanel.add(dishButton);
        coneDishPanel.add(coneButton);
        theFrame.add(coneDishPanel);
        JLabel flavorLable = new JLabel("Choose Flavor(s). 1 to 3 scoops only");
        theFrame.add(flavorLable);
        scoops = 0;
        FlavorAction flavorAction = new FlavorAction();
        vanillaButton.addActionListener(flavorAction);
        chocolateButton.addActionListener(flavorAction);
        strawberryButton.addActionListener(flavorAction);
        vanillaButton.setEnabled(false);
        chocolateButton.setEnabled(false);
        strawberryButton.setEnabled(false);
        JPanel flavorPanel = new JPanel();
        flavorPanel.add(vanillaButton);
        flavorPanel.add(chocolateButton);
        flavorPanel.add(strawberryButton);
        theFrame.add(flavorPanel);
        JLabel toppingLable = new JLabel("Choose topping(s) (for dish of ice cream only).");
        theFrame.add(toppingLable);
        ToppingAction toppingAction = new ToppingAction();
        chocolateSyrupButton.addActionListener(toppingAction);
        whippedCreamButton.addActionListener(toppingAction);
        JPanel toppingPanel = new JPanel();
        toppingPanel.add(chocolateSyrupButton);
        toppingPanel.add(whippedCreamButton);
        theFrame.add(toppingPanel);
        chocolateSyrupButton.setEnabled(false);
        whippedCreamButton.setEnabled(false);
        theFrame.add(finishButton);
        finishButton.addActionListener(new FinishAction());
        theFrame.add(closingButton);
        closingButton.addActionListener(new ClosingAction());
        dish = false;
        theFrame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new IceCreamShop2();
    }

    interface Item {

        public String getDescription();

        public double getCost();
    }

    class Cone implements Item {

        public String getDescription() {
            return "Cone with";
        }

        public double getCost() {
            return 1.00;
        }
    }

    class Dish implements Item {

        public String getDescription() {
            return "Dish of";
        }

        public double getCost() {
            return 0.10;
        }
    }

    class Flavor implements Item {

        String flavor;
        double cost;

        public Flavor(String flavor, double cost) {
            this.flavor = flavor;
            this.cost = cost;
        }

        public String getDescription() {
            return " :" + flavor + ":";
        }

        public double getCost() {
            return cost;
        }
    }

    class Topping implements Item {

        String topping;
        double cost;

        public Topping(String topping, double cost) {
            this.topping = topping;
            this.cost = cost;
        }

        public String getDescription() {
            return " with " + topping;
        }

        public double getCost() {
            return cost;
        }
    }

    class DishOrConeAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String item = event.getActionCommand();
            if (item.equals("Dish")) {
                order.add(new Dish());
                dish = true;
            } // end if
            if (item.equals("Cone")) {
                order.add(new Cone());
            } // end if
            dishButton.setEnabled(false);
            coneButton.setEnabled(false);
            vanillaButton.setEnabled(true);
            chocolateButton.setEnabled(true);
            strawberryButton.setEnabled(true);
            closingButton.setEnabled(false);
        }
    }

    class FlavorAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            scoops++;
            String flavor = event.getActionCommand();
            if (flavor.equals("Vanilla")) {
                order.add(new Flavor(flavor, 1.00));
            } // end if
            if (flavor.equals("Chocolate")) {
                order.add(new Flavor(flavor, 1.25));
            }
            if (flavor.equals("Strawberry")) {
                order.add(new Flavor(flavor, 1.50));
            } // end if
            if (dish) {
                chocolateSyrupButton.setEnabled(true);
                whippedCreamButton.setEnabled(true);
            } // end if
            if (scoops == 3) {
                vanillaButton.setEnabled(false);
                chocolateButton.setEnabled(false);
                strawberryButton.setEnabled(false);
            } // end if
        }
    }

    class ToppingAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String topping = event.getActionCommand();
            dish = false;
            if (topping.equals("Chocolate Syrup")) {
                order.add(new Topping(topping, 1.50));
                chocolateSyrupButton.setEnabled(false);
            } // end if
            if (topping.equals("Whipped Cream")) {
                order.add(new Topping(topping, .75));
                whippedCreamButton.setEnabled(false);
            } // end if
        }
    }

    class FinishAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (order.isEmpty()) {
                return;
            } // end if
            if (order.size() == 1) {
                JOptionPane.showMessageDialog(null, "Must choose ice cream!");
                dishButton.setEnabled(true);
                coneButton.setEnabled(true);
                order.remove(0);
                return;
            }// end if
            String description = "";
            double cost = 0.0;
            for (Item item : order) {
                description += item.getDescription();
                cost += item.getCost();
            }// end for
            JOptionPane.showMessageDialog(null, formatItem("Your order is:", description, cost));
            items.add(order);
            dishButton.setEnabled(true);
            coneButton.setEnabled(true);
            chocolateSyrupButton.setEnabled(false);
            whippedCreamButton.setEnabled(false);
            vanillaButton.setEnabled(false);
            chocolateButton.setEnabled(false);
            strawberryButton.setEnabled(false);
            order = new ArrayList<Item>();
            scoops = 0;
            dish = false;
            closingButton.setEnabled(true);
        }
    }

    String formatItem(String heading, String description, double cost) {
        NumberFormat money = NumberFormat.getCurrencyInstance(Locale.US);
        String out = heading + "\n";
        description = fixFlavors("Vanilla", description);
        description = fixFlavors("Chocolate", description);
        description = fixFlavors("Strawberry", description);
        out += description + "  ";
        out += money.format(cost);
        return out;
    }

    class ClosingAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String out = "Items sold today";
            for (ArrayList<Item> order : items) {
                String description = "";
                double cost = 0.0;
                for (Item item : order) {
                    description += item.getDescription();
                    cost += item.getCost();
                }// end for
                out += formatItem("", description, cost);
            }// end for
            JOptionPane.showMessageDialog(null, out);
            theFrame.dispose();
        }
    }

    String fixFlavors(String flavor, String s) {
        String match = ":" + flavor + ":";
        int first = s.indexOf(match);
        if (first == -1) {
            return s;
        }// end if
        int next = s.indexOf(match, first + match.length());
        if (next == -1) {
            s = s.substring(0, first - 1) + " a scoop of " + flavor + s.substring(first + match.length());
            return s;
        } // end if
        s = s.substring(0, next) + s.substring(next + match.length());
        next = s.indexOf(match, first + match.length());
        if (next == -1) {
            s = s.substring(0, first - 1) + " a double scoop of " + flavor + s.substring(first + match.length());
            return s;
        } // end if
        s = s.substring(0, next) + s.substring(next + match.length());
        s = s.substring(0, first - 1) + " a triple scoop of " + flavor + s.substring(first + match.length());
        return s;
    } // end fixFlavors
}

