added lab7 ex2

This commit is contained in:
Louis Heredero 2024-10-14 10:46:34 +02:00
parent 876ce99c77
commit 8cefde97d3
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
8 changed files with 212 additions and 0 deletions

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

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

View 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() {};
}

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

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

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

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

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