---------------------------------------------public class SoldOutState implements State {GumballMachine gumballMachine;public SoldOutState(GumballMachine gumballMachine) {this.gumballMachine = gumballMachine;}public void insertQuarter(){System.out.println("Sorry,the gumballs are sold out.");}public void ejectQuarter(){System.out.println("Sorry,you haven't insert a quarter.");}public void turnCrank(){System.out.println("Sorry,you haven't insert a quarter");}public void dispense(){//do nothing}}---------------------------------------------
---------------------------------------------public void refill(int count){this.count = count;if(count>0){this.state = noQuarterState;}}---------------------------------------------
/*** 钢笔** @author zangweiren**/public class FountainPen implements FountainPenState {FountainPenState fullState;FountainPenState halfFullState;FountainPenState emptyState;FountainPenState state;int inkLevel = 0;public FountainPen() {fullState = new FullState(this);halfFullState = new HalfFullState(this);emptyState = new EmptyState(this);state = emptyState;}@Overridepublic void empty() {state.empty();}@Overridepublic void fill() {state.fill();}@Overridepublic void write() {state.write();}void setState(FountainPenState state) {this.state = state;}FountainPenState getFullState() {return fullState;}FountainPenState getHalfFullState() {return halfFullState;}FountainPenState getEmptyState() {return emptyState;}}/*** 钢笔状态接口** @author zangweiren**/public interface FountainPenState {void fill();void write();void empty();}/*** 满** @author zangweiren**/public class FullState implements FountainPenState {FountainPen pen;public FullState(FountainPen pen) {this.pen = pen;}@Overridepublic void empty() {pen.inkLevel = 0;pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty now.");}@Overridepublic void fill() {System.out.println("Sorry,the fountain pen is already full.");}@Overridepublic void write() {pen.inkLevel = pen.inkLevel - 1;if (pen.inkLevel == 0) {pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty.");} else {pen.setState(pen.getHalfFullState());System.out.println("The fountain pen is half full.");}}}/*** 半满** @author zangweiren**/public class HalfFullState implements FountainPenState {FountainPen pen;public HalfFullState(FountainPen pen) {this.pen = pen;}@Overridepublic void empty() {pen.inkLevel = 0;pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty now.");}@Overridepublic void fill() {pen.inkLevel = 100;pen.setState(pen.getFullState());System.out.println("Congratulations,the fountain pen is full now.");}@Overridepublic void write() {pen.inkLevel = pen.inkLevel - 1;if (pen.inkLevel == 0) {pen.setState(pen.getEmptyState());System.out.println("The fountain pen is empty.");} else {System.out.println("The fountain pen is half full.");}}}/*** 空** @author zangweiren**/public class EmptyState implements FountainPenState {FountainPen pen;public EmptyState(FountainPen pen) {this.pen = pen;}@Overridepublic void empty() {System.out.println("Sorry,the fountain pen is already empty.");}@Overridepublic void fill() {pen.inkLevel = 100;pen.setState(pen.getFullState());System.out.println("Congratulations,the fountain pen is full now.");}@Overridepublic void write() {System.out.println("Sorry,the fountain pen is empty.");}}
public class TestFountainPen {public static void main(String[] args) {FountainPen pen = new FountainPen();pen.fill();int count = 5;while (count > 0) {pen.write();count--;}pen.empty();pen.write();pen.fill();pen.write();pen.empty();}}
联系客服