Skip to content

Commit ac0f9f1

Browse files
Add find feature to MyDuke
1 parent d9938fe commit ac0f9f1

4 files changed

Lines changed: 239 additions & 135 deletions

File tree

data/saveFile.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ T @ 0 @ dishes
22
D @ 1 @ submission @ 2020-06-06
33
E @ 0 @ play soccer @ 2020-03-04
44
T @ 1 @ dishes
5-
D @ 0 @ submission @ 2020-06-06
6-
E @ 0 @ play soccer @ 2020-03-04
7-
T @ 0 @ dishes
8-
D @ 0 @ submission @ 2020-06-06
9-
D @ 0 @ submission @ 2020-06-06
10-
E @ 0 @ play soccer @ 2020-03-04
5+
D @ 0 @ another submission @ 2020-06-06
6+
E @ 0 @ play table tennis @ 2020-03-04
117
T @ 0 @ dishes
12-
D @ 0 @ submission @ 2020-06-06
13-
E @ 0 @ play soccer @ 2020-03-04
8+
D @ 0 @ another submission @ 2020-06-06
9+
E @ 0 @ play table tennis @ 2020-03-04

src/main/java/MyDuke.java

Lines changed: 149 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ static void deadlineChecker(String[] inputArr) throws NoDeadlineException, NoDat
102102
}
103103
}
104104

105+
static void finderChecker(String[] inputArr) throws NoKeywordException {
106+
if (inputArr.length == 1) {
107+
throw new NoKeywordException(
108+
"Paikia Bot: you want to find what task, limpeh need more information ah. "
109+
+ "input 'find <keyword>. eg, find party");
110+
}
111+
}
112+
105113
}
106114

107115
class Parser {
@@ -139,105 +147,132 @@ String[] getParsedInput() {
139147

140148
void handleInput() {
141149
switch (getParsedInput()[0]) {
142-
case "list":
143-
int counter = 1;
144-
String[] tempArr = new String[100];
145-
if (tasks.getTaskList().isEmpty()) { // improved implementation in case list is empty, gives a clear output
146-
ui.showListEmptyMsg();
147-
} else {
148-
for (Task t : tasks.getTaskList()) { // changed String s to Task t
149-
tempArr[counter - 1] = counter + ". " + t.toString();
150-
counter++;
151-
}
152-
ui.printTasksInList(tempArr);
153-
}
154-
break;
155-
case "done":
156-
try {
157-
MyDuke.indexChecker(getParsedInput());
158-
int ref = Integer.parseInt(getParsedInput()[1]);
159-
tasks.getTaskList().set(ref - 1, tasks.getTaskList().get(ref - 1).setAsDone());
160-
ui.printDoneTaskAlert(tasks.getTaskList().get(ref - 1).toString());
161-
} catch (NoIndexException e) {
162-
ui.printErrorMsg(e.getMessage());
163-
} catch (IndexOutOfBoundsException e) {
164-
ui.printErrorMsg("Paikia Bot: the number that you inputted is invalid leh (more than the "
165-
+ "total number of tasks or less than 1), try again pls ah");
166-
} catch (NumberFormatException e) {
167-
ui.printErrorMsg("Paikia Bot: ur input after 'done' is invalid, reminder that it should be a single integer"
168-
+ "and remember to not leave a space after your input. eg, done 3");
169-
}
170-
break;
171-
case "delete":
172-
try {
173-
MyDuke.indexChecker(getParsedInput());
174-
int ref = Integer.parseInt(getParsedInput()[1]);
175-
Task toRemove = tasks.getTask(ref);
176-
tasks.deleteTask(ref - 1);
177-
ui.printDeletedTaskAlert(toRemove.toString(), tasks.getTaskList().size());
178-
} catch (NoIndexException e) {
179-
ui.printErrorMsg(e.getMessage());
180-
} catch (IndexOutOfBoundsException e) {
181-
ui.printErrorMsg("Paikia Bot: the number that you inputted is invalid leh (more than the total number of tasks or less than 1), try again pls ah");
182-
} catch (NumberFormatException e) {
183-
ui.printErrorMsg("Paikia Bot: ur input after 'done' is invalid, reminder that it should be a single integer"
184-
+ "and remember to not leave a space after your input. eg, done 3");
150+
case "list":
151+
int counter = 1;
152+
String[] tempArr = new String[100];
153+
if (tasks.getTaskList().isEmpty()) { // improved implementation in case list is empty, gives a clear output
154+
ui.showListEmptyMsg();
155+
} else {
156+
for (Task t : tasks.getTaskList()) { // changed String s to Task t
157+
tempArr[counter - 1] = counter + ". " + t.toString();
158+
counter++;
185159
}
186-
break;
187-
case "todo":
188-
try {
189-
MyDuke.todoChecker(getParsedInput());
190-
ToDo todo = new ToDo(getParsedInput()[1], false);
191-
tasks.addTask(todo);
192-
ui.printAddedTodoAlert(todo.toString(), tasks.getTaskList().size());
193-
} catch (NoToDoException e) {
194-
ui.printErrorMsg(e.getMessage());
160+
ui.printTasksInList(tempArr);
161+
}
162+
break;
163+
case "done":
164+
try {
165+
MyDuke.indexChecker(getParsedInput());
166+
int ref = Integer.parseInt(getParsedInput()[1]);
167+
tasks.getTaskList().set(ref - 1, tasks.getTaskList().get(ref - 1).setAsDone());
168+
ui.printDoneTaskAlert(tasks.getTaskList().get(ref - 1).toString());
169+
} catch (NoIndexException e) {
170+
ui.printErrorMsg(e.getMessage());
171+
} catch (IndexOutOfBoundsException e) {
172+
ui.printErrorMsg("Paikia Bot: the number that you inputted is invalid leh (more than the "
173+
+ "total number of tasks or less than 1), try again pls ah");
174+
} catch (NumberFormatException e) {
175+
ui.printErrorMsg("Paikia Bot: ur input after 'done' is invalid, reminder that it should be a single integer"
176+
+ "and remember to not leave a space after your input. eg, done 3");
177+
}
178+
break;
179+
case "delete":
180+
try {
181+
MyDuke.indexChecker(getParsedInput());
182+
int ref = Integer.parseInt(getParsedInput()[1]);
183+
Task toRemove = tasks.getTask(ref);
184+
tasks.deleteTask(ref - 1);
185+
ui.printDeletedTaskAlert(toRemove.toString(), tasks.getTaskList().size());
186+
} catch (NoIndexException e) {
187+
ui.printErrorMsg(e.getMessage());
188+
} catch (IndexOutOfBoundsException e) {
189+
ui.printErrorMsg("Paikia Bot: the number that you inputted is invalid leh (more than the total number of tasks or less than 1), try again pls ah");
190+
} catch (NumberFormatException e) {
191+
ui.printErrorMsg("Paikia Bot: ur input after 'done' is invalid, reminder that it should be a single integer"
192+
+ "and remember to not leave a space after your input. eg, done 3");
193+
}
194+
break;
195+
case "todo":
196+
try {
197+
MyDuke.todoChecker(getParsedInput());
198+
ToDo todo = new ToDo(getParsedInput()[1], false);
199+
tasks.addTask(todo);
200+
ui.printAddedTodoAlert(todo.toString(), tasks.getTaskList().size());
201+
} catch (NoToDoException e) {
202+
ui.printErrorMsg(e.getMessage());
195203

204+
}
205+
break;
206+
case "event":
207+
try {
208+
MyDuke.eventChecker(getParsedInput());
209+
String[] temp = getParsedInput()[1].split("/", 2);
210+
Event event = new Event(LocalDate.parse(temp[1].substring(3)), temp[0], false);
211+
tasks.addTask(event);
212+
ui.printAddedEventAlert(event.toString(), tasks.getTaskList().size());
213+
} catch (NoEventException e) {
214+
ui.printErrorMsg(e.getMessage());
215+
} catch (NoDateException e) {
216+
ui.printErrorMsg(e.getMessage());
217+
} catch (DateTimeParseException e) {
218+
ui.printErrorMsg(new String[]{
219+
"Paikia Bot: oi your date input got format error cannot parse sial: " + getParsedInput()[1].split("/", 2)[1],
220+
"Paikia Bot: the format should be liddis: yyyy-mm-dd, eg. 2020-03-02"
221+
});
222+
}
223+
break;
224+
case "deadline":
225+
try {
226+
MyDuke.deadlineChecker(getParsedInput());
227+
String[] temp = getParsedInput()[1].split("/", 2);
228+
Deadline deadline = new Deadline(LocalDate.parse(temp[1].substring(3)), temp[0], false);
229+
tasks.addTask(deadline);
230+
ui.printAddedDeadlineAlert(deadline.toString(), tasks.getTaskList().size());
231+
} catch (NoDeadlineException e) {
232+
ui.printErrorMsg(e.getMessage());
233+
} catch (NoDateException e) {
234+
ui.printErrorMsg(e.getMessage());
235+
} catch (DateTimeParseException e) {
236+
ui.printErrorMsg(new String[]{
237+
"Paikia Bot: oi your date input got format error cannot parse sial: " + getParsedInput()[1].split("/", 2)[1],
238+
"Paikia Bot: the format should be liddis: yyyy-mm-dd, eg. 2020-03-02"
239+
});
240+
}
241+
break;
242+
case "find":
243+
try {
244+
MyDuke.finderChecker(getParsedInput());
245+
String keyword = this.input.split(" ", 2)[1];
246+
List<Task> searchResultList = new ArrayList<>();
247+
for (Task t : tasks.getTaskList()) {
248+
if (t.info.contains(keyword)) {
249+
searchResultList.add(t);
250+
}
196251
}
197-
break;
198-
case "event":
199-
try {
200-
MyDuke.eventChecker(getParsedInput());
201-
String[] temp = getParsedInput()[1].split("/", 2);
202-
Event event = new Event(LocalDate.parse(temp[1].substring(3)), temp[0], false);
203-
tasks.addTask(event);
204-
ui.printAddedEventAlert(event.toString(), tasks.getTaskList().size());
205-
} catch (NoEventException e) {
206-
ui.printErrorMsg(e.getMessage());
207-
} catch (NoDateException e) {
208-
ui.printErrorMsg(e.getMessage());
209-
} catch (DateTimeParseException e) {
210-
ui.printErrorMsg(new String[]{
211-
"Paikia Bot: oi your date input got format error cannot parse sial: " + getParsedInput()[1].split("/", 2)[1],
212-
"Paikia Bot: the format should be liddis: yyyy-mm-dd, eg. 2020-03-02"
213-
});
214-
}
215-
break;
216-
case "deadline":
217-
try {
218-
MyDuke.deadlineChecker(getParsedInput());
219-
String[] temp = getParsedInput()[1].split("/", 2);
220-
Deadline deadline = new Deadline(LocalDate.parse(temp[1].substring(3)), temp[0], false);
221-
tasks.addTask(deadline);
222-
ui.printAddedDeadlineAlert(deadline.toString(), tasks.getTaskList().size());
223-
} catch (NoDeadlineException e) {
224-
ui.printErrorMsg(e.getMessage());
225-
} catch (NoDateException e) {
226-
ui.printErrorMsg(e.getMessage());
227-
} catch (DateTimeParseException e) {
228-
ui.printErrorMsg(new String[]{
229-
"Paikia Bot: oi your date input got format error cannot parse sial: " + getParsedInput()[1].split("/", 2)[1],
230-
"Paikia Bot: the format should be liddis: yyyy-mm-dd, eg. 2020-03-02"
231-
});
252+
int searchResultCounter = 1;
253+
String[] tempSearchResultArr = new String[100];
254+
if (searchResultList.isEmpty()) { // improved implementation in case list is empty, gives a clear output
255+
ui.showListEmptyMsg();
256+
} else {
257+
for (Task t : searchResultList) { // changed String s to Task t
258+
tempSearchResultArr[searchResultCounter - 1] = searchResultCounter + ". " + t.toString();
259+
searchResultCounter++;
260+
}
261+
ui.printSearchResultList(keyword, tempSearchResultArr);
232262
}
233-
break;
234-
default:
235-
ui.showInputError();
263+
} catch (NoKeywordException e) {
264+
ui.printErrorMsg(e.getMessage());
265+
} catch (DateTimeParseException e) {
266+
ui.printErrorMsg(new String[]{
267+
"Paikia Bot: oi your date input got format error cannot parse sial: " + getParsedInput()[1].split("/", 2)[1],
268+
"Paikia Bot: the format should be liddis: yyyy-mm-dd, eg. 2020-03-02"
269+
});
270+
}
271+
break;
272+
default:
273+
ui.showInputError();
236274
}
237-
238-
239275
}
240-
241276
}
242277

243278
class TaskList {
@@ -370,7 +405,17 @@ String getInput(Scanner sc) {
370405
void print(String[] sArr) {
371406
System.out.println(DASH);
372407
for (String s : sArr) {
373-
// Level-2 adjustments
408+
if (s != null) {
409+
System.out.println(s);
410+
}
411+
}
412+
System.out.println(DASH);
413+
}
414+
415+
void print(String description, String[] sArr) {
416+
System.out.println(DASH);
417+
System.out.println(description);
418+
for (String s : sArr) {
374419
if (s != null) {
375420
System.out.println(s);
376421
}
@@ -457,6 +502,10 @@ void showByeMsg() {
457502
void printTasksInList(String[] tasksArr) {
458503
print(tasksArr);
459504
}
505+
506+
void printSearchResultList(String keyword, String[] tasksArr) {
507+
print("Paikia Bot: dis is your search result for '" + keyword + "'", tasksArr);
508+
}
460509
}
461510

462511
// Level-3 additions
@@ -602,6 +651,12 @@ class NoDeadlineException extends MyDukeException {
602651
}
603652
}
604653

654+
class NoKeywordException extends MyDukeException {
655+
NoKeywordException(String s) {
656+
super(s);
657+
}
658+
}
659+
605660
class NoDateException extends MyDukeException {
606661
NoDateException(String s) {
607662
super(s);

0 commit comments

Comments
 (0)