I am trying to keep my main method as small as possible. An application should merely be bootstrapped by the main method. Therefore I am trying to put the user input session into separate classes/methods. My initial idea was to use AtomicBoolean running
. Unfortunately, I am only allowed to use classes from java.util. That's why I tried it this way:
I created a class Session.java:
public class Session {
protected boolean running = true;
/**
* After starting the session this method remains in a loop until the
* terminate method is called.
*/
void run() {
RailRegister register = new Register();
while (running) {
String input = Terminal.readLine();
try {
UserInterface userInterface = new UserInterface(register);
userInterface.execute(input);
} catch (UnknownCommandException | BadSyntaxException e) {
Terminal.printError(e.getMessage());
}
}
}
// Terminates the session
public void terminate() {
running = false;
}
}
and here is my main class with the main method:
public class Main {
public static void main(String[] args) {
Session session = new Session();
session.run();
}
}
The quit command should terminate the session. However, I am not sure how to access the current session.
public class QuitCommand extends AbstractCommand {
public QuitCommand(RailRegister register) {
super(register);
}
@Override
public void execute(List<String> arguments) throws BadSyntaxException {
// TODO: terminate session
}
}
Can someone show me how this can be implemented?
Aucun commentaire:
Enregistrer un commentaire