package L_State.b;
/**
*还存在如下问题:
* 1:重复代码,如错误信息不会给用户看,可以把所有错误行为写的通用
* 2:dispense()即使没有25分,当转动曲柄后也会执行,可使用布尔值or异常处理
* 3:状态转换都放在状态类中了,可以放在糖果机中
* 4:实例GumballMachine过多后,要将状态实例移到静态实例变量中
*/
public class Main {
public static void main(String[] args) {
GumballMachine gumballMachine = new GumballMachine(5);
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
}
}
package L_State.b;
public class GumballMachine {
// 状态映射到类上
State soldOutState;
State soldState;
State noQuarterState;
State hasQuarterState;
State winnerState;
State state = soldOutState;
int count = 0;
public GumballMachine(int count) {
this.count = count;
soldOutState = new SoldOutState(this);
soldState = new SoldState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
winnerState = new WinnerState(this);
if (count > 0)
state = noQuarterState;
}
public void insertQuarter() {
state.insertQuarter();
}
public void ejectQuarter() {
state.ejectQuarter();
}
public void turnCrank() {
state.turnCrank();
state.dispense();
}
public void setState(State state) {
this.state = state;
}
public void releaseBall() {
System.out.println("出售一个糖果");
if (count != 0)
count--;
}
/**
* 补充糖果
*/
public void refill(int count) {
this.count = count;
state = noQuarterState;
}
// //
public State getSoldOutState() {
return soldOutState;
}
public State getSoldState() {
return soldState;
}
public State getNoQuarterState() {
return noQuarterState;
}
public State getHasQuarterState() {
return hasQuarterState;
}
public State getState() {
return state;
}
public int getCount() {
return count;
}
public State getWinnerState() {
return winnerState;
}
@Override
public String toString() {
return "GumballMachine [state=" + state + ", count=" + count + "]";
}
}
package L_State.b;
import java.util.Random;
public class HasQuarterState implements State {
// 增加随机数产生器,产生10%赢几率
Random randomWinner = new Random(System.currentTimeMillis());
GumballMachine gumballMachine;
public HasQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
public void insertQuarter() {
System.out.println("已经投入过25分");
}
public void ejectQuarter() {
System.out.println("退25分");
gumballMachine.setState(gumballMachine.getNoQuarterState());
}
public void turnCrank() {
System.out.println("转动曲柄");
int winner = randomWinner.nextInt(10);
if (winner == 0 && gumballMachine.getCount() > 1) {
gumballMachine.setState(gumballMachine.getWinnerState());
} else {
gumballMachine.setState(gumballMachine.getSoldState());
}
}
public void dispense() {
System.out.println("没有糖果分配");
}
}
package L_State.b;
public class NoQuarterState implements State {
GumballMachine gumballMachine;
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
public void insertQuarter() {
System.out.println("投入25分");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
public void ejectQuarter() {
System.out.println("你需要投入25分");
}
public void turnCrank() {
System.out.println("没钱,无法售出");
}
public void dispense() {
System.out.println("你需要投入钱");
}
}
package L_State.b;
public class SoldOutState implements State {
GumballMachine gumballMachine;
public SoldOutState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
public void insertQuarter() {
System.out.println("糖果已售空");
}
public void ejectQuarter() {
System.out.println("糖果已售空");
}
public void turnCrank() {
System.out.println("糖果已售空");
}
public void dispense() {
System.out.println("糖果已售空");
}
}
package L_State.b;
public class SoldState implements State {
GumballMachine gumballMachine;
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
public void insertQuarter() {
System.out.println("请等待正在出糖果");
}
public void ejectQuarter() {
System.out.println("对不起,正在转动曲柄");
}
public void turnCrank() {
System.out.println("不能转动2次曲柄");
}
public void dispense() {
gumballMachine.releaseBall();
if (gumballMachine.getCount() > 0) {
gumballMachine.setState(gumballMachine.getNoQuarterState());
} else {
System.out.println("糖果售空");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}
package L_State.b;
public interface State {
void insertQuarter();
void ejectQuarter();
void turnCrank();
void dispense();
}
package L_State.b;
/**
* 10次抽中一次的游戏
*/
public class WinnerState implements State {
GumballMachine gumballMachine;
public WinnerState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
public void insertQuarter() {
System.out.println("请等待正在出糖果");
}
public void ejectQuarter() {
System.out.println("对不起,正在转动曲柄");
}
public void turnCrank() {
System.out.println("不能转动2次曲柄");
}
public void dispense() {
System.out.println("恭喜你获得2个糖果");
gumballMachine.releaseBall();
if (gumballMachine.getCount() == 0) {
gumballMachine.setState(gumballMachine.getSoldOutState());
} else {
gumballMachine.releaseBall();
if (gumballMachine.getCount() > 0) {
gumballMachine.setState(gumballMachine.getNoQuarterState());
} else {
System.out.println("糖果已售空");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}
}