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

Fix PED bugs #60

Merged
merged 10 commits into from
Nov 3, 2023
14 changes: 0 additions & 14 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ Shows a message explaning how to access the help page.

Format: `help`

### Adding a todo: `todo`
Adds a new item to the list of todo items.

Format: `todo n/TODO_NAME d/DEADLINE`

* The `DEADLINE` can be in a natural language format.
* The `TODO_NAME` cannot contain punctuation.

Example of usage:

`todo n/Write the rest of the User Guide d/next week`

`todo n/Refactor the User Guide to remove passive voice d/13/04/2020`

### Adding a meal: `meal_add`
Adds a new item to the list of todo items.

Expand Down
40 changes: 28 additions & 12 deletions src/main/java/seedu/duke/commands/logcommands/DeleteLogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,39 @@ public DeleteLogCommand(List<String> exerciseDetails) {
* Extracts the details of the delete command and deletes the specified exercise.
*
* @return CommandResult that tells the user whether an exercise was successfully removed.
* @throws IncorrectFormatException when the command is not entered with the right type of parameters.
*/
public CommandResult execute() throws IncorrectFormatException {
if (exerciseDetails.size() < 4) {
throw new IncorrectFormatException("The delete log command needs to take at least 4 parameters!");
}
int month = Integer.parseInt(exerciseDetails.get(0));
int day = Integer.parseInt(exerciseDetails.get(1));
String exerciseName = "";
for (int i = 2; i < exerciseDetails.size() - 1; i++) {
exerciseName += exerciseDetails.get(i) + " ";
}
int caloriesBurned = Integer.parseInt(exerciseDetails.get(exerciseDetails.size() - 1));

feedbackToUser = Duke.exerciseLog.removeExercise(month, day, exerciseName.trim(), caloriesBurned) ?
"Successfully removed exercise!" :
"Could not find the specified exercise!";

return new CommandResult(feedbackToUser);
try {
int month = Integer.parseInt(exerciseDetails.get(0));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
}
int day = Integer.parseInt(exerciseDetails.get(1));
if (day <= 0 || day > Duke.exerciseLog.getNumberOfDays(month)) {
throw new IncorrectFormatException("The day you specified does not exist for the month.");
}
String exerciseName = "";
for (int i = 2; i < exerciseDetails.size() - 1; i++) {
exerciseName += exerciseDetails.get(i) + " ";
}
int caloriesBurned = Integer.parseInt(exerciseDetails.get(exerciseDetails.size() - 1));
if (caloriesBurned < 0) {
throw new IncorrectFormatException("You cannot burn a negative number of calories.");
}

feedbackToUser = Duke.exerciseLog.removeExercise(month, day, exerciseName.trim(), caloriesBurned) ?
"Successfully removed exercise!" :
"Could not find the specified exercise!";

return new CommandResult(feedbackToUser);
} catch (NumberFormatException e) {
throw new IncorrectFormatException("Please specify reasonable positive numbers in the month, day, and " +
"calories burned fields");
}
}
}
36 changes: 26 additions & 10 deletions src/main/java/seedu/duke/commands/logcommands/LogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,37 @@ public LogCommand(List<String> exerciseDetails) {
* Extracts the details of the log command and logs the exercise with its specific details.
*
* @return CommandResult telling the user that the exercise was successfully added along with the exercise details.
* @throws IncorrectFormatException when the command is not entered with the right type of parameters.
*/
public CommandResult execute() throws Exception {
if (exerciseDetails.size() < 4) {
throw new IncorrectFormatException("The log command needs to take at least 4 parameters!");
}
int month = Integer.parseInt(exerciseDetails.get(0));
int day = Integer.parseInt(exerciseDetails.get(1));
String exerciseName = "";
for (int i = 2; i < exerciseDetails.size() - 1; i++) {
exerciseName += exerciseDetails.get(i) + " ";
}
int caloriesBurned = Integer.parseInt(exerciseDetails.get(exerciseDetails.size() - 1));

String exerciseDescription = Duke.exerciseLog.addExercise(month, day, exerciseName.trim(), caloriesBurned);

return new CommandResult((feedbackToUser + exerciseDescription).trim());
try {
int month = Integer.parseInt(exerciseDetails.get(0));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
}
int day = Integer.parseInt(exerciseDetails.get(1));
if (day <= 0 || day > Duke.exerciseLog.getNumberOfDays(month)) {
throw new IncorrectFormatException("The day you specified does not exist for the month.");
}
String exerciseName = "";
for (int i = 2; i < exerciseDetails.size() - 1; i++) {
exerciseName += exerciseDetails.get(i) + " ";
}
int caloriesBurned = Integer.parseInt(exerciseDetails.get(exerciseDetails.size() - 1));
if (caloriesBurned < 0) {
throw new IncorrectFormatException("You cannot burn a negative number of calories.");
}

String exerciseDescription = Duke.exerciseLog.addExercise(month, day, exerciseName.trim(), caloriesBurned);

return new CommandResult((feedbackToUser + exerciseDescription).trim());
} catch (NumberFormatException e) {
throw new IncorrectFormatException("Please specify reasonable positive numbers in the month, day, and " +
"calories burned fields");
}
}
}
65 changes: 42 additions & 23 deletions src/main/java/seedu/duke/commands/logcommands/UpdateLogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,55 @@ public UpdateLogCommand(List<String> exerciseDetails) {
* Extracts the details of the update command and updates the specified exercise.
*
* @return CommandResult that tells the user whether an exercise was successfully updated.
* @throws IncorrectFormatException when the command is not entered with the right type of parameters.
*/
public CommandResult execute() throws IncorrectFormatException {
if (exerciseDetails.size() < 4) {
throw new IncorrectFormatException("The update log command needs to take at least 4 parameters!");
}
int month = Integer.parseInt(exerciseDetails.get(0));
int day = Integer.parseInt(exerciseDetails.get(1));
String oldExerciseName = "";
for (int i = 2; i < exerciseDetails.size() - 1; i++) {
oldExerciseName += exerciseDetails.get(i) + " ";
}
int oldCaloriesBurned = Integer.parseInt(exerciseDetails.get(exerciseDetails.size() - 1));

Scanner scanner = new Scanner(System.in);
System.out.println("Please specify the new exercise details:");
String newExerciseString = scanner.nextLine();
String[] newExerciseDetails = newExerciseString.split(" ");
if (newExerciseDetails.length < 2) {
throw new IncorrectFormatException("The new exercise needs to have a name and calories burned!");
}
String newExerciseName = "";
for (int i = 0; i < newExerciseDetails.length - 1; i++) {
newExerciseName += newExerciseDetails[i] + " ";
}
int newCaloriesBurned = Integer.parseInt(newExerciseDetails[newExerciseDetails.length - 1]);

feedbackToUser = Duke.exerciseLog.updateExercise(month, day, oldExerciseName.trim(), oldCaloriesBurned,
newExerciseName.trim(), newCaloriesBurned) ? "Exercise successfully updated!" :
try {
int month = Integer.parseInt(exerciseDetails.get(0));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
}
int day = Integer.parseInt(exerciseDetails.get(1));
if (day <= 0 || day > Duke.exerciseLog.getNumberOfDays(month)) {
throw new IncorrectFormatException("The day you specified does not exist for the month.");
}
String oldExerciseName = "";
for (int i = 2; i < exerciseDetails.size() - 1; i++) {
oldExerciseName += exerciseDetails.get(i) + " ";
}
int oldCaloriesBurned = Integer.parseInt(exerciseDetails.get(exerciseDetails.size() - 1));
if (oldCaloriesBurned < 0) {
throw new IncorrectFormatException("You cannot burn a negative number of calories.");
}

Scanner scanner = new Scanner(System.in);
System.out.println("Please specify the new exercise details:");
String newExerciseString = scanner.nextLine();
String[] newExerciseDetails = newExerciseString.split(" ");
if (newExerciseDetails.length < 2) {
throw new IncorrectFormatException("The new exercise needs to have a name and calories burned!");
}
String newExerciseName = "";
for (int i = 0; i < newExerciseDetails.length - 1; i++) {
newExerciseName += newExerciseDetails[i] + " ";
}
int newCaloriesBurned = Integer.parseInt(newExerciseDetails[newExerciseDetails.length - 1]);
if (newCaloriesBurned < 0) {
throw new IncorrectFormatException("You cannot burn a negative number of calories.");
}

feedbackToUser = Duke.exerciseLog.updateExercise(month, day, oldExerciseName.trim(), oldCaloriesBurned,
newExerciseName.trim(), newCaloriesBurned) ? "Exercise successfully updated!" :
"Could not find exercise to update.";

return new CommandResult(feedbackToUser);
return new CommandResult(feedbackToUser);
} catch (NumberFormatException e) {
throw new IncorrectFormatException("Please specify reasonable positive numbers in the month, day, and " +
"calories burned fields");
}
}
}
39 changes: 25 additions & 14 deletions src/main/java/seedu/duke/commands/logcommands/ViewLogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ViewLogCommand() {
* Assigns the view attribute a specific enum based on the scope at which the user wants to view the exercise log.
*
* @param viewArgs the details of the scope at which the user wants to view the exercise log.
* @throws IncorrectFormatException when the command is not entered with the right type of parameters.
*/
public ViewLogCommand(List<String> viewArgs) throws IncorrectFormatException {
super();
Expand All @@ -53,23 +54,33 @@ public ViewLogCommand(List<String> viewArgs) throws IncorrectFormatException {
if (viewArgs.size() % 2 == 1) {
switch (viewArgs.size()) {
case 3:
viewScope = ViewScope.MONTH;
month = Integer.parseInt(viewArgs.get(2));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
try {
viewScope = ViewScope.MONTH;
month = Integer.parseInt(viewArgs.get(2));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
}
break;
} catch (NumberFormatException e) {
throw new IncorrectFormatException("Please specify reasonable positive numbers in the " +
"month, day, and calories burned fields");
}
break;
case 5:
viewScope = ViewScope.DAY;
month = Integer.parseInt(viewArgs.get(2));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
try {
viewScope = ViewScope.DAY;
month = Integer.parseInt(viewArgs.get(2));
if (month <= 0 || month > 12) {
throw new IncorrectFormatException("The month you specified does not exist.");
}
day = Integer.parseInt(viewArgs.get(4));
if (day <= 0 || day > Duke.exerciseLog.getNumberOfDays(month)) {
throw new IncorrectFormatException("The day you specified does not exist for the month.");
}
break;
} catch (NumberFormatException e) {
throw new IncorrectFormatException("Please specify reasonable positive numbers in the " +
"month, day, and calories burned fields");
}
day = Integer.parseInt(viewArgs.get(4));
if (day <= 0 || day > Duke.exerciseLog.getNumberOfDays(month)) {
throw new IncorrectFormatException("The day you specified does not exist for the month.");
}
break;
default:
throw new IncorrectFormatException("Incorrect view command format.");
}
Expand Down
Loading