Skip to content

Commit

Permalink
Add Increment: Level-8: Dates and Times
Browse files Browse the repository at this point in the history
This version of Duke teaches it how to understand dates and times and, show tasks which happen on a specific date.
  • Loading branch information
zikunz committed Mar 5, 2021
1 parent 684c1d3 commit 8c81793
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 13 deletions.
3 changes: 3 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
D | 0 | 123 | 2020-01-09
E | 0 |321 | 2020-01-09
E | 0 |778 | 2020-02-09
3 changes: 2 additions & 1 deletion src/main/java/Duke/Command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public boolean shouldExit() {
* @throws DukeException the exceptions which can happen
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException {
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
Task task;

switch (taskType) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Duke/Command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public abstract class Command {
* @param storage store the data
* @throws DukeException the exceptions which can happen
*/
public abstract void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException;
public abstract void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException;
}
38 changes: 38 additions & 0 deletions src/main/java/Duke/Command/DateCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Duke.Command;

import Duke.Constant.Message;
import Duke.Constant.SpaceAndLine;
import Duke.Exception.DukeException;
import Duke.Storage.Storage;
import Duke.TaskList.TaskList;
import Duke.Ui.Ui;

import java.time.LocalDate;

public class DateCommand extends Command {
private final String dateString;
private LocalDate date;

public DateCommand(String dateString) {
this.dateString = dateString;
}

@Override
public boolean shouldExit() {
return false;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
ui.printMessage(SpaceAndLine.SEPARATION_LINE);
ui.printMessage(SpaceAndLine.SHORT_SPACE + Message.SHOW_SAME_DATE_TASKS_MESSAGE);

date = LocalDate.parse(dateString);
tasks.findDate(date);

ui.printsameDateTasks(sameDateTasks);
ui.printMessage(SpaceAndLine.SEPARATION_LINE);

}
}
3 changes: 2 additions & 1 deletion src/main/java/Duke/Command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public boolean shouldExit() {
* @throws DukeException the exceptions which can happen
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException {
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
ui.printMessage(SpaceAndLine.SEPARATION_LINE);
ui.printMessage(Message.REMOVE_TASK_MESSAGE);
System.out.println(SpaceAndLine.LONG_SPACE + tasks.getTask(index));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Duke/Command/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public boolean shouldExit() {
* @throws DukeException the exceptions which can happen
* */
@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException {
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
tasks.setTaskToBeDone(index);

ui.printMessage(SpaceAndLine.SEPARATION_LINE);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Duke/Command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public boolean shouldExit() {
* @throws DukeException the exceptions which can happen
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException {
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
// Do nothing
}
}
5 changes: 3 additions & 2 deletions src/main/java/Duke/Command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public boolean shouldExit() {
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException {
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
ui.printMessage(SpaceAndLine.SEPARATION_LINE);
ui.printMessage(SpaceAndLine.SHORT_SPACE + Message.SHOW_MATCHING_TASKS_MESSAGE);
ui.printMessage(SpaceAndLine.SHORT_SPACE + Message.SHOW_MATCHING_KEYWORD_TASKS_MESSAGE);

tasks.findKeyword(keyword);
ui.printMatchedTasks(matchedTasks);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Duke/Command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public boolean shouldExit() {
* @throws DukeException the exceptions which can happen in this function
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks) throws DukeException {
public void execute(TaskList tasks, Ui ui, Storage storage, TaskList matchedTasks, TaskList sameDateTasks)
throws DukeException {
ui.printMessage(SpaceAndLine.SEPARATION_LINE);
switch (tasks.getSize()) {
case 0:
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/Duke/Constant/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public class Message {
SpaceAndLine.SEPARATION_LINE + "\n";
public static final String ADD_TASK_MESSAGE = SpaceAndLine.SHORT_SPACE + "Got it. I've added this task:";
public static final String REMOVE_TASK_MESSAGE = SpaceAndLine.SHORT_SPACE + "Noted. I've removed this task:";
public static final String SHOW_MATCHING_TASKS_MESSAGE = "Here are the matching tasks in your list:";
public static final String SHOW_MATCHING_KEYWORD_TASKS_MESSAGE = "Here are the matching task(s) in your list:";
public static final String EXIT_MESSAGE = SpaceAndLine.SEPARATION_LINE + "\n" +
" Bye. Hope to see you again soon!\n" +
SpaceAndLine.SEPARATION_LINE;
public static final String NON_EXISTING_LETTER_WRONG_MESSAGE = "Something wrong happened!";
public static final String LOADING_ERROR_MESSAGE = "Unable to load any file!";
public static final String SHOW_SAME_DATE_TASKS_MESSAGE =
"Here are the task(s) occurring on the same date in your list:";
}
1 change: 1 addition & 0 deletions src/main/java/Duke/Constant/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class Number {
public static final int DONE_COMMAND_LENGTH_ADD_ONE = 5;
public static final int DELETE_COMMAND_LENGTH_ADD_ONE = 7;
public static final int FIND_COMMAND_LENGTH_ADD_ONE = 5;
public static final int DATE_COMMAND_LENGTH_ADD_ONE = 5;
}
3 changes: 2 additions & 1 deletion src/main/java/Duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Duke {
private Storage storage;
private TaskList tasks;
private TaskList matchedTasks = new TaskList();
private TaskList sameDateTasks = new TaskList();
private Ui ui;
private Parser parser = new Parser();

Expand All @@ -43,7 +44,7 @@ public void run() {
try {
String userInput = ui.readCommand();
Command userCommand = parser.processInput(userInput);
userCommand.execute(tasks, ui, storage, matchedTasks);
userCommand.execute(tasks, ui, storage, matchedTasks, sameDateTasks);
shouldExit = userCommand.shouldExit();
} catch (DukeException e) {
ui.printMessage(Message.WRONG_COMMAND_ERROR_MESSAGE);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/Duke/Parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Parser {
private static final String COMMAND_LIST = "list";
private static final String COMMAND_DELETE = "delete";
private static final String COMMAND_FIND = "find";
private static final String COMMAND_DATE = "date";

/**
* This function calls the correct command to process different user inputs.
Expand Down Expand Up @@ -58,15 +59,17 @@ public Command processInput(String userInput) throws DukeException {
return new AddCommand(TaskType.EVENT_TYPE, userInput.substring(Number.EVENT_COMMAND_LENGTH_ADD_ONE));
case COMMAND_DONE:
return new DoneCommand(Integer.parseInt(userInput.substring(Number.DONE_COMMAND_LENGTH_ADD_ONE)) - 1);
case COMMAND_BYE:
return new ExitCommand();
case COMMAND_LIST:
return new ListCommand();
case COMMAND_DELETE:
return new DeleteCommand
(Integer.parseInt(userInput.substring(Number.DELETE_COMMAND_LENGTH_ADD_ONE)) - 1);
case COMMAND_FIND:
return new FindCommand(userInput.substring(Number.FIND_COMMAND_LENGTH_ADD_ONE));
case COMMAND_DATE:
return new DateCommand(userInput.substring(Number.DATE_COMMAND_LENGTH_ADD_ONE));
case COMMAND_BYE:
return new ExitCommand();
default:
throw new DukeException(Message.WRONG_COMMAND_ERROR_MESSAGE);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/Duke/Task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public String toString() {
public String toPrintedFormat() {
return TaskType.DEADLINE_TYPE + " | " + super.getIntegerType() + " | " + description + " | " + getDeadline();
}

@Override
public String getDate() {
return getDeadline();
}
}
5 changes: 5 additions & 0 deletions src/main/java/Duke/Task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public String toString() {
public String toPrintedFormat() {
return TaskType.EVENT_TYPE + " | " + super.getIntegerType() + " |" + description + " | " + getPeriod();
}

@Override
public String getDate() {
return getPeriod();
}
}
4 changes: 4 additions & 0 deletions src/main/java/Duke/Task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public String toString() {
public String toPrintedFormat() {
return "";
}

public String getDate() {
return "";
}
}
20 changes: 19 additions & 1 deletion src/main/java/Duke/TaskList/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import Duke.Constant.SpaceAndLine;
import Duke.Task.Task;

import java.time.LocalDate;
import java.util.ArrayList;

/**
* Contains the list of tasks and provides methods to add / delete / mark as done.
*/
public class TaskList {
private static ArrayList<Task> tasks = new ArrayList<Task>();
private static ArrayList<Task> matchedTasks = new ArrayList<>();
private static ArrayList<Task> matchedTasks = new ArrayList<Task>();
private static ArrayList<Task> sameDateTasks = new ArrayList<Task>();

public TaskList() {

Expand Down Expand Up @@ -116,4 +118,20 @@ public void DisplayMatchedTasks() {
System.out.println(SpaceAndLine.SHORT_SPACE + (i + 1) + "." + matchedTasks.get(i));
}
}

public void findDate(LocalDate date) {
for (int i = 0; i < tasks.size(); i++) {
String dateString = getTask(i).getDate();
LocalDate convertedDate = LocalDate.parse(dateString);
if (date.equals(convertedDate)) {
sameDateTasks.add(getTask(i));
}
}
}

public void DisplaySameDateTasks() {
for (int i = 0; i < sameDateTasks.size(); i++) {
System.out.println(SpaceAndLine.SHORT_SPACE + (i + 1) + "." + sameDateTasks.get(i));
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/Duke/Ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public void printTasks(TaskList tasks) {
public void printMatchedTasks(TaskList matchedTasks) {
matchedTasks.DisplayMatchedTasks();
}

public void printsameDateTasks(TaskList sameDateTasks) {
sameDateTasks.DisplaySameDateTasks();
}
}

0 comments on commit 8c81793

Please sign in to comment.