Skip to content

Commit

Permalink
Merge pull request #4 from raydenlim/branch-BCD-Extension
Browse files Browse the repository at this point in the history
BCD-Extension
  • Loading branch information
raydenlim authored Sep 15, 2023
2 parents f208b99 + 1c3e9bd commit 57cdbe8
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 5 deletions.
8 changes: 6 additions & 2 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions src/main/java/Command/StatisticsCommand.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 3 additions & 0 deletions src/main/java/Parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/Task/Statistics.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 12 additions & 1 deletion src/main/java/Task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -52,6 +52,7 @@ public String getDescription() {
*/
public void isCompleted() {
isDone = true;
completedDate = LocalDate.now();
}

/**
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/TaskList/TaskList.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -108,4 +111,43 @@ public ArrayList<Task> 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;
}
}
22 changes: 20 additions & 2 deletions src/main/java/Ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.util.ArrayList;

import duke.DukeException;
import task.Statistics;
import task.Task;
import tasklist.TaskList;

Expand All @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -156,4 +158,20 @@ public String showMatchingTasks(ArrayList<Task> 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();
}
}

0 comments on commit 57cdbe8

Please sign in to comment.