Skip to content

Commit

Permalink
Merge pull request #258 from ICubE-/fix-bug-and-maintain-style
Browse files Browse the repository at this point in the history
Simplify FitTrack and fix Step
  • Loading branch information
J0shuaLeong authored Nov 9, 2023
2 parents f43fa63 + c54541a commit 43b20af
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 205 deletions.
142 changes: 85 additions & 57 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@ Type `help` to see a list of commands that you will be able to use in the applic

## Features

* [Viewing help guide : `help`](#viewing-help-guide-help)
* [Editing your profile : `editprofile`](#editing-your-profile-editprofile)
* [Viewing your profile : `viewprofile`](#viewing-your-profile-viewprofile)
* [Checking your current bmi : `bmi`](#checking-your-current-bmi-bmi)
* [Viewing help guide: `help`](#viewing-help-guide-help)
* [Editing your profile: `editprofile`](#editing-your-profile-editprofile)
* [Viewing your profile: `viewprofile`](#viewing-your-profile-viewprofile)
* [Checking your current bmi: `bmi`](#checking-your-current-bmi-bmi)
* [Checking your recommended weight: `checkrecommendedweight`](#checking-your-recommended-weight-checkrecommendedweight)
* [Adding a Meal : `addmeal`](#adding-a-meal-addmeal)
* [Deleting a Meal : `deletemeal`](#deleting-a-meal-deletemeal)
* [Viewing list of all meals : `viewmeal`](#viewing-list-of-all-meals-viewmeal)
* [Finding meals by a keyword : `findmeal`](#finding-meals-by-a-keyword-findmeal)
* [Adding a meal: `addmeal`](#adding-a-meal-addmeal)
* [Deleting a meal: `deletemeal`](#deleting-a-meal-deletemeal)
* [Viewing list of all meals: `viewmeal`](#viewing-list-of-all-meals-viewmeal)
* [Finding meals by a keyword: `findmeal`](#finding-meals-by-a-keyword-findmeal)
* [Checking total calories consumed on a specific date: `caloriesconsumed`](#checking-total-calories-consumed-on-a-specific-date-caloriesconsumed)
* [Adding a workout : `addworkout`](#adding-a-workout-addworkout)
* [Deleting a Workout : `deleteworkout`](#deleting-a-workout-deleteworkout)
* [Viewing list of workout : `viewworkout`](#viewing-list-of-all-workouts-viewworkout)
* [Find workouts by a keyword: `findworkout`](#finding-workouts-by-a-keyword-findworkout)
* [Adding a workout: `addworkout`](#adding-a-workout-addworkout)
* [Deleting a workout: `deleteworkout`](#deleting-a-workout-deleteworkout)
* [Viewing list of workout: `viewworkout`](#viewing-list-of-all-workouts-viewworkout)
* [Finding workouts by a keyword: `findworkout`](#finding-workouts-by-a-keyword-findworkout)
* [Checking total calories burnt on a specific date: `caloriesburnt`](#checking-total-calories-burnt-on-a-specific-date-caloriesburnt)
* [Adding steps: `addsteps`](#Adding-steps-addsteps)
* [Deleting a step entry: `deletesteps`](#Deleting-a-step-entry-deletesteps)
* [Viewing the total number of steps on a specific date: `totalsteps`](#Viewing-the-total-number-of-steps-on-a-specific-date-totalsteps)
* [Viewing list of all steps: `viewsteps`](#Viewing-list-of-all-steps-viewsteps)
* [Exiting the application : `exit`](#exiting-the-application-exit)

### First Time Users
Expand Down Expand Up @@ -340,11 +344,33 @@ These workouts contain the keyword run:
2. [W] slow run (20kcal, 2023-10-29)
```


### Checking total calories burnt on a specific date: `caloriesburnt`
Allows user to check total calories burnt by workouts on a specific date.

**Format**
- `caloriesburnt <DATE>`
- You should type `<DATE>` in format of `yyyy-MM-dd`.

**Example of usage**
```
caloriesburnt 2023-11-04
```

**Expected output**
```
[W] running (100kcal, 2023-11-04)
[W] swimming (200kcal, 2023-11-04)
[W] walking (30kcal, 2023-11-04)
Total calories burnt on 2023-11-04: 230kcal
```


### Adding steps: `addsteps`
Allows user to add their steps walked for a particular day.

**Format**
- `addsteps <STEPS> d/<CALORIES>`
- `addsteps <NUMBER_OF_STEPS> d/<CALORIES>`
- You should type `<DATE>` in format of `yyyy-MM-dd`.

**Example of usage**
Expand All @@ -357,20 +383,8 @@ addsteps 2000 d/2023-10-23
I've added the following steps:
[S] 2000 steps (2023-10-23)
```
### Viewing list of all steps: `viewsteps`
Lists all the steps entries made.

**Example of usage:**
```
viewsteps
```

**Expected output:**
```
These are the steps you have done:
1.[S] 100 steps (2023-10-02)
2.[S] 100 steps (2023-10-02)
```
### Deleting a step entry: `deletesteps`
Allows user to delete a step entry they have added.

Expand All @@ -389,26 +403,40 @@ I've deleted the following step entry:
```


### Checking total calories burnt on a specific date: `caloriesburnt`
Allows user to check total calories burnt by workouts on a specific date.
### Viewing the total number of steps on a specific date: `totalsteps`
Calculates the total number of steps on a specific date and shows to user.

**Format**
- `caloriesburnt <DATE>`
- `totalsteps <DATE>`
- You should type `<DATE>` in format of `yyyy-MM-dd`.

**Example of usage**
**Example of usage:**
```
caloriesburnt 2023-11-04
totalsteps 2023-10-02
```

**Expected output**
**Expected output:**
```
[W] running (100kcal, 2023-11-04)
[W] swimming (200kcal, 2023-11-04)
[W] walking (30kcal, 2023-11-04)
Total calories burnt on 2023-11-04: 230kcal
Total steps taken: 200 steps
```


### Viewing list of all steps: `viewsteps`
Lists all the steps entries made.

**Example of usage:**
```
viewsteps
```

**Expected output:**
```
These are the steps you have done:
1.[S] 100 steps (2023-10-02)
2.[S] 100 steps (2023-10-02)
```


### Exiting the application: `exit`
Exits the application.

Expand Down Expand Up @@ -457,26 +485,26 @@ The contents of workoutList.txt:

## Command Summary

| Features | Commands |
|:----------------------------------------------------|:-------------------------|
| Viewing help guide | `help` |
| Exiting the application | `exit` |
| Editing your profile | `editprofile` |
| Viewing your profile | `viewprofile` |
| Checking your current bmi | `bmi` |
| Checking your recommended weight | `checkrecommendedweight` |
| Adding a Meal | `addmeal` |
| Deleting a Meal | `deletemeal` |
| Viewing list of all meals | `viewmeal` |
| Finding meals by a keyword | `findmeal` |
| Checking total calories consumed on a specific date | `caloriesconsumed` |
| Adding a workout | `addworkout` |
| Deleting a Workout | `deleteworkout` |
| Viewing list of workout | `viewworkout` |
| Find workouts by a keyword | `findworkout` |
| Checking total calories burnt on a specific date | `caloriesburnt` |
| Adding a step entry | `addsteps` |
| Deleting a step entry | `deletesteps` |
| Viewing the list of steps | `viewsteps` |
| Calculating the total steps | `totalsteps` |
| Features | Commands |
|:-----------------------------------------------------|:-------------------------|
| Viewing help guide | `help` |
| Exiting the application | `exit` |
| Editing your profile | `editprofile` |
| Viewing your profile | `viewprofile` |
| Checking your current bmi | `bmi` |
| Checking your recommended weight | `checkrecommendedweight` |
| Adding a Meal | `addmeal` |
| Deleting a Meal | `deletemeal` |
| Viewing list of all meals | `viewmeal` |
| Finding meals by a keyword | `findmeal` |
| Checking total calories consumed on a specific date | `caloriesconsumed` |
| Adding a workout | `addworkout` |
| Deleting a Workout | `deleteworkout` |
| Viewing list of workout | `viewworkout` |
| Find workouts by a keyword | `findworkout` |
| Checking total calories burnt on a specific date | `caloriesburnt` |
| Adding a step entry | `addsteps` |
| Deleting a step entry | `deletesteps` |
| Viewing the total number of steps on a specific date | `totalsteps` |
| Viewing the list of steps | `viewsteps` |

111 changes: 78 additions & 33 deletions src/main/java/fittrack/FitTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fittrack.command.CommandResult;
import fittrack.command.ExitCommand;
import fittrack.parser.CommandParser;
import fittrack.parser.ParseException;
import fittrack.storage.Storage;
import fittrack.storage.Storage.StorageOperationException;
import fittrack.storage.Storage.InvalidStorageFilePathException;
Expand All @@ -22,93 +23,137 @@ public class FitTrack {
public static final String VERSION = "FitTrack - Version 2.1";

private final Ui ui;
private Storage storage;
private final Storage storage;
private UserProfile userProfile;
private MealList mealList;
private WorkoutList workoutList;
private StepList stepList;

private FitTrack() {
ui = new Ui();
userProfile = new UserProfile();
mealList = new MealList();
workoutList = new WorkoutList();
private FitTrack(String[] storagePaths) {
this.ui = new Ui();
this.storage = initializeStorage(storagePaths);
}

/**
* Main entry-point for the FitTrack application.
*/
public static void main(String[] args) {
new FitTrack().run(args);
new FitTrack(args).run();
}

private void run(String[] args) {
start(args);
private void run() {
start();
loopCommandExecution();
end();
}

private void start(String[] args) {
private void start() {
ui.printVersion();
ui.printWelcome();
boolean isProfileLoaded = loadStorage(args);
userProfile.startProfile(userProfile, ui, storage, isProfileLoaded);
load();
}

private void loopCommandExecution() {
Command command;
do {
String userCommandLine = ui.scanCommandLine();
command = CommandParser.parseCommand(userCommandLine);

CommandResult commandResult = executeCommand(command);

ui.printCommandResult(commandResult);
ui.printLine();
} while (!ExitCommand.isExit(command));
}

private CommandResult executeCommand(Command command) {
try {
command.setData(userProfile, mealList, workoutList, stepList, storage);
CommandResult result = command.execute();
storage.save(userProfile, mealList, workoutList, stepList);
return result;
} catch (IOException e) {
System.out.println(e.getMessage());
throw new RuntimeException();
}
command.setData(userProfile, mealList, workoutList, stepList);
CommandResult result = command.execute();
save();
return result;
}

private boolean loadStorage(String[] args) {
boolean isFirstTime = false;
private void end() {
save();
}

// @@author J0shuaLeong
private void load() {
// TODO: This method will be eventually changed due to Joshua's Storage rework.
try {
this.storage = initializeStorage(args);
if (!storage.isProfileFileEmpty()) {
this.userProfile = storage.profileLoad();
ui.printPrompt();
isFirstTime = true;
}
this.mealList = storage.mealLoad();
this.workoutList = storage.workoutLoad();
this.stepList = storage.stepLoad();
return isFirstTime;
} catch (StorageOperationException | InvalidStorageFilePathException e) {
} catch (StorageOperationException e) {
// TODO: Use Ui.
System.out.println("There was a problem with the loading of storage contents.");
ui.printLine();
}
return isFirstTime;

if (userProfile == null) {
initUserProfile();
}
if (mealList == null) {
mealList = new MealList();
}
if (workoutList == null) {
workoutList = new WorkoutList();
}
if (stepList == null) {
stepList = new StepList();
}
save();
}
// @@author

// @@author J0shuaLeong
public void initUserProfile() {
boolean isInputValid = false;
while (!isInputValid) {
try {
String input = ui.scanUserProfile();
userProfile = UserProfile.parseUserProfile(input);
ui.printProfileDetails(userProfile);
isInputValid = true;
} catch (ParseException e) {
ui.printException(e);
}
}
}
// @@author

/**
* Creates the StorageFile object based on the user specified path (if any) or the default storage path.
*
* @param args arguments supplied by the user at program launch
* @throws InvalidStorageFilePathException if the target file path is incorrect.
*/
private Storage initializeStorage(String[] args) throws InvalidStorageFilePathException {
boolean isStorageFileSpecifiedByUser = args.length > 0;
return isStorageFileSpecifiedByUser ? new Storage(args[0], args[1], args[2], args[3]) : new Storage();
// @@author J0shuaLeong
private Storage initializeStorage(String[] args) {
if (args.length == 4) {
try {
return new Storage(args[0], args[1], args[2], args[3]);
} catch (InvalidStorageFilePathException e) {
ui.printStoragePathSettingFailure();
return new Storage();
}
} else if (args.length == 0) {
return new Storage();
} else {
ui.printStoragePathSettingFailure();
return new Storage();
}
}
// @@author J0shuaLeong

private void end() {
private void save() {
try {
storage.save(userProfile, mealList, workoutList, stepList);
} catch (IOException e) {
ui.printSaveFailure();
}
}
}
Loading

0 comments on commit 43b20af

Please sign in to comment.