Skip to content

Commit

Permalink
Remove instanceof from ReminderCommand (nusCS2113-AY1920S1#36)
Browse files Browse the repository at this point in the history
* Remove instanceof from ReminderCommand

* Generalize reminders to tasks
  • Loading branch information
muserr authored Sep 26, 2019
1 parent 1ea3d72 commit b79782d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/main/java/duchess/logic/commands/ReminderCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import duchess.ui.Ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* Searches Tasklist and filters out deadline objects.
Expand Down Expand Up @@ -36,13 +38,10 @@ public void execute(TaskList taskList, Ui ui, Storage storage) throws DukeExcept
* @param taskList of user inputs
*/
private List<Task> addDeadlines(TaskList taskList) {
ArrayList<Task> reminderList = new ArrayList<>();
for (Task task : taskList.getTasks()) {
if (task instanceof Deadline) {
reminderList.add(task);
}
}
return reminderList;
return taskList.getTasks().stream()
.map(task -> task.getReminders())
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/duchess/model/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -50,6 +51,13 @@ public void snooze() {
deadline.setTime(date.getTimeInMillis());
}

@Override
public List<Task> getReminders() {
List<Task> list = new ArrayList<>();
list.add(this);
return list;
}

@Override
public String toString() {
return String.format("[D]%s %s (by: %s)", super.toString(), this.description, formatter.format(this.deadline));
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/duchess/model/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -91,6 +92,11 @@ public void snooze() {
end.setTime(date.getTimeInMillis());
}

@Override
public List<Task> getReminders() {
return new ArrayList<>();
}

@Override
public String toString() {
return String.format("[E]%s %s (at: %s to %s)", super.toString(), this.description,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duchess/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public abstract class Task implements Serializable {
private boolean isDone;
Expand All @@ -27,4 +28,6 @@ public String toString() {
public abstract Schedule isWithinTimeFrame(Date startDate, Date endDate);

public abstract void snooze() throws DukeException;

public abstract List<Task> getReminders();
}
6 changes: 6 additions & 0 deletions src/main/java/duchess/model/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import duchess.logic.commands.exceptions.DukeException;
import duchess.model.Schedule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -41,4 +42,9 @@ public Schedule isWithinTimeFrame(Date startDate, Date endDate) {
public void snooze() throws DukeException {
throw new DukeException("You can't snooze that task.");
}

@Override
public List<Task> getReminders() {
return new ArrayList<>();
}
}

0 comments on commit b79782d

Please sign in to comment.