Skip to content

Commit cc7ebf1

Browse files
committed
add view schedule functionality
1 parent 7cc039d commit cc7ebf1

File tree

7 files changed

+99
-6
lines changed

7 files changed

+99
-6
lines changed

src/main/java/model/DeadLineTask.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package model;
22

33
import exceptions.NoDescriptionException;
4+
5+
import java.time.LocalDate;
46
import java.time.LocalDateTime;
57
import java.util.ArrayList;
68
import java.util.Arrays;
@@ -63,6 +65,11 @@ public ArrayList<String> getDetails() {
6365
this.by.format(DATE_TIME_FORMAT)));
6466
}
6567

68+
@Override
69+
public boolean isOnDate(LocalDate targetDate) {
70+
return by.toLocalDate().equals(targetDate);
71+
}
72+
6673
/**
6774
* Convert task into a string such that the user can view the details from UI.
6875
* @return String.

src/main/java/model/EventTask.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import exceptions.NoDescriptionException;
44

5+
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
57
import java.util.ArrayList;
68
import java.util.Arrays;
7-
import java.time.LocalDateTime;
8-
99

1010
/**
1111
* Represents an event task in the inner-task list.
@@ -65,6 +65,11 @@ public String getTaskType() {
6565
return TASK_TYPE_CHA;
6666
}
6767

68+
@Override
69+
public boolean isOnDate(LocalDate targetDate) {
70+
return at.toLocalDate().equals(targetDate);
71+
}
72+
6873
/**
6974
* Convert task into a string such that the user can view the details from UI.
7075
* @return String.

src/main/java/model/Task.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import exceptions.NoDescriptionException;
44

5+
import java.time.LocalDate;
56
import java.time.LocalDateTime;
67
import java.util.ArrayList;
78
import java.util.Arrays;
@@ -80,6 +81,8 @@ public boolean hasKeyWord(String keyWord) {
8081
return description.toLowerCase().contains(keyWord.toLowerCase());
8182
}
8283

84+
public abstract boolean isOnDate(LocalDate targetDate);
85+
8386
/**
8487
* An abstract method returning an single-character task type.
8588
*/

src/main/java/model/TaskList.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package model;
22

3+
import java.time.LocalDate;
4+
35
import java.util.ArrayList;
46
import java.util.Arrays;
57
import java.util.Collection;
@@ -17,6 +19,7 @@ public class TaskList implements Iterable<Task> {
1719
private static final String ECHO_VIEW_TASK_LIST = "Here are the tasks in your list:\n";
1820
private static final String ECHO_COMPLETE_TASK = "Nice! I've marked this task as done:\n";
1921
private static final String ECHO_FIND_TASK = "Here are the matching tasks in your list:\n";
22+
private static final String ECHO_VIEW_SCHEDULE = "Here are the tasks on Date:";
2023

2124
public TaskList() {
2225

@@ -107,6 +110,29 @@ public String findByKeyWord(String keyWord) {
107110
return matchedTasks.toString();
108111
}
109112

113+
public String findTasksOnDate(LocalDate targetDate) {
114+
StringBuilder tasksOnDate = new StringBuilder();
115+
tasksOnDate.append(ECHO_VIEW_SCHEDULE);
116+
tasksOnDate.append(" ");
117+
tasksOnDate.append(targetDate);
118+
tasksOnDate.append(":\n");
119+
120+
for (int i = 0; i < this.internalList.size(); i++) {
121+
if (this.internalList.get(i) == null) {
122+
continue;
123+
}
124+
Task currentTask = this.internalList.get(i);
125+
126+
if (currentTask.isOnDate(targetDate)) {
127+
tasksOnDate.append(Integer.toString(i + 1));
128+
tasksOnDate.append(".");
129+
tasksOnDate.append(currentTask.toString());
130+
tasksOnDate.append("\n");
131+
}
132+
}
133+
return tasksOnDate.toString();
134+
}
135+
110136
/**
111137
* Converts the task list to a string by looping through the tasks and concatenate
112138
* all the string representation of tasks.

src/main/java/model/ToDoTask.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import exceptions.NoDescriptionException;
44

5+
import java.time.LocalDate;
6+
57
/**
68
* Represents an todo task in the inner-task list.
79
*/
@@ -43,6 +45,11 @@ public String getTaskType() {
4345
return TASK_TYPE_CHA;
4446
}
4547

48+
@Override
49+
public boolean isOnDate(LocalDate targetDate) {
50+
return false;
51+
}
52+
4653
/**
4754
* Convert task into a string such that the user can view the details from UI.
4855
* @return String.

src/main/java/parser/Parser.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ public class Parser {
2525
private static final String DEADLINE_KEY = "(deadline)\\s*(\\S*)\\s*/by\\s*" + DATE_TIME_KEY;
2626
private static final String EVENT_KEY = "(event)\\s*(\\S*)\\s*/at\\s*" + DATE_TIME_KEY;
2727
private static final String FIND_KEY = "(find)\\s*(\\S+)";
28+
private static final String VIEW_SCHEDULE_KEY = "(view schedule)\\s*" + DATE_TIME_KEY;
2829

2930
private static final int POSITION_TARGET_INDEX = 2;
3031
private static final int POSITION_DESCRIPTION = 2;
3132
private static final int POSITION_KEYWORD = 2;
33+
private static final int POSITION_VIEW_SCHEDULE_DATE = 2;
3234
private static final int POSITION_DATE = 3;
3335
private static final int POSITION_TIME = 4;
3436

@@ -38,6 +40,7 @@ public class Parser {
3840
private static final Pattern DEADLINE_PATTERN = Pattern.compile(DEADLINE_KEY);
3941
private static final Pattern EVENT_PATTERN = Pattern.compile(EVENT_KEY);
4042
private static final Pattern FIND_PATTERN = Pattern.compile(FIND_KEY);
43+
private static final Pattern VIEW_SCHEDULE_PATTERN = Pattern.compile(VIEW_SCHEDULE_KEY);
4144

4245
/*Handle the difference that java list start from index 0 will human readable list
4346
starts from 1.*/
@@ -63,6 +66,17 @@ public static LocalDateTime parseDateTime(String dateString, String timeString)
6366
}
6467
}
6568

69+
public static LocalDate parseDate(String dateString) throws
70+
IllegalDateTimeFormatException {
71+
try {
72+
return LocalDate.parse(dateString);
73+
} catch (DateTimeParseException dte) {
74+
throw new IllegalDateTimeFormatException(dte.getMessage() + '\n');
75+
} catch (NullPointerException npe) {
76+
throw new IllegalDateTimeFormatException("Oops!!! The date string is missing!\n");
77+
}
78+
}
79+
6680
/**
6781
* Parse the indicated position from the input command.
6882
* @param pattern Java regular expression pattern.
@@ -106,8 +120,12 @@ private LocalDateTime findDateTime(Pattern pattern, String input) throws Illegal
106120
return parseDateTime(dateString, timeString);
107121
}
108122

109-
private static boolean isExitKey(String input) {
110-
return EXIT_KEY.equals(input);
123+
private LocalDate findDate(Pattern pattern, String input) throws IllegalDateTimeFormatException {
124+
Matcher matcher = pattern.matcher(input);
125+
matcher.find();
126+
String dateString = matcher.group(POSITION_VIEW_SCHEDULE_DATE);
127+
128+
return parseDate(dateString);
111129
}
112130

113131
private String findKeyword(Pattern pattern, String input) {
@@ -117,6 +135,10 @@ private String findKeyword(Pattern pattern, String input) {
117135
return matcher.group(POSITION_KEYWORD);
118136
}
119137

138+
private static boolean isExitKey(String input) {
139+
return EXIT_KEY.equals(input);
140+
}
141+
120142
private static boolean isViewListKey(String input) {
121143
return VIEW_LIST_KEY.equals(input);
122144
}
@@ -146,11 +168,16 @@ private static boolean isEventKey(String input) {
146168
return eventMatcher.find();
147169
}
148170

149-
private static boolean isFindKey(String userInput) {
150-
Matcher findMatcher = FIND_PATTERN.matcher(userInput);
171+
private static boolean isFindKey(String input) {
172+
Matcher findMatcher = FIND_PATTERN.matcher(input);
151173
return findMatcher.find();
152174
}
153175

176+
private static boolean isViewScheduleKey(String input) {
177+
Matcher viewScheduleMatcher = VIEW_SCHEDULE_PATTERN.matcher(input);
178+
return viewScheduleMatcher.find();
179+
}
180+
154181
/**
155182
* Returns corresponding command with the parsed parameters.
156183
* @param userInput String user input.
@@ -184,6 +211,9 @@ public Command parseCommand(String userInput) throws
184211
} else if (Parser.isFindKey(userInput)) {
185212
String keyWord = this.findKeyword(FIND_PATTERN, userInput);
186213
return new FindCommand(keyWord);
214+
} else if (Parser.isViewScheduleKey(userInput)) {
215+
LocalDate targetDate = this.findDate(VIEW_SCHEDULE_PATTERN, userInput);
216+
return new ViewScheduleCommand(targetDate);
187217
}
188218
else {
189219
throw new NoCommandException("OOPS!!! I'm sorry, but I don't know what that means :-(\n");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package parser;
2+
3+
import java.time.LocalDate;
4+
5+
public class ViewScheduleCommand extends Command{
6+
private LocalDate targetDate;
7+
8+
ViewScheduleCommand(LocalDate targetDate) {
9+
this.targetDate = targetDate;
10+
}
11+
12+
public String execute() {
13+
return this.taskList.findTasksOnDate(targetDate);
14+
}
15+
}

0 commit comments

Comments
 (0)