Skip to content

Commit

Permalink
Merge pull request #44 from yijiano/dev/UiParser
Browse files Browse the repository at this point in the history
Refactor exit command handling
  • Loading branch information
yijiano authored Oct 9, 2024
2 parents f4693ec + 4ade379 commit d3921fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/java/seedu/pill/Pill.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

public final class Pill {
private static final Ui ui = new Ui();
private static final Parser parser = new Parser();

/**
* Runs the main loop of the Pill chatbot.
*/
public void run() throws PillException {
Printer.printInitMessage();
String line = ui.getInput();
while (!line.equalsIgnoreCase("exit")) {
Parser.parseCommand(line);
while (parser.getExitFlag()) {
parser.parseCommand(line);
line = ui.getInput();
}
Printer.printExitMessage();
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/seedu/pill/util/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import seedu.pill.exceptions.PillException;

public class Parser {
private boolean exitFlag = false;

/**
* Public constructor for Parser.
Expand All @@ -18,12 +19,15 @@ public Parser() {
* @param input The user's input command from the scanner.
* @throws PillException If command is invalid
*/
public static void parseCommand(String input) throws PillException {
public void parseCommand(String input) throws PillException {
String[] splitInput = input.split(" ", 2);
String commandString = splitInput[0].toLowerCase();

try {
switch (commandString) {
case "exit":
this.exitFlag = true;
break;
case "help":
// TODO: Add "help" command
break;
Expand All @@ -46,4 +50,13 @@ public static void parseCommand(String input) throws PillException {
PillException.printException(e);
}
}

/**
* Returns an exit flag for the Pill bot to exit.
*
* @return The state of exit flag.
*/
public boolean getExitFlag() {
return this.exitFlag;
}
}

0 comments on commit d3921fc

Please sign in to comment.