1 package model; 2 3 import java.util.function.Consumer; 4 5 class Ingredient { 6 7 int max; 8 String name; 9 int quantity; 10 Consumer<Ingredient> onEmptyAction; 11 Dispenser dispenser; 12 13 Ingredient(String name, int max, Dispenser dispenser) { 14 this.dispenser = dispenser; 15 this.name = name; 16 this.max = max; 17 this.quantity = max; 18 } 19 20 void onEmpty(Consumer<Ingredient> f) { 21 this.onEmptyAction = f; 22 } 23 24 boolean empty() { return quantity == 0; } 25 26 void use() { 27 if (empty() && onEmptyAction != null) { 28 onEmptyAction.accept(this); 29 } else { 30 quantity--; 31 dispenser.addIngredient(name); 32 } 33 } 34 35 void refill() { 36 System.out.print(name + "refilled"); 37 quantity = max; 38 } 39 } 40 41