added lab7 ex2
This commit is contained in:
parent
876ce99c77
commit
8cefde97d3
25
src/lab7_state/ex2/Main.java
Normal file
25
src/lab7_state/ex2/Main.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
71
src/lab7_state/ex2/User.java
Normal file
71
src/lab7_state/ex2/User.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
18
src/lab7_state/ex2/UserState.java
Normal file
18
src/lab7_state/ex2/UserState.java
Normal file
@ -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() {};
|
||||
}
|
22
src/lab7_state/ex2/states/InMeetingState.java
Normal file
22
src/lab7_state/ex2/states/InMeetingState.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
16
src/lab7_state/ex2/states/RegisteredState.java
Normal file
16
src/lab7_state/ex2/states/RegisteredState.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
22
src/lab7_state/ex2/states/SpeakingState.java
Normal file
22
src/lab7_state/ex2/states/SpeakingState.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
16
src/lab7_state/ex2/states/UnregisteredState.java
Normal file
16
src/lab7_state/ex2/states/UnregisteredState.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
22
src/lab7_state/ex2/states/WaitingToSpeakState.java
Normal file
22
src/lab7_state/ex2/states/WaitingToSpeakState.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user