There are surely ways to write the following code in a more practical way.But I really want to understand why it is printing the line "Write action (buy, fill, take, remaining, exit):" two times after an action is preformed. It makes no sense to me after the output a user input is asked, so how can it be that it is printed twice? This happens after the action "buy" and "fill" but not with "take" && "remaining". What am I missing? (Just run the code and you will see what I mean!)
import java.util.Scanner;
public class Main {
private static void printMachineState(int water, int milk, int beans, int cups, int money) {
System.out.printf("The coffee machine has:\n%d ml of water\n%d ml of milk\n%d g of coffee beans\n%d disposable cups\n$%d of money\n", water, milk, beans, cups, money);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int water = 400;
int milk = 540;
int beans = 120;
int cups = 9;
int money = 550;
int coffeeEnum;
boolean exitFlag = false;
while (!exitFlag) {
System.out.println("Write action (buy, fill, take, remaining, exit):");
String input = sc.nextLine();
switch (input) {
case "buy":
System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 -cappuccino");
coffeeEnum = sc.nextInt();
if (coffeeEnum == 1) {
if (water < 250) {
System.out.println("Sorry, not enough water!");
break;
}
if (beans < 16) {
System.out.println("Sorry, not enough beans!");
break;
}
if (cups == 0) {
System.out.println("Sorry, not enough cups!");
break;
}
water -= 250;
beans -= 16;
money += 4;
cups--;
System.out.println("I have enough resources, making you a coffee!");
break;
}
if (coffeeEnum == 2) {
if (water < 350) {
System.out.println("Sorry, not enough water!");
break;
}
if (milk < 75) {
System.out.println("Sorry, not enough milk!");
break;
}
if (beans < 20) {
System.out.println("Sorry, not enough beans!");
break;
}
if (cups <= 0) {
System.out.println("Sorry, not enough cups!");
break;
}
water -= 350;
milk -= 75;
beans -= 20;
money += 7;
cups--;
System.out.println("I have enough resources, making you a coffee!");
break;
}
if (coffeeEnum == 3) {
if (water < 200) {
System.out.println("Sorry, not enough water!");
break;
}
if (milk < 100) {
System.out.println("Sorry, not enough milk!");
break;
}
if (beans < 12) {
System.out.println("Sorry, not enough beans!");
break;
}
if (cups == 0) {
System.out.println("Sorry, not enough cups!");
break;
}
water -= 200;
milk -= 100;
beans -= 12;
money += 6;
cups--;
System.out.println("I have enough resources, making you a coffee!");
break;
}
case "fill":
System.out.println("Write how many ml of water you want to add:");
water += sc.nextInt();
System.out.println("Write how many ml of milk you want to add:");
milk += sc.nextInt();
System.out.println("Write how many grams of coffee beans you want to add:");
beans += sc.nextInt();
System.out.println("Write how many disposable cups of coffee you want to add:");
cups += sc.nextInt();
printMachineState(water, milk, beans, cups, money);
break;
case "take":
System.out.printf("I gave you %d\n", money);
money = 0;
break;
case "remaining":
printMachineState(water, milk, beans, cups, money);
break;
case "exit":
exitFlag = true;
break;
default:
break;
}
}
}
}
The scanner method nextInt()
only receives the number, but not the newline that is fed into the scanner after that. So the time the nextLine()
gets called it gets the newline and jumps to the default switch case, starting the while loops over and printing the line again.
An easy and quick fix was to move the instantiation of scanner sc
into the while loop so that every time it loops, a new "clean" empty scanner is made.