I am new in programming. Studying from hyperskills.org. When scanning fill() method, I think program executes an infinite loop. I couldn't understand the debugger module of Intellij IDEA edu.
package machine;
import java.util.Scanner;
public class CoffeeMachine {
public static Scanner scanner = new Scanner(System.in);
//Initial Amounts
private static int waterAmount = 400;
private static int milkAmount = 540;
private static int beansAmount = 120;
private static int disposableCupAmount = 9;
private static int moneyAmount = 550;
//Details of Coffee Types: 0 - espresso, 1 - latte, 2 - cappuccino
private static int coffeeTypeTemp;
final private static int[][] coffeeDetails = {
{0, 250, 0, 16, 4},
{1, 350, 75, 20, 7},
{2, 200, 100, 12, 6}
};
public static void main(String[] args) {
System.out.println("Write action (buy, fill, take, remaining, exit):");
String action = scanner.nextLine();
while (!action.equals("exit")) {
switch (action) {
case "buy":
buy();
System.out.println("Write action (buy, fill, take, remaining, exit):");
action = scanner.nextLine();
break;
case "fill":
fill();
System.out.println("Write action (buy, fill, take, remaining, exit):");
action = scanner.nextLine();
break;
case "take":
take();
System.out.println("Write action (buy, fill, take, remaining, exit):");
action = scanner.nextLine();
break;
case "remaining":
amountOfGoods();
System.out.println("Write action (buy, fill, take, remaining, exit):");
action = scanner.nextLine();
break;
default:
break;
}
}
}
public static void buy() {
System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:");
String coffeeType = scanner.nextLine();
// Initial amount changes according to coffee type
switch (coffeeType) {
case "1":
coffeeTypeTemp = 0;
isEnoughToMakeCoffee(coffeeTypeTemp);
break;
case "2":
coffeeTypeTemp = 1;
isEnoughToMakeCoffee(coffeeTypeTemp);
break;
case "3":
coffeeTypeTemp = 2;
isEnoughToMakeCoffee(coffeeTypeTemp);
break;
default:
break;
}
}
public static void fill() {
System.out.println("Write how many ml of water do you want to add:");
int waterFill = scanner.nextInt();
waterAmount += waterFill;
System.out.println("Write how many ml of milk do you want to add:");
int milkFill = scanner.nextInt();
milkAmount += milkFill;
System.out.println("Write how many grams of coffee beans do you want to add:");
int beansFill = scanner.nextInt();
beansAmount += beansFill;
System.out.println("Write how many disposable cups of coffee do you want to add:");
int disposableCupFill = scanner.nextInt();
disposableCupAmount += disposableCupFill;
}
public static void take() {
System.out.println("I gave you $" + moneyAmount);
moneyAmount = 0;
}
public static void amountOfGoods() {
System.out.println("The coffee machine has:");
System.out.println(waterAmount + " of water");
System.out.println(milkAmount + " of milk");
System.out.println(beansAmount + " of coffee beans");
System.out.println(disposableCupAmount + " of disposable cups");
System.out.println(moneyAmount + " of money");
}
public static void isEnoughToMakeCoffee(int coffeeTypeTemp) {
if ((waterAmount - coffeeDetails[coffeeTypeTemp][1]) < 0) {
System.out.println("Sorry, not enough water!");
} else if((milkAmount - coffeeDetails[coffeeTypeTemp][2]) < 0) {
System.out.println("Sorry, not enough milk!");
} else if((beansAmount - coffeeDetails[coffeeTypeTemp][3]) < 0) {
System.out.println("Sorry, not enough coffee beans!");
} else if((disposableCupAmount) < 0) {
System.out.println("Sorry, not enough disposable cups!");
} else {
System.out.println("I have enough resources, making you a coffee!");
remaindersOfGoods(coffeeTypeTemp);
}
}
public static void remaindersOfGoods(int coffeeTypeTemp) {
waterAmount -= coffeeDetails[coffeeTypeTemp][1];
milkAmount -= coffeeDetails[coffeeTypeTemp][2];
beansAmount -= coffeeDetails[coffeeTypeTemp][3];
disposableCupAmount--;
moneyAmount += coffeeDetails[coffeeTypeTemp][4];
}
}