|
1 |
| -package client.IOutils; |
2 |
| - |
3 |
| -import client.IOutils.readers.ObjectReader; |
4 |
| -import client.commands.commandsUtils.ClientCommandsManager; |
5 |
| -import sharedClasses.data.MusicBand; |
6 |
| -import sharedClasses.commands.Command; |
7 |
| -import sharedClasses.messageUtils.Request; |
8 |
| - |
9 |
| -import java.io.PrintStream; |
10 |
| -import java.util.*; |
11 |
| - |
12 |
| -/** |
13 |
| - * A class for recognizing user input (from the console or file). |
14 |
| - */ |
15 |
| -public class UserInputManager { |
16 |
| - private final ClientCommandsManager clientcommandsManager; |
17 |
| - private final ObjectReader objectReader; |
18 |
| - private final Scanner input; |
19 |
| - private final boolean showMessages; |
20 |
| - private final PrintStream out; |
21 |
| - |
22 |
| - public UserInputManager(ClientCommandsManager clientCommandsManager, Scanner input, boolean showMessages, PrintStream printStream) { |
23 |
| - this.clientcommandsManager = clientCommandsManager; |
24 |
| - this.input = input; |
25 |
| - objectReader = new ObjectReader(input, showMessages); |
26 |
| - this.showMessages = showMessages; |
27 |
| - this.out = printStream; |
28 |
| - } |
29 |
| - |
30 |
| - /** |
31 |
| - * A method for reading user input. |
32 |
| - * |
33 |
| - * @return true if the program execution can be continued; false if the program execution should be stopped. |
34 |
| - */ |
35 |
| - public Request input() { |
36 |
| - printInviteMessage(); |
37 |
| - if (!input.hasNext()) return null; |
38 |
| - String[] s = input.nextLine().split(" "); |
39 |
| - Command command = clientcommandsManager.getCommand(s[0].toUpperCase(Locale.ROOT)); |
40 |
| - try { |
41 |
| - if (command == null) { |
42 |
| - throw new IllegalArgumentException("There's no such command"); |
43 |
| - } |
44 |
| - clientcommandsManager.addToHistory(command.getName()); |
45 |
| - if (!checkArgsCount(s, command)) |
46 |
| - throw new IllegalArgumentException("Wrong amount of arguments. Please, try again! (You can use command \"help\" for more information.)"); |
47 |
| - } catch (NumberFormatException e) { |
48 |
| - throw e; |
49 |
| - } |
50 |
| - return new Request(command, s, readObjectIfNecessary(command)); |
51 |
| - } |
52 |
| - |
53 |
| - /** |
54 |
| - * A method for confirming the user's action. |
55 |
| - * |
56 |
| - * @return true if the user confirms the action; false if the user rejects the action. |
57 |
| - */ |
58 |
| - private boolean askQuestion() { |
59 |
| - while (true) { |
60 |
| - out.println("yes/no?"); |
61 |
| - if (!input.hasNext()) return true; |
62 |
| - String answer = input.nextLine(); |
63 |
| - if (answer.equals("yes")) return true; |
64 |
| - if (answer.equals("no")) return false; |
65 |
| - } |
66 |
| - } |
67 |
| - |
68 |
| - /** |
69 |
| - * Method for checking the number of command parameters. |
70 |
| - * |
71 |
| - * @param s array of user input. |
72 |
| - * @param command the command entered by the user. |
73 |
| - * @return true if the number of arguments is correct, otherwise false. |
74 |
| - */ |
75 |
| - private boolean checkArgsCount(String[] s, Command command) { |
76 |
| - if (command.getCountOfArgs() == s.length - 1) return true; |
77 |
| - return false; |
78 |
| - } |
79 |
| - |
80 |
| - /** |
81 |
| - * A method for reading a collection item, if necessary to execute a command. |
82 |
| - * |
83 |
| - * @param command the current executable command. |
84 |
| - * @return read object {@link MusicBand} or null if the object is not required. |
85 |
| - */ |
86 |
| - private MusicBand readObjectIfNecessary(Command command) { |
87 |
| - if (!command.isNeedObject()) return null; |
88 |
| - return objectReader.readObject(); |
89 |
| - } |
90 |
| - |
91 |
| - /** |
92 |
| - * A method for displaying an input prompt for the user (in the case of reading data from the console). |
93 |
| - */ |
94 |
| - private void printInviteMessage() { |
95 |
| - if (showMessages) System.out.println("Enter command (if you don't know commands, enter command \"help\"):"); |
96 |
| - } |
97 |
| -} |
| 1 | +package IOutils; |
| 2 | + |
| 3 | +import IOutils.readers.ObjectReader; |
| 4 | +import commands.commandsUtils.ClientCommandsManager; |
| 5 | +import data.MusicBand; |
| 6 | +import commands.Command; |
| 7 | +import messageUtils.Request; |
| 8 | + |
| 9 | +import java.io.PrintStream; |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * A class for recognizing user input (from the console or file). |
| 14 | + */ |
| 15 | +public class UserInputManager { |
| 16 | + private final ClientCommandsManager clientcommandsManager; |
| 17 | + private final ObjectReader objectReader; |
| 18 | + private final Scanner input; |
| 19 | + private final boolean showMessages; |
| 20 | + private final PrintStream out; |
| 21 | + |
| 22 | + public UserInputManager(ClientCommandsManager clientCommandsManager, Scanner input, boolean showMessages, PrintStream printStream) { |
| 23 | + this.clientcommandsManager = clientCommandsManager; |
| 24 | + this.input = input; |
| 25 | + objectReader = new ObjectReader(input, showMessages); |
| 26 | + this.showMessages = showMessages; |
| 27 | + this.out = printStream; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * A method for reading user input. |
| 32 | + * |
| 33 | + * @return true if the program execution can be continued; false if the program execution should be stopped. |
| 34 | + */ |
| 35 | + public Request input() { |
| 36 | + printInviteMessage(); |
| 37 | + if (!input.hasNext()) return null; |
| 38 | + String[] s = input.nextLine().split(" "); |
| 39 | + Command command = clientcommandsManager.getCommand(s[0].toUpperCase(Locale.ROOT)); |
| 40 | + try { |
| 41 | + if (command == null) { |
| 42 | + throw new IllegalArgumentException("There's no such command"); |
| 43 | + } |
| 44 | + clientcommandsManager.addToHistory(command.getName()); |
| 45 | + if (!checkArgsCount(s, command)) |
| 46 | + throw new IllegalArgumentException("Wrong amount of arguments. Please, try again! (You can use command \"help\" for more information.)"); |
| 47 | + } catch (NumberFormatException e) { |
| 48 | + throw e; |
| 49 | + } |
| 50 | + return new Request(command, s, readObjectIfNecessary(command)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * A method for confirming the user's action. |
| 55 | + * |
| 56 | + * @return true if the user confirms the action; false if the user rejects the action. |
| 57 | + */ |
| 58 | + private boolean askQuestion() { |
| 59 | + while (true) { |
| 60 | + out.println("yes/no?"); |
| 61 | + if (!input.hasNext()) return true; |
| 62 | + String answer = input.nextLine(); |
| 63 | + if (answer.equals("yes")) return true; |
| 64 | + if (answer.equals("no")) return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Method for checking the number of command parameters. |
| 70 | + * |
| 71 | + * @param s array of user input. |
| 72 | + * @param command the command entered by the user. |
| 73 | + * @return true if the number of arguments is correct, otherwise false. |
| 74 | + */ |
| 75 | + private boolean checkArgsCount(String[] s, Command command) { |
| 76 | + if (command.getCountOfArgs() == s.length - 1) return true; |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * A method for reading a collection item, if necessary to execute a command. |
| 82 | + * |
| 83 | + * @param command the current executable command. |
| 84 | + * @return read object {@link MusicBand} or null if the object is not required. |
| 85 | + */ |
| 86 | + private MusicBand readObjectIfNecessary(Command command) { |
| 87 | + if (!command.isNeedObject()) return null; |
| 88 | + return objectReader.readObject(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * A method for displaying an input prompt for the user (in the case of reading data from the console). |
| 93 | + */ |
| 94 | + private void printInviteMessage() { |
| 95 | + if (showMessages) System.out.println("Enter command (if you don't know commands, enter command \"help\"):"); |
| 96 | + } |
| 97 | +} |
0 commit comments