Skip to content

Commit

Permalink
Fix peer evaluation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang-CH committed Oct 15, 2021
1 parent 3ad2dbf commit 33969b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion data/save.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
T|0|asdqwe
T|0|a
9 changes: 7 additions & 2 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ private void run() {
Command c = Parser.parse(fullCommand);
c.execute(tasks, ui, storage);
isExit = c.isExit();
} catch (DukeException | DateTimeException e) {
} catch (DukeException e) {
System.out.println("duke error");
ui.showError(e.getMessage());
} catch (DateTimeException e) {
ui.showError("Invalid date: date must be in YYYY-MM-DD");
}
ui.showLine();
}
Expand Down Expand Up @@ -202,8 +205,10 @@ private void processInput(String stringInput) {
try {
Command parsedCommand = Parser.parse(stringInput);
parsedCommand.execute(tasks, ui, storage);
} catch (DukeException | DateTimeException e) {
} catch (DukeException e) {
ui.showError(e.getMessage());
} catch (DateTimeException e) {
ui.showError("Invalid date: date must be in YYYY-MM-DD");
}
userInput.clear();
}
Expand Down
43 changes: 26 additions & 17 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
* Parses string inputs from user.
*/
public class Parser {
private static final int COMMAND_EVENT_LENGTH = 6;
private static final int COMMAND_DEADLINE_LENGTH = 9;
private static final int COMMAND_TODO_LENGTH = 5;
private static final int COMMAND_EVENT_LENGTH = 5;
private static final int COMMAND_DEADLINE_LENGTH = 8;
private static final int COMMAND_TODO_LENGTH = 4;

/**
* Parses string inputs from user into a Command.
Expand All @@ -49,13 +49,27 @@ protected static Command parse(String input) throws DukeException, DateTimeParse
return new ListCommand();
case "done":
try {
if (input.contains("|")) {
throw new DukeException("character | is not allowed as input");
}
if (inputArray.length < 2) {
throw new DukeException("A valid index must be provided");
}

selectedTask = Integer.parseInt(inputArray[1]) - 1;
return new DoneCommand(selectedTask);
} catch (NumberFormatException e) {
throw new DukeException("Invalid input for done, index must be an integer.");
}
case "delete":
try {
if (input.contains("|")) {
throw new DukeException("character | is not allowed as input");
}
if (inputArray.length < 2) {
throw new DukeException("A valid index must be provided");
}

selectedTask = Integer.parseInt(inputArray[1]) - 1;
return new DeleteCommand(selectedTask);
} catch (NumberFormatException e) {
Expand All @@ -68,6 +82,9 @@ protected static Command parse(String input) throws DukeException, DateTimeParse
case "deadline":
return parseDeadline(input);
case "todo":
if (input.contains("|")) {
throw new DukeException("character | is not allowed as input");
}
return parseTodo(input);
default:
throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-(");
Expand All @@ -85,22 +102,18 @@ private static Command parseEvent(String input) throws DukeException, DateTimePa
params = input.split("/at");

if (params.length < 2) {
throw new DukeException("Invalid input: missing /at");
throw new DukeException("Invalid input: missing /at date");
}

if (params[0].length() < (COMMAND_EVENT_LENGTH + 2)) {
if (params[0].trim().length() < (COMMAND_EVENT_LENGTH)) {
throw new DukeException("Invalid input: description must not be empty");
}

params[0] = params[0].substring(COMMAND_EVENT_LENGTH,
params[0].length() - 1).trim();
params[1] = params[1].substring(1).trim();
params[1] = params[1].trim();
LocalDate date = LocalDate.parse(params[1]);

if (params[0].equals("")) {
throw new DukeException("Invalid input: description must not be empty");
}

return new AddCommand(new Event(params[0], date));
}

Expand All @@ -115,22 +128,18 @@ private static Command parseDeadline(String input) throws DukeException, DateTim
params = input.split("/by");

if (params.length < 2) {
throw new DukeException("Invalid input: missing /by");
throw new DukeException("Invalid input: missing /by date");
}

if (params[0].length() < (COMMAND_DEADLINE_LENGTH + 2)) {
if (params[0].trim().length() < COMMAND_DEADLINE_LENGTH) {
throw new DukeException("Invalid input: description must not be empty");
}

params[0] = params[0].substring(COMMAND_DEADLINE_LENGTH,
params[0].length() - 1);
params[1] = params[1].substring(1);
params[1] = params[1].trim();
LocalDate date = LocalDate.parse(params[1]);

if (params[0].equals("")) {
throw new DukeException("Invalid input: description must not be empty");
}

return new AddCommand(new Deadline(params[0], date));
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/command/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public DoneCommand(int index) {
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
if (index < 0 || index >= tasks.getSize()) {
ui.showError("Index error: please select the correct index");
return;
}

tasks.getIndex(index).setDone();
ui.showMessage(getMessage(tasks));
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void execute(TaskList tasks, Ui ui, Storage storage) {
@Override
public String getMessage(TaskList tasks) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Your tasks:\n");
for (int i = 0; i < tasks.getSize(); i++) {
stringBuilder.append(String.format("%d. %s\n",
i + 1,
Expand Down

0 comments on commit 33969b2

Please sign in to comment.