Skip to content

Commit

Permalink
Refactor TimeFrame class (nusCS2113-AY1920S1#42)
Browse files Browse the repository at this point in the history
* Reimplement TimeFrameClass

* Refactor ViewScheduleCommand

* Allow for instantaneous tasks

* Implement printing of schedule
  • Loading branch information
mohideenik authored Sep 29, 2019
1 parent 1c586ae commit 72bd9de
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 193 deletions.
35 changes: 16 additions & 19 deletions src/main/java/duchess/logic/commands/ViewScheduleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class ViewScheduleCommand extends Command {
private List<String> words;
private List<TimeFrame> schedules;
private Date start;
private Date end;
private TimeFrame timeFrame;

/**
* Constructor for class.
Expand All @@ -28,9 +25,10 @@ public class ViewScheduleCommand extends Command {
*/
public ViewScheduleCommand(List<String> words) throws DukeException {
this.words = words;
this.schedules = new ArrayList<>();
this.start = returnDate(" 0000");
this.end = returnDate(" 2359");

Date start = processDate(" 0000");
Date end = processDate(" 2359");
this.timeFrame = new TimeFrame(start, end);
}

/**
Expand All @@ -39,7 +37,7 @@ public ViewScheduleCommand(List<String> words) throws DukeException {
* @return Date
* @throws DukeException Exception thrown for invalid or missing date time
*/
private Date returnDate(String time) throws DukeException {
private Date processDate(String time) throws DukeException {
try {
// todo fix bug which allows input 'schedule 12/12/2019' without /for
String dateString = words.get(words.indexOf("/for") + 1) + time;
Expand All @@ -55,17 +53,16 @@ private Date returnDate(String time) throws DukeException {

@Override
public void execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {
for (Task t : taskList.getTasks()) {
TimeFrame tempSchedule = t.getTimeFrame(start, end);
if (tempSchedule != null) {
(this.schedules).add(tempSchedule);
}
}
if (schedules.size() <= 0) {
List<Task> tasksForToday =
taskList.getTasks().stream()
.filter(task -> task.getTimeFrame().fallsWithin(this.timeFrame))
.collect(Collectors.toList());

if (tasksForToday.size() <= 0) {
throw new DukeException("There are no tasks in the schedule.");
} else {
Collections.sort(schedules);
ui.showScheduleResult(schedules, words.get(words.indexOf("/for") + 1));
}

tasksForToday.sort((a, b) -> a.getTimeFrame().compareTo(b.getTimeFrame()));
ui.showScheduleResult(tasksForToday, words.get(words.indexOf("/for") + 1));
}
}
107 changes: 71 additions & 36 deletions src/main/java/duchess/model/TimeFrame.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,98 @@
package duchess.model;

import java.text.SimpleDateFormat;
import duchess.logic.commands.exceptions.DukeException;

import java.util.Date;

public class TimeFrame implements Comparable<TimeFrame> {
private Date time;
private String task;
private boolean isOngoing;
/**
* Start and end points of the timeframe.
*/
private Date start;
private Date end;

/**
* Constructor for schedule.
*
* @param time time to be shown in timetable
* @param toString task details
* Marks timeframe as indefinite, i.e. things that
* don't have a definite start or end time.
*/
public TimeFrame(Date time, String toString) {
this.time = time;
this.task = toString;
this.isOngoing = false;
}
private boolean isIndefinite;

/**
* Constructor for schedule. Tracks ongoing events.
* Marks time frame as instantaneous.
*/
private boolean isInstantaneous;

/**
* Creates a TimeFrame that represents an interval in time.
*
* @param time time to be shown in timetable
* @param toString task details
* @param ongoing event ongoing
* @param start Starting time
* @param end Ending time
*/
public TimeFrame(Date time, String toString, boolean ongoing) {
this.time = time;
this.task = toString;
this.isOngoing = ongoing;
public TimeFrame(Date start, Date end) {
this.start = start;
this.end = end;
this.isIndefinite = false;
this.isInstantaneous = false;
}

private TimeFrame(Date time) {
this.start = time;
this.end = time;
this.isIndefinite = false;
this.isInstantaneous = true;
}

public Date getStart() {
return time;
private TimeFrame() {
this.isIndefinite = true;
this.isInstantaneous = false;
}

public static TimeFrame ofInstantaneousTask(Date time) {
return new TimeFrame(time);
}

public static TimeFrame ofTimelessTask() {
return new TimeFrame();
}

/**
* Changes Date to String.
* Returns true if this TimeFrame lies within the other TimeFrame.
*
* @return string containing time of task
* @param that the other TimeFrame
*/
public String getStartString() {
SimpleDateFormat formatter = new SimpleDateFormat("HHmm");
formatter.setLenient(false);
return formatter.format(time);
}
public boolean fallsWithin(TimeFrame that) {
if (this.isIndefinite || that.isIndefinite) {
return false;
}

public String getTask() {
return task;
return !(this.end.before(that.start) || that.end.before(this.start));
}

public boolean getOngoing() {
return isOngoing;
/**
* Returns true if this TimeFrame clashes with the other TimeFrame.
*
* @param that the other TimeFrame
*/
public boolean clashesWith(TimeFrame that) {
if (this.isInstantaneous || that.isInstantaneous) {
return false;
}

return this.fallsWithin(that);
}

@Override
public int compareTo(TimeFrame timeFrame) {
return time.compareTo(timeFrame.time);
public int compareTo(TimeFrame that) {
if (this.isIndefinite && that.isIndefinite) {
return 0;
} else if (this.isIndefinite) {
return -1;
} else if (that.isIndefinite) {
return 1;
} else if (!this.start.equals(that.start)) {
return this.start.compareTo(that.start);
} else {
return this.end.compareTo(that.end);
}
}
}
18 changes: 2 additions & 16 deletions src/main/java/duchess/model/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,13 @@ public List<Task> getReminders() {
return list;
}

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

@Override
public boolean clashesWith(Task task) {
return false;
}

@Override
public String toString() {
return String.format("[D]%s %s (by: %s)", super.toString(), this.description, formatter.format(this.deadline));
}

@Override
public TimeFrame getTimeFrame(Date startDate, Date endDate) {
if (deadline.compareTo(startDate) >= 0 && deadline.compareTo(endDate) <= 0) {
return new TimeFrame(deadline, String.format("[D]%s %s", super.toString(), this.description));
}
return null;
public TimeFrame getTimeFrame() {
return TimeFrame.ofInstantaneousTask(this.deadline);
}

}
29 changes: 2 additions & 27 deletions src/main/java/duchess/model/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ public Event(List<String> input) throws DukeException {
}
}

/**
* Checks if the task being added clashes with this instance of event.
*
* @param task the task to be added
* @return true if the event clashes, false otherwise
*/
@Override
public boolean clashesWith(Task task) {
return (startClashes((Event) task) || endClashes((Event) task)
|| entireEventClashes((Event) task) || isStartOrEndEqual((Event) task));
}

private boolean startClashes(Event event) {
return event.start.after(this.start) && event.start.before(this.end);
}
Expand Down Expand Up @@ -98,27 +86,14 @@ public List<Task> getReminders() {
return new ArrayList<>();
}

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

@Override
public String toString() {
return String.format("[E]%s %s (at: %s to %s)", super.toString(), this.description,
formatter.format(this.start), formatter.format(this.end));
}

@Override
public TimeFrame getTimeFrame(Date startDate, Date endDate) {
if (start.compareTo(startDate) < 0 && end.compareTo(startDate) >= 0) { // starts before ends after that date
return new TimeFrame(start, String.format("[E]%s %s (at: %s to %s)", super.toString(), this.description,
formatter.format(this.start), formatter.format(this.end)),true);
} else if (start.compareTo(startDate) >= 0 && start.compareTo(endDate) <= 0) { // starts during date
return new TimeFrame(start, String.format("[E]%s %s", super.toString(), this.description));
}
return null;
public TimeFrame getTimeFrame() {
return new TimeFrame(this.start, this.end);
}
}
20 changes: 12 additions & 8 deletions src/main/java/duchess/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import duchess.model.TimeFrame;

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

public abstract class Task implements Serializable {
public abstract class Task implements Serializable, Comparable<Task> {
private boolean isDone;

public Task() {
Expand All @@ -18,20 +17,25 @@ public void markAsDone() {
this.isDone = true;
}

public abstract boolean containsKeyword(String keyword);

@Override
public String toString() {
return "[" + (this.isDone ? "✓" : "✘") + "]";
}

public abstract TimeFrame getTimeFrame(Date startDate, Date endDate);
@Override
public int compareTo(Task that) {
return this.getTimeFrame().compareTo(that.getTimeFrame());
}

public final boolean clashesWith(Task that) {
return this.getTimeFrame().clashesWith(that.getTimeFrame());
}

public abstract TimeFrame getTimeFrame();

public abstract void snooze() throws DukeException;

public abstract List<Task> getReminders();

public abstract List<Task> getClashables();

public abstract boolean clashesWith(Task task);
public abstract boolean containsKeyword(String keyword);
}
2 changes: 0 additions & 2 deletions src/main/java/duchess/model/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public void delete(int i) {
*/
public boolean isClashing(Task newTask) {
return this.getTasks().stream()
.map(Task::getClashables)
.flatMap(Collection::stream)
.anyMatch(task -> task.clashesWith(newTask));
}
}
15 changes: 2 additions & 13 deletions src/main/java/duchess/model/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import duchess.model.TimeFrame;

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

public class Todo extends Task {
Expand Down Expand Up @@ -34,8 +33,8 @@ public String toString() {
}

@Override
public TimeFrame getTimeFrame(Date startDate, Date endDate) {
return null;
public TimeFrame getTimeFrame() {
return TimeFrame.ofTimelessTask();
}

@Override
Expand All @@ -47,14 +46,4 @@ public void snooze() throws DukeException {
public List<Task> getReminders() {
return new ArrayList<>();
}

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

@Override
public boolean clashesWith(Task task) {
return false;
}
}
Loading

0 comments on commit 72bd9de

Please sign in to comment.