-
Notifications
You must be signed in to change notification settings - Fork 0
first draft state #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KKSmith125
wants to merge
1
commit into
main
Choose a base branch
from
StatePatternAssignment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| public class GumballMachine { | ||
| private GumballMachineState soldOutState; | ||
| private GumballMachineState noQuarterState; | ||
| private GumballMachineState hasQuarterState; | ||
| private GumballMachineState soldState; | ||
| private GumballMachineState winnerState; | ||
|
|
||
| private int count = 0; | ||
| private GumballMachineState currentState; | ||
|
|
||
| public GumballMachine(int numberGumballs) { | ||
| soldOutState = new SoldOutState(this); | ||
| noQuarterState = new NoQuarterState(this); | ||
| hasQuarterState = new HasQuarterState(this); | ||
| soldState = new SoldState(this); | ||
| winnerState = new WinnerState(this); | ||
|
|
||
| this.count = numberGumballs; | ||
|
|
||
| currentState = (numberGumballs > 0) ? noQuarterState : soldOutState; | ||
| } | ||
|
|
||
| public void setState(GumballMachineState currentState) { | ||
| this.currentState = currentState; | ||
| } | ||
|
|
||
| public void insertQuarter() { | ||
| currentState.insertQuarter(); | ||
| } | ||
|
|
||
| public void ejectQuarter() { | ||
| currentState.ejectQuarter(); | ||
| } | ||
|
|
||
| public void turnCrank() { | ||
| currentState.turnCrank(); | ||
| currentState.dispense(); | ||
| } | ||
|
|
||
| public void releaseBall() { | ||
| System.out.println("A gumball comes rolling out the slot..."); | ||
| if (count > 0) { | ||
| count = count - 1; | ||
| } | ||
| } | ||
|
|
||
| public int getCount() { | ||
| return count; | ||
| } | ||
|
|
||
| public GumballMachineState getNoQuarterState() { | ||
| return noQuarterState; | ||
| } | ||
|
|
||
| public GumballMachineState getHasQuarterState() { | ||
| return hasQuarterState; | ||
| } | ||
|
|
||
| public GumballMachineState getSoldOutState() { | ||
| return soldOutState; | ||
| } | ||
|
|
||
| public GumballMachineState getSoldState() { | ||
| return soldState; | ||
| } | ||
|
|
||
| public GumballMachineState getWinnerState() { | ||
| return winnerState; | ||
| } | ||
|
|
||
| public void refill(int count) { | ||
| this.count += count; | ||
| System.out.println("The gumball machine was just refilled; its new count is: " + this.count); | ||
| if (currentState == soldOutState) { | ||
| currentState = noQuarterState; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AM-1(F): This should be calling the setState method. |
||
| } | ||
| } | ||
|
|
||
| public String toString() { | ||
| StringBuffer result = new StringBuffer(); | ||
| result.append("\nMighty Gumball, Inc."); | ||
| result.append("\nJava-enabled Standing Gumball Model #2004"); | ||
| result.append("\nInventory: " + count + " gumball"); | ||
| if (count != 1) { | ||
| result.append("s"); | ||
| } | ||
| result.append("\n"); | ||
| result.append("Machine is " + currentState.getStateToString() + "\n"); | ||
| return result.toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| //State Interface | ||
| //Should showcase actions on the state only | ||
| public interface GumballMachineState { | ||
| void insertQuarter(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AM-2(F): These should be public |
||
|
|
||
| void ejectQuarter(); | ||
|
|
||
| void turnCrank(); | ||
|
|
||
| void dispense(); | ||
|
|
||
| String getStateToString(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| public class GumballMachineTestDrive { | ||
|
|
||
| public static void main(String[] args) { | ||
| GumballMachine gumballMachine = new GumballMachine(10); | ||
|
|
||
| System.out.println(gumballMachine); | ||
|
|
||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
|
|
||
| System.out.println(gumballMachine); | ||
|
|
||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
|
|
||
| System.out.println(gumballMachine); | ||
|
|
||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
|
|
||
| System.out.println(gumballMachine); | ||
|
|
||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
|
|
||
| System.out.println(gumballMachine); | ||
|
|
||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
| gumballMachine.insertQuarter(); | ||
| gumballMachine.turnCrank(); | ||
|
|
||
| System.out.println(gumballMachine); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class HasQuarterState implements GumballMachineState { | ||
| private Random randomWinner = new Random(System.currentTimeMillis()); | ||
| private GumballMachine gumballMachine; | ||
|
|
||
| public HasQuarterState(GumballMachine gumballMachine) { | ||
| this.gumballMachine = gumballMachine; | ||
| } | ||
|
|
||
| @Override | ||
| public void insertQuarter() { | ||
| System.out.println("You can't insert another quarter"); | ||
| } | ||
|
|
||
| @Override | ||
| public void ejectQuarter() { | ||
| System.out.println("Quarter returned"); | ||
| gumballMachine.setState(gumballMachine.getNoQuarterState()); | ||
| } | ||
|
|
||
| @Override | ||
| public void turnCrank() { | ||
| System.out.println("You turned..."); | ||
| int winner = randomWinner.nextInt(10); | ||
| if ((winner == 0) && (gumballMachine.getCount() > 1)) { | ||
| gumballMachine.setState(gumballMachine.getWinnerState()); | ||
| } else { | ||
| gumballMachine.setState(gumballMachine.getSoldState()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void dispense() { | ||
| System.out.println("No gumball dispensed"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getStateToString() { | ||
| return "waiting for turn of crank"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| public class NoQuarterState implements GumballMachineState { | ||
| private GumballMachine gumballMachine; | ||
|
|
||
| public NoQuarterState(GumballMachine gumballMachine) { | ||
| this.gumballMachine = gumballMachine; | ||
| } | ||
|
|
||
| @Override | ||
| public void insertQuarter() { | ||
| System.out.println("You inserted a quarter"); | ||
| gumballMachine.setState(gumballMachine.getHasQuarterState()); | ||
| } | ||
|
|
||
| @Override | ||
| public void ejectQuarter() { | ||
| System.out.println("You haven't inserted a quarter"); | ||
| } | ||
|
|
||
| @Override | ||
| public void turnCrank() { | ||
| System.out.println("You turned, but there's no quarter"); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispense() { | ||
| System.out.println("You need to pay first"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getStateToString() { | ||
| return "waiting for quarter"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| public class SoldOutState implements GumballMachineState { | ||
| private GumballMachine gumballMachine; | ||
|
|
||
| public SoldOutState(GumballMachine gumballMachine) { | ||
| this.gumballMachine = gumballMachine; | ||
| } | ||
|
|
||
| @Override | ||
| public void insertQuarter() { | ||
| System.out.println("You can't insert a quarter, the machine is sold out"); | ||
| } | ||
|
|
||
| @Override | ||
| public void ejectQuarter() { | ||
| System.out.println("You can't eject, you haven't inserted a quarter yet"); | ||
| } | ||
|
|
||
| @Override | ||
| public void turnCrank() { | ||
| System.out.println("You turned, but there are no gumballs"); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispense() { | ||
| System.out.println("No gumball dispensed"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getStateToString() { | ||
| return "sold out"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| public class SoldState implements GumballMachineState { | ||
| private GumballMachine gumballMachine; | ||
|
|
||
| public SoldState(GumballMachine gumballMachine) { | ||
| this.gumballMachine = gumballMachine; | ||
| } | ||
|
|
||
| @Override | ||
| public void insertQuarter() { | ||
| System.out.println("Please wait, we're already giving you a gumball"); | ||
| } | ||
|
|
||
| @Override | ||
| public void ejectQuarter() { | ||
| System.out.println("Sorry, you already turned the crank"); | ||
| } | ||
|
|
||
| @Override | ||
| public void turnCrank() { | ||
| System.out.println("Turning twice doesn't get you another gumball!"); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispense() { | ||
| gumballMachine.releaseBall(); | ||
| if (gumballMachine.getCount() > 0) { | ||
| gumballMachine.setState(gumballMachine.getNoQuarterState()); | ||
| } else { | ||
| System.out.println("Oops, out of gumballs!"); | ||
| gumballMachine.setState(gumballMachine.getSoldOutState()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getStateToString() { | ||
| return "dispensing a gumball"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package assignments.state_pattern; | ||
|
|
||
| public class WinnerState implements GumballMachineState { | ||
| private GumballMachine gumballMachine; | ||
|
|
||
| public WinnerState(GumballMachine gumballMachine) { | ||
| this.gumballMachine = gumballMachine; | ||
| } | ||
|
|
||
| @Override | ||
| public void insertQuarter() { | ||
| System.out.println("Please wait, we're already giving you a Gumball"); | ||
| } | ||
|
|
||
| @Override | ||
| public void ejectQuarter() { | ||
| System.out.println("Sorry, you already turned the crank"); | ||
| } | ||
|
|
||
| @Override | ||
| public void turnCrank() { | ||
| System.out.println("Turning again doesn't get you another gumball!"); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispense() { | ||
| gumballMachine.releaseBall(); | ||
| if (gumballMachine.getCount() <= 0) { | ||
| gumballMachine.setState(gumballMachine.getSoldOutState()); | ||
| } else { | ||
| gumballMachine.releaseBall(); | ||
| System.out.println("YOU'RE A WINNER! You got two gumballs for your quarter"); | ||
| if (gumballMachine.getCount() > 0) { | ||
| gumballMachine.setState(gumballMachine.getNoQuarterState()); | ||
| } else { | ||
| System.out.println("Oops, out of gumballs!"); | ||
| gumballMachine.setState(gumballMachine.getSoldOutState()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getStateToString() { | ||
| return "despensing two gumballs for your quarter, because YOU'RE A WINNER!"; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AM-3(F): (No points removed): This is technically a change in state therefore the states should implement this method, but it breaks SOLID, since we are forcing the concretes to implement something they often cant define. There is no good answer, I just wanted to point it out.