diff --git a/src/lab7_state/ex2/Main.java b/src/lab7_state/ex2/Main.java new file mode 100644 index 0000000..ab4df8f --- /dev/null +++ b/src/lab7_state/ex2/Main.java @@ -0,0 +1,25 @@ +package lab7_state.ex2; + +public class Main { + public static void main(String[] args) { + User user = new User("Baryhobal"); + user.connect(); + user.enter(); + user.leave(); + + user.enter(); + user.ask(); + user.leave(); + + user.enter(); + user.ask(); + user.handOver(); + user.leave(); + + user.enter(); + user.ask(); + user.handOver(); + user.over(); + user.leave(); + } +} diff --git a/src/lab7_state/ex2/User.java b/src/lab7_state/ex2/User.java new file mode 100644 index 0000000..a850f29 --- /dev/null +++ b/src/lab7_state/ex2/User.java @@ -0,0 +1,71 @@ +package lab7_state.ex2; + +import lab7_state.ex2.states.*; + +public class User { + private String name; + private UserState currentState; + private UserState unregisteredState; + private UserState registeredState; + private UserState inMeetingState; + private UserState waitingToSpeakState; + private UserState speakingState; + + public User(String name) { + this.name = name; + unregisteredState = new UnregisteredState(this); + registeredState = new RegisteredState(this); + inMeetingState = new InMeetingState(this); + waitingToSpeakState = new WaitingToSpeakState(this); + speakingState = new SpeakingState(this); + currentState = unregisteredState; + } + + public void setCurrentState(UserState currentState) { + this.currentState = currentState; + } + + public UserState getUnregisteredState() { + return unregisteredState; + } + + public UserState getRegisteredState() { + return registeredState; + } + + public UserState getInMeetingState() { + return inMeetingState; + } + + public UserState getWaitingToSpeakState() { + return waitingToSpeakState; + } + + public UserState getSpeakingState() { + return speakingState; + } + + public void connect() { + currentState.connect(); + } + public void enter() { + currentState.enter(); + } + public void leave() { + currentState.leave(); + } + public void ask() { + currentState.ask(); + } + public void handOver() { + currentState.handOver(); + } + public void over() { + currentState.over(); + } + + @Override + public String toString() { + return "User " + name; + } +} diff --git a/src/lab7_state/ex2/UserState.java b/src/lab7_state/ex2/UserState.java new file mode 100644 index 0000000..80ba3d8 --- /dev/null +++ b/src/lab7_state/ex2/UserState.java @@ -0,0 +1,18 @@ +package lab7_state.ex2; + +public abstract class UserState { + protected User user; + public UserState(User user) { + this.user = user; + } + protected void log(String msg) { + System.out.println("[" + user + "] " + msg); + } + + public void connect() {}; + public void enter() {}; + public void leave() {}; + public void ask() {}; + public void handOver() {}; + public void over() {}; +} diff --git a/src/lab7_state/ex2/states/InMeetingState.java b/src/lab7_state/ex2/states/InMeetingState.java new file mode 100644 index 0000000..2d47f44 --- /dev/null +++ b/src/lab7_state/ex2/states/InMeetingState.java @@ -0,0 +1,22 @@ +package lab7_state.ex2.states; + +import lab7_state.ex2.User; +import lab7_state.ex2.UserState; + +public class InMeetingState extends UserState { + public InMeetingState(User user) { + super(user); + } + + @Override + public void leave() { + user.setCurrentState(user.getRegisteredState()); + log("Left meeting"); + } + + @Override + public void ask() { + user.setCurrentState(user.getWaitingToSpeakState()); + log("Asking to speak"); + } +} diff --git a/src/lab7_state/ex2/states/RegisteredState.java b/src/lab7_state/ex2/states/RegisteredState.java new file mode 100644 index 0000000..38c6e3b --- /dev/null +++ b/src/lab7_state/ex2/states/RegisteredState.java @@ -0,0 +1,16 @@ +package lab7_state.ex2.states; + +import lab7_state.ex2.User; +import lab7_state.ex2.UserState; + +public class RegisteredState extends UserState { + public RegisteredState(User user) { + super(user); + } + + @Override + public void enter() { + user.setCurrentState(user.getInMeetingState()); + log("Entered meeting"); + } +} diff --git a/src/lab7_state/ex2/states/SpeakingState.java b/src/lab7_state/ex2/states/SpeakingState.java new file mode 100644 index 0000000..4cb4d5d --- /dev/null +++ b/src/lab7_state/ex2/states/SpeakingState.java @@ -0,0 +1,22 @@ +package lab7_state.ex2.states; + +import lab7_state.ex2.User; +import lab7_state.ex2.UserState; + +public class SpeakingState extends UserState { + public SpeakingState(User user) { + super(user); + } + + @Override + public void over() { + user.setCurrentState(user.getInMeetingState()); + log("Finished speaking"); + } + + @Override + public void leave() { + user.setCurrentState(user.getRegisteredState()); + log("Finished speaking, left meeting"); + } +} diff --git a/src/lab7_state/ex2/states/UnregisteredState.java b/src/lab7_state/ex2/states/UnregisteredState.java new file mode 100644 index 0000000..7b1a770 --- /dev/null +++ b/src/lab7_state/ex2/states/UnregisteredState.java @@ -0,0 +1,16 @@ +package lab7_state.ex2.states; + +import lab7_state.ex2.User; +import lab7_state.ex2.UserState; + +public class UnregisteredState extends UserState { + public UnregisteredState(User user) { + super(user); + } + + @Override + public void connect() { + user.setCurrentState(user.getRegisteredState()); + log("Registered"); + } +} diff --git a/src/lab7_state/ex2/states/WaitingToSpeakState.java b/src/lab7_state/ex2/states/WaitingToSpeakState.java new file mode 100644 index 0000000..c9889b4 --- /dev/null +++ b/src/lab7_state/ex2/states/WaitingToSpeakState.java @@ -0,0 +1,22 @@ +package lab7_state.ex2.states; + +import lab7_state.ex2.User; +import lab7_state.ex2.UserState; + +public class WaitingToSpeakState extends UserState { + public WaitingToSpeakState(User user) { + super(user); + } + + @Override + public void leave() { + user.setCurrentState(user.getRegisteredState()); + log("Lost patience, left meeting"); + } + + @Override + public void handOver() { + user.setCurrentState(user.getSpeakingState()); + log("Starts speaking"); + } +}