forked from nusCS2113-AY1920S1/PersonalAssistant-Duke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TimeFrame class (nusCS2113-AY1920S1#42)
* Reimplement TimeFrameClass * Refactor ViewScheduleCommand * Allow for instantaneous tasks * Implement printing of schedule
- Loading branch information
1 parent
1c586ae
commit 72bd9de
Showing
9 changed files
with
112 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.