From 707b932c8bbb2f6e67534739cfbedeefb241e36f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Jasi=C5=84ski?= Date: Mon, 11 Oct 2021 21:54:58 +0200 Subject: [PATCH] added Inventory and VendingMachine --- src/main/java/products/Inventory.java | 38 ++++++++ src/main/java/products/VendingMachine.java | 15 +++ .../java/products/VendingMachineFactory.java | 7 ++ .../java/products/VendingMachineImpl.java | 48 ++++++++++ src/main/java/products/main.java | 96 +++++++++++++------ 5 files changed, 176 insertions(+), 28 deletions(-) create mode 100644 src/main/java/products/Inventory.java create mode 100644 src/main/java/products/VendingMachine.java create mode 100644 src/main/java/products/VendingMachineFactory.java create mode 100644 src/main/java/products/VendingMachineImpl.java diff --git a/src/main/java/products/Inventory.java b/src/main/java/products/Inventory.java new file mode 100644 index 0000000..b097640 --- /dev/null +++ b/src/main/java/products/Inventory.java @@ -0,0 +1,38 @@ +package main.java.products; + +import java.util.HashMap; +import java.util.Map; + +public class Inventory { + private Map inventory = new HashMap(); + + public int getQuantity(T item){ + Integer value = inventory.get(item); + return value == null? 0 : value ; + } + + public void add(T item){ + int count = inventory.get(item); + inventory.put(item, count+1); + } + + public void deduct(T item) { + if (hasItem(item)) { + int count = inventory.get(item); + inventory.put(item, count - 1); + } + } + + public boolean hasItem(T item){ + return getQuantity(item) > 0; + } + + public void clear(){ + inventory.clear(); + } + + public void put(T item, int quantity) { + inventory.put(item, quantity); + } +} + diff --git a/src/main/java/products/VendingMachine.java b/src/main/java/products/VendingMachine.java new file mode 100644 index 0000000..9f1cf9e --- /dev/null +++ b/src/main/java/products/VendingMachine.java @@ -0,0 +1,15 @@ +package main.java.products; + +import java.util.List; + + +public interface VendingMachine { + public long selectProductAndGetPrice(Product product); + + public void insertCoin(Denomination denomination); + + public List refund(); + + public void reset(); +} + diff --git a/src/main/java/products/VendingMachineFactory.java b/src/main/java/products/VendingMachineFactory.java new file mode 100644 index 0000000..519e295 --- /dev/null +++ b/src/main/java/products/VendingMachineFactory.java @@ -0,0 +1,7 @@ +package main.java.products; + +public class VendingMachineFactory { + public static VendingMachine createVendingMachine() { + return new VendingMachineImpl(); + } +} diff --git a/src/main/java/products/VendingMachineImpl.java b/src/main/java/products/VendingMachineImpl.java new file mode 100644 index 0000000..3daee8f --- /dev/null +++ b/src/main/java/products/VendingMachineImpl.java @@ -0,0 +1,48 @@ +package main.java.products; + +import java.util.List; + +public class VendingMachineImpl implements VendingMachine { + + private Inventory cashInventory = new Inventory(); + private Inventory productInventory = new Inventory(); + private long totalSales; + private Product currentItem; + private long currentBalance; + + public VendingMachineImpl() { + initialize(); + } + + private void initialize() { + //change to better values + for (Denomination c : Denomination.values()) { + cashInventory.put(c, 5); + } + + for () { + productInventory.put(i, 5); + } + + } + + @Override + public long selectProductAndGetPrice(Product product) { + return 0; + } + + @Override + public void insertCoin(Denomination denomination) { + + } + + @Override + public List refund() { + return null; + } + + @Override + public void reset() { + + } +} diff --git a/src/main/java/products/main.java b/src/main/java/products/main.java index f2f834c..b4e9c12 100644 --- a/src/main/java/products/main.java +++ b/src/main/java/products/main.java @@ -1,5 +1,6 @@ package main.java.products; +import java.util.Objects; import java.util.Scanner; public class main { @@ -11,35 +12,52 @@ public class main { System.out.println("2. A snack"); System.out.println("3. A meal"); - - int opt3 = sc.nextInt(); - if (opt3 == 1) { - orderADrink(); - } else if (opt3 == 2) { - orderASnack(); - } else if (opt3 == 3){ - orderAMeal(); + int selectedType = sc.nextInt(); + double price = 0; + if (selectedType == 1) { + price = orderADrink(); + } else if (selectedType == 2) { + price = orderASnack(); + } else if (selectedType == 3) { + price = orderAMeal(); } + + System.out.println("Choose payment option"); + System.out.println("1. Cash"); + System.out.println("2. Credit card"); + + int paymentType = sc.nextInt(); + if (paymentType == 1) { + payInCash(price); + } else if (paymentType == 2) { + payWithCard(price); + } + + } - private static void orderADrink() { - DrinkMenu drinkMenu = new DrinkMenu(); - drinkMenu.showMenu(); - int opt; - Drink orderedDrink = null; - while (orderedDrink == null) { - opt = sc.nextInt(); - if (opt < 1 || opt > 4) { - System.out.println("You have chosen wrong option"); - } else { - orderedDrink = drinkMenu.get(opt - 1); - } - } - System.out.println("You ordered " + orderedDrink); - System.out.println("Price: " + orderedDrink.getPrice()); - } - private static void orderASnack() { + + + private static double orderADrink() { + DrinkMenu drinkMenu = new DrinkMenu(); + drinkMenu.showMenu(); + int opt; + Drink orderedDrink = null; + while (orderedDrink == null) { + opt = sc.nextInt(); + if (opt < 1 || opt > 4) { + System.out.println("You have chosen wrong option"); + } else { + orderedDrink = drinkMenu.get(opt - 1); + } + } + System.out.println("You ordered " + orderedDrink); + System.out.println("Price: " + orderedDrink.getPrice()); + return orderedDrink.getPrice(); + } + + private static double orderASnack() { SnackMenu snackMenu = new SnackMenu(); snackMenu.showMenu(); int opt; @@ -54,9 +72,11 @@ public class main { } System.out.println("You ordered " + orderedSnack); System.out.println("Price: " + orderedSnack.getPrice()); + return orderedSnack.getPrice(); + } - private static void orderAMeal() { + private static double orderAMeal() { MealMenu mealMenu = new MealMenu(); mealMenu.showMenu(); int opt; @@ -71,9 +91,29 @@ public class main { } System.out.println("You ordered " + orderedMeal); System.out.println("Price: " + orderedMeal.getPrice()); + return orderedMeal.getPrice(); + + } + + private static void payWithCard(double price) { + System.out.println("Follow the instructions on the pin pad"); + } + + private static void payInCash(double price) { + System.out.println("Insert money"); + System.out.println("Type inserted denominations, type 'q' when done."); + double sum= 0; + while (!Objects.equals(sc.nextLine(), "q")){ + double denomination = sc.nextDouble(); + sum+=denomination; + + } + if (sum > price) + giveChange(); + } + + private static void giveChange() { } - - }