Skip to content

Commit

Permalink
Merge pull request #25 from tjingsheng/fix-bug
Browse files Browse the repository at this point in the history
fix-bugs
  • Loading branch information
tjingsheng authored Sep 20, 2023
2 parents ee492bd + 82df297 commit 4e28cbc
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 37 deletions.
Binary file added docs/Ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/main/java/enums/CommandWord.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public static CommandWord commandWordToValueMap(String value) {
}

/**
* Gets the string representation of the command word.
* Gets the string representation of the `CommandWord`.
*
* @return The string representation of the command word.
* @return The string representation of the `CommandWord`.
*/
public String toValue() {
return this.value;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/enums/ExceptionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum ExceptionMessage {
private final String value;

/**
* Constructs a `Exception Message` enum with the given value.
* Constructs an `ExceptionMessage` enum with the given value.
*
* @param value The string representation of the command word.
*/
Expand All @@ -47,10 +47,10 @@ public enum ExceptionMessage {
}

/**
* Gets the string representation of the exception message with the appropriate args word.
* Gets the string representation of the `ExceptionMessage` with the appropriate args word.
*
* @param args The arguments to replace placeholders in the exception message.
* @return The string representation of the exception message with the appropriate command word.
* @return The string representation of the `ExceptionMessage` with the appropriate command word.
*/
public String toFormattedValue(String ... args) {
return String.format(this.value, (Object[]) args);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/enums/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public enum FilePath {
/**
* Constructs a `FilePath` enum with the given value.
*
* @param value The string representation of the file path.
* @param value The string representation of the `FilePath`.
*/
FilePath(String value) {
this.value = value;
}

/**
* Gets the string representation of the file path.
* Gets the string representation of the `FilePath`.
*
* @return The string representation of the file path.
* @return The string representation of the `FilePath`.
*/
public String toValue() {
return this.value;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/enums/TaskType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public enum TaskType {
}

/**
* Returns the symbol associated with the task type.
* Returns the symbol associated with the `TaskType`.
*
* @return The symbol associated with the task type.
* @return The symbol associated with the `TaskType`.
*/
public String toSymbol() {
return this.symbol;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/enums/WoofMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
public enum WoofMessage {
WOOF_TITLE("Woof Woof"),
HI("Woof Woof! I'm Doggo\nWhat can I do for you?"),
HI("Woof Woof! I'm Oreo!!!\nWhat can I do for you?\nsend `help` for help."),
BYE("Bye. Hope to see you again soon!\nOffing myself... woof :("),
CONFUSED("OOPS!!! I'm sorry, I don't know what '%s' means :(\nsend `help` for help."),
CONFUSED("OOPS!!! I'm sorry, I don't know what is\n'%s'\nsend `help` for help."),
TASK_LIST_COUNT("You have %s tasks in the task list."),
TASK_ADDED("Got it. I've added this task:\n%s%s%s"),
NO_MATCHING_TASKS("No tasks matched your keyword(s)!\n"),
Expand Down Expand Up @@ -52,6 +52,7 @@ public enum WoofMessage {
+ "Notes:\n"
+ "- Replace `<description>` with a task description.\n"
+ "- `<date>` should follow the yyyy-mm-dd date format.\n"
+ " e.g. 2023-12-31\n"
+ "- `<taskIndex>` should be the index of the task in the list\n"
+ " you want to manage.\n\n"
+ "Feel free to ask for help using the `help` command if you\n"
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/exceptions/WoofException.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public class WoofException extends RuntimeException {
/**
* Constructs a new WoofException with the specified error message.
* Constructs a new `WoofException` with the specified error message.
*
* @param message The error message describing the exception.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/exceptions/WoofInvalidCommandException.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public class WoofInvalidCommandException extends WoofException {
/**
* Constructs a new WoofInvalidCommandException with the specified error message.
* Constructs a new `WoofInvalidCommandException` with the specified error message.
*
* @param message The error message describing the invalid command.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/exceptions/WoofStorageException.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class WoofStorageException extends WoofException {
/**
* Constructs a new WoofStorageException with the specified error message.
* Constructs a new `WoofStorageException` with the specified error message.
*
* @param message The error message describing the storage-related exception.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static String[] getArgs(String rawCommand) {

ArrayList<String> result = new ArrayList<>();

String[] words = rawCommand.split("\\s+");
String[] words = rawCommand.trim().split(" ");
if (words.length == 0) {
return result.toArray(new String[0]);
}
Expand All @@ -87,7 +87,7 @@ public static String[] getArgs(String rawCommand) {
result.add(currentWord);
subCommand = new StringBuilder();
} else {
subCommand.append(" ").append(currentWord.trim());
subCommand.append(" ").append(currentWord);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public static String getUserTitle() {
* @return A string representing a horizontal divider line.
*/
public static String getDividerLine() {
return "\n" + "═".repeat(Woof.getChatWidth());
return '\n' + "═".repeat(Woof.getChatWidth());
}
}
34 changes: 24 additions & 10 deletions src/main/java/woof/Woof.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Scanner;

import command.Command;
import command.NullCommand;
import enums.ExceptionMessage;
import exceptions.WoofException;
import parser.Parser;
Expand All @@ -15,7 +16,7 @@

/**
* The `Woof` class is the main class for the Woof CLI application.
* It handles user interactions and the core functionality of the CLI application.
* It handles user interactions and the core functionality of the application.
*/
public class Woof {
private static final int CHAT_WIDTH = 60;
Expand All @@ -35,20 +36,29 @@ public static void main(String[] args) {
public static void runWoofCli() {
Scanner scanner = new Scanner(System.in);
System.out.print(Ui.getHelloWorldMessage());
boolean isConversing = true;
while (isConversing) {
converse(scanner);
scanner.close();
}

/**
* Handles the conversation loop of the Woof CLI application.
*
* @param scanner The Scanner object for user input.
*/
public static void converse(Scanner scanner) {
Command command = new NullCommand("");
while (!command.isByeCommand()) {
TaskList taskList = TaskFileHandler.readFromFile();

System.out.println(Ui.getUserTitle());
String rawCommand = Ui.getUserInput(scanner);

System.out.println(Ui.getBotTitle());
Command command = Parser.parse(rawCommand);
command = Parser.parse(rawCommand);
String response = command.execute(taskList);
String wrappedResponse = wrapText(response, "\n" , getChatWidth());
System.out.print(wrappedResponse);
String wrappedResponse = wrapText(response, "" , getChatWidth());
System.out.println(wrappedResponse);

isConversing = !command.isByeCommand();
TaskFileHandler.saveToFile(taskList);
}
}
Expand Down Expand Up @@ -102,13 +112,17 @@ public static String wrapText(String message, String separator, int length) {
for (int i = 0; i < message.length(); i++) {
char currentChar = message.charAt(i);
if (currentChar == '\n') {
result.append(currentChar);
result.append(separator);
currentLineLength = -1;
}
if (currentLineLength >= length) {
} else if (currentLineLength >= length) {
result.append('\n');
result.append(separator);
result.append(currentChar);
currentLineLength = 0;
} else {
result.append(currentChar);
}
result.append(currentChar);
currentLineLength++;
}
return result.toString();
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/woofwoof/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package woofwoof;

import java.util.Objects;

import javafx.application.Application;
import woof.Woof;

/**
* A launcher class to workaround classpath issues.
* The Launcher class is responsible for launching the WoofWoof application.
*/
public class Launcher {
/**
* The main method of the Launcher class.
*
* @param args The command-line arguments. If 'cli' is provided as an argument, it launches the CLI mode;
* otherwise, it launches the GUI mode.
*/
public static void main(String[] args) {
Application.launch(WoofWoof.class, args);
if (args.length != 1 || !Objects.equals(args[0], "cli")) {
Application.launch(WoofWoof.class, args);
} else {
Woof.main(null);
}
}
}
20 changes: 13 additions & 7 deletions src/main/java/woofwoof/WoofWoof.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/**
* The `WoofWoof` class is the main class for the Woof GUI application.
* It handles user interactions and the core functionality of the application.
* The `WoofWoof` class relies on the existence of the `Woof` class, as it only extends `Woof` to support GUI.
*/
public class WoofWoof extends Application {
private static final Font FONT = loadCustomFont();
Expand Down Expand Up @@ -104,6 +104,12 @@ public void initialize() {
this.userInput.setFont(getFont());
this.sendButton.setFont(getFont());
this.clearButton.setFont(getFont());

this.dialogArea.getChildren().addAll(
DialogBox.getBotDialog(
Woof.wrapText(WoofMessage.HI.toFormattedValue(), "", Woof.getChatWidth()), this.doggo
)
);
}

/**
Expand Down Expand Up @@ -133,7 +139,7 @@ public void start(Stage primaryStage) {
fxmlLoader.<WoofWoof>getController().setWoof(this.woof);
primaryStage.show();
} catch (IOException e) {
System.out.printf("oh no\n%s\n", e.getMessage());
System.out.printf("really oh no\n%s\n", e.getMessage());
}
}

Expand Down Expand Up @@ -185,12 +191,12 @@ private void loadCssStyles() {
*/
@FXML
private void handleUserSubmit() {
String message = this.userInput.getText();
if (!message.trim().isEmpty()) {
String message = this.userInput.getText().trim();
if (!message.isEmpty()) {
String response = processMessage(message);
this.dialogArea.getChildren().addAll(
DialogBox.getUserDialog(Woof.wrapText(message, "\n", Woof.getChatWidth()), this.user),
DialogBox.getBotDialog(Woof.wrapText(response, "\n", Woof.getChatWidth()), this.doggo)
DialogBox.getUserDialog(Woof.wrapText(message, "", Woof.getChatWidth()), this.user),
DialogBox.getBotDialog(Woof.wrapText(response, "", Woof.getChatWidth()), this.doggo)
);
this.userInput.clear();
}
Expand All @@ -216,7 +222,7 @@ private void scheduleCloseAfterDelay() {
currentStage.close();
executorService.shutdown();
}),
2, TimeUnit.SECONDS
1, TimeUnit.SECONDS
);
}

Expand Down
1 change: 1 addition & 0 deletions src/test/java/command/HelpCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void testExecuteShowsHelpMessage() {
+ "Notes:" + System.lineSeparator()
+ "- Replace `<description>` with a task description." + System.lineSeparator()
+ "- `<date>` should follow the yyyy-mm-dd date format." + System.lineSeparator()
+ " e.g. 2023-12-31" + System.lineSeparator()
+ "- `<taskIndex>` should be the index of the task in the list" + System.lineSeparator()
+ " you want to manage." + System.lineSeparator()
+ System.lineSeparator()
Expand Down

0 comments on commit 4e28cbc

Please sign in to comment.