Skip to content

Commit

Permalink
Resolved incomplete commands, fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
waffledood committed Jan 14, 2022
1 parent 006262d commit 5fc4dbb
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,54 +91,65 @@ else if (userInput.toLowerCase().matches("^mark \\d+") || userInput.toLowerCase(

// Add - adds Task to list
// 3 types of Tasks: ToDo's, Events, Deadlines
else if (userInput.toLowerCase().matches("^todo .*") || userInput.toLowerCase().matches("^event .*") || userInput.toLowerCase().matches("^deadline .*")) {
else if (userInput.matches("^todo .*|^todo") || userInput.toLowerCase().matches("^event .*|^event") || userInput.toLowerCase().matches("^deadline .*|^deadline")) {
String[] textStrings = userInput.split(" ");
String taskType = textStrings[0].toLowerCase();
String taskDescription = String.join(" ", Arrays.copyOfRange(textStrings, 1, textStrings.length));

System.out.println(String.format("%sNoted. I've added this task:", indentationText));

Task task = new Task("");

// ToDo's
if (taskType.equals("todo")) {

task = new Todo(taskDescription);
taskList.add(task);

// Events & Deadlines
} else {
if (userInput.toLowerCase().matches("^todo .+") || userInput.toLowerCase().matches("^event .+") || userInput.toLowerCase().matches("^deadline .+")) {

String taskDescription = String.join(" ", Arrays.copyOfRange(textStrings, 1, textStrings.length));

String[] taskDescriptionStrings = taskDescription.split("/");
String taskDescriptionText = taskDescriptionStrings[0].strip();
String[] taskDescriptionTimeStrings = taskDescriptionStrings[1].split(" ");
String taskDescriptionTime = String.join(" ", Arrays.copyOfRange(taskDescriptionTimeStrings, 1, taskDescriptionTimeStrings.length)).strip();
// TODO - only printed when a task has been added
System.out.println(String.format("%sNoted. I've added this task:", indentationText));

// Events
if (taskType.equals("event")) {
Task task = new Task("");

// ToDo's
if (taskType.equals("todo")) {

task = new Event(taskDescriptionText, taskDescriptionTime);
task = new Todo(taskDescription);
taskList.add(task);

// Deadlines
} else if (taskType.equals("deadline")) {
// Events & Deadlines
} else {

task = new Deadline(taskDescriptionText, taskDescriptionTime);
taskList.add(task);
if (!taskDescription.matches(".+ /by .+|.+ /at .+")) {
throw new DukeException("Your " + taskType + " command is incorrectly formatted. Please try again.");
}

String[] taskDescriptionStrings = taskDescription.split("/");
String taskDescriptionText = taskDescriptionStrings[0].strip();
String[] taskDescriptionTimeStrings = taskDescriptionStrings[1].split(" ");
String taskDescriptionTime = String.join(" ", Arrays.copyOfRange(taskDescriptionTimeStrings, 1, taskDescriptionTimeStrings.length)).strip();

// Events
if (taskType.equals("event")) {

task = new Event(taskDescriptionText, taskDescriptionTime);
taskList.add(task);

// Deadlines
} else if (taskType.equals("deadline")) {

task = new Deadline(taskDescriptionText, taskDescriptionTime);
taskList.add(task);
}

}

}

System.out.println(String.format("%s%s", indentationTaskStatus, task));
System.out.println(String.format("%sNow you have %d tasks in the list.", indentationText, taskList.size()));
System.out.println(String.format("%s%s", indentationTaskStatus, task));
System.out.println(String.format("%sNow you have %d tasks in the list.", indentationText, taskList.size()));
} else {
throw new DukeException("I'm sorry, your " + taskType + " command is missing a task description. Please try again.");
}
} else {
throw new DukeException("I'm sorry, you've input a command I don't recognize. Please try again.");
}

}

catch(DukeException e) {
System.out.println(String.format("%sException: %s", indentationText, e.getMessage()));
System.out.println(String.format("%s%s", indentationText, e.getMessage()));
}
}

Expand Down

0 comments on commit 5fc4dbb

Please sign in to comment.