From f67d6f07bc1046ffef838d9b33b45738563469db Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Tue, 2 Oct 2018 13:28:15 +0300 Subject: [PATCH 1/5] Stage #2: Count needed resources --- src/coffeeMachine/Main.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/coffeeMachine/Main.java b/src/coffeeMachine/Main.java index 39e98c9..5babbd9 100644 --- a/src/coffeeMachine/Main.java +++ b/src/coffeeMachine/Main.java @@ -1,7 +1,25 @@ package coffeeMachine; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - System.out.print("Hello world!"); + Scanner scanner = new Scanner(System.in); + System.out.print("Write how many cups of coffee you will need:"); + try { + int cups = scanner.nextInt(); + System.out.printf("For %d cups of coffee you will need: %n", cups); + System.out.printf("%d ml of water %n", cups*WATER); + System.out.printf("%d ml of milk %n", cups*MILK); + System.out.printf("%d g of coffee beans %n", cups*BEANS); + + } catch (Exception ioe) { + System.out.println("Cups should be an Integer value"); + } } -} \ No newline at end of file + + private static final int WATER = 200; + private static final int MILK = 50; + private static final int BEANS = 15; + +} From 3c0a1fb04e884503ecc673d89e7ae48df058311e Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Tue, 2 Oct 2018 14:56:44 +0300 Subject: [PATCH 2/5] Stage #3: Check if there are enough resources --- src/coffeeMachine/Main.java | 107 +++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 9 deletions(-) diff --git a/src/coffeeMachine/Main.java b/src/coffeeMachine/Main.java index 5babbd9..77759bf 100644 --- a/src/coffeeMachine/Main.java +++ b/src/coffeeMachine/Main.java @@ -3,23 +3,112 @@ import java.util.Scanner; public class Main { + private static final int WATER_CONSUMPTION = 200; + private static final int MILK_CONSUMPTION = 50; + private static final int BEANS_CONSUMPTION = 15; + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); - System.out.print("Write how many cups of coffee you will need:"); + try { + System.out.print("Write how many ml of water the coffee machine has: "); + int water = scanner.nextInt(); + System.out.print("Write how many ml of milk the coffee machine has: "); + int milk = scanner.nextInt(); + System.out.print("Write how many grams of coffee the coffee machine has: "); + int beans = scanner.nextInt(); + System.out.print("Write how many cups of coffee you will need: "); int cups = scanner.nextInt(); - System.out.printf("For %d cups of coffee you will need: %n", cups); - System.out.printf("%d ml of water %n", cups*WATER); - System.out.printf("%d ml of milk %n", cups*MILK); - System.out.printf("%d g of coffee beans %n", cups*BEANS); + CoffeeMachine machine = new CoffeeMachine.Builder(WATER_CONSUMPTION, + MILK_CONSUMPTION, BEANS_CONSUMPTION).water(water).beans(beans).milk(milk).build(); + + int capacity = machine.getBrewCapacity(); + + if (capacity > cups) { + System.out.printf("Yes, I can make that amount of coffee (and even %d more than that) %n", capacity-cups); + } else if (capacity == cups) { + System.out.println("Yes, I can make that amount of coffee"); + } else { + System.out.printf("No, I can make only %d cups of coffee %n", capacity); + } + } catch (Exception ioe) { - System.out.println("Cups should be an Integer value"); + System.out.println("Please enter an an Integer value"); } } +} + +class CoffeeMachine { + + int getBrewCapacity() { + int wLim = water / waterConsumption; + int mLim = milk / milkConsumption; + int bLim = beans / beansConsumption; + + if (wLim <= mLim && wLim <= bLim) { + return wLim; + } else if (mLim <= wLim && mLim <= bLim) { + return mLim; + } else { + return bLim; + } + } - private static final int WATER = 200; - private static final int MILK = 50; - private static final int BEANS = 15; + //capacity + private final int water; + private final int milk; + private final int beans; + //per cup consumption + private final int waterConsumption; + private final int milkConsumption; + private final int beansConsumption; + + static class Builder { + //capacity + private int water = 0; + private int milk = 0; + private int beans = 0; + + //per cup consumption + private final int waterConsumption; + private final int milkConsumption; + private final int beansConsumption; + + Builder (int waterConsumption, int milkConsumption, int beansConsumption) { + this.waterConsumption = waterConsumption; + this.milkConsumption = milkConsumption; + this.beansConsumption = beansConsumption; + } + + Builder water(int val) { + water = val; + return this; + } + + Builder milk(int val) { + milk = val; + return this; + } + + Builder beans(int val) { + beans = val; + return this; + } + + CoffeeMachine build() { + return new CoffeeMachine(this); + } + } + + private CoffeeMachine(Builder builder) { + waterConsumption = builder.waterConsumption; + milkConsumption = builder.milkConsumption; + beansConsumption = builder.beansConsumption; + water = builder.water; + milk = builder.milk; + beans = builder.beans; + } } From 054d2f6430be70eec13d1681e9efe0b3cc14c258 Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Thu, 4 Oct 2018 09:00:45 +0300 Subject: [PATCH 3/5] Stage #4: Choosing different actions --- src/coffeeMachine/Main.java | 292 +++++++++++++++++++++++++----------- 1 file changed, 202 insertions(+), 90 deletions(-) diff --git a/src/coffeeMachine/Main.java b/src/coffeeMachine/Main.java index 77759bf..228dfb0 100644 --- a/src/coffeeMachine/Main.java +++ b/src/coffeeMachine/Main.java @@ -1,114 +1,226 @@ package coffeeMachine; -import java.util.Scanner; +import java.util.*; public class Main { - private static final int WATER_CONSUMPTION = 200; - private static final int MILK_CONSUMPTION = 50; - private static final int BEANS_CONSUMPTION = 15; public static void main(String[] args) { + boolean cont = true; + VendingMachine m = new VendingMachine(); + System.out.println(m.getState()); + String line; + Scanner scanner = new Scanner(System.in); + + while (cont) { + System.out.print("Write action (buy, fill, take, exit):"); + try { + line = scanner.nextLine().trim(); + switch (line) { + case "buy": + System.out.print("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:"); + line = scanner.nextLine().trim(); + switch (line) { + case "1": + m.order(Drink.ESPRESSO); + break; + + case "2": + m.order(Drink.LATTE); + break; + + case "3": + m.order(Drink.CAPPUCHINO); + break; + + default: + System.out.println("Unsupported drink"); + } + System.out.print(m.getState()); + System.out.println(); + break; + + case "fill": + System.out.println("Write how many ml of water do you want to add:"); + int water = Integer.parseInt(scanner.nextLine()); + System.out.println("Write how many ml of milk do you want to add:"); + int milk = Integer.parseInt(scanner.nextLine()); + System.out.println("Write how many grams of coffee beans do you want to add:"); + int beans = Integer.parseInt(scanner.nextLine()); + System.out.println("Write how many disposable cups do you want to add:"); + int cups = Integer.parseInt(scanner.nextLine()); + m.refill(Refill.getInstance(water,milk,beans,cups)); + System.out.print(m.getState()); + System.out.println(); + break; + + case "take": + int cash = m.take(); + System.out.println("I gave you $" + cash); + System.out.print(m.getState()); + System.out.println(); + break; + + case "exit": + cont = false; + break; + + default: + //System.out.println("Unsupported command"); + } //eof switch + } catch (Exception e) { + System.out.println(e.getMessage()); + } + }//eof while + System.out.println("Bye!"); + }//eof main() +} + +class Refill implements Coffeeable { + private Refill() {} + private static final Refill INSTANCE = new Refill(); - Scanner scanner = new Scanner(System.in); + public static final Refill getInstance(int water, int milk, int beans, int cups) { + resources.put(ResourceType.WATER, water); + resources.put(ResourceType.MILK, milk); + resources.put(ResourceType.BEAN, beans); + resources.put(ResourceType.CUPS, cups); + return INSTANCE; + } + + public Map getResources(){ + return resources; + } + + private static final Map resources = new HashMap<>(); +} - try { - System.out.print("Write how many ml of water the coffee machine has: "); - int water = scanner.nextInt(); - System.out.print("Write how many ml of milk the coffee machine has: "); - int milk = scanner.nextInt(); - System.out.print("Write how many grams of coffee the coffee machine has: "); - int beans = scanner.nextInt(); - System.out.print("Write how many cups of coffee you will need: "); - int cups = scanner.nextInt(); +class Drink implements Coffeeable, Payable { + public static final Drink ESPRESSO = new Drink(250,0,16,4); + public static final Drink LATTE = new Drink(350,75,20,7); + public static final Drink CAPPUCHINO = new Drink(200,100,12,6); + + public boolean isCredit(){ + return true; + } + + public int getCashValue(){ + return cost; + } + + private Map resources = new HashMap<>(); + private int cost; + + private Drink(int water, int milk, int beans, int cost) { + resources.put(ResourceType.WATER, water); + resources.put(ResourceType.MILK, milk); + resources.put(ResourceType.BEAN, beans); + resources.put(ResourceType.CUPS, 1); + this.cost = cost; + } + + public Map getResources(){ + return resources; + } +} + + +class VendingMachine implements Coffeeable, Payable { + + public VendingMachine() { + resources.put(ResourceType.WATER, 0); + resources.put(ResourceType.MILK, 0); + resources.put(ResourceType.BEAN, 0); + resources.put(ResourceType.CUPS, 0); + } - CoffeeMachine machine = new CoffeeMachine.Builder(WATER_CONSUMPTION, - MILK_CONSUMPTION, BEANS_CONSUMPTION).water(water).beans(beans).milk(milk).build(); + public String getState() { + StringBuilder sb = new StringBuilder(); + + sb.append("The coffee machine has:"); + sb.append(System.getProperty("line.separator")); + sb.append(resources.get(ResourceType.WATER) + " of water"); + sb.append(System.getProperty("line.separator")); + sb.append(resources.get(ResourceType.MILK) + " of milk"); + sb.append(System.getProperty("line.separator")); + sb.append(resources.get(ResourceType.BEAN) + " of coffee beans"); + sb.append(System.getProperty("line.separator")); + sb.append(resources.get(ResourceType.CUPS) + " of disposable cups"); + sb.append(System.getProperty("line.separator")); + sb.append(getCashValue() + "$ in cash"); + + return sb.toString(); + } - int capacity = machine.getBrewCapacity(); + public int take() { + int result = getCashValue(); + cash = 0; + return result; + } - if (capacity > cups) { - System.out.printf("Yes, I can make that amount of coffee (and even %d more than that) %n", capacity-cups); - } else if (capacity == cups) { - System.out.println("Yes, I can make that amount of coffee"); + public void refill(Coffeeable refill) { + if (refill != null) { + for (ResourceType resourceType : refill.getResources().keySet()) { + int avail = (resources.get(resourceType) == null) ? 0 : resources.get(resourceType); + int additional = refill.getResources().get(resourceType); + if (additional > 0) { + setResource(resourceType, avail+additional); } else { - System.out.printf("No, I can make only %d cups of coffee %n", capacity); + throw new IllegalArgumentException("Refill cannot be negative"); } - - } catch (Exception ioe) { - System.out.println("Please enter an an Integer value"); } + } else throw new IllegalArgumentException("Cannot refill a null"); } -} -class CoffeeMachine { + void setResource(ResourceType type, int val){ + if (val >= 0) { + resources.put(type, val); + } else throw new IllegalArgumentException("Resource "+ type +" value cannot be negative, got " + val); + } - int getBrewCapacity() { - int wLim = water / waterConsumption; - int mLim = milk / milkConsumption; - int bLim = beans / beansConsumption; + public Map getResources() { + return resources; + } - if (wLim <= mLim && wLim <= bLim) { - return wLim; - } else if (mLim <= wLim && mLim <= bLim) { - return mLim; - } else { - return bLim; - } + public boolean isCredit() { + return false; } - //capacity - private final int water; - private final int milk; - private final int beans; - - //per cup consumption - private final int waterConsumption; - private final int milkConsumption; - private final int beansConsumption; - - static class Builder { - //capacity - private int water = 0; - private int milk = 0; - private int beans = 0; - - //per cup consumption - private final int waterConsumption; - private final int milkConsumption; - private final int beansConsumption; - - Builder (int waterConsumption, int milkConsumption, int beansConsumption) { - this.waterConsumption = waterConsumption; - this.milkConsumption = milkConsumption; - this.beansConsumption = beansConsumption; - } - - Builder water(int val) { - water = val; - return this; - } - - Builder milk(int val) { - milk = val; - return this; - } - - Builder beans(int val) { - beans = val; - return this; - } - - CoffeeMachine build() { - return new CoffeeMachine(this); - } + public int getCashValue() { + return cash; } - private CoffeeMachine(Builder builder) { - waterConsumption = builder.waterConsumption; - milkConsumption = builder.milkConsumption; - beansConsumption = builder.beansConsumption; - water = builder.water; - milk = builder.milk; - beans = builder.beans; + public void order(Drink drink) throws MissingResourceException, IllegalArgumentException { + if (drink != null) { + for (ResourceType resourceType : drink.getResources().keySet()) { + int avail = (resources.get(resourceType) == null) ? 0 : resources.get(resourceType); + int required = (drink.getResources().get(resourceType) == null) ? 0 : drink.getResources().get(resourceType); + if ((avail-required) < 0) throw new MissingResourceException(resourceType.toString() + " is missing in this machine. Refill required. ", "ResourceType", resourceType.toString()); + setResource(resourceType, avail-required); + } + if (drink.getCashValue() >= 0) { + cash += drink.getCashValue(); + } else throw new IllegalArgumentException("Cannot vend a drink with negative cost"); + } else throw new IllegalArgumentException("Cannot vend a null drink"); } + + private Map resources = new HashMap<>(); + private int cash = 0; +} + +interface Coffeeable { + enum ResourceType {BEAN, MILK, WATER, CUPS} + public Map getResources(); +} + +interface Payable { + /** + Determines type of transaction with this item. + If true, item costs some value (even if its 0), i.e. Credit Payable. + This item can interact only with opposite Payable, i.e. DebitPayable, + which should return return false and accumulate values provided with + Credit Payables. + **/ + public boolean isCredit(); + + public int getCashValue(); } From 9c71ad990072c8539533bae9c793fc11895c38c9 Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Thu, 4 Oct 2018 19:12:55 +0300 Subject: [PATCH 4/5] Stage #5: Creating main loop --- src/coffeeMachine/Main.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/coffeeMachine/Main.java b/src/coffeeMachine/Main.java index 228dfb0..c433b41 100644 --- a/src/coffeeMachine/Main.java +++ b/src/coffeeMachine/Main.java @@ -7,35 +7,40 @@ public class Main { public static void main(String[] args) { boolean cont = true; VendingMachine m = new VendingMachine(); - System.out.println(m.getState()); + System.out.println("Hello"); String line; Scanner scanner = new Scanner(System.in); while (cont) { - System.out.print("Write action (buy, fill, take, exit):"); + System.out.print("Write action (buy, fill, take, exit, remaining):"); try { line = scanner.nextLine().trim(); switch (line) { case "buy": - System.out.print("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:"); + System.out.print("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - return to main menu:"); line = scanner.nextLine().trim(); switch (line) { case "1": m.order(Drink.ESPRESSO); + System.out.println("Espresso is ready."); break; case "2": m.order(Drink.LATTE); + System.out.println("Latte is ready."); break; case "3": m.order(Drink.CAPPUCHINO); + System.out.println("Cappuchino is ready."); + break; + + case "back": break; default: System.out.println("Unsupported drink"); } - System.out.print(m.getState()); System.out.println(); break; @@ -49,17 +54,17 @@ public static void main(String[] args) { System.out.println("Write how many disposable cups do you want to add:"); int cups = Integer.parseInt(scanner.nextLine()); m.refill(Refill.getInstance(water,milk,beans,cups)); - System.out.print(m.getState()); - System.out.println(); break; case "take": int cash = m.take(); System.out.println("I gave you $" + cash); - System.out.print(m.getState()); - System.out.println(); break; + case "remaining": + System.out.println(m.getState()); + break; + case "exit": cont = false; break; From a63b42fcc32c466b396a8a5b00c3a14d6763758b Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Thu, 4 Oct 2018 19:19:35 +0300 Subject: [PATCH 5/5] Stage #6 Processing coffee machine states --- src/coffeeMachine/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coffeeMachine/Main.java b/src/coffeeMachine/Main.java index c433b41..f441c0e 100644 --- a/src/coffeeMachine/Main.java +++ b/src/coffeeMachine/Main.java @@ -167,7 +167,7 @@ public void refill(Coffeeable refill) { for (ResourceType resourceType : refill.getResources().keySet()) { int avail = (resources.get(resourceType) == null) ? 0 : resources.get(resourceType); int additional = refill.getResources().get(resourceType); - if (additional > 0) { + if (additional >= 0) { setResource(resourceType, avail+additional); } else { throw new IllegalArgumentException("Refill cannot be negative");