用Java语言实现:咖啡自动售货机。当用户把一元硬币投入咖啡自动售货机,就会得到一杯热咖啡。咖啡自动售货机一 共有三 种状态,分别是”有咖啡 ,无人投币”,”有咖啡 ,有人投币”和“无咖啡”状态。 使用状态模式模拟咖啡自动售货机。
时间: 2024-03-01 12:53:12 浏览: 95
好的,我了解了你的问题。以下是用Java语言实现咖啡自动售货机的代码:
首先,我们需要定义一个状态接口,里面包含三个方法:投币、退币、出售咖啡。
```
public interface State {
void insertCoin();
void ejectCoin();
void dispenseCoffee();
}
```
然后,我们需要实现三个状态类:有咖啡,无人投币状态、有咖啡,有人投币状态和无咖啡状态。
```
public class HasCoffeeNoCoinState implements State {
private CoffeeVendingMachine vendingMachine;
public HasCoffeeNoCoinState(CoffeeVendingMachine vendingMachine) {
this.vendingMachine = vendingMachine;
}
public void insertCoin() {
System.out.println("您已经投过币了!");
}
public void ejectCoin() {
System.out.println("退币成功!");
vendingMachine.setState(vendingMachine.getNoCoffeeState());
}
public void dispenseCoffee() {
System.out.println("正在为您提供一杯热咖啡!");
vendingMachine.setState(vendingMachine.getNoCoffeeState());
}
}
public class HasCoffeeHasCoinState implements State {
private CoffeeVendingMachine vendingMachine;
public HasCoffeeHasCoinState(CoffeeVendingMachine vendingMachine) {
this.vendingMachine = vendingMachine;
}
public void insertCoin() {
System.out.println("您已经投过币了!");
}
public void ejectCoin() {
System.out.println("退币成功!");
vendingMachine.setState(vendingMachine.getHasCoffeeNoCoinState());
}
public void dispenseCoffee() {
System.out.println("正在为您提供一杯热咖啡!");
vendingMachine.setState(vendingMachine.getNoCoffeeState());
}
}
public class NoCoffeeState implements State {
private CoffeeVendingMachine vendingMachine;
public NoCoffeeState(CoffeeVendingMachine vendingMachine) {
this.vendingMachine = vendingMachine;
}
public void insertCoin() {
System.out.println("正在准备咖啡,请稍等!");
vendingMachine.setState(vendingMachine.getHasCoffeeHasCoinState());
}
public void ejectCoin() {
System.out.println("您还没有投币!");
}
public void dispenseCoffee() {
System.out.println("咖啡已售罄!");
}
}
```
最后,我们需要定义一个咖啡自动售货机类,包含三种状态和当前状态。
```
public class CoffeeVendingMachine {
private State hasCoffeeNoCoinState;
private State hasCoffeeHasCoinState;
private State noCoffeeState;
private State currentState;
public CoffeeVendingMachine() {
hasCoffeeNoCoinState = new HasCoffeeNoCoinState(this);
hasCoffeeHasCoinState = new HasCoffeeHasCoinState(this);
noCoffeeState = new NoCoffeeState(this);
currentState = noCoffeeState;
}
public void setState(State state) {
this.currentState = state;
}
public State getHasCoffeeNoCoinState() {
return hasCoffeeNoCoinState;
}
public State getHasCoffeeHasCoinState() {
return hasCoffeeHasCoinState;
}
public State getNoCoffeeState() {
return noCoffeeState;
}
public void insertCoin() {
currentState.insertCoin();
}
public void ejectCoin() {
currentState.ejectCoin();
}
public void dispenseCoffee() {
currentState.dispenseCoffee();
}
}
```
现在,我们可以创建一个咖啡自动售货机对象,并进行投币、退币和出售咖啡的操作了。
```
public class Main {
public static void main(String[] args) {
CoffeeVendingMachine vendingMachine = new CoffeeVendingMachine();
vendingMachine.insertCoin();
vendingMachine.ejectCoin();
vendingMachine.insertCoin();
vendingMachine.dispenseCoffee();
}
}
```
输出结果如下:
```
正在准备咖啡,请稍等!
您还没有投币!
正在准备咖啡,请稍等!
正在为您提供一杯热咖啡!
```
这就是用Java语言实现咖啡自动售货机的代码。希望对你有所帮助!
阅读全文