FancyMachine.java

1    package model; 
2     
3    import java.util.Scanner; 
4     
5    class FancyMachine extends Machine { 
6         
7        final static int SUGAR_MAX = 20; 
8        final static int COFFEE_MAX = 20; 
9        final static int MILK_MAX = 20; 
10       final static int CHOCOLATE_MAX = 20; 
11       final static int WATER_MAX = Integer.MAX_VALUE; 
12       final static int CUP_MAX = 10; 
13        
14       private Ingredient hotwater, coffee, milk, sugar, chocolate, cups; 
15       private CashPayment payment; 
16       private CardPayment cardreader; 
17       private FancyDisplay display; 
18       boolean useCard = false; 
19    
20       FancyMachine() { 
21           super("Dave's Famous Coffee Machine"); 
22       } 
23    
24       void initialiseIngredients() { 
25           hotwater  = new Ingredient("Water", WATER_MAX, dispenser); 
26           coffee    = new Ingredient("Coffee", COFFEE_MAX, dispenser); 
27           milk      = new Ingredient("Milk", MILK_MAX, dispenser); 
28           sugar     = new Ingredient("Sugar", SUGAR_MAX, dispenser); 
29           chocolate = new Ingredient("Chocolate", CHOCOLATE_MAX, dispenser); 
30           cups      = new Ingredient("Cup", CUP_MAX, dispenser); 
31           payment   = new CashPayment(); 
32           cardreader = new CardPayment(); 
33           display   = new FancyDisplay(); 
34    
35           coffee.onEmpty( s -> { display.setText("Coffee Empty."); } ); 
36           milk.onEmpty( s -> { display.setText("Milk Empty."); } ); 
37           sugar.onEmpty( s -> { display.setText("Sugar Empty."); } ); 
38           chocolate.onEmpty( s -> { display.setText("Chocolate Empty."); } ); 
39           cups.onEmpty( s -> { display.setText("No cups available. Please use own cup"); } ); 
40       } 
41    
42       void populateMenu() { 
43           menu.add(new Button(1, "Tea", b -> this.tea())); 
44           menu.add(new Button(2, "Coffee", b -> this.coffee())); 
45           menu.add(new Button(3, "Milk", b -> this.milk())); 
46           menu.add(new Button(4, "Sugar", b -> this.sugar())); 
47           menu.add(new Button(5, "Cup", b -> this.cup())); 
48           menu.add(new Button(6, "Insert Coin", b -> this.coin())); 
49           menu.add(new Button(7, "Insert Card", b -> { this.useCard = true;} )); 
50           menu.add(new TextItem("")); 
51           menu.add(new TextItem("The fancy stuff")); 
52           menu.add(new Button(11, "Espresso", b -> this.espresso())); 
53           menu.add(new Button(12, "Latte", b -> this.latte())); 
54           menu.add(new Button(13, "Cappuccino", b -> this.cappuccino())); 
55           menu.add(new Button(14, "Hot Chocolate", b -> this.hotchocolate())); 
56           menu.add(new TextItem("")); 
57           menu.add(new Button(50, "Cancel", b -> this.cancel())); 
58           menu.add(new TextItem("")); 
59           menu.add(new Button(100, "Dispense", b -> this.dispense())); 
60           menu.add(new TextItem("")); 
61           menu.add(new TextItem("----------------------------")); 
62           menu.add(new TextItem("Maintenance Menu")); 
63           menu.add(new TextItem("")); 
64           menu.add(new Button(401, "Refill Coffee", b -> this.refillCoffee())); 
65           menu.add(new Button(402, "Refill Milk", b -> this.refillMilk())); 
66           menu.add(new Button(403, "Refill Sugar", b -> this.refillSugar())); 
67           menu.add(new Button(404, "Refill Cups", b -> this.refillCups())); 
68       } 
69    
70       void tea() { 
71           hotwater.use(); 
72       } 
73    
74       void coffee() { 
75           coffee.use(); 
76       } 
77    
78       void milk() { 
79           milk.use(); 
80       } 
81    
82       void sugar() { 
83           sugar.use(); 
84       } 
85    
86       void espresso() { 
87           coffee.use(); 
88       } 
89        
90       void latte() { 
91           coffee.use(); 
92           milk.use(); 
93           milk.use(); 
94           milk.use(); 
95       } 
96        
97       void cappuccino() { 
98           coffee.use(); 
99           milk.use(); 
100          milk.use(); 
101      } 
102       
103      void hotchocolate() { 
104          sugar.use(); 
105          hotwater.use(); 
106          milk.use(); 
107          chocolate.use(); 
108      } 
109   
110   
111      void coin() { 
112          payment.insert(); 
113      } 
114       
115      void cup() { 
116          cups.use(); 
117      } 
118       
119      void dispense() { 
120          if (useCard) { 
121            cardreader.pay(); 
122          } else if (payment.enough(1)) { 
123              dispenser.dispense(); 
124              int change = payment.deduct(1); 
125              System.out.println("Change given: " + change + " coin(s)"); 
126          } else { 
127              System.out.println("Insufficient funds"); 
128          } 
129          useCard = false; 
130      } 
131   
132      void cancel() { 
133          useCard = false; 
134          int change = payment.cancel(); 
135          dispenser.cancel(); 
136          display.setText("Order cancelled."); 
137          System.out.println("Returned " + change + " coin(s);"); 
138      } 
139   
140      void refillCoffee () { 
141          coffee.refill(); 
142      } 
143   
144      void refillSugar () { 
145          sugar.refill(); 
146      } 
147   
148      void refillMilk () { 
149          milk.refill(); 
150      } 
151   
152      void refillCups () { 
153          cups.refill(); 
154      } 
155   
156      public static void main(String[] args) { 
157          FancyMachine machine = new FancyMachine(); 
158          machine.run(); 
159      } 
160  }