Projekt_APO-Automat_spozywczy/VendingMachine.java

191 lines
7.9 KiB
Java
Raw Normal View History

2021-10-13 13:57:46 +02:00
package apo;
import java.util.ArrayList;
import java.util.Scanner;
public class VendingMachine {
private final ArrayList<Drink> drinks = new ArrayList<>();
private final ArrayList<Sweets> sweets = new ArrayList<>();
private final ArrayList<HotMeals> hotMeals = new ArrayList<>();
private final Scanner scanner = new Scanner(System.in);
public void init() {
initProducts();
start();
}
private void start() {
System.out.println("======= MENU =======");
System.out.println("- NAPOJE -");
for (Drink drink : drinks) {
System.out.println(drink.getName() + ", " + drink.getPrice() + "zl");
}
System.out.println();
System.out.println("- SLODYCZE -");
for (Sweets sweet : sweets) {
System.out.println(sweet.getName() + ", " + sweet.getPrice() + "zl");
}
System.out.println();
System.out.println("- CIEPLE POSILKI -");
for (HotMeals hotMeal : hotMeals) {
System.out.println(hotMeal.getName() + ", " + hotMeal.getPrice() + "zl");
}
System.out.println();
System.out.println("Prosze wybrac produkt");
String userChoice = scanner.nextLine();
paymentMethod(userChoice);
}
private void paymentMethod(String userChoice) {
String payment;
switch (userChoice) {
case "Coca-cola" -> {
System.out.println("WYBRANO: " + userChoice + ", " + drinks.get(0).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, drinks.get(0).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
}
case "Sprite" -> {
System.out.println("WYBRANO: " + userChoice + ", " + drinks.get(1).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, drinks.get(1).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
}
case "Woda" -> {
System.out.println("WYBRANO: " + userChoice + ", " + drinks.get(2).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, drinks.get(2).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
}
case "Snickers" -> {
System.out.println("WYBRANO: " + userChoice + ", " + sweets.get(0).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, sweets.get(0).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
}
case "Bounty" -> {
System.out.println("WYBRANO: " + userChoice + ", " + sweets.get(1).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, sweets.get(1).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
}
case "Lion" -> {
System.out.println("WYBRANO: " + userChoice + ", " + sweets.get(2).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, sweets.get(2).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
}
case "Spaghetti" -> {
System.out.println("WYBRANO: " + userChoice + ", " + hotMeals.get(0).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, hotMeals.get(0).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
System.out.println("W magazynie zostało: " + updateStorage());
}
case "Zupa" -> {
System.out.println("WYBRANO: " + userChoice + ", " + hotMeals.get(1).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, hotMeals.get(1).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
System.out.println("W magazynie zostało: " + updateStorage());
}
case "Frytki z kurczakiem" -> {
System.out.println("WYBRANO: " + userChoice + ", " + hotMeals.get(2).getPrice());
System.out.println("Prosze wybrac metode platnosci: GOTOWKA / KARTA");
payment = scanner.nextLine();
if (payment.equals("gotowka")) {
double cashFromUser = scanner.nextDouble();
cash(cashFromUser, hotMeals.get(2).getPrice());
} else if (payment.equals("karta")) {
creditCard();
}
System.out.println("W magazynie zostało: " + updateStorage());
}
default -> System.out.println("NIEZNANY PRODUKT!");
}
}
private int updateStorage() {
int storage = 100;
return storage - 1;
}
private void cash(double cashFromUser, double price) {
if (cashFromUser == price) {
System.out.println("GOTOWKA PRZYJETA!");
System.out.println("Prosze odebrac produkt");
} else if (cashFromUser < price) {
System.out.println("Za malo gotowki!");
} else {
System.out.println("Reszta: " + (cashFromUser - price) + "zl");
}
}
private void creditCard() {
System.out.println("TRANSAKCJA ZAAKCEPTOWANA!");
System.out.println("Prosze odebrac produkt");
}
private void initProducts() {
drinks.add(new Drink("Coca-cola", 3.99, 500));
drinks.add(new Drink("Sprite", 2.99, 330));
drinks.add(new Drink("Woda", 1.49, 500));
sweets.add(new Sweets("Snickers", 1.99));
sweets.add(new Sweets("Bounty", 2.49));
sweets.add(new Sweets("Lion", 1.49));
hotMeals.add(new HotMeals("Spaghetti", 9.99));
hotMeals.add(new HotMeals("Zupa", 8.99));
hotMeals.add(new HotMeals("Frytki z kurczakiem", 19.99));
}
}