Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/State1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kz.edu.nu.cs.exercise;

public class State1 extends State {

public State1(StateContext sc) {
this.sc = sc;
this.accept = false;
}

@Override
public void actionA(){
sc.setCurrentState(sc.state2);
}

@Override
public void actionB(){
sc.setCurrentState(sc.state1);
}

}
21 changes: 21 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/State2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kz.edu.nu.cs.exercise;

public class State2 extends State {

public State2(StateContext sc) {
this.sc = sc;
this.accept = false;
}

@Override
public void actionA() {

sc.setCurrentState(sc.state3);
}

@Override
public void actionB() {

sc.setCurrentState(sc.state1);
}
}
19 changes: 19 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/State3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kz.edu.nu.cs.exercise;

public class State3 extends State {

public State3(StateContext sc) {
this.sc = sc;
this.accept = true;
}

@Override
public void actionA() {
sc.setCurrentState(sc.state3);
}

@Override
public void actionB() {
sc.setCurrentState(sc.state2);
}
}
22 changes: 12 additions & 10 deletions src/main/java/kz/edu/nu/cs/exercise/StateContext.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package kz.edu.nu.cs.exercise;

public class StateContext {
final State state1 = new State1(this);
final State state2 = new State2(this);
final State state3 = new State3(this);

public final State state1 = new State1(this);
public final State state2 = new State2(this);
public final State state3 = new State3(this);

private State currentState;

public StateContext() {

this.currentState = state1;
}

public void actionA() {
// complete this method by
// delegation to the current state
this.getCurrentState().actionA();
}

public void actionB() {
// complete this method
// delegate to the current state
this.getCurrentState().actionB();

}

public boolean inAcceptState() {
// complete this method and return correct value
// delegate to the current state
return false;

return this.getCurrentState().isAccept();

}

public State getCurrentState() {

return currentState;
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/kz/edu/nu/cs/exercise/TestStatePattern.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kz.edu.nu.cs.exercise;



import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down