Skip to content

Commit

Permalink
Merge pull request #4 from Ella-e/branch-B-Reminders
Browse files Browse the repository at this point in the history
Branch b reminders
  • Loading branch information
Ella-e authored Feb 15, 2024
2 parents a4bd475 + ea64415 commit c3b6e61
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 48 deletions.
7 changes: 6 additions & 1 deletion data/plaudern.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
T|0|a
D|1|abc |2022-02-20
D|0|cd |2023-11-01
D|0|something aa |2024-01-04
T|1|abc
T|0|abc
T|0|cdd
13 changes: 10 additions & 3 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public Duke(String filePath) {
* Runs the chatbot.
*/
public void run() {
ui.onEnter();
String response;
System.out.println(ui.onEnter());
String response = "";
do {
response = getResponse(ui.getUserInput());
} while (!Objects.equals(response, "Bye. Hope to see you again soon!"));
Expand All @@ -69,7 +69,6 @@ public String getResponse(String userInput) {
// the parser object contains all the current user input line information
Parser parser = new Parser();
parser.parse(userInput);
System.out.println(userInput);

// check for end the session
if (parser.getCurrentKey().equals(KeyEnum.EXITKEY)) {
Expand Down Expand Up @@ -120,4 +119,12 @@ public String getResponse(String userInput) {
}
return response;
}

/**
* Gets next due tasks.
* @return next due tasks
*/
public String getNextDueTasks() {
return tasks.nextDueTasksToString(2);
}
}
3 changes: 0 additions & 3 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
*/
public class Main extends Application {

private Duke duke = new Duke();

@Override
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml"));
AnchorPane ap = fxmlLoader.load();
Scene scene = new Scene(ap);
stage.setScene(scene);
fxmlLoader.<MainWindow>getController().setDuke(duke);
stage.show();
} catch (IOException e) {
e.printStackTrace();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/control/DialogBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
Expand Down Expand Up @@ -37,6 +38,8 @@ private DialogBox(String text, Image img) {

dialog.setText(text);
displayPicture.setImage(img);
Insets padding = new Insets(10);
dialog.setPadding(padding);
}

/**
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/duke/control/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import java.util.Objects;

import duke.Duke;
import duke.exceptions.BaseException;
import duke.ui.UI;
import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

/**
* Controller for MainWindow. Provides the layout for the other controls.
*/
public class MainWindow extends AnchorPane {
private static final String WELCOME_MESSAGE = new UI().onEnter();
private static final String ERROR_MASSAGE = "Oops, some error occurs";
@FXML
private ScrollPane scrollPane;
@FXML
Expand All @@ -31,13 +37,26 @@ public class MainWindow extends AnchorPane {
private final Image dukeImage =
new Image(Objects.requireNonNull(this.getClass().getResourceAsStream("/images/bot.png")));

/**
* Initializes the chatbot.
*/
@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
}

public void setDuke(Duke d) {
duke = d;
try {
this.duke = new Duke();
String welcomeString = WELCOME_MESSAGE + "\n" + duke.getNextDueTasks();
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
dialogContainer.getChildren().add(
DialogBox.getDukeDialog(welcomeString, dukeImage)
);
} catch (BaseException e) {
dialogContainer.getChildren().add(
DialogBox.getDukeDialog(ERROR_MASSAGE, dukeImage)
);
PauseTransition pause = new PauseTransition(Duration.seconds(3));
pause.setOnFinished(event -> System.exit(0));
pause.play();
}
}

/**
Expand All @@ -53,6 +72,12 @@ private void handleUserInput() {
DialogBox.getDukeDialog(response, dukeImage)
);
userInput.clear();
if (response.equals(new UI().onExit())) {
// Close the window after 2 seconds
PauseTransition pause = new PauseTransition(Duration.seconds(2));
pause.setOnFinished(event -> System.exit(0));
pause.play();
}
}
}

11 changes: 7 additions & 4 deletions src/main/java/duke/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Scanner;

Expand All @@ -12,6 +14,7 @@
import duke.tasks.Task;
import duke.tasks.TaskList;
import duke.tasks.Todo;
import duke.utils.Parser;

/**
* Class represent a file and contains functions processing a file.
Expand Down Expand Up @@ -95,7 +98,7 @@ public void writeTasksToFile(TaskList taskList) throws IOException {
* @param str String format of the task
* @return The respective task object
*/
private Task stringToTask(String str) throws IOException {
private Task stringToTask(String str) throws IOException, DateTimeParseException {
assert str != null : "String should not be null";
String[] strSplit = str.split("\\|");
if (strSplit.length <= 1) {
Expand All @@ -109,12 +112,12 @@ private Task stringToTask(String str) throws IOException {
task = new Todo(status, detail);
break;
case "D":
String by = strSplit[3];
LocalDate by = Parser.formatDate(strSplit[3]);
task = new Deadline(status, detail, by);
break;
case "E":
String from = strSplit[3];
String to = strSplit[4];
LocalDate from = Parser.formatDate(strSplit[3]);
LocalDate to = Parser.formatDate(strSplit[4]);
task = new Event(status, detail, from, to);
break;
default:
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import java.time.LocalDate;
import java.time.format.DateTimeParseException;

import duke.exceptions.InvalidDateTimeException;
import duke.utils.DukeDateFormater;
import duke.utils.Parser;


/**
* Class represent Task type Deadline.
*/
public class Deadline extends Task {
private LocalDate by;
private DukeDateFormater formater = new DukeDateFormater();

/**
* Initializes a Deadline object with given params.
Expand All @@ -21,13 +19,9 @@ public class Deadline extends Task {
* @param by Task end time.
* @throws DateTimeParseException If the end time is invalid.
*/
public Deadline(Boolean status, String detail, String by) throws DateTimeParseException {
public Deadline(Boolean status, String detail, LocalDate by) throws DateTimeParseException {
super(status, detail);
try {
this.by = this.formater.stringToDate(by);
} catch (DateTimeParseException e) {
throw new InvalidDateTimeException();
}
this.by = by;
}

/**
Expand All @@ -36,11 +30,15 @@ public Deadline(Boolean status, String detail, String by) throws DateTimeParseEx
*/
@Override
public String inFileStringFormat() {
return "D|" + super.inFileStringFormat() + "|" + this.by.toString();
return "D|" + super.inFileStringFormat() + "|" + by.toString();
}

@Override
public String toString() {
return "[D]" + super.toString() + "(by: " + this.formater.dateToString(this.by) + ")";
return "[D]" + super.toString() + "(by: " + Parser.FORMATER.dateToString(this.by) + ")";
}

public LocalDate getBy() {
return by;
}
}
25 changes: 20 additions & 5 deletions src/main/java/duke/tasks/Event.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package duke.tasks;

import java.time.LocalDate;

import duke.utils.Parser;


/**
* Class represent Task type Event.
*/
public class Event extends Task {
private String start;
private String by;
private LocalDate start;
private LocalDate by;

/**
* Initializes an Event object with given params.
Expand All @@ -15,7 +20,7 @@ public class Event extends Task {
* @param start task start time.
* @param by task end time.
*/
public Event(Boolean status, String detail, String start, String by) {
public Event(Boolean status, String detail, LocalDate start, LocalDate by) {
super(status, detail);
this.start = start;
this.by = by;
Expand All @@ -28,11 +33,21 @@ public Event(Boolean status, String detail, String start, String by) {
*/
@Override
public String inFileStringFormat() {
return "E|" + super.inFileStringFormat() + "|" + this.start + "|" + this.by;
return "E|" + super.inFileStringFormat() + "|" + start
+ "|" + by;
}

@Override
public String toString() {
return "[E]" + super.toString() + "(from: " + start + " to: " + by + ")";
return "[E]" + super.toString() + "(from: " + Parser.FORMATER.dateToString(start)
+ " to: " + Parser.FORMATER.dateToString(by) + ")";
}

public LocalDate getStart() {
return start;
}

public LocalDate getBy() {
return by;
}
}
48 changes: 46 additions & 2 deletions src/main/java/duke/tasks/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package duke.tasks;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;

import duke.exceptions.EmptyBodyException;
import duke.exceptions.InvalidDateTimeException;
Expand Down Expand Up @@ -70,7 +72,7 @@ public String listTask() {
* @throws EmptyBodyException If the command body is empty.
* @throws InvalidDateTimeException If the due date for deadline is not in recognisable format.
*/
public Task addTask(String detail, String from, String to, KeyEnum currentKey)
public Task addTask(String detail, LocalDate from, LocalDate to, KeyEnum currentKey)
throws EmptyBodyException, InvalidDateTimeException {
Task task = null;
switch (currentKey) {
Expand Down Expand Up @@ -139,12 +141,54 @@ public Task deleteTaskById(Integer id) throws OutOfBoundException, IndexOutOfBou
public TaskList findTasks(String word) {
ArrayList<Task> matchedTasks = new ArrayList<>();
// Iterate through the task list
System.out.println(word);
for (Task task : tasks) {
if (task.getDetail().contains(word)) {
matchedTasks.add(task);
}
}
return new TaskList(matchedTasks);
}

/**
* Finds the list of tasks that are due next.
* @param numOfTasks number of tasks to be returned
* @return list of task that most recent due
*/
public TaskList findNextDueTasks(int numOfTasks) {
ArrayList<Task> nextTasks = new ArrayList<>(tasks);
nextTasks.sort(new Comparator<Task>() {
@Override
public int compare(Task o1, Task o2) {
if (o1 instanceof Deadline && o2 instanceof Deadline) {
return ((Deadline) o1).getBy().compareTo(((Deadline) o2).getBy());
} else if (o1 instanceof Event && o2 instanceof Event) {
return ((Event) o1).getBy().compareTo(((Event) o2).getBy());
} else if (o1 instanceof Todo) {
return 1;
} else if (o2 instanceof Todo) {
return -1;
}
return 0;
}
});

if (nextTasks.size() <= numOfTasks) {
return new TaskList(nextTasks);
}

ArrayList<Task> tempTasks = new ArrayList<>();
int counter = numOfTasks;
for (Task task: nextTasks) {
if (counter <= 0) {
break;
}
tempTasks.add(task);
counter--;
}
return new TaskList(tempTasks);
}

public String nextDueTasksToString(int numOfTasks) {
return "==Reminder==\nThose Tasks are due next:\n" + findNextDueTasks(numOfTasks).listTask();
}
}
4 changes: 2 additions & 2 deletions src/main/java/duke/ui/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class UI {
/**
* Displays greeting sentence.
*/
public void onEnter() {
System.out.println("Hello! I'm Plaudern\nWhat can I do for you?");
public String onEnter() {
return "Hello! I'm Plaudern\nWhat can I do for you?\n";
}

/**
Expand Down
Loading

0 comments on commit c3b6e61

Please sign in to comment.