Skip to content

Commit 63ab020

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#84 from patricktan6/branch-ViewBmi
Added view bmi
2 parents 4cc0972 + 39294f0 commit 63ab020

File tree

14 files changed

+157
-29
lines changed

14 files changed

+157
-29
lines changed

src/main/java/seedu/address/logic/commands/AddHeightCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ public class AddHeightCommand extends Command {
1212
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds user's height to fitNUS. "
1313
+ "Parameters: "
1414
+ PREFIX_HEIGHT + "HEIGHT "
15-
+ "...\n"
15+
+ "\n"
1616
+ "Example: " + COMMAND_WORD + " "
1717
+ PREFIX_HEIGHT + "170 ";
1818

19-
public static final String MESSAGE_SUCCESS = "Height added: %d cm";
19+
public static final String MESSAGE_SUCCESS = "Height added: %.2f cm";
2020
public static final String MESSAGE_INVALID_HEIGHT = "This is not a valid height";
2121

22-
private final int height;
22+
private final double height;
2323

2424
/**
25-
* Creates an ExerciseAddCommand to add the specified {@code Exercise}
25+
* Creates an AddHeightCommand to add the specified height in centimetres.
2626
*/
27-
public AddHeightCommand(int height) {
27+
public AddHeightCommand(double height) {
2828
requireNonNull(height);
2929
this.height = height;
3030
}

src/main/java/seedu/address/logic/commands/AddWeightCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ public class AddWeightCommand extends Command {
1212
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds user's weight to fitNUS. "
1313
+ "Parameters: "
1414
+ PREFIX_WEIGHT + "WEIGHT "
15-
+ "...\n"
15+
+ "\n"
1616
+ "Example: " + COMMAND_WORD + " "
1717
+ PREFIX_WEIGHT + "72 ";
1818

19-
public static final String MESSAGE_SUCCESS = "Weight added: %d kg";
19+
public static final String MESSAGE_SUCCESS = "Weight added: %.2f kg";
2020
public static final String MESSAGE_INVALID_WEIGHT = "This is not a valid weight";
2121

22-
private final int weight;
22+
private final double weight;
2323

2424
/**
25-
* Creates an ExerciseAddCommand to add the specified {@code Exercise}
25+
* Creates an AddWeightCommand to add the specified weight in kilograms.
2626
*/
27-
public AddWeightCommand(int weight) {
27+
public AddWeightCommand(double weight) {
2828
requireNonNull(weight);
2929
this.weight = weight;
3030
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import seedu.address.logic.commands.exceptions.CommandException;
6+
import seedu.address.model.Model;
7+
8+
public class BmiCommand extends Command {
9+
public static final String COMMAND_WORD = "bmi";
10+
11+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Views the user's BMI. "
12+
+ "\n"
13+
+ "Example: " + COMMAND_WORD;
14+
15+
public static final String MESSAGE_SUCCESS = "Your BMI is %.2f.";
16+
public static final String MESSAGE_INVALID_BMI = "Your BMI is not available. "
17+
+ "Please make sure that you have updated your height and weight.";
18+
19+
/**
20+
* Creates a BmiCommand to get the BMI Index.
21+
*/
22+
public BmiCommand() {
23+
}
24+
25+
@Override
26+
public CommandResult execute(Model model) throws CommandException {
27+
requireNonNull(model);
28+
29+
double bmi = model.getBmi();
30+
if (Double.isNaN(bmi)) {
31+
throw new CommandException(MESSAGE_INVALID_BMI);
32+
}
33+
return new CommandResult(String.format(MESSAGE_SUCCESS, bmi));
34+
}
35+
36+
@Override
37+
public boolean equals(Object other) {
38+
return other == this; // short circuit if same object
39+
}
40+
41+
}

src/main/java/seedu/address/logic/parser/AddHeightCommandParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public AddHeightCommand parse(String args) throws ParseException {
2525
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddHeightCommand.MESSAGE_USAGE));
2626
}
2727

28-
int height = ParserUtil.parseHeight(argMultimap.getValue(PREFIX_HEIGHT).get());
28+
double height = ParserUtil.parseHeight(argMultimap.getValue(PREFIX_HEIGHT).get());
2929

3030
return new AddHeightCommand(height);
3131
}

src/main/java/seedu/address/logic/parser/AddWeightCommandParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public AddWeightCommand parse(String args) throws ParseException {
2525
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddWeightCommand.MESSAGE_USAGE));
2626
}
2727

28-
int weight = ParserUtil.parseHeight(argMultimap.getValue(PREFIX_WEIGHT).get());
28+
double weight = ParserUtil.parseWeight(argMultimap.getValue(PREFIX_WEIGHT).get());
2929

3030
return new AddWeightCommand(weight);
3131
}

src/main/java/seedu/address/logic/parser/AddressBookParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import seedu.address.logic.commands.AddCommand;
1010
import seedu.address.logic.commands.AddHeightCommand;
1111
import seedu.address.logic.commands.AddWeightCommand;
12+
import seedu.address.logic.commands.BmiCommand;
1213
import seedu.address.logic.commands.ClearCommand;
1314
import seedu.address.logic.commands.Command;
1415
import seedu.address.logic.commands.DeleteCommand;
@@ -156,6 +157,9 @@ public Command parseCommand(String userInput) throws ParseException {
156157
case AddWeightCommand.COMMAND_WORD:
157158
return new AddWeightCommandParser().parse(arguments);
158159

160+
case BmiCommand.COMMAND_WORD:
161+
return new BmiCommand();
162+
159163
case RoutineDeleteExerciseCommand.COMMAND_WORD:
160164
return new RoutineDeleteExerciseCommandParser().parse(arguments);
161165

src/main/java/seedu/address/logic/parser/ParserUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,19 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
173173
/**
174174
* Parses {@code String height} into a {@code int}.
175175
*/
176-
public static int parseHeight(String height) {
176+
public static double parseHeight(String height) {
177177
requireNonNull(height);
178178
String trimmedHeight = height.trim();
179-
return Integer.parseInt(trimmedHeight);
179+
return Double.parseDouble(trimmedHeight);
180180
}
181181

182182
/**
183183
* Parses {@code String weight} into a {@code int}.
184184
*/
185-
public static int parseWeight(String weight) {
185+
public static double parseWeight(String weight) {
186186
requireNonNull(weight);
187187
String trimmedWeight = weight.trim();
188-
return Integer.parseInt(trimmedWeight);
188+
return Double.parseDouble(trimmedWeight);
189189
}
190190

191191
}

src/main/java/seedu/address/model/AddressBook.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class AddressBook implements ReadOnlyAddressBook {
2626
private final UniqueExerciseList exercises;
2727
private final UniqueRoutineList routines;
2828
private final UniqueLessonList lessons;
29-
private int height;
30-
private int weight;
29+
private double height = Double.NaN;
30+
private double weight = Double.NaN;
3131
private final Timetable timetable;
3232

3333
/*
@@ -58,16 +58,26 @@ public AddressBook(ReadOnlyAddressBook toBeCopied) {
5858

5959
//// user-level operations
6060

61-
public void addHeight(int height) {
61+
/**
62+
* Adds the height of the user.
63+
*
64+
* @param height the height of the user.
65+
*/
66+
public void addHeight(double height) {
6267
this.height = height;
6368
}
6469

65-
public void addWeight(int weight) {
70+
/**
71+
* Adds the weight of the user.
72+
*
73+
* @param weight the weight of the user.
74+
*/
75+
public void addWeight(double weight) {
6676
this.weight = weight;
6777
}
6878

6979
public double getBmi() {
70-
return weight / Math.pow((height / 100.0), 2);
80+
return this.weight / Math.pow((this.height / 100.0), 2);
7181
}
7282

7383
//// list overwrite operations
@@ -123,6 +133,8 @@ public void resetData(ReadOnlyAddressBook newData) {
123133
setLessons(newData.getLessonList());
124134
setRoutines(newData.getRoutineList());
125135
setSlots(newData.getSlotList());
136+
addHeight(newData.getHeight());
137+
addWeight(newData.getWeight());
126138
}
127139

128140
//// person-level operations
@@ -161,6 +173,7 @@ public boolean hasLesson(Lesson lesson) {
161173

162174
/**
163175
* Returns true if the slot is already occupied in the timetable.
176+
*
164177
* @param slot The slot to be checked.
165178
* @return true if the slot is already occupied in the timetable.
166179
*/
@@ -171,6 +184,7 @@ public boolean hasSlot(Slot slot) {
171184

172185
/**
173186
* Returns true if the slot has overlapping duration with another slot in the timetable.
187+
*
174188
* @param slot The slot to be checked.
175189
* @return true if the slot has overlapping duration with another slot in the timetable.
176190
*/
@@ -318,6 +332,16 @@ public ObservableList<Slot> getSlotList() {
318332
return timetable.getSlotList();
319333
}
320334

335+
@Override
336+
public double getHeight() {
337+
return height;
338+
}
339+
340+
@Override
341+
public double getWeight() {
342+
return weight;
343+
}
344+
321345
@Override
322346
public boolean equals(Object other) {
323347
return other == this // short circuit if same object
@@ -367,6 +391,7 @@ public void addExerciseToRoutine(Routine r, Exercise e) {
367391

368392
/**
369393
* Adds a Slot to the Timetable in fitNUS.
394+
*
370395
* @param slot The slot to be added.
371396
*/
372397
public void addSlotToTimetable(Slot slot) {
@@ -375,6 +400,7 @@ public void addSlotToTimetable(Slot slot) {
375400

376401
/**
377402
* Retrieves the Lesson object from UniqueLessonList that the user specified.
403+
*
378404
* @param lesson Lesson object that the user wants.
379405
* @return Lesson object that exists within fitNUS that the user is looking for.
380406
*/
@@ -384,6 +410,7 @@ public Lesson retrieveLesson(Lesson lesson) {
384410

385411
/**
386412
* Retrieves the Routine object from UniqueRoutineList that the user specified.
413+
*
387414
* @param routine Routine object that the user wants.
388415
* @return Routine object that exists within fitNUS that the user is looking for.
389416
*/

src/main/java/seedu/address/model/Model.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public interface Model {
2525
*/
2626
Predicate<Exercise> PREDICATE_SHOW_ALL_EXERCISES = unused -> true;
2727

28-
/** {@code Predicate} that always evaluate to true */
28+
/**
29+
* {@code Predicate} that always evaluate to true
30+
*/
2931
Predicate<Routine> PREDICATE_SHOW_ALL_ROUTINES = unused -> true;
3032

3133
Predicate<Lesson> PREDICATE_SHOW_ALL_LESSONS = unused -> true;
@@ -147,6 +149,7 @@ public interface Model {
147149

148150
/**
149151
* Updates the filter of the filtered lesson list to filter by the given {@code predicate}.
152+
*
150153
* @throws NullPointerException if {@code predicate} is null.
151154
*/
152155
void updateFilteredLessonList(Predicate<Lesson> predicate);
@@ -206,13 +209,18 @@ public interface Model {
206209
void deleteRoutine(Routine target);
207210

208211
boolean hasSlot(Slot slot);
212+
209213
boolean hasOverlappingSlot(Slot slot);
214+
210215
void addSlotToTimetable(Slot slot);
216+
211217
void deleteSlotFromTimetable(Slot target);
212218

213-
void addHeight(int height);
219+
void addHeight(double height);
220+
221+
void addWeight(double weight);
214222

215-
void addWeight(int weight);
223+
double getBmi();
216224

217225
/**
218226
* Returns true if a lesson with the same details as {@code lesson} exists in timetable.
@@ -239,13 +247,15 @@ public interface Model {
239247

240248
/**
241249
* Retrieves the Lesson object from UniqueLessonList that the user specified.
250+
*
242251
* @param lesson Lesson object that the user wants.
243252
* @return Lesson object that exists within fitNUS that the user is looking for.
244253
*/
245254
Lesson retrieveLesson(Lesson lesson);
246255

247256
/**
248257
* Retrieves the Routine object from UniqueRoutineList that the user specified.
258+
*
249259
* @param routine Routine object that the user wants.
250260
* @return Routine object that exists within fitNUS that the user is looking for.
251261
*/

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,20 @@ public String listRoutines() {
202202
}
203203

204204
@Override
205-
public void addHeight(int height) {
205+
public void addHeight(double height) {
206206
addressBook.addHeight(height);
207207
}
208208

209209
@Override
210-
public void addWeight(int weight) {
210+
public void addWeight(double weight) {
211211
addressBook.addWeight(weight);
212212
}
213213

214+
@Override
215+
public double getBmi() {
216+
return addressBook.getBmi();
217+
}
218+
214219
/**
215220
* Adds a Lesson into fitNUS.
216221
*
@@ -272,6 +277,7 @@ public void viewRoutine(Routine r) {
272277
}
273278

274279
//=========== Filtered Person List Accessors =============================================================
280+
275281
/**
276282
* Returns an unmodifiable view of the list of {@code Person} backed by the internal list of
277283
* {@code versionedAddressBook}
@@ -280,6 +286,7 @@ public void viewRoutine(Routine r) {
280286
public ObservableList<Routine> getFilteredRoutineList() {
281287
return filteredRoutine;
282288
}
289+
283290
/**
284291
* Returns an unmodifiable view of the list of {@code Person} backed by the internal list of
285292
* {@code versionedAddressBook}
@@ -330,6 +337,7 @@ public void updateFilteredRoutineList(Predicate<Routine> predicate) {
330337
requireNonNull(predicate);
331338
filteredRoutine.setPredicate(predicate);
332339
}
340+
333341
@Override
334342
public void updateFilteredPersonList(Predicate<Person> predicate) {
335343
requireNonNull(predicate);

0 commit comments

Comments
 (0)