From 1c3e9bde10b2f8d1829ab71829e896dbb24e7193 Mon Sep 17 00:00:00 2001 From: raydenlim <109991591+raydenlim@users.noreply.github.com> Date: Fri, 15 Sep 2023 22:39:59 +0800 Subject: [PATCH] BCD-Extension --- data/tasks.txt | 8 +- src/main/java/Command/StatisticsCommand.java | 32 +++++++ src/main/java/Parser/Parser.java | 3 + src/main/java/Task/Statistics.java | 99 ++++++++++++++++++++ src/main/java/Task/Task.java | 13 ++- src/main/java/TaskList/TaskList.java | 42 +++++++++ src/main/java/Ui/Ui.java | 22 ++++- 7 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Command/StatisticsCommand.java create mode 100644 src/main/java/Task/Statistics.java diff --git a/data/tasks.txt b/data/tasks.txt index 9967022d22..178cd4ba7d 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -1,2 +1,6 @@ -[D][ ] uabrgiub (by: 12 Dec 1212 00:00) -[A][ ] iguiugbf +[A][X] engaou +[A][X] ngoaunjvlao +[A][X] ibagirbg +[A][X] ijbauiebg +[A][ ] rjgbiebgkabg +[D][ ] bgiabrg (by: uvbk) diff --git a/src/main/java/Command/StatisticsCommand.java b/src/main/java/Command/StatisticsCommand.java new file mode 100644 index 0000000000..4fddd46673 --- /dev/null +++ b/src/main/java/Command/StatisticsCommand.java @@ -0,0 +1,32 @@ +package command; + +import duke.DukeException; +import storage.Storage; +import task.Statistics; +import tasklist.TaskList; +import ui.Ui; + +/** + * The `StatisticsCommand` class represents a command to calculate and display task statistics. + * It calculates statistics such as the number of tasks completed within the last week, + * the total number of tasks completed, and the percentages of tasks completed. + * + * @author raydenlim + * @version 0.0.0 + */ +public class StatisticsCommand extends Command { + /** + * Executes the statistics command by calculating task statistics and returning the result as a formatted string. + * + * @param taskList The `TaskList` containing the tasks for which statistics will be calculated. + * @param ui The user interface component used to display the statistics. + * @param storage The storage component used to load and save data (not used in this command). + * @return A formatted string containing the calculated task statistics. + * @throws DukeException If there is an error calculating or displaying the statistics. + */ + @Override + public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + Statistics statistics = taskList.calculateStatistics(); + return ui.showStatistics(statistics); + } +} diff --git a/src/main/java/Parser/Parser.java b/src/main/java/Parser/Parser.java index afce11dabe..6a826ce560 100644 --- a/src/main/java/Parser/Parser.java +++ b/src/main/java/Parser/Parser.java @@ -13,6 +13,7 @@ import command.HelpCommand; import command.ListCommand; import command.MarkCommand; +import command.StatisticsCommand; import command.ToDoCommand; import command.UnmarkCommand; import duke.DukeException; @@ -146,6 +147,8 @@ public static Command parse(String input) throws DukeException { return new HelpCommand(); case "find": return createFindCommand(input); + case "stats": + return new StatisticsCommand(); default: throw new DukeException("I'm sorry, but I don't understand that command."); } diff --git a/src/main/java/Task/Statistics.java b/src/main/java/Task/Statistics.java new file mode 100644 index 0000000000..3b52153beb --- /dev/null +++ b/src/main/java/Task/Statistics.java @@ -0,0 +1,99 @@ +package task; + +/** + * The `Statistics` class represents task statistics, including the number of tasks completed within the last week, + * the total number of tasks completed, the percentage of tasks completed within the last week, and the percentage of + * total tasks completed. + * + * @author raydenlim + * @version 0.0.0 + */ +public class Statistics { + private int tasksCompletedThisWeek; + private int totalTasksCompleted; + private double percentageCompletedThisWeek; + private double percentageTotalCompleted; + + /** + * Constructs a new `Statistics` object with default values. + * Initially, all statistics values are set to zero. + */ + public Statistics() { + tasksCompletedThisWeek = 0; + totalTasksCompleted = 0; + percentageCompletedThisWeek = 0.0; + percentageTotalCompleted = 0.0; + } + + /** + * Gets the number of tasks completed within the last week. + * + * @return The number of tasks completed within the last week. + */ + public int getTasksCompletedThisWeek() { + return tasksCompletedThisWeek; + } + + /** + * Sets the number of tasks completed within the last week. + * + * @param tasksCompletedThisWeek The number of tasks completed within the last week. + */ + public void setTasksCompletedThisWeek(int tasksCompletedThisWeek) { + this.tasksCompletedThisWeek = tasksCompletedThisWeek; + } + + /** + * Gets the total number of tasks completed. + * + * @return The total number of tasks completed. + */ + public int getTotalTasksCompleted() { + return totalTasksCompleted; + } + + /** + * Sets the total number of tasks completed. + * + * @param totalTasksCompleted The total number of tasks completed. + */ + public void setTotalTasksCompleted(int totalTasksCompleted) { + this.totalTasksCompleted = totalTasksCompleted; + } + + /** + * Gets the percentage of tasks completed within the last week. + * + * @return The percentage of tasks completed within the last week. + */ + public double getPercentageCompletedThisWeek() { + return percentageCompletedThisWeek; + } + + /** + * Sets the percentage of tasks completed within the last week. + * + * @param percentCompletedThisWeek The percentage of tasks completed within the last week. + */ + public void setPercentageCompletedThisWeek(double percentCompletedThisWeek) { + this.percentageCompletedThisWeek = percentCompletedThisWeek; + } + + /** + * Gets the percentage of total tasks completed. + * + * @return The percentage of total tasks completed. + */ + public double getPercentageTotalCompleted() { + return percentageTotalCompleted; + } + + /** + * Sets the percentage of total tasks completed. + * + * @param percentTotalCompleted The percentage of total tasks completed. + */ + public void setPercentageTotalCompleted(double percentTotalCompleted) { + this.percentageTotalCompleted = percentTotalCompleted; + } +} diff --git a/src/main/java/Task/Task.java b/src/main/java/Task/Task.java index 79aee4b10c..2f8ace02e3 100644 --- a/src/main/java/Task/Task.java +++ b/src/main/java/Task/Task.java @@ -17,7 +17,7 @@ public class Task { protected String description; protected boolean isDone; - protected String dateTime; + private LocalDate completedDate; /** * Constructs a new task with the specified description. @@ -52,6 +52,7 @@ public String getDescription() { */ public void isCompleted() { isDone = true; + completedDate = LocalDate.now(); } /** @@ -188,6 +189,16 @@ public LocalDateTime parseDateTime15(String dateTime) { } } + /** + * Retrieves the date when a task was marked as completed. + * + * @return The `LocalDate` representing the date when the task was completed, + * or null if the task is not completed yet. + */ + public LocalDate getCompletedDate() { + return completedDate; + } + @Override public String toString() { return getTask() + getStatusIcon() + " " + description; diff --git a/src/main/java/TaskList/TaskList.java b/src/main/java/TaskList/TaskList.java index 436a740945..c7b2dfb0fc 100644 --- a/src/main/java/TaskList/TaskList.java +++ b/src/main/java/TaskList/TaskList.java @@ -1,8 +1,11 @@ package tasklist; +import java.time.DayOfWeek; +import java.time.LocalDate; import java.util.ArrayList; import duke.DukeException; +import task.Statistics; import task.Task; /** @@ -108,4 +111,43 @@ public ArrayList findTasks(String keyword) { } return matchingTasks; } + + /** + * Calculates and returns task statistics based on the tasks in the associated `TaskList`. + * This method calculates the following statistics: + * - The number of tasks completed within the last week. + * - The total number of tasks completed. + * - The total number of tasks in the `TaskList`. + * - The percentage of tasks completed within the last week. + * - The percentage of total tasks completed. + * + * @return A `Statistics` object containing the calculated task statistics. + */ + public Statistics calculateStatistics() { + Statistics statistics = new Statistics(); + LocalDate now = LocalDate.now(); + + int tasksCompletedThisWeek = 0; + int totalTasksCompleted = 0; + int totalTasks = taskList.size(); + + for (Task task : taskList) { + if (task.checkIsDone()) { + totalTasksCompleted++; + if (task.getCompletedDate().isAfter(now.with(DayOfWeek.MONDAY))) { + tasksCompletedThisWeek++; + } + } + } + + double percentageCompletedThisWeek = (double) tasksCompletedThisWeek / totalTasks * 100; + double percentageTotalCompleted = (double) totalTasksCompleted / totalTasks * 100; + + statistics.setTasksCompletedThisWeek(tasksCompletedThisWeek); + statistics.setTotalTasksCompleted(totalTasksCompleted); + statistics.setPercentageCompletedThisWeek(percentageCompletedThisWeek); + statistics.setPercentageTotalCompleted(percentageTotalCompleted); + + return statistics; + } } diff --git a/src/main/java/Ui/Ui.java b/src/main/java/Ui/Ui.java index 4ba5d2bff3..988abe2383 100644 --- a/src/main/java/Ui/Ui.java +++ b/src/main/java/Ui/Ui.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import duke.DukeException; +import task.Statistics; import task.Task; import tasklist.TaskList; @@ -15,7 +16,7 @@ public class Ui { private static final String HELP_DESK = "List of Commands: Add, Deadline, Event, Todo, Echo," - + "Mark, Unmark, Delete, Find, Bye\n" + + "Mark, Unmark, Delete, Find, Stats, Bye\n" + "1. Add - Add a task to the list\n" + "2. Deadline - Add a task with a deadline\n" + "3. Event - Add an event task\n" @@ -25,7 +26,8 @@ public class Ui { + "7. Unmark - Unmark a task as done\n" + "8. Delete - Delete a task\n" + "9. Find - Find tasks by keyword\n" - + "10. Bye - Exit the program"; + + "10. Stats - Shows your work progress\n" + + "11. Bye - Exit the program"; /** * Displays a welcome message to the user when the application starts. @@ -156,4 +158,20 @@ public String showMatchingTasks(ArrayList matchingTasks) { } return output.toString(); } + + /** + * Generates a textual representation of task statistics and returns it as a formatted string. + * + * @param statistics The `Statistics` object containing task statistics to be displayed. + * @return A formatted string containing task statistics information. + */ + public String showStatistics(Statistics statistics) { + StringBuilder output = new StringBuilder(); + output.append("Here are the task statistics:\n"); + output.append("Tasks completed last week: " + statistics.getTasksCompletedThisWeek() + "\n"); + output.append(String.format("%.2f%% completed this week\n", statistics.getPercentageCompletedThisWeek())); + output.append("Total tasks completed: " + statistics.getTotalTasksCompleted() + "\n"); + output.append(String.format("%.2f%% total completed\n", statistics.getPercentageTotalCompleted())); + return output.toString(); + } }