Skip to content

Commit

Permalink
Fixed window auto resizing after input
Browse files Browse the repository at this point in the history
  • Loading branch information
lennoxtr committed Feb 20, 2023
1 parent 3ea6be2 commit 5e7e644
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 66 deletions.
5 changes: 3 additions & 2 deletions Saved Progress/MY_GRAND_PLAN.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
false TODO DRINK WATER
false TODO WATCH NGN
true TODO DRINK WATER
true TODO WATCH NGN
false DEADLINE NGN /BY 20-02-2023 2359
false TODO DRINK TEA
147 changes: 89 additions & 58 deletions src/main/java/duke/io/input/exception/UserInputException.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package duke.io.input.exception;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import duke.util.Parser;

/**
* An implementation of the {@code DukeException} template. It is used
* to detect errors within user's input, and throw the corresponding
Expand All @@ -27,74 +31,101 @@ public class UserInputException extends Exception {
public static void checkUserInput(String input, int currentSize) throws DukeException {
String[] inputSplitArray = input.split(" ");
List<String> inputSplitList = Arrays.asList(inputSplitArray);
int lengthOfCommand = inputSplitList.size();

String command = inputSplitList.get(0);
if (command.equals("TODO")) {
if (inputSplitList.size() <= 1) {
throw new DukeException("EMPTY TO-DO COMMAND... DOING NOTHING MUCH?");
}
} else if (command.equals("DEADLINE")) {
if (input.contains(" /BY ")) {
if (inputSplitList.size() <= 3) {
throw new DukeException("MORE INFO NEEDED FOR DEADLINE");
}
} else {
throw new DukeException("NO DEADLINE. FREE TIME ISN'T SO EASY TO COME BY.");
}
} else if (command.equals("EVENT")) {
if (input.contains(" /FROM ") && input.contains(" /TO ")) {
if (inputSplitList.size() <= 5) {
throw new DukeException("MORE INFO NEEDED FOR EVENT");
}
} else {
throw new DukeException("MY CREATOR DIDN'T MAKE ME SMART ENOUGH TO INFER INFO FROM THIS...");
}
} else if (command.equals("MARK")) {
if (inputSplitList.size() == 1) {
throw new DukeException("MY CREATOR DIDN'T MAKE ME SMART ENOUGH TO INFER INFO FROM THIS...");
if (command.equals("TODO") || command.equals("DEADLINE") || command.equals("EVENT")) {
checkParsingTask(input, lengthOfCommand);
} else if (command.equals("MARK") || command.equals("UNMARK") || command.equals("DELETE")) {
int requestedIndex = Integer.valueOf(inputSplitList.get(1)) - 1;
checkMarkingAndDelete(requestedIndex, lengthOfCommand, currentSize);
} else if (command.equals("LIST")) {
if (lengthOfCommand > 1) {
throw new DukeException("ARE YOU EXPECTING ME TO DO SOMETHING WITH THE EXTRA INFO?");
}
if (inputSplitList.size() > 2) {
throw new DukeException("TOO MUCH INFO...");
} else {
if (Integer.valueOf(inputSplitList.get(1)) > currentSize) {
if (currentSize > 1) {
throw new DukeException("YOU ONLY HAVE " + currentSize + " TASKS");
} else {
throw new DukeException("YOU ONLY HAVE " + currentSize + " TASK");
}
}
} else if (command.equals("FIND")) {
if (lengthOfCommand > 2) {
throw new DukeException("THIS PLACE... IT MADE ME WEAK! I CAN ONLY SEARCH 1 KEYWORD NOW");
} else if (lengthOfCommand == 1) {
throw new DukeException("WHAT EXACTLY DO YOU WANT TO FIND...");
}
} else if (command.equals("SCHEDULE")) {
checkSchedule(inputSplitList.get(1));
} else {
throw new DukeException("COMMAND NOT FOUND... WHAT ARE YOU UP TO...");
}
}

} else if (command.equals("UNMARK")) {
if (inputSplitList.size() == 1) {
throw new DukeException("MY CREATOR DIDN'T MAKE ME SMART ENOUGH TO INFER INFO FROM THIS...");
}
if (inputSplitList.size() > 2) {
throw new DukeException("TOO MUCH INFO...");
} else {
if (Integer.valueOf(inputSplitList.get(1)) > currentSize) {
throw new DukeException("YOU DON'T HAVE THAT MANY TASKS");
}
}
private static void checkParsingTask(String input, int lengthOfCommand) throws DukeException {
String command = input.split(" ")[0];
if (command.equals("TODO")) {
checkToDo(input, lengthOfCommand);
} else if (command.equals("DEADLINE")) {
checkDeadline(input, lengthOfCommand);
} else {
checkEvent(input, lengthOfCommand);
}
try {
Parser.parseTask(input);
} catch (Exception e) {
throw new DukeException("UH OH..." + e.getMessage());
}
}

} else if (command.equals("DELETE")) {
if (inputSplitList.size() > 2) {
throw new DukeException("TOO MUCH INFO...");
} else {
if (Integer.valueOf(inputSplitList.get(1)) > currentSize) {
throw new DukeException("YOU DON'T HAVE THAT MANY TASKS");
}
}
private static void checkToDo(String input, int lengthOfCommand) throws DukeException {
if (lengthOfCommand <= 1) {
throw new DukeException("EMPTY TO-DO COMMAND... DOING NOTHING MUCH?");
}
}

} else if (command.equals("LIST")) {
if (inputSplitList.size() > 1) {
throw new DukeException("ARE YOU EXPECTING ME TO DO SOMETHING WITH THE EXTRA INFO?");
private static void checkDeadline(String input, int lengthOfCommand) throws DukeException {
if (input.contains(" /BY ")) {
if (lengthOfCommand <= 3) {
throw new DukeException("MORE INFO NEEDED FOR DEADLINE");
}
} else if (command.equals("FIND")) {
} else {
throw new DukeException("NO DEADLINE. FREE TIME ISN'T SO EASY TO COME BY.");
}
}

} else if (command.equals("SCHEDULE")) {
private static void checkEvent(String input, int lengthOfCommand) throws DukeException {
if (input.contains(" /FROM ") && input.contains(" /TO ")) {
if (lengthOfCommand <= 5) {
throw new DukeException("MORE INFO NEEDED FOR EVENT");
}
} else {
throw new DukeException("MY CREATOR DIDN'T MAKE ME SMART ENOUGH TO INFER INFO FROM THIS...");
}
}

private static void checkMarkingAndDelete(int requestedIndex,
int lengthOfCommand,
int currentSize) throws DukeException {
if (lengthOfCommand == 1) {
throw new DukeException("MY CREATOR DIDN'T MAKE ME SMART ENOUGH TO INFER INFO FROM THIS...");
} else if (lengthOfCommand > 2) {
throw new DukeException("TOO MUCH INFO...");
} else {
throw new DukeException("COMMAND NOT FOUND... WHAT ARE YOU UP TO...");
if (requestedIndex > currentSize) {
if (currentSize > 1) {
throw new DukeException("YOU ONLY HAVE " + currentSize + " TASKS");
} else {
throw new DukeException("YOU ONLY HAVE " + currentSize + " TASK");
}
} else if (requestedIndex < 0) {
throw new DukeException("BAD AT MATH? THAT INDEX DOESN'T EXIST");
}
}
}

private static void checkSchedule(String dateAsString) throws DukeException {
try {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate scheduleDate = LocalDate.parse(dateAsString, dateFormat);
} catch (Exception e) {
throw new DukeException("WRONG DATE FORMAT... HOW FRUSTRATING");
}
}
}


8 changes: 4 additions & 4 deletions src/main/java/duke/io/input/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public MainWindow() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"/view/MainWindow.fxml"));
fxmlLoader.setController(this);
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
fxmlLoader.load();
} catch (IOException exception) {
exception.printStackTrace();;
exception.printStackTrace();
}
}

Expand All @@ -73,6 +73,8 @@ public void greet() {
Greeting greeting = new Greeting();
String response = UserInterface.printGuiLogo();
String toPrintOut = greeting.toString();
System.out.println(DialogBox.getDukeDialog(response, dukeImage));
System.out.println("I ran");
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(response, dukeImage),
DialogBox.getDukeDialog(toPrintOut, dukeImage));
Expand Down Expand Up @@ -120,7 +122,6 @@ private int runDuke() {

@FXML
private void handleUserInput() {
getScene().getWindow().sizeToScene();
if (this.firstTimeRunningDukeFlag < 0) {
firstTimeRunningDukeFlag = runDuke();

Expand Down Expand Up @@ -157,7 +158,6 @@ private void handleUserInput() {
DialogBox.getUserDialog(input, userImage));

Storage.saveProgressGui(input, this.currentEvent.getTaskList());
System.exit(0);
}
} else {
return;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Storage {

private HashMap<String, TaskList> keywordDatabase;
private HashMap<String, PriorityQueue<Pair<LocalDateTime, Task>>> taskScheduleOnDates;

/**
* Construct the {@code Storage} object with
* empty database
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/DialogBox.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<fx:root alignment="TOP_RIGHT" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="200.0" prefHeight="298.0" prefWidth="539.0" spacing="5.0" type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">
<fx:root alignment="TOP_RIGHT" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="200.0" prefHeight="298.0" prefWidth="539.0" spacing="5.0" type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx/11" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="dialog" alignment="TOP_RIGHT" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="273.0" prefWidth="428.0" text="Label" wrapText="true">
<font>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/MainWindow.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<fx:root maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="700.0" type="AnchorPane" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">
<fx:root maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="700.0" type="AnchorPane" xmlns="http://javafx.com/javafx/11" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextField fx:id="userInput" layoutY="758.0" onAction="#handleUserInput" prefHeight="42.0" prefWidth="724.0" promptText="Enter Text" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="76.0" />
<Button fx:id="sendButton" alignment="CENTER" layoutX="724.0" layoutY="758.0" mnemonicParsing="false" onAction="#handleUserInput" prefHeight="42.0" prefWidth="76.0" text="Send" textFill="#898686" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
Expand Down

0 comments on commit 5e7e644

Please sign in to comment.