forked from nus-cs2113-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
UiInput.java
65 lines (55 loc) · 1.93 KB
/
UiInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package seedu.dietbook.ui;
import seedu.dietbook.logger.MainLogger;
import seedu.dietbook.exception.DietException;
import java.util.Scanner;
import java.util.logging.Level;
/**
* Represents a text user interface that deals with taking in user commands.
* A <code>UiInput</code> object deals with user interaction by taking in user inputs and processing them.
*/
public class UiInput {
private static Scanner scanner = new Scanner(System.in);
private final MainLogger mainLogger;
private final UiHelper uiHelper;
/**
* Constructs a <code>UiInput</code> object.
*/
UiInput() {
uiHelper = new UiHelper();
mainLogger = new MainLogger(UiInput.class.getName());
}
/**
* Returns the non-empty user command that has been trimmed.
*
* @return The non-empty user command that has been trimmed.
*/
String getCommand() throws DietException {
String command = readCommand();
String processedCommand = processCommand(command);
mainLogger.log(Level.FINE, "Processed user command: " + processedCommand);
return processedCommand;
}
/**
* Reads in and returns the user command.
*
* @return The user command.
*/
String readCommand() {
return scanner.nextLine();
}
/**
* Returns the trimmed user command if its is not empty, else a DietException is thrown.
*
* @param command The user command.
* @return The trimmed user command if it is not empty.
* @throws DietException If the the user command is empty after trimming.
*/
String processCommand(String command) throws DietException {
mainLogger.log(Level.FINE, "User command to process: " + command);
if (uiHelper.isEmptyString(command)) {
mainLogger.log(Level.WARNING, "Command is empty!");
throw new DietException("Command is empty!");
}
return uiHelper.trimString(command);
}
}