Skip to content

Instantly share code, notes, and snippets.

@DoRightt
Last active June 16, 2020 07:16
Show Gist options
  • Save DoRightt/9f279530a64d45655882d08ffd68ea83 to your computer and use it in GitHub Desktop.
Save DoRightt/9f279530a64d45655882d08ffd68ea83 to your computer and use it in GitHub Desktop.
interface State {
insertQuarter(): any;
ejectQuarter(): any;
turnCrank(): any;
dispense(): any;
}
class SoldState implements State {
gumBallMachine: GumBallMachine;
constructor(gumBallMachine: GumBallMachine) {
this.gumBallMachine = gumBallMachine;
}
insertQuarter() {
console.log("Please wait, we are already giving you a ball");
}
ejectQuarter() {
console.log("Sorry, but you already turned the crank");
}
turnCrank() {
console.log("Turning crank twice, doesn't get you another gumball");
}
dispense() {
this.gumBallMachine.releaseBall();
if (this.gumBallMachine.getCount() > 0) {
this.gumBallMachine.setState(this.gumBallMachine.getNoQuarterState());
} else {
console.log("Out of gumballs");
this.gumBallMachine.setState(this.gumBallMachine.getSoldOutState());
}
}
}
class SoldOutState implements State {
gumBallMachine: GumBallMachine;
constructor(gumBallMachine: GumBallMachine) {
this.gumBallMachine = gumBallMachine;
}
insertQuarter() {
console.log("You can't insert quarter, gumballs sold out");
}
ejectQuarter() {
console.log("You have not inserted a quarter");
}
turnCrank() {
console.log("You can't turn the crank, gumballs sold out");
}
dispense() {
console.log("Gumballs is sold out");
}
}
class NoQuarterState implements State {
gumBallMachine: GumBallMachine;
constructor(gumBallMachine: GumBallMachine) {
this.gumBallMachine = gumBallMachine;
}
insertQuarter() {
console.log("You inserted a quarter");
this.gumBallMachine.setState(this.gumBallMachine.getHasQuarterState());
}
ejectQuarter() {
console.log("You have not inserted a quarter");
}
turnCrank() {
console.log("You turned, but there is no quarter");
}
dispense() {
console.log("You need to pay first");
}
}
class HasQuarterState implements State {
randomWinner: number = +Math.floor(Math.random() * 10 + 1);
gumBallMachine: GumBallMachine;
constructor(gumBallMachine: GumBallMachine) {
this.gumBallMachine = gumBallMachine;
}
insertQuarter() {
console.log("You cant insert another quarter");
}
ejectQuarter() {
console.log("Quarter returned");
this.gumBallMachine.setState(this.gumBallMachine.getNoQuarterState());
}
turnCrank() {
console.log("You turned...");
if (this.randomWinner === 1 && this.gumBallMachine.getCount() > 1) {
this.gumBallMachine.setState(this.gumBallMachine.getWinnerState());
} else {
this.gumBallMachine.setState(this.gumBallMachine.getSoldState());
}
}
dispense() {
console.log("No gumball dispensed");
}
}
class WinnerState implements State {
gumBallMachine: GumBallMachine;
constructor(gumBallMachine: GumBallMachine) {
this.gumBallMachine = gumBallMachine;
}
insertQuarter() {
console.log("Please wait, we are already giving you a ball");
}
ejectQuarter() {
console.log("Sorry, but you already turned the crank");
}
turnCrank() {
console.log("Turning crank twice, doesn't get you another gumball");
}
dispense() {
console.log("You are a winner. You get two gumballs");
this.gumBallMachine.releaseBall();
if (this.gumBallMachine.getCount() === 0) {
console.log("Out of gumballs");
this.gumBallMachine.setState(this.gumBallMachine.getSoldOutState());
} else {
if (this.gumBallMachine.getCount() > 0) {
this.gumBallMachine.setState(this.gumBallMachine.getNoQuarterState());
} else {
console.log("Out of gumballs");
this.gumBallMachine.setState(this.gumBallMachine.getSoldOutState());
}
}
}
}
class GumBallMachine {
soldOutState: State;
noQuarterState: State;
hasQuarterState: State;
soldState: State;
winnerState: State;
private state: State = this.soldOutState;
private count: number = 0;
constructor(numberGumballs: number) {
this.soldOutState = new SoldOutState(this);
this.noQuarterState = new NoQuarterState(this);
this.hasQuarterState = new HasQuarterState(this);
this.soldState = new SoldState(this);
this.winnerState = new WinnerState(this);
this.count = numberGumballs;
if (this.count > 0) {
this.state = this.noQuarterState;
}
}
insertQuarter() {
this.state.insertQuarter();
}
ejectQuarter() {
this.state.ejectQuarter();
}
turnCrank() {
this.state.turnCrank();
this.state.dispense();
}
refill(count: number) {
this.count += count;
if (this.count > 0) {
this.state = this.getNoQuarterState();
}
}
setState(state: State) {
this.state = state;
}
getHasQuarterState() {
return this.hasQuarterState;
}
getNoQuarterState() {
return this.noQuarterState;
}
getSoldOutState() {
return this.soldOutState;
}
getSoldState() {
return this.soldState;
}
getWinnerState() {
return this.winnerState;
}
getCount() {
return this.count;
}
releaseBall() {
console.log("A gumbull comes rolling out the slot...");
if (this.count !== 0) {
this.count -= 1;
}
}
}
const testMachine = new GumBallMachine(2);
console.log("-----------------");
testMachine.insertQuarter();
testMachine.insertQuarter();
testMachine.turnCrank();
console.log("-----------------");
testMachine.insertQuarter();
testMachine.ejectQuarter();
testMachine.turnCrank();
console.log("-----------------");
testMachine.insertQuarter();
testMachine.turnCrank();
console.log("-----------------");
testMachine.refill(10);
testMachine.insertQuarter();
testMachine.turnCrank();
console.log(testMachine.getCount());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment