forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2441: Providing example for Implementing simple State Machine wi…
…th Java Enums.
- Loading branch information
PRIFTI
committed
Jan 15, 2019
1 parent
f1fa9c8
commit 4c9ea99
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...ellaneous-1/src/main/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestState.java
This file contains 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 com.baeldung.algorithms.enumstatemachine; | ||
|
||
public enum LeaveRequestState { | ||
|
||
Submitted { | ||
@Override | ||
public LeaveRequestState nextState() { | ||
System.out.println("Starting the Leave Request and sending to Team Leader for approval."); | ||
return Escalated; | ||
} | ||
|
||
@Override | ||
public String responsiblePerson() { | ||
return "Employee"; | ||
} | ||
}, | ||
Escalated { | ||
@Override | ||
public LeaveRequestState nextState() { | ||
System.out.println("Reviewing the Leave Request and escalating to Department Manager."); | ||
return Approved; | ||
} | ||
|
||
@Override | ||
public String responsiblePerson() { | ||
return "Team Leader"; | ||
} | ||
}, | ||
Approved { | ||
@Override | ||
public LeaveRequestState nextState() { | ||
System.out.println("Approving the Leave Request."); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String responsiblePerson() { | ||
return "Department Manager"; | ||
} | ||
}; | ||
|
||
public abstract String responsiblePerson(); | ||
|
||
public abstract LeaveRequestState nextState(); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...neous-1/src/test/java/com/baeldung/algorithms/enumstatemachine/LeaveRequestStateTest.java
This file contains 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,41 @@ | ||
package com.baeldung.algorithms.enumstatemachine; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class LeaveRequestStateTest { | ||
|
||
@Test | ||
public void givenLeaveRequest_whenStateEscalated_thenResponsibleIsTeamLeader() { | ||
LeaveRequestState state = LeaveRequestState.Escalated; | ||
|
||
String responsible = state.responsiblePerson(); | ||
|
||
assertEquals(responsible, "Team Leader"); | ||
} | ||
|
||
|
||
@Test | ||
public void givenLeaveRequest_whenStateApproved_thenResponsibleIsDepartmentManager() { | ||
LeaveRequestState state = LeaveRequestState.Approved; | ||
|
||
String responsible = state.responsiblePerson(); | ||
|
||
assertEquals(responsible, "Department Manager"); | ||
} | ||
|
||
@Test | ||
public void givenLeaveRequest_whenNextStateIsCalled_thenStateIsChanged() { | ||
LeaveRequestState state = LeaveRequestState.Submitted; | ||
|
||
state = state.nextState(); | ||
assertEquals(state, LeaveRequestState.Escalated); | ||
|
||
state = state.nextState(); | ||
assertEquals(state, LeaveRequestState.Approved); | ||
|
||
state = state.nextState(); | ||
assertEquals(state, LeaveRequestState.Approved); | ||
} | ||
} |