├── CoffeeMachine.java ├── CoffeeMaker.java └── README.md /CoffeeMachine.java: -------------------------------------------------------------------------------- 1 | package CoffeeMachine; 2 | 3 | import java.util.Objects; 4 | import java.util.Scanner; 5 | 6 | public class CoffeeMachine { 7 | 8 | public static int initialWater = 400; 9 | public static int initialMilk = 540; 10 | public static int initialBeans = 120; 11 | public static int initialCups = 9; 12 | public static int initialMoney = 550; 13 | 14 | public static void exit(){ 15 | Scanner scanner = new Scanner(System.in); 16 | String exit = scanner.nextLine(); 17 | if (Objects.equals(exit, "exit")) System.exit(0); 18 | scanner.close(); 19 | } 20 | 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | while (true){ 24 | System.out.println("Write action (buy, fill, take, remaining, exit):"); 25 | String name = scanner.next(); 26 | if (name.equals("exit")){ 27 | break; 28 | } else switch (name){ 29 | case "buy" -> buy(); 30 | case "fill" -> fill(); 31 | case "take" -> take(); 32 | case "remaining" -> remaining(); 33 | } 34 | 35 | } 36 | 37 | } 38 | public static void buy(){ 39 | int cCups = 1; 40 | // espresso 41 | int eWater = 250; 42 | int eBeans = 16; 43 | int eCosts = 4; 44 | // latte 45 | int lWater = 350; 46 | int lMilk = 75; 47 | int lBeans = 20; 48 | int lCosts = 7; 49 | // cappuccino 50 | int cWater = 200; 51 | int cMilk = 100; 52 | int cBeans = 12; 53 | int cCosts = 6; 54 | System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:"); 55 | Scanner scanner = new Scanner(System.in); 56 | int buy = scanner.nextInt(); 57 | if (initialWater < eWater | initialWater < lWater | initialWater < cWater) System.out.println("Sorry, not enough water!"); 58 | else if (initialBeans < eBeans | initialBeans < lBeans | initialBeans < cBeans) System.out.println("Sorry, not enough beans!"); 59 | else if (initialMilk < lMilk | initialMilk < cMilk) System.out.println("Sorry, not enough milk!"); 60 | else switch (buy) { 61 | case 1 -> System.out.println("The coffee machine has:\n" + 62 | (initialWater - eWater) + " ml of water\n" + 63 | initialMilk + " ml of milk\n" + (initialBeans - eBeans) + " ml of coffee beans\n" + 64 | (initialCups - cCups) + " disposable cups\n" + "$" + (initialMoney + eCosts) + " of money"); 65 | 66 | case 2 -> System.out.println("The coffee machine has:\n" + (initialWater - lWater) + " ml of water\n" + 67 | (initialMilk - lMilk) + " ml of milk\n" + (initialBeans - lBeans) + " ml of coffee beans\n" + 68 | (initialCups - cCups) + " disposable cups\n" + "$" + (initialMoney + lCosts) + " of money"); 69 | 70 | case 3 -> System.out.println("The coffee machine has:\n" + (initialWater - cWater) + " ml of water\n" + 71 | (initialMilk - cMilk) + " ml of milk\n" + (initialBeans - cBeans) + " ml of coffee beans\n" + 72 | (initialCups - cCups) + " disposable cups\n" + "$" + (initialMoney + cCosts) + " of money"); 73 | 74 | default -> System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:"); 75 | } 76 | scanner.close(); 77 | } 78 | public static void fill(){ 79 | Scanner scanner = new Scanner(System.in); 80 | System.out.println("Write how many ml of water you want to add:"); 81 | int addWater = scanner.nextInt(); 82 | System.out.println("Write how many ml of milk you want to add:"); 83 | int addMilk = scanner.nextInt(); 84 | System.out.println("Write how many grams of coffee beans you want to add:"); 85 | int addBeans = scanner.nextInt(); 86 | System.out.println("Write how many disposable cups of coffee you want to add:"); 87 | int addCups = scanner.nextInt(); 88 | System.out.println("\nThe coffee machine has:\n" + (initialWater + addWater) + " ml of water\n" + 89 | (initialMilk + addMilk) + " ml of milk\n" + (initialBeans + addBeans) + " g of coffee beans\n" + 90 | (initialCups + addCups) + " disposable cups\n" + "$" + initialMoney + " of money\n"); 91 | scanner.close(); 92 | } 93 | public static void take(){ 94 | System.out.println("I gave you " + "$" + initialMoney); 95 | System.out.println("\nThe coffee machine has:\n" + initialWater + " ml of water\n" + 96 | initialMilk + " ml of milk\n" + initialBeans + " g of coffee beans\n" + 97 | initialCups + " disposable cups\n" + "$" + 0 + " of money\n"); 98 | } 99 | public static void remaining() { 100 | int cCups = 1; 101 | // espresso 102 | int eWater = 250; 103 | int eBeans = 16; 104 | int eCosts = 4; 105 | // latte 106 | int lWater = 350; 107 | int lMilk = 75; 108 | int lBeans = 20; 109 | int lCosts = 7; 110 | // cappuccino 111 | int cWater = 200; 112 | int cMilk = 100; 113 | int cBeans = 12; 114 | int cCosts = 6; 115 | System.out.println(""" 116 | 117 | The coffee machine has: 118 | 400 ml of water 119 | 540 ml of milk 120 | 120 g of coffee beans 121 | 9 disposable cups 122 | $550 of money"""); 123 | System.out.println("\nWrite action (buy, fill, take, remaining, exit):"); 124 | Scanner scanner = new Scanner(System.in); 125 | if ("buy".equals(scanner.nextLine())) { 126 | System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:"); 127 | int buy = scanner.nextInt(); 128 | if (initialWater < eWater | initialWater < lWater | initialWater < cWater) 129 | System.out.println("Sorry, not enough water!"); 130 | else if (initialBeans < eBeans | initialBeans < lBeans | initialBeans < cBeans) 131 | System.out.println("Sorry, not enough beans!"); 132 | else if (initialMilk < lMilk | initialMilk < cMilk) System.out.println("Sorry, not enough milk!"); 133 | else switch (buy) { 134 | case 1, 2, 3 -> System.out.println("I have enough resources, making you a coffee!\n\nWhat do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:"); 135 | 136 | default -> 137 | System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:"); 138 | 139 | } 140 | scanner.close(); 141 | } 142 | } 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | } 156 | -------------------------------------------------------------------------------- /CoffeeMaker.java: -------------------------------------------------------------------------------- 1 | package CoffeeMachine; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * Interaction with the machine using a single method 7 | * - save the state of the machine 8 | */ 9 | 10 | public class CoffeeMaker { 11 | 12 | public static void main(String[] args) { 13 | 14 | 15 | } 16 | 17 | public static void action(String invocation) { 18 | Scanner scanner = new Scanner(System.in); 19 | invocation = scanner.next(); 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HyperSkill_Java 2 | course Coffee Machine 3 | --------------------------------------------------------------------------------