Skip to content

Commit

Permalink
Merge branch 'branch-BCD-Extension'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluiexo authored and Pluiexo committed Feb 14, 2024
2 parents 0d47b66 + e7d7bae commit c15548b
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 28 deletions.
15 changes: 11 additions & 4 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
import duke.DukeException;
import duke.task.Actions;
import duke.task.Manage;
import duke.task.Query;
import duke.task.TaskManager;
import duke.ui.Ui;

/**
* Parser class to make sense of the input commands and decides the actions to give.
*/
public class Parser {
static final Pattern PATTERN_MANAGE = Pattern.compile("((?i)unmark|mark|delete) (\\d+)");
static final Pattern PATTERN_ACTIONS = Pattern.compile("((?i)todo|deadline|event) (.+)");
static final Pattern PATTERN_QUERY = Pattern.compile("((?i)find) (.+)");
private static final Pattern PATTERN_MANAGE = Pattern.compile("((?i)unmark|mark|delete) (\\d+)");
private static final Pattern PATTERN_ACTIONS = Pattern.compile("((?i)todo|deadline|event) (.+)");
private static final Pattern PATTERN_QUERY = Pattern.compile("((?i)find|view) (.+)");

private static final String[] CLEAR = new String[]{"clear"};

private static boolean isDead = false;

/**
Expand Down Expand Up @@ -45,7 +49,10 @@ public static String[] parse(String command, TaskManager manager) throws DukeExc
Actions act = Actions.valueOf(actionMatch.group(1).toUpperCase());
return manager.addTask(act, actionMatch.group(2));
} else if (queryMatch.matches()) {
return manager.findTask(queryMatch.group(2).trim());
Query act = Query.valueOf(queryMatch.group(1).toUpperCase());
return manager.queryTasks(act, queryMatch.group(2).trim());
} else if (command.matches("((?i)clear)")) {
return CLEAR;
} else {
throw new DukeException("invalid");
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package duke.task;

import java.time.LocalDateTime;
import java.util.Optional;

import duke.parser.DateHandler;
import duke.storage.SaveType;

/**
* Deadline task to keep track of a task with one deadline.
Expand Down Expand Up @@ -35,6 +37,14 @@ public Deadline(String description, LocalDateTime by) {
byDateTime = by;
}

public Optional<LocalDateTime> getDateTime() {
if (byDateTime != null) {
return Optional.of(byDateTime);
} else {
return Optional.empty();
}
}


@Override
public String saveFile() {
Expand All @@ -53,4 +63,9 @@ public String toString() {
return "[D]" + super.toString() + " (by: " + DateHandler.formatDate(byDateTime) + ")";
}
}

@Override
public SaveType getType() {
return SaveType.DEADLINE;
}
}
15 changes: 15 additions & 0 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package duke.task;

import java.time.LocalDateTime;
import java.util.Optional;

import duke.parser.DateHandler;
import duke.storage.SaveType;


/**
Expand Down Expand Up @@ -42,6 +44,15 @@ public Event(String description, LocalDateTime from, LocalDateTime by) {
byDateTime = by;
}


public Optional<LocalDateTime> getDateTime() {
if (byDateTime != null) {
return Optional.of(byDateTime);
} else {
return Optional.empty();
}
}

@Override
public String saveFile() {
if (byDateTime == null) {
Expand All @@ -63,4 +74,8 @@ public String toString() {
+ DateHandler.formatDate(byDateTime) + ")";
}
}
@Override
public SaveType getType() {
return SaveType.EVENT;
}
}
8 changes: 8 additions & 0 deletions src/main/java/duke/task/Query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package duke.task;

/**
* Actions that are relateed to Querying the task.
*/
public enum Query {
FIND, VIEW
}
10 changes: 10 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.task;

import duke.storage.SaveType;

/**
* Basic class of a Task.
*/
Expand Down Expand Up @@ -67,5 +69,13 @@ public String getStatusIcon() {
return (isDone ? "X" : " ");
}

/**
* Checks what type of task this is .
* @return The type of Task it is
*/
public SaveType getType() {
return null;
}

}

119 changes: 98 additions & 21 deletions src/main/java/duke/task/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import duke.DukeException;
import duke.parser.DateHandler;
import duke.storage.SaveType;

/**
* Manager class to manage and keep track of all the actions being performed on the task.
Expand All @@ -25,6 +26,7 @@ public class TaskManager {
private static final String RESPONSE_REMOVE = "Noted. I've removed this task";
private static final String RESPONSE_ADD = "Got it. I've added this task:";
private static final String RESPONSE_FIND = "Here are the matching tasks in your list";
private static final String RESPONSE_VIEW_DATES = "Here are the task scheduled on that date!!";

private static final String RESPONSE_EMPTY = "Your list is empty!!!!Add something! ";
private static final String RESPONSE_EMPTY_SEARCH = "Sorry I couldn't find anything that fits that search :(";
Expand Down Expand Up @@ -150,10 +152,13 @@ private static Task constructEvent(String by, String from, String description) t

Optional<LocalDate> testByDate = DateHandler.checkDate(by);
Optional<LocalDate> testFromDate = DateHandler.checkDate(from);

Optional<LocalDateTime> combineByDate =
testByDate.flatMap(byDate -> Optional.of(LocalDateTime.of(byDate, getTimeFromString(by))));

Optional<LocalDateTime> combineFromDate =
testFromDate.flatMap(fromDate -> Optional.of(LocalDateTime.of(fromDate, getTimeFromString(from))));

return combineByDate.flatMap(byDate -> combineFromDate.flatMap(fromDate -> Optional.of(new Event(description,
fromDate,
byDate))))
Expand Down Expand Up @@ -238,6 +243,98 @@ private String[] buildManageResponse(Manage act, String response, Task item) thr
return ret;
}


/**
* Querys the task with the corresponding query action.
*
* @param act a valid qeury action
* @param instruction query parameters to find
* @return A String array of values to return to the Ui to print.
* @throws DukeException Invalid query of tasks.
*/
public String[] queryTasks(Query act, String instruction) throws DukeException {

String[] ret;
switch (act) {
case FIND:
ret = findTask(instruction);
break;
case VIEW:
ret = viewByDate(instruction);
break;
default:
throw new DukeException("queryError");
}
return ret;

}

/**
* Find the task in the current list.
*
* @param search The keywords to search.
* @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());

if (!foundTask.isEmpty()) {
foundTask.add(0, RESPONSE_FIND);
return foundTask.toArray(String[]::new);

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

}
}

/**
* Returns the task scheduled on that particular date.
*
* @param date the date to query.
* @return A string array of the task to print to the Ui.
* @throws DukeException throws invalid query error.
*/
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(this::isValidDateTask)
.filter(item -> isMatchDate(item.getType(), item, inputDate))
.map(item -> items.indexOf(item) + 1 + ". " + item).collect(Collectors.toList());

if (!foundDates.isEmpty()) {
foundDates.add(0, RESPONSE_VIEW_DATES);
return foundDates.toArray(String[]::new);

} else {
System.out.println("Found nothing");
return new String[]{RESPONSE_EMPTY_SEARCH};

}
}

private boolean isValidDateTask(Task item) {
return item.getType().equals(SaveType.EVENT) || item.getType().equals(SaveType.DEADLINE);
}

private boolean isMatchDate(SaveType type, Task first, LocalDate second) {
if (type.equals(SaveType.DEADLINE)) {
Deadline test = (Deadline) first;
Optional<LocalDateTime> compareDate = test.getDateTime();
return compareDate.map(localDateTime -> localDateTime.toLocalDate().equals(second)).orElse(false);

} else if (type.equals(SaveType.EVENT)) {
Event test = (Event) first;
Optional<LocalDateTime> compareDate = test.getDateTime();
return compareDate.map(localDateTime -> localDateTime.toLocalDate().equals(second)).orElse(false);


} else {
return false;
}
}

/**
* Gets the current items in the TaskManager and produce them into a save format
*
Expand Down Expand Up @@ -265,7 +362,7 @@ public String[] listItems() {
String[] ret = new String[items.size()];
ret[0] = RESPONSE_LISTING;
for (int i = 0; i < items.size(); i++) {
ret[i] = i + 1 + "." + items.get(i);
ret[i] = i + 1 + ". " + items.get(i);
}
return ret;
}
Expand All @@ -278,24 +375,4 @@ public void setUpdate(boolean hasChanged) {
this.hasChanged = hasChanged;
}

/**
* Find the task in the current list.
*
* @param search The keywords to search.
* @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());

if (!foundTask.isEmpty()) {
foundTask.add(0, RESPONSE_FIND);
return foundTask.toArray(String[]::new);

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

}
}

}
7 changes: 7 additions & 0 deletions src/main/java/duke/task/Todo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.task;

import duke.storage.SaveType;

/**
* Create a todo class that only has a name.
*/
Expand All @@ -23,4 +25,9 @@ public String saveFile() {
public String toString() {
return "[T]" + super.toString();
}

@Override
public SaveType getType() {
return SaveType.TODO;
}
}
12 changes: 9 additions & 3 deletions src/main/java/duke/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ private void printDukeMessage(String msg) {
@FXML
private void handleUserInput() {
String input = userInput.getText();
String response = duke.getResponse(input);
dialogContainer.getChildren()
.addAll(DialogBox.getUserDialog(input, userImage), DialogBox.getDukeDialog(response, dukeImage));
String response = duke.getResponse(input).trim();
if (response.matches("((?i)clear)")) {
dialogContainer.getChildren().clear();
} else {
dialogContainer.getChildren()
.addAll(DialogBox.getUserDialog(input, userImage), DialogBox.getDukeDialog(response,
dukeImage));
}

userInput.clear();
//Inspired from :
// https://stackoverflow.com/questions/27334455/how-to-close-a-stage-after-a-certain-amount-of-time-javafx
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public static String handleError(String msg) {
case "manageError":
errorResponse = "Opppps!!!! Invalid manage action ";
break;
case "queryError":
errorResponse = "Query invalid search command !";
break;
case "dateError":
errorResponse = "OOPS!!! Incorrect date format!!!";
break;
Expand Down

0 comments on commit c15548b

Please sign in to comment.