Skip to content

Commit

Permalink
Merge pull request #49 from lmtaek/master
Browse files Browse the repository at this point in the history
[AY1920S1-CS2113-T13-2]-lmtaek-B-RecurringTasks
  • Loading branch information
lmtaek authored Sep 17, 2019
2 parents 48efb71 + 2cb17be commit e2f9750
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 58 deletions.
19 changes: 11 additions & 8 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
E | 0 | fsahufias | 30/06/2020 0316 | false
D | 0 | abc | 27/07/1996 2130 | false
E | 0 | fsahufias | 30/06/2020 0316 | ONCE
D | 0 | abc | 24/09/2019 0915 | DAILY
P | 0 | abc | 27/07/1996 2130 | 27/06/2029 1630
D | 0 | abc | 27/07/1996 2130 | false
T | 1 | abc | false
D | 0 | abc | 17/09/2019 2130 | false
E | 0 | werq | 17/09/2019 1500 | false
T | 0 | homework | false
E | 0 | abc | 06/06/2016 1530 | false
D | 0 | abc | 24/09/2019 1145 | DAILY
T | 1 | abc | DAILY
D | 0 | abc | 17/09/2019 2130 | ONCE
E | 0 | werq | 17/09/2019 1500 | ONCE
T | 0 | homework | ONCE
E | 0 | abc | 24/09/2019 1530 | DAILY
P | 0 | abc | 27/08/2019 1630 | 29/11/2020 1630
P | 0 | abc | 27/08/2019 1630 | 29/11/2020 1630
P | 0 | abc | 27/08/2019 1630 | 29/11/2020 1630
E | 1 | dog | 24/09/2019 0001 | MONTHLY
D | 0 | cat | 11/10/2019 0001 | DAILY
E | 0 | rabbit | 23/09/2019 0909 | WEEKLY
19 changes: 10 additions & 9 deletions src/main/java/duke/command/RecurringCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public class RecurringCommand extends Command {
* Used to identify the task being marked as recurring.
*/
private int taskIndex;
protected Task.RecurringFrequency frequency;

public RecurringCommand(int taskIndex) {
public RecurringCommand(int taskIndex, Task.RecurringFrequency frequency) {
super();
this.taskIndex = taskIndex;
this.frequency = frequency;
}

/**
Expand All @@ -43,16 +45,15 @@ public RecurringCommand(int taskIndex) {
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
try {
Task recurringTask = tasks.getTask(taskIndex);
if (recurringTask.getDateTime() != null) {
if (!recurringTask.isTaskRecurring()) {
recurringTask.makeTaskRecurring();
ui.makeRecurring(recurringTask);
Task task = tasks.getTask(taskIndex);
if (task.getDateTime() != null) {
if (!task.isTaskRecurring()) {
task.makeTaskRecurring(this.frequency);
ui.makeRecurring(task);
} else {
System.out.println("This task is already marked as recurring!");
}
recurringTask.recurringTaskTimeUpdate();
storage.save(tasks.fullTaskList());
} else {
recurringTask.updateLocalDateTime(LocalDateTime.now().toString());
}
} catch (DukeException e) {
throw new DukeException("I couldn't make the task recurring. " + e);
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/duke/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,20 @@ public static Command parse(String ss) throws DukeException {
}
case "recurring":
try {
int index = Integer.parseInt(command[1]);
return new RecurringCommand(index);
String[] parsedInput = command[1].split(" /");
int index = Integer.parseInt(parsedInput[0]);
if (parsedInput[1] != null) {
parsedInput[1] = parsedInput[1].trim();
if (parsedInput[1].toLowerCase().contains("weekly")) {
return new RecurringCommand(index, Task.RecurringFrequency.WEEKLY);
} else if (parsedInput[1].toLowerCase().contains("monthly")) {
return new RecurringCommand(index, Task.RecurringFrequency.MONTHLY);
} else if (parsedInput[1].toLowerCase().contains("daily")) {
return new RecurringCommand(index, Task.RecurringFrequency.DAILY);
} else {
return new RecurringCommand(index, Task.RecurringFrequency.DAILY);
}
}
} catch (Exception e) {
throw new DukeException("Failed to make your task recurring." + e.getMessage());
}
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/duke/core/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public ArrayList<Task> load() throws DukeException {
if (newTask[1].equals("1")) {
x.markAsDone();
}
if (newTask[3].equals("true")) {
x.makeTaskRecurring();
if ((newTask[3] != null) && !(newTask[3].equals("ONCE"))) {
x.makeTaskRecurring(giveFrequency(newTask[3]));
}
tasks.add(x);
}
Expand All @@ -57,8 +57,8 @@ else if (newTask[0].equals("D")) {
if (newTask[1].equals("1")) {
t.markAsDone();
}
if (newTask[4].equals("true")) {
t.makeTaskRecurring();
if ((newTask[4] != null) && !(newTask[4].equals("ONCE"))) {
t.makeTaskRecurring(giveFrequency(newTask[4]));
}
tasks.add(t);
}
Expand All @@ -67,8 +67,8 @@ else if (newTask[0].equals("E")) {
if (newTask[1].equals("1")) {
t.markAsDone();
}
if (newTask[4].equals("true")) {
t.makeTaskRecurring();
if ((newTask[4] != null) && !(newTask[4].equals("ONCE"))) {
t.makeTaskRecurring(giveFrequency(newTask[4]));
}
tasks.add(t);
}
Expand All @@ -84,9 +84,6 @@ else if (newTask[0].equals("P")) {
if (newTask[1].equals("1")) {
x.markAsDone();
}
if (newTask[4].equals("true")) {
x.makeTaskRecurring();
}
tasks.add(x);
}

Expand Down Expand Up @@ -115,4 +112,17 @@ public void save(ArrayList<Task> task) throws DukeException {
}
}

private Task.RecurringFrequency giveFrequency(String string) {
switch (string) {
case "DAILY":
return Task.RecurringFrequency.DAILY;
case "WEEKLY":
return Task.RecurringFrequency.WEEKLY;
case "MONTHLY":
return Task.RecurringFrequency.MONTHLY;
default:
return Task.RecurringFrequency.ONCE;
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/duke/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void showWelcome() {
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("Hello! I'm Duke\nWhat can I do for you?");
System.out.println("Hello! I'm Duke\nWhat can I do for you?\n");
}

/**
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import duke.core.DateTimeParser;
import duke.core.DukeException;

import java.time.format.DateTimeFormatter;

/**
* Represents a task with a deadline. It is
* extended from the Task class.
Expand Down Expand Up @@ -39,7 +41,21 @@ public Deadline(String description, String dateTime) throws DukeException {
*/
@Override
public String toString() {
return "[D]" + super.printStatus() + " (by: " + dateTimeEnglish + ")";

if (recurringTask != null) {
DateTimeFormatter newDateFormatter = DateTimeFormatter.ofPattern("dd/MM/YYYY HHmm");
String newDate = recurringTask.recurringTaskTimeUpdate(this).format(newDateFormatter);
this.dateTime = newDate;
try {
this.dateTimeEnglish = DateTimeParser.convertToEnglishDateTime(dateTime);
} catch (DukeException e) {
System.out.println("I couldn't convert your given time. " + e);
}
}
return "[D]"
+ super.printStatus()
+ " (by: "
+ dateTimeEnglish + ")";
}

/**
Expand All @@ -48,14 +64,18 @@ public String toString() {
* @return A string in a specific format to be stored in a local file.
*/
public String writeTxt() {
String frequency = "ONCE";
if (isTaskRecurring()) {
frequency = recurringTask.getFrequency().toString();
}
return "D | "
+ (isDone() ? "1" : "0")
+ " | "
+ getDescription()
+ " | "
+ dateTime
+ " | "
+ this.isRecurring;
+ frequency;
}

}
25 changes: 23 additions & 2 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import duke.core.DateTimeParser;
import duke.core.DukeException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Represents a task with a event. It is
* extended from the Task class.
Expand Down Expand Up @@ -37,7 +40,21 @@ public Event(String description, String dateTime) throws DukeException {
*/
@Override
public String toString() {
return "[E]" + super.printStatus() + " (at: " + dateTimeEnglish + ")";

if (recurringTask != null) {
DateTimeFormatter newDateFormatter = DateTimeFormatter.ofPattern("dd/MM/YYYY HHmm");
String newDate = recurringTask.recurringTaskTimeUpdate(this).format(newDateFormatter);
this.dateTime = newDate;
try {
this.dateTimeEnglish = DateTimeParser.convertToEnglishDateTime(dateTime);
} catch (DukeException e) {
System.out.println("I couldn't convert your given time. " + e);
}
}
return "[E]"
+ super.printStatus()
+ " (at: "
+ dateTimeEnglish + ")";
}

/**
Expand All @@ -46,13 +63,17 @@ public String toString() {
* @return A string in a specific format to be stored in a local file.
*/
public String writeTxt() {
String frequency = "ONCE";
if (isTaskRecurring()) {
frequency = recurringTask.getFrequency().toString();
}
return "E | "
+ (isDone() ? "1" : "0")
+ " | "
+ getDescription()
+ " | "
+ dateTime
+ " | "
+ this.isRecurring;
+ frequency;
}
}
63 changes: 63 additions & 0 deletions src/main/java/duke/task/RecurringTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package duke.task;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

public class RecurringTask {

public enum RecurringFrequency { DAILY, WEEKLY, MONTHLY};
public enum TaskType { TODO, DEADLINE, EVENT}

private LocalDateTime lastRecordedTime;
private RecurringFrequency frequency;
private TaskType taskType;

public RecurringTask(Task task, RecurringFrequency frequency) {
if (task instanceof Todo) { this.taskType = TaskType.TODO; }
if (task instanceof Deadline) { this.taskType = TaskType.DEADLINE; }
if (task instanceof Event) { this.taskType = TaskType.EVENT; }

if (task.getDateTime() != null) {
lastRecordedTime = task.getDateTime();
}
this.frequency = frequency;
}

public RecurringFrequency getFrequency() { return frequency; }

/**
* When a task is recurring, method compares current time to listed date.
* If the task's date is outdated, then it will update to be for the next day.
*/
public LocalDateTime recurringTaskTimeUpdate(Task task) {
if (lastRecordedTime != null) {
try {
LocalDateTime currentTime = LocalDateTime.now();
if (lastRecordedTime.isBefore(currentTime)) {

switch (this.frequency) {
case DAILY:
while (lastRecordedTime.isBefore(currentTime) || task.isDone()) {
lastRecordedTime = lastRecordedTime.plusDays(1);
if (task.isDone()) { task.isDone = false; }
}
case WEEKLY:
while (lastRecordedTime.isBefore(currentTime) || task.isDone()) {
lastRecordedTime = lastRecordedTime.plusWeeks(1);
if (task.isDone) { task.isDone = false; }
}
case MONTHLY:
while (lastRecordedTime.isBefore(currentTime) || task.isDone()) {
lastRecordedTime = lastRecordedTime.plusMonths(1);
if (task.isDone) { task.isDone = false; }
}
}
}
} catch (DateTimeParseException e) {
System.out.println("I couldn't update your recurring events' times.");
}
}
return lastRecordedTime;
}
}
Loading

0 comments on commit e2f9750

Please sign in to comment.