104 lines
3.3 KiB
Java
104 lines
3.3 KiB
Java
import java.util.Scanner;
|
|
|
|
public class VendingMachine {
|
|
|
|
public VendingMachine() {}
|
|
|
|
public double selectDrink() {
|
|
int drinkSelection;
|
|
double drinkCost = 0;
|
|
|
|
drinkSelection = presentDrinkMenu();
|
|
|
|
if (drinkSelection == 1) drinkCost = 3.00;
|
|
else if (drinkSelection == 2) drinkCost = 1.00;
|
|
else if (drinkSelection == 3) drinkCost = 5.00;
|
|
return drinkCost;
|
|
}
|
|
|
|
public double insertCoins(double drinkCost) {
|
|
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 < drinkCost);
|
|
return amount;
|
|
}
|
|
|
|
public void checkChange(double amount, double drinkCost)
|
|
{
|
|
double change = 0.0;
|
|
if (amount > drinkCost)
|
|
{
|
|
change = amount - drinkCost;
|
|
System.out.printf("Change $ %.2f \n", change);
|
|
}
|
|
}
|
|
|
|
public void printReceipt()
|
|
{
|
|
System.out.println("Please collect your drink");
|
|
System.out.println("Thank you!!");
|
|
}
|
|
|
|
private int presentDrinkMenu() {
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
int drinkSelection;
|
|
|
|
System.out.println("====== Vending Machine ======");
|
|
System.out.println("|1. Buy Beer ($3.00) |");
|
|
System.out.println("|2. Buy Coke ($1.00) |");
|
|
System.out.println("|3. Buy Green Tea ($5.00) |");
|
|
System.out.println("|============================");
|
|
|
|
do {
|
|
System.out.println("Please enter selection: ");
|
|
drinkSelection = sc.nextInt();
|
|
} while (drinkSelection < 1 || drinkSelection > 3);
|
|
|
|
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;
|
|
};
|
|
}
|
|
} |