Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. package viceCity.core;
  2.  
  3. import viceCity.common.Command;
  4. import viceCity.core.interfaces.Controller;
  5. import viceCity.core.interfaces.Engine;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.util.Arrays;
  11.  
  12. public class EngineImpl implements Engine {
  13. private Controller controller;
  14. private BufferedReader reader;
  15.  
  16. public EngineImpl(Controller controller) {
  17. this.controller = controller;
  18. this.reader = new BufferedReader(new InputStreamReader(System.in));
  19. }
  20.  
  21. @Override
  22. public void run() {
  23. while (true) {
  24. String result = null;
  25. try {
  26. result = processInput();
  27.  
  28. if (result.equals(Command.Exit.name())) {
  29. break;
  30. }
  31. } catch (NullPointerException | IllegalArgumentException | IOException e) {
  32. result = e.getMessage();
  33. }
  34.  
  35. System.out.println(result);
  36. }
  37. }
  38.  
  39. private String processInput() throws IOException {
  40. String input = this.reader.readLine();
  41. String[] tokens = input.split("\\s+");
  42.  
  43. Command command = Command.valueOf(tokens[0]);
  44. String result = null;
  45. String[] data = Arrays.stream(tokens).skip(1).toArray(String[]::new);
  46.  
  47. switch (command) {
  48. case AddPlayer:
  49. result = this.controller.addPlayer(data[0]);
  50. break;
  51. case AddGun:
  52. result = this.controller.addGun(data[0],data[1]);
  53. break;
  54. case AddGunToPlayer:
  55. result = this.controller.addGunToPlayer(data[0]);
  56. break;
  57. case Fight:
  58. result = this.controller.fight();
  59. break;
  60. case Exit:
  61. result = Command.Exit.name();
  62. break;
  63. }
  64. return result;
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement