Skip to content

Commit ec9289f

Browse files
committed
Implement find free time feature
1 parent 62cbe0a commit ec9289f

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

src/main/java/devin/Devin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public String getResponse(String text) throws DevinException, IOException {
5353
case "deadline" -> Command.deadlineCommand(list, texts, storage);
5454
case "event" -> Command.eventCommand(list, texts, storage);
5555
case "find" -> Command.findCommand(list, texts);
56+
case "schedule" -> Command.scheduleCommand(list);
5657
default -> throw new DevinException("""
5758
Unknown command.
5859
Please choose one of the following commands:

src/main/java/devin/command/Command.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package devin.command;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.Comparator;
47

58
import devin.Devin;
69
import devin.exception.DevinException;
@@ -183,4 +186,14 @@ public static String findCommand(TaskList list, String[] texts) throws DevinExce
183186
texts[0] = "";
184187
return list.findTask(String.join(" ", texts));
185188
}
189+
190+
/**
191+
* A schedule command to find all free time slot and display it to user.
192+
*
193+
* @param list of all tasks.
194+
* @return a message showing all free time slot.
195+
*/
196+
public static String scheduleCommand(TaskList list) {
197+
return list.getTimedTasks();
198+
}
186199
}

src/main/java/devin/task/Deadline.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public Deadline(String description, LocalDateTime by, boolean isDone) {
2424
this.by = by;
2525
}
2626

27+
@Override
28+
public LocalDateTime getEndTime() {
29+
return by;
30+
}
31+
2732
@Override
2833
public String toFileString() {
2934
return "D | " + super.toFileString() + " | " + by.format(FORMATTER1);

src/main/java/devin/task/Event.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public Event(String description, LocalDateTime from, LocalDateTime to, boolean i
2727
this.to = to;
2828
}
2929

30+
@Override
31+
public LocalDateTime getStartTime() {
32+
return from;
33+
}
34+
35+
@Override
36+
public LocalDateTime getEndTime() {
37+
return to;
38+
}
39+
3040
@Override
3141
public String toFileString() {
3242
return "E | " + super.toFileString() + " | " + from.format(FORMATTER1) + " | " + to.format(FORMATTER1);
@@ -36,4 +46,5 @@ public String toFileString() {
3646
public String toString() {
3747
return "[E]" + super.toString() + " (from: " + from.format(FORMATTER2) + " to: " + to.format(FORMATTER2) + ")";
3848
}
49+
3950
}

src/main/java/devin/task/Task.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package devin.task;
22

3+
import java.time.LocalDateTime;
4+
35
/**
46
* Representation of a Task.
57
*/
@@ -49,6 +51,14 @@ public void markTask() {
4951
public void unmarkTask() {
5052
this.isDone = false;
5153
}
54+
55+
public LocalDateTime getStartTime() {
56+
return null;
57+
}
58+
59+
public LocalDateTime getEndTime() {
60+
return null;
61+
}
5262

5363
public String toFileString() {
5464
return getStatusIcon() + " | " + this.name;
@@ -58,4 +68,6 @@ public String toFileString() {
5868
public String toString() {
5969
return "[" + getStatusIcon() + "] " + this.name;
6070
}
71+
72+
6173
}

src/main/java/devin/task/TaskList.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package devin.task;
22

33
import java.io.IOException;
4+
import java.time.Duration;
5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
47
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.Comparator;
510
import java.util.regex.Matcher;
611
import java.util.regex.Pattern;
712
import java.util.stream.Collectors;
@@ -128,4 +133,41 @@ public String findTask(String keyword) {
128133
public ArrayList<Task> getTasks() {
129134
return tasks;
130135
}
136+
137+
/**
138+
* Find all the free time slot that the user can schedule.
139+
*
140+
* @return a message of all free time slot
141+
*/
142+
public String getTimedTasks() {
143+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy HHmm");
144+
145+
LocalDateTime now = LocalDateTime.now();
146+
147+
ArrayList<Task> timedTasks = tasks.stream()
148+
.filter(task -> task.getStartTime() != null && task.getEndTime() != null)
149+
.sorted(Comparator.comparing(Task::getStartTime))
150+
.collect(Collectors.toCollection(ArrayList::new));
151+
152+
StringBuilder freeSlots = new StringBuilder();
153+
LocalDateTime previousEndTime = now;
154+
155+
for (Task task : timedTasks) {
156+
if (previousEndTime.isBefore(task.getStartTime())) {
157+
freeSlots.append("Free slot from ")
158+
.append(previousEndTime.format(formatter))
159+
.append(" to ")
160+
.append(task.getStartTime().format(formatter))
161+
.append("\n"); // Newline separator
162+
}
163+
previousEndTime = task.getEndTime();
164+
}
165+
166+
freeSlots.append("Free slot from ")
167+
.append(previousEndTime.format(formatter))
168+
.append(" to forever")
169+
.append("\n");
170+
171+
return freeSlots.toString().trim();
172+
}
131173
}

0 commit comments

Comments
 (0)