Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Java streams to handle matching strings and refactor code #3

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 66 additions & 49 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package duke;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Stream;

/**
* Processes the information from the user. Based upon the user's commands, calls upon and relays the necessary
* information accordingly.
*/
public class Parser {

private static final Set<String> VALID_COMMANDS = Set.of("list", "mark", "unmark", "todo", "event", "deadline",
"delete");
private String inputString;

public Parser(String inputString) {
Expand All @@ -33,14 +35,17 @@ public String getString() {
*/
public void process(TaskList tasks, Ui ui) throws EmptyTaskException, InvalidCommandException,
EmptyCommandException, TaskListOutOfBoundsException {
String command = inputString.split(" ", 2)[0];
if (command.equals("list")) {
String[] parts = inputString.split(" ", 2);
String instruction = parts[0];
String remainingInput = parts.length > 1 ? parts[1] : "";
throwInvalidInputExceptions();

if (instruction.equals("list")) {
ui.display(tasks);
return;
}
throwInvalidInputExceptions();
String remainingInput = inputString.split(" ", 2)[1];
System.out.println(performCommandAndGetParseResponse(tasks, ui, command, remainingInput));
System.out.println(performCommandAndGetParseResponse(tasks, ui, instruction, remainingInput));
}

/**
Expand All @@ -49,84 +54,96 @@ public void process(TaskList tasks, Ui ui) throws EmptyTaskException, InvalidCom
* @param tasks This contains the user's tasks listed thus far.
* @param ui The instance holding the current's user's interface for the programme.
* @return Returns the appropriate response to match the user's instruction.
* @throws EmptyTaskException
* @throws InvalidCommandException
* @throws EmptyCommandException
* @throws EmptyTaskException If a task without description is provided, an exception will be thrown.
* @throws InvalidCommandException If an invalid command is given, an exception will be thrown.
* @throws EmptyCommandException If an empty command is given, an exception will be thrown.
*/
public String stringProcess(TaskList tasks, Ui ui) throws EmptyTaskException, InvalidCommandException,
EmptyCommandException, TaskListOutOfBoundsException {
String command = inputString.split(" ", 2)[0];
if (command.equals("list")) {
String[] parts = inputString.split(" ", 2);
String instruction = parts[0];
String remainingInput = parts.length > 1 ? parts[1] : "";

throwInvalidInputExceptions();

if (instruction.equals("list")) {
return ui.guiDisplay(tasks);
}

throwInvalidInputExceptions();
String remainingInput = inputString.split(" ", 2)[1];
return performCommandAndGetParseResponse(tasks, ui, command, remainingInput);
return performCommandAndGetParseResponse(tasks, ui, instruction, remainingInput);
}

private void throwInvalidInputExceptions() throws EmptyCommandException, InvalidCommandException {
if (inputString.split(" ", 2).length == 0) {
throw new EmptyCommandException();
}
boolean flag1 = !(Arrays.asList("list", "mark", "unmark", "todo", "event", "deadline", "delete")
.contains(inputString.split(" ", 2)[0]));
if (inputString.split(" ", 2).length == 1 && flag1) {
throw new InvalidCommandException();
}
boolean flag2 = Arrays.asList("todo", "event", "deadline", "delete").contains(inputString.split(" ", 2)[0]);
if (inputString.split(" ", 2).length == 1 && flag2) {
throw new EmptyTaskException();
}
}



private static String performCommandAndGetParseResponse(
TaskList tasks, Ui ui, String command, String remainingInput)
TaskList tasks, Ui ui, String instruction, String remainingInput)
throws InvalidCommandException, TaskListOutOfBoundsException {
switch (command) {
case "mark" -> {
int idx = Integer.parseInt(remainingInput) - 1;
tasks.set(idx, true);
return "Nice! I've marked this task as done:\n" + tasks.get(idx);
}
case "unmark" -> {
switch (instruction) {
case "mark":
case "unmark": {
int idx = Integer.parseInt(remainingInput) - 1;
tasks.set(idx, false);
return "OK, I've marked this task as not done yet:\n" + tasks.get(idx);
tasks.set(idx, instruction.equals("mark"));
return (instruction.equals("mark") ? "Nice! " : "OK, ")
+ "I've marked this task as " + (instruction.equals("mark") ? "done" : "not done yet") + ":\n"
+ tasks.get(idx);
}
case "todo" -> {
case "todo": {
Todo task = new Todo(remainingInput);
tasks.add(task);
return ui.guiTaskAddOrDeleteDisplay(task, "add", tasks);
}
case "deadline" -> {
String name = remainingInput.split(" /by ", 2)[0];
LocalDate endDate = LocalDate.parse(remainingInput.split(" /by ", 2)[1]);
case "deadline": {
String[] deadlineParts = remainingInput.split(" /by ", 2);
String name = deadlineParts[0];
LocalDate endDate = LocalDate.parse(deadlineParts[1]);
Deadline task = new Deadline(name, endDate);
tasks.add(task);
return ui.guiTaskAddOrDeleteDisplay(task, "add", tasks);
}
case "event" -> {
String name = remainingInput.split(" /from ", 2)[0];
remainingInput = remainingInput.split(" /from ", 2)[1];
String start = remainingInput.split(" /to ", 2)[0];
String end = remainingInput.split(" /to ", 2)[1];
case "event": {
String[] eventParts = remainingInput.split(" /from ", 2);
String name = eventParts[0];
String[] timeParts = eventParts[1].split(" /to ", 2);
String start = timeParts[0];
String end = timeParts[1];
Event task = new Event(name, start, end);
tasks.add(task);
return ui.guiTaskAddOrDeleteDisplay(task, "add", tasks);
}
case "delete" -> {
case "delete": {
int idx = Integer.parseInt(remainingInput) - 1;
Task taskToBeDeleted = tasks.get(idx);
assert (tasks.size() > idx);
tasks.delete(idx);
return ui.guiTaskAddOrDeleteDisplay(taskToBeDeleted, "delet", tasks);
}
case "find" -> {
case "find": {
TaskList matchingTasks = tasks.findAll(remainingInput);
return ui.guiDisplaySearch(matchingTasks);

}
default -> throw new InvalidCommandException();
default: {
throw new InvalidCommandException();
}
}
}
private void throwInvalidInputExceptions() throws EmptyCommandException, InvalidCommandException {
String[] parts = inputString.split(" ", 2);
String instruction = parts[0];
String remainingInput = parts.length > 1 ? parts[1] : "";

if (!VALID_COMMANDS.contains(instruction)) {
throw new InvalidCommandException();
}
if (remainingInput.isEmpty()) {
if (Stream.of("todo", "event", "deadline", "delete").anyMatch(instruction::equals)) {
throw new EmptyTaskException();
} else {
throw new EmptyCommandException();
}
}
}

}