如下是一份Java全自動咖啡機代碼:

```import java.util.Scanner;
public class AutomaticCoffeeMachine { private int water; private int milk; private int coffeeBeans; private int disposableCups; private int money;
public AutomaticCoffeeMachine(int water, int milk, int coffeeBeans, int disposableCups, int money) { this.water = water; this.milk = milk; this.coffeeBeans = coffeeBeans; this.disposableCups = disposableCups; this.money = money; }
public void getStatus() { System.out.println("The coffee machine has:"); System.out.println(water + " of water"); System.out.println(milk + " of milk"); System.out.println(coffeeBeans + " of coffee beans"); System.out.println(disposableCups + " of disposable cups"); System.out.println(money + " of money"); }
public void makeCoffee(int waterNeeded, int milkNeeded, int coffeeBeansNeeded, int cost) { if (water < waterNeeded) { System.out.println("Sorry, not enough water!"); } else if (milk < milkNeeded) { System.out.println("Sorry, not enough milk!"); } else if (coffeeBeans < coffeeBeansNeeded) { System.out.println("Sorry, not enough coffee beans!"); } else if (disposableCups == 0) { System.out.println("Sorry, not enough disposable cups!"); } else { System.out.println("I have enough resources, making you a coffee!"); water -= waterNeeded; milk -= milkNeeded; coffeeBeans -= coffeeBeansNeeded; disposableCups -= 1; money += cost; } }
public void buyCoffee() { Scanner scanner = new Scanner(System.in); System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:"); int choice = scanner.nextInt(); switch (choice) { case 1: makeCoffee(250, 0, 16, 4); break; case 2: makeCoffee(350, 75, 20, 7); break; case 3: makeCoffee(200, 100, 12, 6); break; default: System.out.println("Invalid choice!"); } }
public void fillSupplies() { Scanner scanner = new Scanner(System.in); System.out.println("How many ml of water do you want to add?"); int waterToAdd = scanner.nextInt(); System.out.println("How many ml of milk do you want to add?"); int milkToAdd = scanner.nextInt(); System.out.println("How many grams of coffee beans do you want to add?"); int coffeeBeansToAdd = scanner.nextInt(); System.out.println("How many disposable cups do you want to add?"); int cupsToAdd = scanner.nextInt(); water += waterToAdd; milk += milkToAdd; coffeeBeans += coffeeBeansToAdd; disposableCups += cupsToAdd; }
public void takeMoney() { System.out.println("I gave you $" + money); money = 0; }
public static void main(String[] args) { AutomaticCoffeeMachine coffeeMachine = new AutomaticCoffeeMachine(400, 540, 120, 9, 550);
Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Write action (buy, fill, take, remaining, exit):"); String action = scanner.nextLine(); switch (action) { case "buy": coffeeMachine.buyCoffee(); break; case "fill": coffeeMachine.fillSupplies(); break; case "take": coffeeMachine.takeMoney(); break; case "remaining": coffeeMachine.getStatus(); break; case "exit": return; default: System.out.println("Invalid action!"); } } }}```
該代碼實現了一個具有以下特點的全自動咖啡機:
1. 初始狀態下水、牛奶、咖啡豆、杯子、錢等資源的數量已經被預設。2. 程序提供了以下功能選項:購買咖啡、補充咖啡機所需的水、牛奶、咖啡豆和杯子、取走已有的錢和查詢咖啡機現有的狀態。3. 購買咖啡時,程序會檢查咖啡機中現有的資源是否足夠制作所需的咖啡。如果資源不足,程序會提示。4. 購買咖啡成功后,咖啡機會扣除所需的資源數量并增加所得的錢數。


























