From 3bcf434cdc5659d54607ad4d425f08ad4b942623 Mon Sep 17 00:00:00 2001 From: Feyi Date: Thu, 28 Mar 2024 12:03:06 +0000 Subject: [PATCH 1/4] Stage 5 update --- CoffeeMachine.java | 288 +++++++++++++++++++++++---------------------- 1 file changed, 146 insertions(+), 142 deletions(-) diff --git a/CoffeeMachine.java b/CoffeeMachine.java index e53d184..3afe608 100644 --- a/CoffeeMachine.java +++ b/CoffeeMachine.java @@ -1,152 +1,156 @@ -//package machine; -// -//import java.util.Scanner; -// -//public class CoffeeMachine { -// public static void main(String[] args) { -// Scanner scanner = new Scanner(System.in); -// -// int count = 0; -// -// while (true) { -// -// System.out.println("Write Action (buy, fill, take, remaining, exit)"); -// String action = scanner.nextLine(); -// -// count += 1; -// -// if (action.equals("buy")) { -// if (count > 1) { -// System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back-to main menu"); -// } else { -// System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:"); -// } -// String orderOption = scanner.nextLine(); -// Buy(orderOption); -// } else if (action.equals("fill")) { -// System.out.println("Write how many ml of water you want to add:"); -// int waterRequired = scanner.nextInt(); -// System.out.println("Write how many ml of milk you want to add:"); -// int milkRequired = scanner.nextInt(); -// System.out.println("Write how many g of coffee beans you want to add:"); -// int coffeeRequired = scanner.nextInt(); -// System.out.println("Write how many disposable cups you want to add:"); -// int cupsRequired = scanner.nextInt(); -// fill(waterRequired, milkRequired, coffeeRequired, cupsRequired); -// } else if (action.equals("remaining")) { -// remaining(); -// } else if (action.equals("exit")) { -// break; -// } else if (action.equals("take")) { -// take(); -// } else if (action.equals("Back")) { -// continue; -// } else { -// continue; -// } -// } -// -// } -// -// static int intialAmountOfWater = 400; -// static int intialAmountofMilk = 540; -// static int intialAmountofCoffeBeans = 120; -// static int intialAmountOfCups = 9; -// static int intialAmountOfMoney = 550; -// -// public static void Buy(String CoffeeType) { -// if (CoffeeType.equals("1")) { -// checkEspresso(); -// } else if (CoffeeType.equals("2")) { -// checkLatte(); -// } else if (CoffeeType.equals("3")) { -// checkCappunicino(); -// } else { -// System.out.println("Invalid input. Enter a number"); -// } -// } -// -// public static void fill(int water, int milk, int coffee, int cups) { -// intialAmountOfWater += water; -// intialAmountofMilk += milk; -// intialAmountofCoffeBeans += coffee; -// intialAmountOfCups += cups; -// intialAmountOfMoney += 0; -// } -// -// public static void take() { -// System.out.println("I gave you " + (intialAmountOfMoney)); -// intialAmountOfMoney -= intialAmountOfMoney; -// } -// -// public static void remaining() { -// System.out.println("This coffee machine has:"); -// System.out.println((intialAmountOfWater) + " ml of water"); -// System.out.println((intialAmountofMilk) + " ml of milk"); -// System.out.println((intialAmountofCoffeBeans) + " g of coffee beans"); -// System.out.println((intialAmountOfCups) + " disposable cups"); -// System.out.println("$" + (intialAmountOfMoney) + " of money"); -// } -// -// public static void checkEspresso() { -// if (intialAmountOfWater < 250) { -// System.out.println("Sorry, not enough water!"); -// } else if (intialAmountofCoffeBeans < 16) { -// System.out.println("Sorry, not enough coffee!"); -// } else { -// System.out.println("I have enough resources, making you a coffee"); -// intialAmountOfWater -= 250; -// intialAmountofCoffeBeans -= 16; -// intialAmountOfCups -= 1; -// intialAmountOfMoney += 4; -// } -// } -// -// public static void checkLatte() { -// if (intialAmountOfWater < 350) { -// System.out.println("Sorry, not enough water!"); -// } else if (intialAmountofMilk < 75) { -// System.out.println("Sorry, not enough milk!"); -// } else if (intialAmountofCoffeBeans < 20) { -// System.out.println("Sorry, not enough coffee!"); -// } else { -// System.out.println("I have enough resources, making you a coffee"); -// intialAmountOfWater -= 350; -// intialAmountofMilk -= 75; -// intialAmountofCoffeBeans -= 20; -// intialAmountOfCups -= 1; -// intialAmountOfMoney += 7; -// } -// } -// -// public static void checkCappunicino() { -// if (intialAmountOfWater < 350) { -// System.out.println("Sorry, not enough water!"); -// } else if (intialAmountofMilk < 75) { -// System.out.println("Sorry, not enough milk!"); -// } else if (intialAmountofCoffeBeans < 20) { -// System.out.println("Sorry, not enough coffee!"); -// } else { -// System.out.println("I have enough resources, making you a coffee"); -// intialAmountOfWater -= 200; -// intialAmountofMilk -= 100; -// intialAmountofCoffeBeans -= 12; -// intialAmountOfCups -= 1; -// intialAmountOfMoney += 6; -// } -// } -// -// -//} -// - package machine; import java.util.Scanner; public class CoffeeMachine { + private static final Scanner scanner = new Scanner(System.in); + private static int water = 400; + private static int milk = 540; + private static int coffeeBeans = 120; + private static int cups = 9; + private static int money = 550; + + public static void printResources() { + System.out.println("The coffee machine has"); + System.out.println(water + " ml of water"); + System.out.println(milk + " ml of milk"); + System.out.println(coffeeBeans + " g of coffee beans"); + System.out.println(cups + " disposable cups"); + System.out.println("$" + money + " of money"); + System.out.println(); + } + + public static void buyCappuccino() { + processDrink(200, 100, 12, 6); + } + + public static void buyLatte() { + processDrink(350, 75, 20, 7); + } + + public static void buyEspresso() { + processDrink(250, 0, 16, 4); + } + + public static void processDrink(int waterPerCup, int milkPerCup, int coffeeBeansPerCup, int moneyTaken) { + + if (water < waterPerCup) { + System.out.println("Sorry, not enough water!"); + } else if (milk < milkPerCup) { + System.out.println("Sorry, not enough milk!"); + } else if (coffeeBeans < coffeeBeansPerCup) { + System.out.println("Sorry, not enough coffee beans!"); + } else if (cups < 1) { + System.out.println("Sorry, not enough disposable cups!"); + } else { + water -= waterPerCup; + milk -= milkPerCup; + coffeeBeans -= coffeeBeansPerCup; + money += moneyTaken; + cups--; + System.out.println("I have enough resources, making you a coffee!"); + } + + System.out.println(); + } + + public static void Buy(int coffeeType) { + switch (coffeeType) { + case 1: + buyEspresso(); + break; + case 2: + buyLatte(); + break; + case 3: + buyCappuccino(); + break; + default: + break; + } + } + + public static void fillResources() { + int waterAdded = getWater(); + int milkAdded = getMilk(); + int coffeeAdded = getCoffee(); + int cupsAdded = getCups(); + + water += waterAdded; + milk += milkAdded; + coffeeBeans += coffeeAdded; + cups += cupsAdded; + + System.out.println(); + + } + + public static void takeMoney() { + + System.out.println("I gave you $" + money); + money = 0; + + System.out.println(); + } + + public static int getCoffeeType() { + System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino"); + return Integer.parseInt(scanner.nextLine()); + } + + public static int getWater() { + System.out.println("Write how many ml of water you want to add: "); + return Integer.parseInt(scanner.nextLine()); + } + + public static int getMilk() { + System.out.println("Write how many ml of milk you want to add: "); + return Integer.parseInt(scanner.nextLine()); + } + + public static int getCoffee() { + System.out.println("Write how many grams of coffee beans you want to add:"); + return Integer.parseInt(scanner.nextLine()); + } + + public static int getCups() { + System.out.println("Write how many disposable cups you want to add: "); + return Integer.parseInt(scanner.nextLine()); + } + + + public static void main(String[] args) { + + while (true) { + + System.out.println("Write action (buy, fill, take, remaining, exit):"); + String action = scanner.nextLine(); + + System.out.println(); + switch (action) { + case "buy": + int coffeeType = getCoffeeType(); + Buy(coffeeType); + break; + case "fill": + fillResources(); + break; + case "take": + takeMoney(); + break; + case "remaining": + printResources(); + break; + case "exit": + System.exit(0); + break; + default: + break; + } + } + } } From a9bdac74353c69802ecbfd8a6c9ddddf21c9da05 Mon Sep 17 00:00:00 2001 From: Feyi Date: Thu, 28 Mar 2024 12:48:15 +0000 Subject: [PATCH 2/4] Stage 1 --- Stage1/CoffeeMachine.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Stage1/CoffeeMachine.java diff --git a/Stage1/CoffeeMachine.java b/Stage1/CoffeeMachine.java new file mode 100644 index 0000000..da48c8f --- /dev/null +++ b/Stage1/CoffeeMachine.java @@ -0,0 +1,13 @@ +package machine.Stage1; + +public class CoffeeMachine { + public static void main(String[] args) { + System.out.println("Starting to make a coffee"); + System.out.println("Grinding coffee beans"); + System.out.println("Boiling water"); + System.out.println("Mixing boiled water with crushed coffee beans"); + System.out.println("Pouring coffee into the cup"); + System.out.println("Pouring some milk into the cup"); + System.out.println("Coffee is ready!"); + } +} From 9320a6bcf70890c38cf7a6e1b6be6b4c5a5bdce8 Mon Sep 17 00:00:00 2001 From: Feyi Date: Thu, 28 Mar 2024 12:53:51 +0000 Subject: [PATCH 3/4] Stage 1 --- Stage1/CoffeeMachine.java | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 Stage1/CoffeeMachine.java diff --git a/Stage1/CoffeeMachine.java b/Stage1/CoffeeMachine.java deleted file mode 100644 index da48c8f..0000000 --- a/Stage1/CoffeeMachine.java +++ /dev/null @@ -1,13 +0,0 @@ -package machine.Stage1; - -public class CoffeeMachine { - public static void main(String[] args) { - System.out.println("Starting to make a coffee"); - System.out.println("Grinding coffee beans"); - System.out.println("Boiling water"); - System.out.println("Mixing boiled water with crushed coffee beans"); - System.out.println("Pouring coffee into the cup"); - System.out.println("Pouring some milk into the cup"); - System.out.println("Coffee is ready!"); - } -} From a5e3cd807caf6dd530b6187112340b533b11ca14 Mon Sep 17 00:00:00 2001 From: Feyi Date: Thu, 28 Mar 2024 13:56:15 +0000 Subject: [PATCH 4/4] Fixing Failing Test --- CoffeeMachine.java | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/CoffeeMachine.java b/CoffeeMachine.java index 3afe608..73ea27e 100644 --- a/CoffeeMachine.java +++ b/CoffeeMachine.java @@ -55,17 +55,19 @@ public static void processDrink(int waterPerCup, int milkPerCup, int coffeeBeans System.out.println(); } - public static void Buy(int coffeeType) { + public static void Buy(String coffeeType) { switch (coffeeType) { - case 1: + case "1": buyEspresso(); break; - case 2: + case "2": buyLatte(); break; - case 3: + case "3": buyCappuccino(); break; + case "back": + return; default: break; } @@ -94,9 +96,9 @@ public static void takeMoney() { System.out.println(); } - public static int getCoffeeType() { - System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino"); - return Integer.parseInt(scanner.nextLine()); + public static String getCoffeeType() { + System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:"); + return scanner.nextLine(); } public static int getWater() { @@ -119,19 +121,23 @@ public static int getCups() { return Integer.parseInt(scanner.nextLine()); } + public static String mainMenu() { + System.out.println("Write action (buy, fill, take, remaining, exit):"); + return scanner.nextLine(); + } + public static void main(String[] args) { while (true) { - System.out.println("Write action (buy, fill, take, remaining, exit):"); - String action = scanner.nextLine(); + String action = mainMenu(); System.out.println(); switch (action) { case "buy": - int coffeeType = getCoffeeType(); + String coffeeType = getCoffeeType(); Buy(coffeeType); break; case "fill": @@ -155,3 +161,4 @@ public static void main(String[] args) { +