diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index 5abceaebbc..c65fdceab7 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -25,7 +25,7 @@ public UnmarkCommand(Integer number) { @Override public String execute(TaskList tasks) { String message = "OK, I've marked this task as not done yet:\n"; - Task unMarkedTask = tasks.unMarkTask(super.index); + Task unMarkedTask = tasks.unmarkTask(super.index); return message + unMarkedTask; } diff --git a/src/main/java/duke/functionality/Parser.java b/src/main/java/duke/functionality/Parser.java index 3883a9e394..253ada6ff0 100644 --- a/src/main/java/duke/functionality/Parser.java +++ b/src/main/java/duke/functionality/Parser.java @@ -30,6 +30,9 @@ public class Parser { private static final int TODO_OFFSET = 4; private static final int DEADLINE_OFFSET = 8; private static final int INPUT_OFFSET = 3; + private static final int FIRST_INPUT = 0; + private static final int SECOND_INPUT = 1; + private static final int THIRD_INPUT = 2; /** * Returns the formatted date. @@ -75,6 +78,12 @@ public static String timeToString(LocalTime input) { return time; } + private static void checkDescriptionLength(String description, String command) throws IncompleteCommandException { + if (description.length() == 0) { + throw new IncompleteCommandException(command); + } + } + /** * Returns the respective command from user input. * @param input user input. Eg, "todo run". @@ -82,8 +91,8 @@ public static String timeToString(LocalTime input) { * @throws DukeException when user input for date and time are not of the correct form. */ public static Command parse(String input) throws DukeException { - String[] inputSplit = input.split(" "); //split input by space - String command = inputSplit[0]; + String[] inputSplit = input.split(" "); + String command = inputSplit[FIRST_INPUT]; if (command.equals("bye")) { return new ExitCommand(); @@ -92,67 +101,54 @@ public static Command parse(String input) throws DukeException { } else if (command.equals("todo")) { String description = input.substring(TODO_OFFSET).trim(); - - if (description.length() == 0) { - throw new IncompleteCommandException(command); - } + checkDescriptionLength(description, command); return new AddCommand(new Todo(description)); } else if (command.equals("deadline")) { String[] inputSlash = input.split("/"); - String description = inputSlash[0].substring(DEADLINE_OFFSET).trim(); - - if (description.length() == 0) { - throw new IncompleteCommandException(command); - } + String description = inputSlash[FIRST_INPUT].substring(DEADLINE_OFFSET).trim(); + checkDescriptionLength(description, command); - String durationInput = inputSlash[1].substring(INPUT_OFFSET); + String durationInput = inputSlash[SECOND_INPUT].substring(INPUT_OFFSET); String[] splitDuration = durationInput.split(" "); - LocalDate date; - LocalTime time; + try { - date = formatDate(splitDuration[0]); - time = formatTime(splitDuration[1]); + LocalDate date = formatDate(splitDuration[FIRST_INPUT]); + LocalTime time = formatTime(splitDuration[SECOND_INPUT]); + return new AddCommand(new Deadline(description, date, time)); + } catch (DateTimeParseException e) { throw new DukeException("The expected input form is deadline xxx /by yyyy-mm-dd hhmm"); } - return new AddCommand(new Deadline(description, date, time)); - } else if (command.equals("event")) { String[] inputSlash = input.split("/"); - String description = inputSlash[0].substring(EVENT_OFFSET).trim(); + String description = inputSlash[FIRST_INPUT].substring(EVENT_OFFSET).trim(); + checkDescriptionLength(description, command); - if (description.length() == 0) { - throw new IncompleteCommandException(command); - } - - String durationInput = inputSlash[1].substring(INPUT_OFFSET); + String durationInput = inputSlash[SECOND_INPUT].substring(INPUT_OFFSET); String[] splitDuration = durationInput.split(" "); - LocalDate date; - LocalTime startTime; - LocalTime endTime; + try { - date = formatDate(splitDuration[0]); - startTime = formatTime(splitDuration[1]); - endTime = formatTime(splitDuration[2]); + LocalDate date = formatDate(splitDuration[FIRST_INPUT]); + LocalTime startTime = formatTime(splitDuration[SECOND_INPUT]); + LocalTime endTime = formatTime(splitDuration[THIRD_INPUT]); + return new AddCommand(new Event(description, date, startTime, endTime)); } catch (DateTimeParseException e) { throw new DukeException("The expected input form is event xxx /at yyyy-mm-dd hhmm hhmm"); } - return new AddCommand(new Event(description, date, startTime, endTime)); - } else if (command.equals("mark")) { - return new MarkCommand(Integer.parseInt(inputSplit[1])); + return new MarkCommand(Integer.parseInt(inputSplit[SECOND_INPUT])); } else if (command.equals("unmark")) { - return new UnmarkCommand(Integer.parseInt(inputSplit[1])); + return new UnmarkCommand(Integer.parseInt(inputSplit[SECOND_INPUT])); } else if (command.equals("delete")) { - return new DeleteCommand(Integer.parseInt(inputSplit[1])); + return new DeleteCommand(Integer.parseInt(inputSplit[SECOND_INPUT])); } else if (command.equals("find")) { - return new FindCommand(inputSplit[1]); + return new FindCommand(inputSplit[SECOND_INPUT]); } else if (command.equals("")) { throw new BlankCommandException(); diff --git a/src/main/java/duke/functionality/Storage.java b/src/main/java/duke/functionality/Storage.java index e0a454aa91..2e1e75e170 100644 --- a/src/main/java/duke/functionality/Storage.java +++ b/src/main/java/duke/functionality/Storage.java @@ -18,6 +18,12 @@ public class Storage { protected static String pwd; protected static String path; + private static final int FIRST_INPUT = 0; + private static final int SECOND_INPUT = 1; + private static final int THIRD_INPUT = 2; + private static final int FOURTH_INPUT = 3; + private static final int FIFTH_INPUT = 4; + private static final int SIXTH_INPUT = 5; /** * Constructor for Storage class. @@ -69,34 +75,39 @@ public TaskList readFileDataAndStoreInList(File file) throws FileNotFoundExcepti TaskList newTaskList = new TaskList(); while ((sc.hasNextLine())) { String input = sc.nextLine(); - String[] inputSplit = input.split("\\|"); //split input by | - String task = inputSplit[0]; - int mark = Integer.parseInt(inputSplit[1]); + String[] inputSplit = input.split("\\|"); + String task = inputSplit[FIRST_INPUT]; + int mark = Integer.parseInt(inputSplit[SECOND_INPUT]); if (task.equals("T")) { - Todo tempTask = new Todo(inputSplit[2]); - if (mark == 1) { - tempTask.setTaskDone(); - } - newTaskList.addToList(tempTask); + Todo tempTask = new Todo(inputSplit[THIRD_INPUT]); + newTaskList.addToList(checkTaskDone(mark, tempTask)); + } else if (task.equals("D")) { - Deadline tempTask = new Deadline(inputSplit[2], Parser.formatDate(inputSplit[3]), - Parser.formatTime(inputSplit[4])); - if (mark == 1) { - tempTask.setTaskDone(); - } - newTaskList.addToList(tempTask); + Deadline tempTask = new Deadline(inputSplit[THIRD_INPUT], Parser.formatDate(inputSplit[FOURTH_INPUT]), + Parser.formatTime(inputSplit[FIFTH_INPUT])); + newTaskList.addToList(checkTaskDone(mark, tempTask)); + } else if (task.equals("E")) { - Event tempTask = new Event(inputSplit[2], Parser.formatDate(inputSplit[3]) , - Parser.formatTime(inputSplit[4]), Parser.formatTime(inputSplit[5])); - if (mark == 1) { - tempTask.setTaskDone(); - } - newTaskList.addToList(tempTask); + Event tempTask = new Event(inputSplit[THIRD_INPUT], Parser.formatDate(inputSplit[FOURTH_INPUT]) , + Parser.formatTime(inputSplit[FIFTH_INPUT]), Parser.formatTime(inputSplit[SIXTH_INPUT])); + newTaskList.addToList(checkTaskDone(mark, tempTask)); } } return newTaskList; } + /** + * Returns nothing, but used to check if a Task is done or not. + * @param mark indicator, 1 is done 0 is not done. + * @param task the Task object to be checked. + */ + public Task checkTaskDone(int mark, Task task) { + if(mark == 1){ + task.setTaskDone(); + } + return task; + } + /** * Returns a crafted output to be stored in the text file. * @param task the task created in Parser class. diff --git a/src/main/java/duke/functionality/TaskList.java b/src/main/java/duke/functionality/TaskList.java index 01979a279b..11c3ef8745 100644 --- a/src/main/java/duke/functionality/TaskList.java +++ b/src/main/java/duke/functionality/TaskList.java @@ -33,8 +33,8 @@ public Task deleteTask(int taskNum) { * @return the specified task to be marked. */ public Task markTask(int taskNum) { - int actualTaskNum = taskNum - 1; //minus 1 as list index is from 0 - Task markedTask = taskList.get(actualTaskNum); // get the task from the array + int actualTaskNum = taskNum - 1; + Task markedTask = taskList.get(actualTaskNum); markedTask.setTaskDone(); return markedTask; } @@ -44,9 +44,9 @@ public Task markTask(int taskNum) { * @param taskNum an indicator to the index of taskList. * @return the specified task to be unmarked. */ - public Task unMarkTask(int taskNum) { + public Task unmarkTask(int taskNum) { int actualTaskNum = taskNum - 1; - Task unMarkedtask = taskList.get(actualTaskNum); // get the task from the array + Task unMarkedtask = taskList.get(actualTaskNum); unMarkedtask.setTaskNotDone(); return unMarkedtask; } diff --git a/src/main/java/duke/functionality/Ui.java b/src/main/java/duke/functionality/Ui.java deleted file mode 100644 index 7c7c235b52..0000000000 --- a/src/main/java/duke/functionality/Ui.java +++ /dev/null @@ -1,57 +0,0 @@ -package duke.functionality; - -import java.util.Scanner; - -import duke.exception.BlankCommandException; - -/** - * Represents the User Interface of the Duke project. A Ui object corresponds - * to all user interface displays. Such as the messages. - */ -public class Ui { - private static final String GREETING = "Hello! I'm TaskJamie\nWhat can i do for you?"; - private static final String ENDING = "Bye. Hope to see you again soon!"; - - /** - * Returns nothing, but prints out the greeting message of Duke TaskBot. - */ - public void showGreeting() { - System.out.println(GREETING); - } - - /** - * Returns nothing, but prints out the leaving message of Duke TaskBot. - */ - public void showEnding() { - System.out.println(ENDING); - } - - /** - * Returns nothing, but prints out the error messages caught by Duke TaskBot. - */ - public void showError(String error) { - System.out.println(error); - } - - /** - * Returns nothing, but prints out the error messages when loading in the text file. - */ - public void showLoadingError(String error) { - System.out.println(error); - } - - /** - * Returns a full command gotten from user input. - * @return full command. - * @throws BlankCommandException if user inputs nothing. Eg, " ". - */ - public String readCommand() throws BlankCommandException { - Scanner sc = new Scanner(System.in); - String input; - input = sc.nextLine(); - if (input.length() == 0) { - throw new BlankCommandException(); - } - return input; - } -}