Javakurs/Übungsaufgaben/OOPStateCharts
< Javakurs | Übungsaufgaben
Aufgabenstellung
.
Schreibe ein Programm, welches in objekt orientierter Weise, das hier gezeigte Diagramm als StateChart erzeugt.
Hilfestellung
Benutze die folgende Klasse als Basis für alle weiteren Zustände:
/**
* Base Class for all states
*/
class State {
// ! returns a new state, if transition is correct
public State transition(String signal) {
return this;
}
}
Wobei jeder Aufruf von transition entweder das gleiche Objekt (this) oder ein neues Objekt zurückliefert, je nach dem, ob ein Statusübergang statt findet, oder nicht. Die main-Methode könnte dann wie folgt aussehen:
/**
* Class file starting main loop
*/
public class StateCharts {
/**
* Main class, creating the state chart
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// set the state chart to be initial at state initial
State current_state = new InitialState();
// loop through the state till exit is reached
while (current_state != null) {
// print information about current class
System.out.println("Current state: "
+ current_state.getClass().getName());
// get signal for next state
System.out.print("signal to be send: ");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String signal = reader.readLine();
// get next state if needed
current_state = current_state.transition(signal);
System.out.println();
}
// inform user
System.out.println("StateChart is at end state.");
}
}
