Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kenneth Tan] iP #176

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9b2170b
Level-0
SemiColonKen Oct 10, 2024
2fdeff9
Level-0
SemiColonKen Oct 13, 2024
6aa72b3
Level-0
SemiColonKen Oct 13, 2024
1372eaa
Level-1
SemiColonKen Oct 13, 2024
bf43ec2
Level-1
SemiColonKen Oct 13, 2024
3d84e1c
Level-2
SemiColonKen Oct 13, 2024
02fccb1
Level-0
SemiColonKen Oct 13, 2024
d645d1e
Level-0
SemiColonKen Oct 13, 2024
da7a373
Level-1
SemiColonKen Oct 13, 2024
98d29ab
Level-2
SemiColonKen Oct 13, 2024
22bc111
Level-3
SemiColonKen Oct 13, 2024
21a6c09
Level-4
SemiColonKen Oct 13, 2024
4ddff06
Level-4
SemiColonKen Oct 13, 2024
4686943
Level-4
SemiColonKen Oct 13, 2024
24c4425
Level-4
SemiColonKen Oct 13, 2024
08036ed
Level-5
SemiColonKen Oct 13, 2024
174eb8f
Merge branch 'branch-Level-5'
Oct 13, 2024
f2ffef3
Level-6
SemiColonKen Oct 13, 2024
2e81f9e
Implemented Level-7
Oct 14, 2024
b6734f7
Merge branch 'branch-Level-6'
Oct 14, 2024
41cbb08
Merge branch 'branch-Level-7'
Oct 14, 2024
a337c2d
A-MoreOOP
SemiColonKen Oct 15, 2024
b526ab8
Level-9
SemiColonKen Oct 15, 2024
4cc1ab2
Level-9
SemiColonKen Oct 15, 2024
7211d20
Level-9
SemiColonKen Oct 15, 2024
f8cc044
Resolved merge conflicts
Oct 15, 2024
4f2a295
Merge pull request #1 from SemiColonKen/branch-Level-9
SemiColonKen Oct 15, 2024
48f5dab
A-JavaDoc
Oct 15, 2024
165b854
A-JavaDoc
Oct 15, 2024
b1675c0
A-JavaDoc
Oct 15, 2024
ddcb0a2
A-JavaDoc
Oct 15, 2024
0ab47a6
Resolved merge conflicts
Oct 15, 2024
0fe6e6c
Merge pull request #2 from SemiColonKen/branch-A-JavaDoc
SemiColonKen Oct 15, 2024
e193e2e
Added JavaDoc for FindCommand
Oct 15, 2024
af556b4
Merge pull request #3 from SemiColonKen/branch-A-JavaDoc
SemiColonKen Oct 15, 2024
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
28 changes: 28 additions & 0 deletions src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main.java;

/**
* Represents the command to add a task to the task list.
*/
public class AddCommand extends Command {
private final Task task;

public AddCommand(Task task) {
this.task = task;
}

/**
* Perform the add command by adding the task to the task list,
* saving the task list and notifying the user.
*
* @param tasks The task list.
* @param ui The UI component which the user will see.
* @param storage The storage component to save the updated task list.
* @throws KenChatException If there is an error when saving the task list.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException {
tasks.addTask(task);
storage.save(tasks.getTasks());
ui.showTaskAdded(task, tasks.getSize());
}
}
28 changes: 28 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main.java;

/**
* Represents an abstract command that can be performed with the task management chat bot.
* Specific commands like add or delete will extend this class.
*/
public abstract class Command {

/**
* Performs the command using the given task list, UI and storage components.
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
* @throws KenChatException If there is an error when performing the command.
*/
public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException;

/**
* Notifying the chat bat on its running status after performing the command.
* All commands will return true, except only when ExitCommand is called.
*
* @return true If the chat bot should continue running.
*/
public boolean isRunning() {
return true;
}
}
35 changes: 35 additions & 0 deletions src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main.java;

/**
* Represents a command to delete a task from the task list.
*/
public class DeleteCommand extends Command {
private final int index;

public DeleteCommand(int index) {
this.index = index - 1;
}

/**
* Performs the delete command, removing the specific task from the task list, using the task index.
* This is followed by saving to the storage and notifying the user through a display from the UI.
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
* @throws KenChatException If the task list is empty, or if the task number is invalid.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException {
if (tasks.getSize() == 0) {
throw new KenChatException(KenChatException.getTaskNotExistMessage());
}
if (index < 0 || index >= tasks.getSize()) {
throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage());
}
Task removedTask = tasks.getTask(index);
tasks.deleteTask(index);
storage.save(tasks.getTasks());
ui.showTaskRemoved(removedTask, tasks.getSize());
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main.java;

/**
* Represents a command to exit the chat bot.
*/
public class ExitCommand extends Command {

/**
* Performs the exit command. No other action is required.
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {}

/**
* Notify the chat bot to terminate.
*
* @return false so that the chat bot will stop running.
*/
@Override
public boolean isRunning() {
return false;
}
}
43 changes: 43 additions & 0 deletions src/main/java/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main.java;

import java.util.Map;

/**
* Represents a command to find tasks in the task list according to keyword(s).
*/
public class FindCommand extends Command {
private final String keyword;

public FindCommand(String keyword) {
this.keyword = keyword;
}

/**
* Performs the find command to search for tasks containing the specific keyword(s).
*
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
* @throws KenChatException If an error occurs while performing the command.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException {
// Use the findTasksByKeywordWithIndex method that returns a Map of <index, task>
Map<Integer, Task> matchingTasks = tasks.findTasksByKeywordWithIndex(keyword);
if (matchingTasks.isEmpty()) {
ui.showError("No matching tasks found for: " + keyword);
} else {
ui.printLine();
ui.showMessage("Here are the matching tasks in your list:");
// Display the matching tasks with the correct indices
for (Map.Entry<Integer, Task> entry : matchingTasks.entrySet()) {
int index = entry.getKey(); // The original index of the task in the task list
Task task = entry.getValue(); // The task itself
ui.showMessage(index + ". " + task); // Output task with its original index
}
ui.printLine();
System.out.println();
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main.java;

/**
* Represents a command to display help information to the user.
*/
public class HelpCommand extends Command {

/**
* Performs the help command by displaying all the valid commands the user can enter.
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
ui.showHelp();
}
}
55 changes: 55 additions & 0 deletions src/main/java/KenChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main.java;

/**
* The main class for KenChat application, managing the UI, task storage and command execution.
*/
public class KenChat {
private final Storage storage;
private TaskList tasks;
private final Ui ui;

/**
* Initialises the KenChat application with the specific file path for storage.
*
* @param filePath The file path where task list is stored.
*/
public KenChat(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
} catch (KenChatException e) {
tasks = new TaskList();
ui.showLoadingError();
}
}

/**
* Runs the KenChat application, processing user commands until exit is prompted.
*/
public void run() {
ui.showStart();
boolean isRunning = true;
while (isRunning) {
try {
String fullCommand = ui.readCommand();
Command command = Parser.parse(fullCommand);
command.execute(tasks, ui, storage);
isRunning = command.isRunning();
} catch (KenChatException e) {
ui.showError(e.getMessage());
}
}
ui.showEnd();
}

/**
* The main entry point of the KenChat application.
*
* @param args Command line arguments (not used).
* @throws KenChatException If there is an error during initialization.
*/
public static void main(String[] args) throws KenChatException {
new KenChat("data/KenChat.txt").run();
}
}
69 changes: 69 additions & 0 deletions src/main/java/KenChatException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main.java;

/**
* Exception class for the KenChat application
*/
public class KenChatException extends Exception {

public KenChatException(String message) {
super(message);
}

/**
* Returns an error message because of an empty description for a specific command
* @param command The command that has the missing description.
* @return The error message.
*/
public static String getEmptyDescriptionMessage(String command) {
return "Description is missing! Enter a description for the command. Use: " + command + " <description>";
}

public static String getUnknownCommandMessage() {
return "Wrong Command! Use <help> for full list of commands. ";
}

public static String getInvalidDeadlineFormatMessage() {
return "Wrong deadline format. Use: deadline <description> /by <date> Note: <date> can be date and/or time.";
}

public static String getInvalidEventFormatMessage() {
return "Wrong event format. Use: event <description> /from <start_time> /to <end_time> Note: <start_time> and <end_time> can be date and/or time.";
}

public static String getTaskNotExistMessage() {
return "No task exists.";
}

public static String getInvalidTaskNumberMessage() {
return "Please enter a valid task number. Use: mark/unmark/delete <number(s)>";
}

public static String getTaskNumberDoesNotExistMessage() {
return "That task number does not exist. Use <list> to see full task list.";
}

/**
* Returns an error message because of a missing task number for Mark/Unmark/Delete command.
*
* @param action The command that is supposed to carry out the action of Mark/Unmark/Delete.
* @return The error message.
*/
public static String getEmptyTaskNumberMessage(String action) {
return "Task number is missing!! Please specify the task number to " + action + ".";
}

public static String emptyCommand() {
return "Empty command!! Use <help> for full list of commands.";
}

public static String multipleSpaces() {
return "Too many spaces between words! Ensure each word is separated by only a space.";
}
public static String directoryCreationFailure() {
return "Failed to create directory.";
}

public static String getKeyWordMissing() {
return "Keyword is missing. Use: find <keyword>";
}
}
24 changes: 24 additions & 0 deletions src/main/java/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main.java;

/**
* Represents a command to list all the tasks in the task list.
*/
public class ListCommand extends Command {

/**
* Performs the list command, displaying all the tasks in the task list via the UI.
* If the task list is empty, an exception is thrown.
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
* @throws KenChatException If the task list is empty, which will display that there is not task in the tasklist.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException {
if (tasks.getSize() == 0) {
throw new KenChatException(KenChatException.getTaskNotExistMessage());
}
ui.showTasksList(tasks.getTasks());
}
}
35 changes: 35 additions & 0 deletions src/main/java/MarkCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main.java;

/**
* Performs a command to mark a task as completed in the task list.
*/
public class MarkCommand extends Command {
private final int index;

public MarkCommand(int index) {
this.index = index - 1;
}

/**
* Performs the mark command, marking the specific task as done.
* Saves the updated task list to the storage and notifying the user through display via the UI
*
* @param tasks The task list that the command will work on.
* @param ui The UI component of the chat bot.
* @param storage The storage component of the chat bot.
* @throws KenChatException If the task list is empty or the index is invalid.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws KenChatException {
if (tasks.getSize() == 0) {
throw new KenChatException(KenChatException.getTaskNotExistMessage());
}
if (index < 0 || index >= tasks.getSize()) {
throw new KenChatException(KenChatException.getTaskNumberDoesNotExistMessage());
}
Task task = tasks.getTask(index);
task.markAsDone();
storage.save(tasks.getTasks());
ui.showTaskMarked(task);
}
}
Loading