Hallo,
Ich mache gerade eine Übung in der ich eine Kaffeemaschine simulieren, soll. Das Menu soll in einer Endlosschleife laufen und man soll Getränke kaufen können(solange genug Zutaten in der Maschine sind). Das funktionierte auch alles perfekt.
Nun musste ich die
Da jetzt der befehl exit zum Abbruch des kaufs hinzukommen sollte, musste ich die Methode abändern. der Rückgabetyp von
Beim ersten Schleifen durchlauf klappt auch alles, aber nachdem das Getränk gekauft wurde, wird eine Exception ausgelöst.
Die Exception wird zwar durch
Ich mache gerade eine Übung in der ich eine Kaffeemaschine simulieren, soll. Das Menu soll in einer Endlosschleife laufen und man soll Getränke kaufen können(solange genug Zutaten in der Maschine sind). Das funktionierte auch alles perfekt.
Nun musste ich die
actionBuy()
funktion umschreiben, da ein neues Parameter zur Getränkeauswahl hinzukam. Vorher wurde der Benutzer nur Aufgefordert eine Zahl von 1 bis 3 einzugeben. Diese Zahl wurde zurückgegeben und ich habe die Methode buy(int selection)
damit aufgerufen. Diese hat dann entsprechend des übergebenen Integers die Methode für das Getränk gestartet. Da jetzt der befehl exit zum Abbruch des kaufs hinzukommen sollte, musste ich die Methode abändern. der Rückgabetyp von
actionBuy()
ist nun String und dementsprechend erwartet buy()
nun einen String als Parameter.Beim ersten Schleifen durchlauf klappt auch alles, aber nachdem das Getränk gekauft wurde, wird eine Exception ausgelöst.
Die Exception wird zwar durch
startEngine()
ausgelöst, aber vor der dem Umschreiben der actionBuy()
und buy()
wurde Sie noch nicht ausgelöst. Ich denke es liegt darann, dass der Scanner nichts zum lesen hat. Aber wie ich das lösen kann ist mir nicht bewusst. Ich dachte der Scanner "wartet" auf eine eingabe. So war es bisher zumindest...
Java:
package machine;
import java.util.Scanner;
public class CoffeeMachine {
static int water = 400;
static int milk = 540;
static int coffeeBeans = 120;
static int money = 550;
static int cups = 9;
public static final Drink ESPRESSO = new Drink("Espresso", 250, 0, 16, 4);
public static final Drink LATTE = new Drink("Latte", 350, 75, 30, 7);
public static final Drink CAPPUCCINO = new Drink("Cappuccino", 200, 100, 12, 6);
public static class Drink {
String name;
int water, milk, coffeeBeans, money, cups;
public Drink(String name, int water, int milk, int coffeeBeans, int money) {
this.name = name;
this.water = water;
this.milk = milk;
this.coffeeBeans = coffeeBeans;
this.money = money;
this.cups = 1;
}
}
public static void main(String[] args) {
startEngine();
}
private static void startEngine() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Write action (buy, fill, take, remaining, exit)");
String action = scanner.next();
if (action.equalsIgnoreCase("buy")) {
buy(actionBuy());
} else if (action.equalsIgnoreCase("fill")) {
actionFill();
} else if (action.equalsIgnoreCase("take")) {
actionTake();
} else if (action.equalsIgnoreCase("remaining")) {
showInfo();
} else if (action.equalsIgnoreCase("exit")) {
break;
}
}
}
private static void showInfo() {
System.out.println();
System.out.printf("The coffee machine has:%n" +
"%d of water%n" +
"%d of milk%n" +
"%d of coffee beans%n" +
"%d of disposable cups%n" +
"$%d of money%n",
water, milk, coffeeBeans, cups, money
);
}
private static void actionTake() {
System.out.printf("I gave you $%d/n", money);
money = 0;
}
private static void actionFill() {
Scanner scanner = new Scanner(System.in);
System.out.println("Write how many ml of water do you want to add: ");
water += scanner.nextInt();
System.out.println("Write how many ml of milk do you want to add: ");
milk += scanner.nextInt();
System.out.println("Write how many grams of coffee beans do you want to add: ");
coffeeBeans += scanner.nextInt();
System.out.println("Write how many disposable cups of coffee do you want to add: ");
cups += scanner.nextInt();
scanner.close();
}
private static String actionBuy() {
System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ");
Scanner scanner = new Scanner(System.in);
String selection = scanner.next();
scanner.close();
return selection;
}
private static void buy(String selection) {
if (selection.equalsIgnoreCase("1")) {
buyEspresso();
} else if (selection.equalsIgnoreCase("2")) {
buyLatte();
} else if(selection.equalsIgnoreCase("3")){
buyCappuccino();
}else {
return;
}
}
private static void buyEspresso() {
if (water <= ESPRESSO.water) {
System.out.println("Sorry not enough water");
} else if (coffeeBeans <= ESPRESSO.coffeeBeans) {
System.out.println("Sorry not enough coffee Beans");
} else if (cups <= 1) {
System.out.println("Sorry not enough cups");
} else {
System.out.println("I have enough resources, making you a coffee!");
water -= ESPRESSO.water;
coffeeBeans -= ESPRESSO.coffeeBeans;
cups--;
money += ESPRESSO.money;
}
}
private static void buyLatte() {
if (water <= LATTE.water) {
System.out.println("Sorry not enough water");
} else if (coffeeBeans <= LATTE.coffeeBeans) {
System.out.println("Sorry not enough coffee Beans");
} else if (cups <= 1) {
System.out.println("Sorry not enough cups");
} else if (milk <= LATTE.milk) {
System.out.println("Sorry not enough milk");
} else {
System.out.println("I have enough resources, making you a coffee!");
water -= LATTE.water;
coffeeBeans -= LATTE.coffeeBeans;
cups--;
money += LATTE.money;
milk -= LATTE.milk;
}
}
private static void buyCappuccino() {
if (water <= CAPPUCCINO.water) {
System.out.println("Sorry not enough water");
} else if (coffeeBeans <= CAPPUCCINO.coffeeBeans) {
System.out.println("Sorry not enough coffee Beans");
} else if (cups <= 1) {
System.out.println("Sorry not enough cups");
} else if (milk <= CAPPUCCINO.milk) {
System.out.println("Sorry not enough milk");
} else {
System.out.println("I have enough resources, making you a coffee!");
water -= CAPPUCCINO.water;
coffeeBeans -= CAPPUCCINO.coffeeBeans;
cups--;
money += CAPPUCCINO.money;
milk -= CAPPUCCINO.milk;
}
}
}
Code:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at machine.CoffeeMachine.startEngine(CoffeeMachine.java:45)
at machine.CoffeeMachine.main(CoffeeMachine.java:36)