Skip to content

Commit

Permalink
Change exerciselog command names (#92)
Browse files Browse the repository at this point in the history
* Change exerciselog-related command words

* Create Storage as abstract class

* Update documentation with newest exerciselog commands
  • Loading branch information
Remy9926 authored Nov 14, 2023
1 parent a95ed63 commit b7515da
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 50 deletions.
34 changes: 28 additions & 6 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ Example of usage:

`log 12 24 Volleyball 5`

### `update` - Updating an exercise
### `updatelog` - Updating an exercise
Updates the specified exercise within the Exercise Log if the old exercise can be found. If the old exercise is not
found, then the user will not be prompted to provide new details.

Format: `update MONTH DAY OLD_EXERCISE_NAME OLD_CALORIES_BURNED` `NEW_EXERCISE_NAME NEW_CALORIES_BURNED`
Format: `updatelog MONTH DAY OLD_EXERCISE_NAME OLD_CALORIES_BURNED` `NEW_EXERCISE_NAME NEW_CALORIES_BURNED`

* The `MONTH` is an integer specifying the month in which the exercise was performed (1-12 inclusive)
* The `DAY` is an integer specifying the day of the month in which the exercise was performed
Expand All @@ -125,23 +125,45 @@ Format: `update MONTH DAY OLD_EXERCISE_NAME OLD_CALORIES_BURNED` `NEW_EXERCISE_N

Example of usage:

`update 1 26 Basketball 179`
`updatelog 1 26 Basketball 179`
`Rugby 55`

`update 12 24 Volleyball 5`
`updatelog 12 24 Volleyball 5`
`Hockey 98`

### `view` - Viewing Exercises
### `viewlog` - Viewing Exercises
View the number of, or each exercise in a day, month, or the entire log.

Format `view VIEW_TYPE VIEW_SCOPE`
Format `viewlog VIEW_TYPE VIEW_SCOPE`

* The `VIEW_TYPE` is either `total` or `exercises` depending on whether you want to view the total number of exercises
or each exercise and its details
* The `VIEW_SCOPE` can take on either `all` to see all exercises in the log, `month MONTH` where `MONTH` is the month
whose exercises you want to view, or `month MONTH day DAY` where MONTH is the same as above, but `DAY` is the specific
day of the month whose exercises you want to view.

Example of usage:

`viewlog exercises all`

`viewlog total month 1 day 24`

### `deletelog` - Deleting Exercises
Delete the exericse on the specified month and day

Format `deletelog MONTH DAY EXERCISE_NAME CALORIES_BURNED`

* The `MONTH` is an integer specifying the month in which the exercise was performed (1-12 inclusive)
* The `DAY` is an integer specifying the day of the month in which the exercise was performed
* The `OLD_EXERCISE_NAME` is a string specifying the name of the exercise to be deleted
* The `OLD_CALORIES_BURNED` is an integer specifying the number of calories burned by the exercise to be deleted

Example of usage:

`deletelog 6 16 Running 179`

`deletelog 2 27 Hockey 5`

### `set on` - Setting up a calorie goal
Set up a calorie goal to achieve. The goal are expected to help user accomplish a particular amount of calorie consumption before a **deadline**.

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import seedu.duke.storagefile.DataManager;
import seedu.duke.storagefile.GoalStorage;
import seedu.duke.ui.TextUi;
import seedu.duke.storagefile.StorageFile;
import seedu.duke.storagefile.ExerciseLogStorage;

/**
* Entry point of the Address Book application.
Expand All @@ -29,7 +29,7 @@ public class Duke {
public static GoalList goalList = new GoalList();
public static GoalList achievedGoals = new GoalList();
public static Log exerciseLog = new Log();
public static StorageFile exerciseLogStorage;
public static ExerciseLogStorage exerciseLogStorage;
public static GoalStorage goalStorage;
public static AchmStorage achmStorage;
private static ArrayList<Meal> meals = new ArrayList<Meal>();
Expand Down Expand Up @@ -62,7 +62,7 @@ public void run(String[] launchArgs) {
*/
private void start(String[] launchArgs) {
try {
ui = new TextUi();
this.ui = new TextUi();
exerciseLogStorage = StorageFile.initializeStorage(dirPath, exerciseLogFilePath);
exerciseLogStorage.checkForLogTextFile(exerciseLog);
goalStorage = GoalStorage.initializeGoalStorage(dirPath, goalFilePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
import seedu.duke.data.exception.IncorrectFormatException;

public class DeleteLogCommand extends Command {
public static final String COMMAND_WORD = "delete";
public static final String COMMAND_WORD = "deletelog";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Delete an exercise from the exercise log.\n"
+ "\tExample: " + COMMAND_WORD + " 1 2 Running 68";
public String feedbackToUser;
private List<String> exerciseDetails;

public DeleteLogCommand() {
super();
}

public DeleteLogCommand(List<String> exerciseDetails) {
super();
this.exerciseDetails = exerciseDetails;
Expand Down Expand Up @@ -59,7 +55,7 @@ public CommandResult execute() throws IncorrectFormatException, IOException {
feedbackToUser = Duke.exerciseLog.removeExercise(month, day, exerciseName.trim(), caloriesBurned) ?
"Successfully removed exercise!" :
"Could not find the specified exercise!";
Duke.exerciseLogStorage.removeExerciseFromFile(month, day, exerciseName.trim().split(" "), caloriesBurned);
Duke.exerciseLogStorage.deleteFromStorage(month, day, exerciseName.trim().split(" "), caloriesBurned);

return new CommandResult(feedbackToUser);
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CommandResult execute() throws IncorrectFormatException, IOException {
}

String exerciseDescription = Duke.exerciseLog.addExercise(month, day, exerciseName.trim(), caloriesBurned);
Duke.exerciseLogStorage.writeExerciseToFile(month, day, exerciseName.trim().split(" "), caloriesBurned);
Duke.exerciseLogStorage.writeToStorage(month, day, exerciseName.trim().split(" "), caloriesBurned);

return new CommandResult((feedbackToUser + exerciseDescription).trim());
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import seedu.duke.data.exception.IncorrectFormatException;

public class UpdateLogCommand extends Command {
public static final String COMMAND_WORD = "update";
public static final String COMMAND_WORD = "updatelog";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Updates an exercise from the exercise log.\n"
+ "\tExample: " + COMMAND_WORD + " 1 2 Football 89" + "\n" + "\tPlease specify the new exercise details:\n"
+ "\tSquash 44";
Expand Down Expand Up @@ -85,7 +85,7 @@ public CommandResult execute() throws IncorrectFormatException, IOException {
feedbackToUser = Duke.exerciseLog.updateExercise(month, day, oldExerciseName.trim(), oldCaloriesBurned,
newExerciseName.trim(), newCaloriesBurned) ? "Exercise successfully updated!" :
"Could not find exercise to update.";
Duke.exerciseLogStorage.updateExerciseInFile(month, day,
Duke.exerciseLogStorage.updateInStorage(month, day,
oldExerciseName.trim().split(" "), oldCaloriesBurned,
newExerciseName.trim().split(" "), newCaloriesBurned);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum ViewScope {
}

public class ViewLogCommand extends Command {
public static final String COMMAND_WORD = "view";
public static final String COMMAND_WORD = "viewlog";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Displays exercises from the exercise log.\n"
+ "\tExample: " + COMMAND_WORD + " exercises all" + " | " + COMMAND_WORD + "total all";
private ViewType viewType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package seedu.duke.storagefile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import seedu.duke.exerciselog.Log;

public class StorageFile {
public class ExerciseLogStorage extends Storage {
protected File dir;
protected File textFile;
protected FileWriter writeFile;

public StorageFile(String dirName, String textFileName) {
public ExerciseLogStorage(String dirName, String textFileName) {
dir = new File(dirName);
textFile = new File(textFileName);
}

public static StorageFile initializeStorage(String dirName, String textFilePath) {
return new StorageFile(dirName, textFilePath);
public static ExerciseLogStorage initializeStorage(String dirName, String textFilePath) {
return new ExerciseLogStorage(dirName, textFilePath);
}

public void checkForLogTextFile(Log exerciseLog) throws IOException {
if (dir.exists() && textFile.exists()) {
try {
Scanner s = new Scanner(textFile);
while (s.hasNextLine()) {
textToExercise(s.nextLine().split(","), exerciseLog);
}
s.close();
} catch (FileNotFoundException e) {
System.out.println(e);
Scanner s = new Scanner(textFile);
while (s.hasNextLine()) {
textToExercise(s.nextLine().split(","), exerciseLog);
}
s.close();
}
if (!dir.exists()) {
dir.mkdir();
Expand All @@ -51,29 +46,28 @@ public static void textToExercise(String[] exerciseDetails, Log exerciseLog) {
exerciseLog.addExercise(month, day, exerciseName, caloriesBurned);
}

public void writeExerciseToFile(int month, int day, String[] exerciseName, int caloriesBurned)
public void writeToStorage(int month, int day, String[] exerciseName, int caloriesBurned)
throws IOException {
String writeToFile = "";
writeToFile += Integer.toString(month) + ",";
writeToFile += Integer.toString(day) + ",";
writeToFile += month + ",";
writeToFile += day + ",";
writeToFile += String.join("_", exerciseName);
writeToFile += "," + Integer.toString(caloriesBurned);
writeToFile += "," + caloriesBurned;
writeFile.write(writeToFile + "\n");
writeFile.flush();
}

public void removeExerciseFromFile(int month, int day, String[] exerciseName, int caloriesBurned)
public void deleteFromStorage(int month, int day, String[] exerciseName, int caloriesBurned)
throws IOException {
//duplicated code
Scanner readFile = new Scanner(textFile);
File tempFile = new File("./data/temp.txt");
FileWriter tempWriter = new FileWriter(tempFile.toPath().toString());

String removeLine = "";
removeLine += Integer.toString(month) + ",";
removeLine += Integer.toString(day) + ",";
removeLine += month + ",";
removeLine += day + ",";
removeLine += String.join("_", exerciseName);
removeLine += "," + Integer.toString(caloriesBurned);
removeLine += "," + caloriesBurned;

while (readFile.hasNextLine()) {
String line = readFile.nextLine();
Expand All @@ -89,23 +83,23 @@ public void removeExerciseFromFile(int month, int day, String[] exerciseName, in
writeFile = new FileWriter(textFile.toPath().toString(), true);
}

public void updateExerciseInFile(int month, int day, String[] oldExerciseName, int oldCaloriesBurned,
String[] newExerciseName, int newCaloriesBurned) throws IOException {
public void updateInStorage(int month, int day, String[] oldExerciseName, int oldCaloriesBurned,
String[] newExerciseName, int newCaloriesBurned) throws IOException {
Scanner readFile = new Scanner(textFile);
File tempFile = new File("./data/temp.txt");
FileWriter tempWriter = new FileWriter(tempFile.toPath().toString());

String oldLine = "";
oldLine += Integer.toString(month) + ",";
oldLine += Integer.toString(day) + ",";
oldLine += month + ",";
oldLine += day + ",";
oldLine += String.join("_", oldExerciseName);
oldLine += "," + Integer.toString(oldCaloriesBurned);
oldLine += "," + oldCaloriesBurned;

String newLine = "";
newLine += Integer.toString(month) + ",";
newLine += Integer.toString(day) + ",";
newLine += month + ",";
newLine += day + ",";
newLine += String.join("_", newExerciseName);
newLine += "," + Integer.toString(newCaloriesBurned);
newLine += "," + newCaloriesBurned;

while (readFile.hasNextLine()) {
String line = readFile.nextLine();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/storagefile/GoalStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class GoalStorage extends StorageFile {
public class GoalStorage extends ExerciseLogStorage {

public GoalStorage(String dirName, String textFileName) {
super(dirName, textFileName);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/duke/storagefile/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package seedu.duke.storagefile;

import java.io.IOException;

abstract class Storage {
public abstract void writeToStorage(int firstInt, int secondInt, String[] stringArray, int thirdInt)
throws IOException;

public abstract void deleteFromStorage(int firstInt, int secondInt, String[] stringArray, int thirdInt)
throws IOException;

public abstract void updateInStorage(int firstInt, int secondInt, String[] firstStringArray, int thirdInt,
String[] secondStringArray, int fourthInt) throws IOException;
}

0 comments on commit b7515da

Please sign in to comment.