I'm having a slight problem implementing pass-by-value in a project. In case of buyOption
, fillOption
and takeOption
.
I don't know how to make changed value apply to whole program and not only to methods. Any tips/solution/feedback would be highly appreciated.
public class CoffeeMachine {
static int water = 400;
static int milk = 540;
static int beans = 120;
static int cups = 9;
static int money = 550;
static boolean exit = false;
public static void main(String[] args) {
do {
printMenu(water, milk, beans, cups, money);
} while (!exit);
}
private static void printMenu(int water, int milk, int beans, int cups, int money) {
Scanner scanner = new Scanner(System.in);
System.out.println("Write action (buy, fill, take, remaining, exit): ");
String input = scanner.nextLine();
switch (input) {
case "buy":
buyOption(water, milk, beans, cups, money);
break;
case "fill":
fillOption(water, milk, beans, cups, money);
break;
case "take":
takeOption(water, milk, beans, cups, money);
break;
case "remaining":
contentOption(water, milk, beans, cups, money);
break;
case "exit":
exit = true;
System.exit(0);
break;
default:
System.out.println("Wrong option!");
break;
}
}
private static void contentOption(int water, int milk, int beans, int cups, int money) {
System.out.println("The coffee machine has: ");
System.out.println(water + " of water");
System.out.println(milk + " of milk");
System.out.println(beans + " of coffee beans");
System.out.println(cups + " of disposable cups");
System.out.println(money + " of money");
}
private static void buyOption(int water, int milk, int beans, int cups, int money) {
Scanner scanner = new Scanner(System.in);
String temp = null;
System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ");
String input = scanner.nextLine();
switch (input) {
case "1":
if (water >= 250 && beans >= 16 && cups >= 1) {
water -= 250;
beans -= 16;
cups -= 1;
money += 4;
System.out.println("I have enough resources, making you a coffee!");
} else {
if (water < 250) {
temp = "water";
} else if (beans < 16) {
temp = "beans";
} else if (cups < 1) {
temp = "cups";
}
System.out.println("Sorry, not enough " + temp + "!");
}
break;
case "2":
if (water >= 350 && milk >= 75 && beans >= 20 && cups >= 1) {
water -= 350;
milk -= 75;
beans -= 20;
cups -= 1;
money += 7;
System.out.println("I have enough resources, making you a coffee!");
} else {
if (water < 350) {
temp = "water";
} else if (milk < 75) {
temp = "milk";
} else if (beans < 20) {
temp = "beans";
} else if (cups < 1) {
temp = "cups";
}
System.out.println("Sorry, not enough " + temp + "!");
}
break;
case "3":
if (water >= 200 && milk >= 100 && beans >= 12 && cups >= 1) {
water -= 200;
milk -= 100;
beans -= 12;
cups -= 1;
money += 6;
System.out.println("I have enough resources, making you a coffee!");
} else {
if (water < 200) {
temp = "water";
} else if (milk < 100) {
temp = "milk";
} else if (beans < 12) {
temp = "beans";
} else if (cups < 1) {
temp = "cups";
}
System.out.println("Sorry, not enough " + temp + "!");
}
break;
case "back":
break;
default:
System.out.println("Wrong option!");
}
}
private static void fillOption(int water, int milk, int beans, int cups, int money) {
Scanner scanner = new Scanner(System.in);
System.out.println("Write how many ml of water do you want to add: ");
water = water + scanner.nextInt();
System.out.println("Write how many ml of milk do you want to add: ");
milk = milk + scanner.nextInt();
System.out.println("Write how many grams of coffee beans do you want to add: ");
beans = beans + scanner.nextInt();
System.out.println("Write how many disposable cups of coffee do you want to add: ");
cups = cups + scanner.nextInt();
}
private static void takeOption(int water, int milk, int beans, int cups, int money) {
System.out.println("I gave you " + money);
money = 0;
}
}