Skip to content
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

State Pattern/State Winner, has a bug in GumballMachine.java #16

Open
yousifAlneamy opened this issue Nov 29, 2017 · 0 comments
Open

State Pattern/State Winner, has a bug in GumballMachine.java #16

yousifAlneamy opened this issue Nov 29, 2017 · 0 comments

Comments

@yousifAlneamy
Copy link

state property has null value if the object was created with zero gumballs like: new GumballMachine(0)

Inside GumballMachine.java you have the following:

State soldOutState; // soldOutState is null
// other properties
State state = soldOutState; // state is null

public GumballMachine(int numberGumballs) {
	soldOutState = new SoldOutState(this); // soldOutState has a new reference
	// other code
	this.count = numberGumballs;
 	if (numberGumballs > 0) { // if this is false, then state will still be null
		state = noQuarterState;
	}
}

So, to fix this it's better to add else statement in the constructor to give state the new soldOutState value, like:

 if (numberGumballs > 0) { // if this is false, then state will be soldOutState
	state = noQuarterState;
} else {
        state = soldOutState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant