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

Implements stats feature #114

Merged
merged 28 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8e8b85c
Add StatsCommandParser
jietung Oct 21, 2019
294c0dc
Add StatsCommand
jietung Oct 21, 2019
970a84b
Add methods in ParseUtil
jietung Oct 21, 2019
eda80c9
Add prefix in CliSyntax
jietung Oct 21, 2019
8b28d1d
Add StatsFactory
jietung Oct 21, 2019
b96a2a6
Add Statistic
jietung Oct 22, 2019
aa4114a
Add default statistic
jietung Oct 22, 2019
8a8fb14
Add BarChartPanel
jietung Oct 22, 2019
24d8594
Add LineChartPanel
jietung Oct 22, 2019
7dbc682
Add PieChartPanel
jietung Oct 22, 2019
1972b2b
Refactor code
jietung Oct 22, 2019
ff3e256
Add javadoc and refactor code
jietung Oct 22, 2019
3564cb3
Add update statistic when model is edited
jietung Oct 23, 2019
e0bbb5c
Merge branch 'master' into stats
jietung Oct 23, 2019
48787f4
Merge branch 'master' into stats
jietung Oct 23, 2019
d21a9f5
Refactor code for statistic in model
jietung Oct 24, 2019
7f08e47
Add statistic methods in modelstub in test
jietung Oct 24, 2019
d7fe210
Edit values return for barchart
jietung Oct 24, 2019
5c84bc7
Add javadoc comments
jietung Oct 24, 2019
e09f656
Add newline at EOF in fxml
jietung Oct 24, 2019
9437726
Merge branch 'master' into stats
jietung Oct 24, 2019
5d3ba34
Add restrictions to number of days between start date and end date fo…
jietung Oct 24, 2019
f3acd64
Refactor code
jietung Oct 25, 2019
a7109bc
Update user guide
jietung Oct 25, 2019
b27e29b
Merge branch 'master' into stats
jietung Oct 25, 2019
7f8bd44
Add newline at EOF in data/exercisedatabases.json
jietung Oct 25, 2019
6b52ef9
Change strings in ParserUtil and store as final String in Date and St…
jietung Oct 26, 2019
84b5db1
Merge branch 'master' into stats
jietung Oct 26, 2019
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 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.
*/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be slightly more descriptive about this comment. Maybe "Returns the Statistic object currently in focus" or something that describe its purpose.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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));
}

}
101 changes: 101 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,101 @@
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 {
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