Skip to content

Commit

Permalink
Implment printing error message to GUI version of Duke
Browse files Browse the repository at this point in the history
  • Loading branch information
lennoxtr committed Feb 20, 2023
1 parent eaaaed4 commit 3ea6be2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Saved Progress/MY_GRAND_PLAN.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
false TODO DRINK WATER
true TODO WATCH NGN
false TODO WATCH NGN
false DEADLINE NGN /BY 20-02-2023 2359
9 changes: 7 additions & 2 deletions src/main/java/duke/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
* A launcher class to workaround classpath issues.
*/


//modify launcher to initiate both GUI and CLI versions of Duke
public class Launcher {

/**
* Launch Duke
* @param args GUI if the user wants to run the GUI version of Duke
* Default (no args) is CLI version
*/

public static void main(String[] args) {
assert args != null;
if (args.length > 0 && args[0].equals("GUI")) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/duke/io/input/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.IOException;

import duke.util.Parser;
import duke.util.Storage;
import duke.util.TaskList;
import duke.workflow.Event;
import duke.workflow.Greeting;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -121,14 +123,27 @@ private void handleUserInput() {
getScene().getWindow().sizeToScene();
if (this.firstTimeRunningDukeFlag < 0) {
firstTimeRunningDukeFlag = runDuke();

} else if (firstTimeRunningDukeFlag > 0) {
if (!this.currentEvent.isFinalEvent()) {
String input = userInput.getText();
userInput.clear();
TaskList currentTaskList = this.currentEvent.getTaskList();
boolean validInput = Parser.checkInputValidity(input, currentTaskList.getSize());
this.currentEvent = this.currentEvent.toNextEvent(input);

dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage));

if (!validInput) {
String warning = Parser.getWarningGui(input, currentTaskList.getSize());
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(warning, dukeImage));
}

dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(this.currentEvent.toString(), dukeImage));

if (this.currentEvent.isFinalEvent()) {
String saveProgressQuery = "SAVE YOUR GRAND PLAN FOR ANOTHER DAY? ";
dialogContainer.getChildren().add(
Expand All @@ -141,7 +156,7 @@ private void handleUserInput() {
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage));

Storage.saveProgressGUI(input, this.currentEvent.getTaskList());
Storage.saveProgressGui(input, this.currentEvent.getTaskList());
System.exit(0);
}
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/duke/util/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,24 @@ public static boolean checkInputValidity(String userCommand, int tasklistSize) {
}
return true;
}

/**
* Get the warning message for GUI version of Duke if user input is invalid
*
* @param userCommand the user input
* @param tasklistSize the current number of tasks
*
* @return a String that contains the exception
*/

public static String getWarningGui(String userCommand, int tasklistSize) {
try {
UserInputException.checkUserInput(userCommand, tasklistSize);
} catch (DukeException exception) {
return exception.toString();
} catch (Exception exception) {
return "ERRRR ERROR ERRR. SYSTEM FAILURE. UNKNOWN EXCEPTION. ERR ERR";
}
return "";
}
}
33 changes: 16 additions & 17 deletions src/main/java/duke/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.List;


import javafx.util.Pair;

Expand All @@ -27,13 +26,14 @@
*/

public class Storage {
private HashMap<String, TaskList> keywordDatabase;
private HashMap<String, PriorityQueue<Pair<LocalDateTime, Task>>> taskScheduleOnDates;

private static final String SAVED_FILE_PATH =
FileSystems.getDefault().getPath("").toAbsolutePath() +
Path.of("/Saved Progress/").toString();
private static final String SAVE_FILE_NAME = "/" + "MY_GRAND_PLAN.txt";
FileSystems.getDefault().getPath("").toAbsolutePath()
+ Path.of("/Saved Progress/").toString();
private static final String SAVE_FILE_NAME = "/MY_GRAND_PLAN.txt";

private HashMap<String, TaskList> keywordDatabase;
private HashMap<String, PriorityQueue<Pair<LocalDateTime, Task>>> taskScheduleOnDates;
/**
* Construct the {@code Storage} object with
* empty database
Expand Down Expand Up @@ -95,7 +95,7 @@ public static TaskList loadProgress() {

String actionAndDate = "";
for (int i = 1; i < availableTaskAsList.size(); i++) {
actionAndDate += availableTaskAsList.get(i) + " ";
actionAndDate += availableTaskAsList.get(i) + " ";
}

actionAndDate = actionAndDate.stripTrailing();
Expand Down Expand Up @@ -150,8 +150,8 @@ public Storage addToDatabase(Task task) {
*/

private void addToKeywordStorage(Task task) {
String toUpdateKeywordDatabase = task.getNature() +
" " + task.getAction() + " " + task.getTimeInfo();
String toUpdateKeywordDatabase = task.getNature()
+ " " + task.getAction() + " " + task.getTimeInfo();

for (String keyword : toUpdateKeywordDatabase.split(" ")) {
if (this.keywordDatabase.containsKey(keyword)) {
Expand Down Expand Up @@ -182,8 +182,8 @@ public Storage removeFromStorage(Task task) {
}

private void removeFromKeywordStorage(Task task) {
String removedTask = task.getNature() +
" " + task.getAction() + " " + task.getTimeInfo();
String removedTask = task.getNature()
+ " " + task.getAction() + " " + task.getTimeInfo();
for (String keyword : removedTask.split(" ")) {
if (this.keywordDatabase.containsKey(keyword)) {
TaskList currentList = this.keywordDatabase.get(keyword);
Expand Down Expand Up @@ -223,8 +223,8 @@ private void removeFromScheduleStorage(Task task) {
Pair<LocalDateTime, Task> deletedPair = new Pair<>(datesOfTasks.get(0), task);

for (Pair<LocalDateTime, Task> pair : scheduleOnDate) {
if (pair.getKey().equals(datesOfTasks.get(0)) &&
pair.getValue().toString().equals(task.toString())) {
if (pair.getKey().equals(datesOfTasks.get(0))
&& pair.getValue().toString().equals(task.toString())) {
deletedPair = pair;
break;
}
Expand Down Expand Up @@ -266,7 +266,7 @@ public static void saveProgressQuery(TaskList listOfCurrentTasks) {
*
*/

public static void saveProgressGUI(String userCommand, TaskList listOfCurrentTasks) {
public static void saveProgressGui(String userCommand, TaskList listOfCurrentTasks) {
if (userCommand.equals("YES")) {
saveProgress(listOfCurrentTasks);
}
Expand Down Expand Up @@ -300,7 +300,6 @@ private void addTaskToSchedule(Task task) {

while (dateBeginFormatted.compareTo(dateEndFormatted) <= 0) {
String searchDate = dateBeginFormatted.toString();
System.out.println(searchDate);
PriorityQueue<Pair<LocalDateTime, Task>> currentQueue;
if (this.taskScheduleOnDates.containsKey(searchDate)) {
currentQueue = this.taskScheduleOnDates.get(searchDate);
Expand Down

0 comments on commit 3ea6be2

Please sign in to comment.