Skip to content

Commit

Permalink
Change index system for query functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluiexo authored and Pluiexo committed Feb 15, 2024
1 parent 17b3929 commit adeaa0b
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main/java/duke/task/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import duke.DukeException;
import duke.parser.DateHandler;
Expand Down Expand Up @@ -276,12 +277,13 @@ public String[] queryTasks(Query act, String instruction) throws DukeException {
* @return A list of items containing the search results.
*/
public String[] findTask(String search) {
List<String> foundTask = items.stream().filter(item -> item.toString().contains(search))
.map(item -> items.indexOf(item) + 1 + ". " + item).collect(Collectors.toList());
List<String> foundTask = items.stream().map(Task::toString).filter(string -> string.contains(search))
.collect(Collectors.toList());

if (!foundTask.isEmpty()) {
foundTask.add(0, RESPONSE_FIND);
return foundTask.toArray(String[]::new);
List<String> print = iterateWithIndex(foundTask);
print.add(0, RESPONSE_FIND);
return print.toArray(String[]::new);

} else {
return new String[]{RESPONSE_EMPTY_SEARCH};
Expand All @@ -299,19 +301,26 @@ public String[] findTask(String search) {
public String[] viewByDate(String date) throws DukeException {
LocalDate inputDate = DateHandler.checkDate(date).orElseThrow(() -> new DukeException("dateError"));
System.out.println(inputDate);
List<String> foundDates = items.stream().filter(item -> isMatchDate(item.getType(), item, inputDate))
.map(item -> items.indexOf(item) + 1 + ". " + item).collect(Collectors.toList());
List<String> foundDates =
items.stream().filter(item -> isMatchDate(item.getType(), item, inputDate)).map(Task::toString)
.collect(Collectors.toList());

if (!foundDates.isEmpty()) {
foundDates.add(0, RESPONSE_VIEW_DATES);
return foundDates.toArray(String[]::new);
List<String> print = iterateWithIndex(foundDates);
print.add(0, RESPONSE_VIEW_DATES);
return print.toArray(String[]::new);

} else {
return new String[]{RESPONSE_EMPTY_SEARCH};

}
}

private static List<String> iterateWithIndex(List<String> toReturn) {
return IntStream.range(0, toReturn.size()).mapToObj(i -> (i + 1) + ". " + toReturn.get(i))
.collect(Collectors.toList());
}

private boolean isMatchDate(SaveType type, Task first, LocalDate second) {
if (type.equals(SaveType.DEADLINE)) {
Deadline test = (Deadline) first;
Expand Down

0 comments on commit adeaa0b

Please sign in to comment.