Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
D|0|return book|Sunday
E|1|project meeting|Mon 2pm|4pm
T|1|submit report
T|0|return book
6 changes: 4 additions & 2 deletions src/main/java/opus/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import opus.commands.CommandResult;



Expand Down Expand Up @@ -97,14 +98,15 @@ public void start(Stage stage) {

private void handleUserInput() {
String userText = userInput.getText();
String opusText = opus.getResponse(userInput.getText());
CommandResult commandResult = opus.getResponse(userText);
String opusText = commandResult.getFeedbackToUser();
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText, userImage),
DialogBox.getOpusDialog(opusText, opusImage)
);
userInput.clear();

if (opus.isExit()) {
if (commandResult.isExit()) {
Platform.exit();
}
}
Expand Down
157 changes: 16 additions & 141 deletions src/main/java/opus/Opus.java
Original file line number Diff line number Diff line change
@@ -1,166 +1,41 @@
package opus;

import opus.exceptions.OpusException;
import opus.commands.Command;
import opus.commands.CommandResult;

/**
* The Opus class is the main entry point of the Duke task manager application.
* It initializes the necessary components (UI, storage, task list) and runs
* the main loop to interact with the user, handle commands, and manage tasks.
* Represents the main class of the program.
*/
public class Opus {

private Storage storage;
private TaskList taskList;
private boolean isExit = false;

/**
* Initializes the Opus application with the specified file path for storage.
* Loads tasks from storage and sets up UI and task list.
* Creates a new instance of Opus.
*
* @param filePath Path to the file where tasks are stored.
* @param filePath The file path of the storage file.
*/
public Opus(String filePath) {
storage = new Storage(filePath);
taskList = new TaskList(storage.load());
}

/**
* Handles the bye command by saving tasks to storage and setting the exit flag.
* Gets the response to the user input.
*
* @return The response message indicating the application will exit.
* @throws OpusException If an error occurs while saving tasks.
* @param input The user input.
* @return The response to the user.
*/
private String handleByeCommand() throws OpusException {
storage.save(taskList.getTasks());
isExit = true; // Set the exit flag
return "Bye. Hope to see you again soon!";
}

/**
* Returns whether the application should exit.
*
* @return True if the application should exit, false otherwise.
*/
public boolean isExit() {
return isExit;
}

/**
* Handles the list command by displaying all tasks in the task list.
*
* @return The response message containing all tasks in the list.
*/
private String handleListCommand() {
String response = "";
for (Task task : taskList.getTasks()) {
response += task.toString() + "\n";
}
return response;
}
/**
* Handles the mark command by marking a task as done.
*
* @param words The command split into words.
* @return The response message indicating the task has been marked as done.
* @throws OpusException If the task number is not specified or is invalid.
*/
private String handleMarkCommand(String[] words) throws OpusException {
if (words.length < 2) {
throw new OpusException("Please specify the task number to mark.");
}
int i = Integer.parseInt(words[1]) - 1;
assert i >= 0 && i < taskList.getSize() : "Index out of bounds: " + i;
taskList.getTask(i).markAsDone();
return "Nice! I've marked this task as done:\n" + taskList.getTask(i).toString();
}
/**
* Handles the delete command by removing a task from the list.
*
* @param words The command split into words.
* @return The response message indicating the task has been deleted.
* @throws OpusException If the task number is not specified or is invalid.
*/
private String handleDeleteCommand(String[] words) throws OpusException {
String response = "";
if (words.length < 2) {
throw new OpusException("Please specify the task number to delete.");
}
int i = Integer.parseInt(words[1]) - 1;
assert i >= 0 && i < taskList.getSize() : "Index out of bounds: " + i;
response = "Noted. I've removed this task:\n" + taskList.getTask(i).toString() + "\n";
taskList.removeTask(i);
response += "Now you have " + taskList.getSize() + " tasks in the list.";
return response;
}
/**
* Handles the help command by displaying a list of available commands.
*
* @return The response message containing the list of commands.
*/
private String handleHelpCommand() {
String response = "Here are the commands you can use:\n";
response += "1. list - List all tasks\n";
response += "2. mark <index> - Mark a task as done\n";
response += "3. delete <index> - Delete a task\n";
response += "4. deadline <task> /by <end-date> - Add a deadline by the end date\n";
response += "5. event <task> /from <start-date> /to <end-date>";
response += " - Add an event with start and end dates\n";
response += "6. todotask <> - Add a Todo\n";
response += "7. bye - Exit the application";
return response;
}


/**
* Main method to launch the Opus application.
* @param args Command-line arguments, not used.
*/
public static void main(String[] args) {
new Opus("data/tasks.txt");
}

public String getResponse(String input) {
String[] words = Parser.parse(input);
String response = "";
public CommandResult getResponse(String input) {
try {
assert words.length > 0 : "Command cannot be empty";
if (words[0].equals("bye")) {
response = handleByeCommand();
} else if (words[0].equals("list")) {
response = handleListCommand();
} else if (words[0].equals("mark")) {
response = handleMarkCommand(words);
} else if (words[0].equals("delete")) {
response = handleDeleteCommand(words);
} else if (words[0].equals("help")) {
response = handleHelpCommand();
} else {
if (words[0].equals("todo")) {
assert words.length > 1 : "The description of a todo cannot be empty";
if (words.length <= 1) {
throw new OpusEmptyDescriptionException("The description of a todo cannot be empty.");
}
Task todo = new ToDo(words[1]);
taskList.addTask(todo);
} else if (words[0].equals("deadline")) {
assert words.length > 1 : "Deadline details are missing";
String[] parts = Parser.parseDeadlineDetails(words[1]);
Task deadline = new Deadline(parts[0], parts[1]);
taskList.addTask(deadline);
} else if (words[0].equals("event")) {
assert words.length > 1 : "Event details are missing";
String[] parts = Parser.parseEventDetails(words[1]);
Task event = new Event(parts[0], parts[1], parts[2]);
taskList.addTask(event);
} else {
throw new OpusUnknownCommandException("I'm sorry, but I don't know what that means.");
}
response = "Got it. I've added this task:\n";
response += taskList.getTask(taskList.getSize() - 1).toString() + "\n";
response += "Now you have " + taskList.getSize() + " tasks in the list.";
return response;
}
Command command = Parser.parse(input);
String response = command.execute(taskList, storage);
boolean isExit = command.isExit();
return new CommandResult(response, isExit);
} catch (OpusException e) {
return e.getMessage();
return new CommandResult(e.getMessage(), false);
}
return response;
}
}
17 changes: 0 additions & 17 deletions src/main/java/opus/OpusException.java

This file was deleted.

Loading