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
7 changes: 7 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,7 @@
package kz.edu.nu.cs.exercise;

public class State1 extends State{

public State1(StateContext sc) {}

}
7 changes: 7 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,7 @@
package kz.edu.nu.cs.exercise;

public class State2 extends State{

public State2(StateContext sc) {}

}
7 changes: 7 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,7 @@
package kz.edu.nu.cs.exercise;

public class State3 extends State{

public State3(StateContext sc) {}

}
89 changes: 51 additions & 38 deletions src/main/java/kz/edu/nu/cs/exercise/StateContext.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
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);

private State currentState;

public StateContext() {
this.currentState = state1;
}

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

public void actionB() {
// complete this method
// delegate to the current state
}

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

public State getCurrentState() {
return currentState;
}

public void setCurrentState(State currentState) {
this.currentState = currentState;
}

}
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);

private State currentState;

public StateContext() {
this.currentState = state1;
}

public void actionA() {
if(currentState.equals(state1)){
currentState = state2;
}else if(currentState.equals(state2)){
currentState = state3;
}else{
currentState = state3;
}
}

public void actionB() {
if(currentState.equals(state1)){
currentState = state1;
}else if(currentState.equals(state2)){
currentState = state1;
}else{
currentState = state2;
}
}

public boolean inAcceptState() {
if(currentState.equals(state3)){
return true;
}else {
return false;
}

}

public State getCurrentState() {
return currentState;
}

public void setCurrentState(State currentState) {
this.currentState = currentState;
}

}