如下是一份Java全自動(dòng)咖啡機(jī)代碼:

```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!"); } } }}```
該代碼實(shí)現(xiàn)了一個(gè)具有以下特點(diǎn)的全自動(dòng)咖啡機(jī):
1. 初始狀態(tài)下水、牛奶、咖啡豆、杯子、錢(qián)等資源的數(shù)量已經(jīng)被預(yù)設(shè)。2. 程序提供了以下功能選項(xiàng):購(gòu)買(mǎi)咖啡、補(bǔ)充咖啡機(jī)所需的水、牛奶、咖啡豆和杯子、取走已有的錢(qián)和查詢(xún)咖啡機(jī)現(xiàn)有的狀態(tài)。3. 購(gòu)買(mǎi)咖啡時(shí),程序會(huì)檢查咖啡機(jī)中現(xiàn)有的資源是否足夠制作所需的咖啡。如果資源不足,程序會(huì)提示。4. 購(gòu)買(mǎi)咖啡成功后,咖啡機(jī)會(huì)扣除所需的資源數(shù)量并增加所得的錢(qián)數(shù)。
(完)























