forked from nus-cs2103-AY1920S2/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from AY1920S2-CS2103T-F11-2/graph-impl
[v1.4] Graph Implementation
- Loading branch information
Showing
28 changed files
with
1,308 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/seedu/address/logic/commands/GraphCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import static seedu.address.logic.parser.CliSyntax.PREFIX_AXIS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ENDDATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EXERCISE_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STARTDATE; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.logic.parser.Prefix; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.client.Client; | ||
import seedu.address.model.exercise.Exercise; | ||
import seedu.address.model.exercise.UniqueExerciseList; | ||
import seedu.address.model.graph.Graph; | ||
|
||
/** | ||
* Displays a graph of exercises done by a client in FitBiz. | ||
*/ | ||
public class GraphCommand extends Command { | ||
|
||
public static final List<Prefix> PREFIXES = List.of(PREFIX_EXERCISE_NAME, PREFIX_AXIS, | ||
PREFIX_STARTDATE, PREFIX_ENDDATE); | ||
|
||
public static final String COMMAND_WORD = "graph"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Displays the graph of a specified exercise " | ||
+ "of the client currently in view. " | ||
+ "Start date cannot be after end date. \n" | ||
+ "Parameters: " | ||
+ PREFIX_EXERCISE_NAME + "EXERCISE_NAME " | ||
+ PREFIX_AXIS + "Y_AXIS " | ||
+ PREFIX_STARTDATE + "START_DATE " | ||
+ PREFIX_ENDDATE + "END_DATE \n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_EXERCISE_NAME + "push up " | ||
+ PREFIX_AXIS + "reps " | ||
+ PREFIX_STARTDATE + "01-01-2020 " | ||
+ PREFIX_ENDDATE + "01-04-2020"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Showing exercise graph for %1$s's %2$s from %3$s to %4$s. \n" | ||
+ "Note that any new updates to the exercises (via add-e, edit-e, delete-e) will not be reflected " | ||
+ "on the current graph."; | ||
|
||
public static final String MESSAGE_CLIENT_NOT_IN_VIEW = "You currently do not have a client in view, " | ||
+ "use the view-c command to view a client first"; | ||
|
||
public static final String MESSAGE_EXERCISE_NOT_IN_LIST = "Graph cannot be plotted as no such exercise is" | ||
+ " recorded for this client from %1$s to %2$s."; | ||
|
||
public static final String MESSAGE_NO_GRAPH_FOR_AXIS = "There is no graph to plot for this axis specified. " | ||
+ "Please choose a different axis or a different exercise."; | ||
|
||
|
||
private final Graph graph; | ||
|
||
public GraphCommand(Graph graph) { | ||
requireNonNull(graph); | ||
this.graph = graph; | ||
} | ||
|
||
public static boolean isValidTimeFrame(LocalDate startDate, LocalDate endDate) { | ||
return (endDate.compareTo(startDate) >= 0); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
if (!model.hasClientInView()) { | ||
throw new CommandException(MESSAGE_CLIENT_NOT_IN_VIEW); | ||
} | ||
|
||
Client clientInView = model.getClientInView(); | ||
UniqueExerciseList clientInViewExerciseList = clientInView.getExerciseList(); | ||
|
||
if (!clientInViewExerciseList.containsNameWithinDate(graph.getExerciseName(), | ||
graph.getStartDate(), graph.getEndDate())) { | ||
throw new CommandException(String.format(MESSAGE_EXERCISE_NOT_IN_LIST, | ||
graph.getStartDate(), graph.getEndDate())); | ||
} | ||
|
||
List<Exercise> graphList = graph.generateGraphList(clientInView); | ||
|
||
if (graphList.size() == 0) { | ||
throw new CommandException(MESSAGE_NO_GRAPH_FOR_AXIS); | ||
} | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, clientInView.getName().toString(), | ||
graph.getExerciseName().toString(), graph.getStartDate().toString(), graph.getEndDate().toString()), | ||
graph.getAxis().getAxisType(), graphList); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof GraphCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
GraphCommand e = (GraphCommand) other; | ||
return graph.equals(e.graph); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return graph.getExerciseName().value | ||
+ " " + graph.getAxis().value | ||
+ " " + graph.getStartDate().toString() | ||
+ " " + graph.getEndDate().toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/seedu/address/logic/parser/GraphCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_AXIS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ENDDATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EXERCISE_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STARTDATE; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.GraphCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.exercise.ExerciseName; | ||
import seedu.address.model.graph.Axis; | ||
import seedu.address.model.graph.EndDate; | ||
import seedu.address.model.graph.Graph; | ||
import seedu.address.model.graph.StartDate; | ||
|
||
/** | ||
* Parses input arguments and creates a new GraphCommand object | ||
*/ | ||
public class GraphCommandParser implements Parser<GraphCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the | ||
* GraphCommand and returns an GraphCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public GraphCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_EXERCISE_NAME, | ||
PREFIX_AXIS, PREFIX_STARTDATE, PREFIX_ENDDATE); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_EXERCISE_NAME, PREFIX_AXIS, | ||
PREFIX_STARTDATE, PREFIX_ENDDATE)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, GraphCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
ExerciseName exerciseName = ParserUtil.parseExerciseName(argMultimap.getValue(PREFIX_EXERCISE_NAME).get()); | ||
Axis axis = ParserUtil.parseAxis(argMultimap.getValue(PREFIX_AXIS).get()); | ||
StartDate startDate = ParserUtil.parseStartDate(argMultimap.getValue(PREFIX_STARTDATE).get()); | ||
EndDate endDate = ParserUtil.parseEndDate(argMultimap.getValue(PREFIX_ENDDATE).get()); | ||
|
||
if (!GraphCommand.isValidTimeFrame(startDate.value, endDate.value)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, GraphCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Graph graph = new Graph(exerciseName, axis, startDate, endDate); | ||
return new GraphCommand(graph); | ||
} | ||
|
||
/* | ||
* Returns true if none of the prefixes contains empty {@code Optional} values | ||
* in the given {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.