Skip to content

Commit

Permalink
Merge branch 'branch-Level-6'
Browse files Browse the repository at this point in the history
  • Loading branch information
SibingWu committed Feb 14, 2020
2 parents feb4454 + 673a491 commit 3278712
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
58 changes: 50 additions & 8 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static duke.utils.Constants.LIST_COMMAND;
import static duke.utils.Constants.BYE_COMMAND;
import static duke.utils.Constants.DONE_COMMAND;
import static duke.utils.Constants.DELETE_COMMAND;
import static duke.utils.Constants.TODO_COMMAND;
import static duke.utils.Constants.DEADLINE_COMMAND;
import static duke.utils.Constants.EVENT_COMMAND;
Expand Down Expand Up @@ -49,8 +50,16 @@ public static void main(String[] args) {
displayInvalidTaskNumberMessage();
}
break;
case BYE_COMMAND:
displayExitMessage();
case DELETE_COMMAND:
try {
deleteTask(Integer.parseInt(split[1].trim()));
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (NumberFormatException e) {
displayInvalidTaskNumberMessage();
} catch (ChatboxException e) {
displayInvalidTaskNumberMessage();
}
break;
case TODO_COMMAND:
try {
Expand All @@ -64,15 +73,22 @@ public static void main(String[] args) {
addDeadline(split[1]);
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (ChatboxException e) {
displayTimeMissingMessage();
}
break;
case EVENT_COMMAND:
try {
addEvent(split[1]);
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (ChatboxException e) {
displayTimeMissingMessage();
}
break;
case BYE_COMMAND:
displayExitMessage();
break;
default:
displayCommandNotFoundMessage();
break;
Expand Down Expand Up @@ -111,14 +127,22 @@ private static void markAsDone(int taskNumber) throws ChatboxException {
System.out.print(" ");
System.out.println(task);
}

private static void deleteTask(int taskNumber) throws ChatboxException {
if (taskNumber > tasks.size() || taskNumber <= 0) {
throw new ChatboxException();
}

System.out.println("Noted. I've removed this task: ");
System.out.print(" ");
System.out.println(tasks.get(taskNumber - 1));
tasks.remove(taskNumber - 1);
System.out.println(String.format("Now you have %d tasks in the list.", tasks.size()));
}

private static void displayInvalidTaskNumberMessage() {
System.out.println("Please enter a valid task number~");
}

private static void displayExitMessage() {
System.out.println("Bye. Hope to see you again soon!");
}

private static void addTodo(String description) {
String taskDescription = description.trim();
Expand All @@ -127,17 +151,27 @@ private static void addTodo(String description) {
displayAddTaskMessage(task);
}

private static void addDeadline(String description) {
private static void addDeadline(String description) throws ChatboxException {
String[] taskBy = description.split(DEADLINE_MARKER);

if (taskBy.length != 2) {
throw new ChatboxException();
}

String taskDescription = taskBy[0].trim();
String by = taskBy[1].trim();
Task task = new Deadline(taskDescription, by);
tasks.add(task);
displayAddTaskMessage(task);
}

private static void addEvent(String description) {
private static void addEvent(String description) throws ChatboxException {
String[] taskAt = description.split(EVENT_MARKER);

if (taskAt.length != 2) {
throw new ChatboxException();
}

String taskDescription = taskAt[0].trim();
String at = taskAt[1].trim();
Task task = new Event(taskDescription, at);
Expand All @@ -152,11 +186,19 @@ private static void displayAddTaskMessage(Task task) {
System.out.println(String.format("Now you have %d tasks in the list.", tasks.size()));
}

private static void displayExitMessage() {
System.out.println("Bye. Hope to see you again soon!");
}

private static void displayCommandNotFoundMessage() {
System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-(");
}

private static void displayEmptyDescriptionMessage(String command) {
System.out.println(String.format("OOPS!!! The description of a %s cannot be empty.", command));
}

private static void displayTimeMissingMessage() {
System.out.println("Oops! Time is missing!");
}
}
1 change: 1 addition & 0 deletions src/main/java/duke/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Constants {
public static final String LIST_COMMAND = "list";
public static final String BYE_COMMAND = "bye";
public static final String DONE_COMMAND = "done";
public static final String DELETE_COMMAND = "delete";
public static final String TODO_COMMAND = "todo";
public static final String DEADLINE_COMMAND = "deadline";
public static final String EVENT_COMMAND = "event";
Expand Down

0 comments on commit 3278712

Please sign in to comment.