fork download
  1. import java.util.*;
  2. class Odai12151 {
  3. static class Context {
  4. static interface State {
  5. String reply(String s);
  6. }
  7. private State state = (s) -> null;
  8. private Map<String, State> map = new HashMap<>();
  9. public Context() {
  10. map.put("wake up", (s) -> "hi".equals(s) ? "hello" : null);
  11. map.put("sleep", (s) -> "hi".equals(s) ? "zzz..." : null);
  12. }
  13. private void p(String s) {
  14. if (s != null) System.out.println(s);
  15. }
  16. public void execute(String s) {
  17. p(state.reply(s));
  18. if (map.containsKey(s)) state = map.get(s);
  19. }
  20. }
  21. public static void main(String[] args) {
  22. Context c = new Context();
  23. String[] messages = {"hi", "wake up", "hi", "sleep", "hi", "bye"};
  24. for (String m : messages) {
  25. System.out.println(">> " + m);
  26. c.execute(m);
  27. }
  28. }
  29. }
  30.  
Success #stdin #stdout 0.21s 2184192KB
stdin
Standard input is empty
stdout
>> hi
>> wake up
>> hi
hello
>> sleep
>> hi
zzz...
>> bye