import java.util.Scanner; /* You could also use "import drinks.*" instead of the 4 lines below */ import drinks.Beer; import drinks.Coke; import drinks.Drink; import drinks.GreenTea; public class VendingMachine { private static final Drink[] availableDrinks = new Drink[3]; int drinkSelection = 0; public VendingMachine() { availableDrinks[0] = new Beer(); availableDrinks[1] = new Coke(); availableDrinks[2] = new GreenTea(); } public double selectDrink() { drinkSelection = presentDrinkMenu() - 1; // This " - 1 " is to keep "drinkSelection" within the "availableDrinks" array range return availableDrinks[drinkSelection].getPrice(); } public double insertCoins() { double amount = 0.0; Scanner sc = new Scanner(System.in); printCoinOptions(); do { char coin = sc.next().charAt(0); amount += processCoinAmount(coin); System.out.printf("Coins inserted: %.2f \n", amount); } while (amount < availableDrinks[drinkSelection].getPrice()); return amount; } public void checkChange(double amount) { double change = 0.0; if (amount > availableDrinks[drinkSelection].getPrice()) { change = amount - availableDrinks[drinkSelection].getPrice(); System.out.printf("Change $ %.2f \n", change); } } public void printReceipt() { System.out.println("Please collect your drink"); System.out.println("Thank you!!"); } private Integer presentDrinkMenu() { Scanner sc = new Scanner(System.in); int drinkSelection; System.out.print("====== Vending Machine ====== \n"); /* Note. The for loop below is just an enhanced way of writing the following: for (int i = 0; i < availableDrinks.length; i++) { System.out.printf("|%d. Buy %s ($%.2f) \n", i + 1, availableDrinks[i].getName(), availableDrinks[i].getPrice()); } */ int i = 1; for (Drink drink : availableDrinks) { System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); } System.out.print("|============================ \n"); do { System.out.println("Please enter selection: "); drinkSelection = sc.nextInt(); } while (drinkSelection < 1 || drinkSelection > (availableDrinks.length + 1)); return drinkSelection; } private void printCoinOptions() { System.out.println("Please insert coins: "); System.out.println("========== Coins Input ==========="); System.out.println("|Enter 'Q' for ten cents input |"); System.out.println("|Enter 'T' for twenty cents input|"); System.out.println("|Enter 'F' for fifty cents input |"); System.out.println("|Enter 'N' for a dollar input |"); System.out.println("=================================="); } private double processCoinAmount(char coin) { /* Note. The code below is just an enhanced way of writing the following: switch (coin) { case 'Q': case 'q': return 0.10; case 'T': case 't': return 0.20; case 'F': case 'f': return 0.50; case 'N': case 'n': return 1.00; } return 0.0; This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html */ return switch (coin) { case 'Q', 'q' -> 0.10; case 'T', 't' -> 0.20; case 'F', 'f' -> 0.50; case 'N', 'n' -> 1.00; default -> 0.0; }; } }