Skip to content

Commit

Permalink
Implements stats feature (#114)
Browse files Browse the repository at this point in the history
* Add StatsCommandParser

* Add StatsCommand

* Add methods in ParseUtil

* Add prefix in CliSyntax

* Add StatsFactory

* Add Statistic

* Add default statistic

* Add BarChartPanel

* Add LineChartPanel

* Add PieChartPanel

* Refactor code

* Add javadoc and refactor code

* Add update statistic when model is edited

* Refactor code for statistic in model

* Add statistic methods in modelstub in test

* Edit values return for barchart

* Add javadoc comments

* Add newline at EOF in fxml

* Add restrictions to number of days between start date and end date for stats command

* Refactor code

* Update user guide

* Add newline at EOF in data/exercisedatabases.json

* Change strings in ParserUtil and store as final String in Date and Statistic

Closes #71, closes #72 and closes #73
  • Loading branch information
jietung authored and t-cheepeng committed Oct 26, 2019
1 parent ff18877 commit 780bbaa
Show file tree
Hide file tree
Showing 29 changed files with 1,148 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ Format: `stats t/CAT_NAME h/CHART_TYPE [s/START_DATE] [d/END_DATE]`

****
* Supported chart types: Pie Chart, Line Chart, Bar Chart
* Supported category: exercise, calories
* If no `START_DATE` and `END_DATE` are provided, the recent 7 days of history will be used.
* If any date is provided, both `START_DATE` and `END_DATE` dates must be there.
* The maximum range between `START_DATE` and `END_DATE` is 31 days.
****

Example:
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/exercise/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import seedu.exercise.commons.core.State;
import seedu.exercise.logic.commands.CommandResult;
import seedu.exercise.logic.commands.exceptions.CommandException;
import seedu.exercise.logic.commands.statistic.Statistic;
import seedu.exercise.logic.parser.exceptions.ParseException;
import seedu.exercise.model.ReadOnlyResourceBook;
import seedu.exercise.model.conflict.Conflict;
Expand Down Expand Up @@ -79,6 +80,11 @@ public interface Logic {
*/
void setGuiSettings(GuiSettings guiSettings);

/**
* Returns the Statistic object currently in focus.
*/
Statistic getStatistic();

/**
* Returns the conflict that needs to be resolved in {@code Model}.
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/exercise/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.exercise.logic.commands.CommandResult;
import seedu.exercise.logic.commands.ResolveCommand;
import seedu.exercise.logic.commands.exceptions.CommandException;
import seedu.exercise.logic.commands.statistic.Statistic;
import seedu.exercise.logic.parser.ExerciseBookParser;
import seedu.exercise.logic.parser.exceptions.ParseException;
import seedu.exercise.model.Model;
Expand Down Expand Up @@ -114,9 +115,14 @@ public void setGuiSettings(GuiSettings guiSettings) {
}

@Override
public Statistic getStatistic() {
return model.getStatistic();
}

public Conflict getConflict() {
requireMainAppState(State.IN_CONFLICT);
return model.getConflict();

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public CommandResult execute(Model model) throws CommandException {
}

model.addExercise(exerciseToAdd);
model.updateStatistic();
eventPayload.put(KEY_EXERCISE_TO_ADD, exerciseToAdd);
EventHistory.getInstance().addCommandToUndoStack(this);
return new CommandResult(String.format(MESSAGE_SUCCESS, exerciseToAdd));
Expand All @@ -75,6 +76,7 @@ public EventPayload<Exercise> getPayload() {
@Override
public String getResourceType() {
return RESOURCE_TYPE;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public CommandResult execute(Model model) throws CommandException {
Exercise exerciseToDelete = lastShownList.get(targetIndex.getZeroBased());
eventPayload.put(KEY_EXERCISE_TO_DELETE, exerciseToDelete);
model.deleteExercise(exerciseToDelete);
model.updateStatistic();
EventHistory.getInstance().addCommandToUndoStack(this);
return new CommandResult(String.format(MESSAGE_DELETE_EXERCISE_SUCCESS, exerciseToDelete));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public CommandResult execute(Model model) throws CommandException {
model.setExercise(exerciseToEdit, editedExercise);
EventHistory.getInstance().addCommandToUndoStack(this);
model.updateFilteredExerciseList(Model.PREDICATE_SHOW_ALL_EXERCISES);
model.updateStatistic();
return new CommandResult(String.format(MESSAGE_EDIT_EXERCISE_SUCCESS, editedExercise));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public CommandResult execute(Model model) throws CommandException {
}

Event eventToRedo = eventHistory.redo(model);
model.updateStatistic();
return new CommandResult(
String.format(MESSAGE_SUCCESS, eventToRedo));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CommandResult execute(Model model) throws CommandException {
}

Event eventToUndo = eventHistory.undo(model);
model.updateStatistic();
return new CommandResult(String.format(MESSAGE_SUCCESS, eventToUndo));
}

}
106 changes: 106 additions & 0 deletions src/main/java/seedu/exercise/logic/commands/statistic/Statistic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package seedu.exercise.logic.commands.statistic;

import static java.util.Objects.requireNonNull;
import static seedu.exercise.commons.util.CollectionUtil.requireAllNonNull;

import java.util.ArrayList;

import seedu.exercise.model.property.Date;

/**
* Represents a Statistic with data needed to generate chart.
*/
public class Statistic {

public static final String MESSAGE_INVALID_CATEGORY = "Category can only be \'exercise\' or \'calories\'";
public static final String MESSAGE_INVALID_CHART_TYPE = "Chart type can only be \'piechart\' or "
+ "\'linechart\' or \'barchart\'";

private String category;
private String chart;
private Date startDate;
private Date endDate;
private ArrayList<String> properties;
private ArrayList<Double> values;

/**
* Every field must be present and not null.
*/
public Statistic(String category, String chart, Date startDate, Date endDate,
ArrayList<String> properties, ArrayList<Double> values) {
requireAllNonNull(category, chart, startDate, endDate, properties, values);
this.category = category;
this.chart = chart;
this.startDate = startDate;
this.endDate = endDate;
this.properties = properties;
this.values = values;
}

/**
* Resets the existing data of this {@code Statistic} with {@code newStatistic}.
*/
public void resetData(Statistic newStatistic) {
requireNonNull(newStatistic);
setCategory(newStatistic.getCategory());
setChart(newStatistic.getChart());
setStartDate(newStatistic.getStartDate());
setEndDate(newStatistic.getEndDate());
setProperties(newStatistic.getProperties());
setValues(newStatistic.getValues());
}

private void setCategory(String category) {
requireNonNull(category);
this.category = category;
}

private void setChart(String chart) {
requireNonNull(chart);
this.chart = chart;
}

private void setStartDate(Date startDate) {
requireNonNull(startDate);
this.startDate = startDate;
}

private void setEndDate(Date endDate) {
requireNonNull(endDate);
this.endDate = endDate;
}

private void setProperties(ArrayList<String> properties) {
requireNonNull(properties);
this.properties = properties;
}

private void setValues(ArrayList<Double> values) {
requireNonNull(values);
this.values = values;
}

public String getCategory() {
return category;
}

public String getChart() {
return chart;
}

public Date getStartDate() {
return startDate;
}

public Date getEndDate() {
return endDate;
}

public ArrayList<String> getProperties() {
return properties;
}

public ArrayList<Double> getValues() {
return values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package seedu.exercise.logic.commands.statistic;

import static java.util.Objects.requireNonNull;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_CATEGORY;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_CHART;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_END_DATE;
import static seedu.exercise.logic.parser.CliSyntax.PREFIX_START_DATE;

import seedu.exercise.logic.commands.Command;
import seedu.exercise.logic.commands.CommandResult;
import seedu.exercise.model.Model;
import seedu.exercise.model.ReadOnlyResourceBook;
import seedu.exercise.model.property.Date;
import seedu.exercise.model.resource.Exercise;

/**
* Generate statistic with given parameters.
*/
public class StatsCommand extends Command {

public static final String COMMAND_WORD = "stats";

public static final String MESSAGE_STATS_DISPLAY_SUCCESS = "Chart displayed.";

public static final String MESSAGE_USAGE = "Parameters: "
+ PREFIX_CATEGORY + "CATEGORY "
+ PREFIX_CHART + "CHART TYPE "
+ PREFIX_START_DATE + "START DATE "
+ PREFIX_END_DATE + "END DATE " + "\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_CATEGORY + "calories "
+ PREFIX_CHART + "barchart "
+ PREFIX_START_DATE + "30/03/2019 "
+ PREFIX_END_DATE + "05/04/2019 ";

private final String chart;
private final String category;
private final Date startDate;
private final Date endDate;

/**
* Creates a StatsCommand to generate statistic.
*/
public StatsCommand(String chart, String category, Date startDate, Date endDate) {
this.chart = chart;
this.category = category;
this.startDate = startDate;
this.endDate = endDate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
ReadOnlyResourceBook<Exercise> exercises = model.getExerciseBookData();
StatsFactory statsFactory = new StatsFactory(exercises, chart, category, startDate, endDate);
Statistic statistic = statsFactory.generateStatistic();
model.setStatistic(statistic);
return new CommandResult(MESSAGE_STATS_DISPLAY_SUCCESS);
}

@Override
public boolean equals(Object other) {
return other == this;
}
}
Loading

0 comments on commit 780bbaa

Please sign in to comment.