added Inventory and VendingMachine

This commit is contained in:
Konrad Jasiński 2021-10-11 21:54:58 +02:00
parent cb38d7392b
commit 707b932c8b
5 changed files with 176 additions and 28 deletions

View File

@ -0,0 +1,38 @@
package main.java.products;
import java.util.HashMap;
import java.util.Map;
public class Inventory<T> {
private Map<T, Integer> inventory = new HashMap<T, Integer>();
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);
}
}

View File

@ -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<Denomination> refund();
public void reset();
}

View File

@ -0,0 +1,7 @@
package main.java.products;
public class VendingMachineFactory {
public static VendingMachine createVendingMachine() {
return new VendingMachineImpl();
}
}

View File

@ -0,0 +1,48 @@
package main.java.products;
import java.util.List;
public class VendingMachineImpl implements VendingMachine {
private Inventory<Object> cashInventory = new Inventory<Object>();
private Inventory<Product> productInventory = new Inventory<Product>();
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<Denomination> refund() {
return null;
}
@Override
public void reset() {
}
}

View File

@ -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() {
}
}