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

[Issue: 101] Clean up and toString versions of DateTime support methods #103

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions src/main/java/seedu/dietbook/calculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public int calculateCalorie() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateCalorie(LocalDateTime startTime) {
public int calculateCalorie(FoodList foodList, LocalDateTime startTime) {
int calorie = 0;
for (int i = 0; i < FoodList.getFoodsAfterDateTime(startTime).size(); i++) {
calorie += FoodList.getFoodsAfterDateTime(startTime).get(i).getCalorie();
for (int i = 0; i < foodList.getFoodsAfterDateTime(startTime).size(); i++) {
calorie += foodList.getFoodsAfterDateTime(startTime).get(i).getCalorie();
}
return calorie;
}
Expand All @@ -72,10 +72,10 @@ public int calculateCalorie(LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateCalorie(LocalDateTime startTime, LocalDateTime endTime) {
public int calculateCalorie(FoodList foodList, LocalDateTime startTime, LocalDateTime endTime) {
int calorie = 0;
for (int i = 0; i < FoodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
calorie += FoodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getCalorie();
for (int i = 0; i < foodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
calorie += foodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getCalorie();
}
return calorie;
}
Expand All @@ -98,10 +98,10 @@ public int calculateCarb() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateCarb(LocalDateTime startTime) {
public int calculateCarb(FoodList foodList, LocalDateTime startTime) {
int carb = 0;
for (int i = 0; i < FoodList.getFoodsAfterDateTime(startTime).size(); i++) {
carb += FoodList.getFoodsAfterDateTime(startTime).get(i).getCarbohydrate();
for (int i = 0; i < foodList.getFoodsAfterDateTime(startTime).size(); i++) {
carb += foodList.getFoodsAfterDateTime(startTime).get(i).getCarbohydrate();
}
return carb;
}
Expand All @@ -116,10 +116,10 @@ public int calculateCarb(LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateCarb(LocalDateTime startTime, LocalDateTime endTime) {
public int calculateCarb(FoodList foodList, LocalDateTime startTime, LocalDateTime endTime) {
int carb = 0;
for (int i = 0; i < FoodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
carb += FoodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getCarbohydrate();
for (int i = 0; i < foodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
carb += foodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getCarbohydrate();
}
return carb;
}
Expand All @@ -142,10 +142,10 @@ public int calculateProtein() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateProtein(LocalDateTime startTime) {
public int calculateProtein(FoodList foodList, LocalDateTime startTime) {
int protein = 0;
for (int i = 0; i < FoodList.getFoodsAfterDateTime(startTime).size(); i++) {
protein += FoodList.getFoodsAfterDateTime(startTime).get(i).getProtein();
for (int i = 0; i < foodList.getFoodsAfterDateTime(startTime).size(); i++) {
protein += foodList.getFoodsAfterDateTime(startTime).get(i).getProtein();
}
return protein;
}
Expand All @@ -160,10 +160,10 @@ public int calculateProtein(LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateProtein(LocalDateTime startTime, LocalDateTime endTime) {
public int calculateProtein(FoodList foodList, LocalDateTime startTime, LocalDateTime endTime) {
int protein = 0;
for (int i = 0; i < FoodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
protein += FoodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getProtein();
for (int i = 0; i < foodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
protein += foodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getProtein();
}
return protein;
}
Expand All @@ -186,10 +186,10 @@ public int calculateFat() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateFat(LocalDateTime startTime) {
public int calculateFat(FoodList foodList, LocalDateTime startTime) {
int fat = 0;
for (int i = 0; i < FoodList.getFoodsAfterDateTime(startTime).size(); i++) {
fat += FoodList.getFoodsAfterDateTime(startTime).get(i).getFat();
for (int i = 0; i < foodList.getFoodsAfterDateTime(startTime).size(); i++) {
fat += foodList.getFoodsAfterDateTime(startTime).get(i).getFat();
}
return fat;
}
Expand All @@ -204,10 +204,10 @@ public int calculateFat(LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
public int calculateFat(LocalDateTime startTime, LocalDateTime endTime) {
public int calculateFat(FoodList foodList, LocalDateTime startTime, LocalDateTime endTime) {
int fat = 0;
for (int i = 0; i < FoodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
fat += FoodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getFat();
for (int i = 0; i < foodList.getFoodsInDateTimeRange(startTime, endTime).size(); i++) {
fat += foodList.getFoodsInDateTimeRange(startTime, endTime).get(i).getFat();
}
return fat;
}
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/seedu/dietbook/list/FoodList.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* This is a stateful object.
*/
public class FoodList {
private static ArrayList<FoodEntry> foodEntries;
private ArrayList<FoodEntry> foodEntries;

/**
* Default constructor that instantiates FoodList with an empty foodentry arraylist.
Expand Down Expand Up @@ -121,7 +121,7 @@ public List<Food> getPortionedFoods() {
/**
* Obtain list of foods consumed after specified timing.
*/
public static List<Food> getFoodsAfterDateTime(LocalDateTime dateTime) {
public List<Food> getFoodsAfterDateTime(LocalDateTime dateTime) {
List<FoodEntry> entriesAfterDateTime = FoodListManager.filterListByDate(foodEntries, dateTime);
return FoodListManager.listToFoods(entriesAfterDateTime);
}
Expand All @@ -137,7 +137,7 @@ public List<Food> getPortionedFoodsAfterDateTime(LocalDateTime dateTime) {
/**
* Obtain list of foods consumed within the range of a specified timing.
*/
public static List<Food> getFoodsInDateTimeRange(LocalDateTime start, LocalDateTime end) {
public List<Food> getFoodsInDateTimeRange(LocalDateTime start, LocalDateTime end) {
List<FoodEntry> entriesInRange = FoodListManager.filterListByDate(foodEntries, start, end);
return FoodListManager.listToFoods(entriesInRange);
}
Expand All @@ -158,6 +158,7 @@ public List<Integer> getPortionSizes() {
return FoodListManager.listToPortionSizes(foodEntries);
}


/**
* Obtain list of LocalDateTimes for when the entries were made.
* (For storage purposes)
Expand All @@ -171,4 +172,25 @@ public String toString() {
return FoodListManager.listToString(foodEntries);
}

/**
* Returns toString representation of the segmented list based on DateTime.
* @param dateTime Start DateTime.
* @return string representation of FoodList
*/
public String getAfterDateTimeToString(LocalDateTime dateTime) {
List<FoodEntry> entriesAfterDateTime = FoodListManager.filterListByDate(foodEntries, dateTime);
return FoodListManager.listToString(entriesAfterDateTime);
}

/**
* Returns toString representation of the segmented list based on DateTime (within a range of 2 datetimes).
* @param start start DateTime.
* @param end end DateTime.
* @return string representation of FoodList
*/
public String getInDateTimeRangeToString(LocalDateTime start, LocalDateTime end) {
List<FoodEntry> entriesInRange = FoodListManager.filterListByDate(foodEntries, start, end);
return FoodListManager.listToString(entriesInRange);
}

}
44 changes: 0 additions & 44 deletions src/test/java/seedu/dietbook/food/Food.java

This file was deleted.

15 changes: 11 additions & 4 deletions src/test/java/seedu/dietbook/list/FoodListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ void dateComparisonTest() {

@Test
void dateFilterAfterTest() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps consider using the three-part naming style for the tests.

assertEquals(list.toString(), list.getAfterDateTimeToString(LocalDateTime.MIN));

LocalDateTime timeNow = LocalDateTime.now();

assertTrue(list.getFoodsAfterDateTime(timeNow).size() == 0);
assertEquals(list.getFoodsAfterDateTime(LocalDateTime.MIN).toString(),
list.getFoods().toString());
assertEquals(list.getFoods().toString(),
list.getFoodsAfterDateTime(LocalDateTime.MIN).toString());


// add new entries:
if (! LocalDateTime.now().isAfter(timeNow)) { // Execution is too fast that now() = timeNow.
Expand All @@ -77,11 +80,17 @@ void dateFilterAfterTest() {
}
list.addFood(1, food);
assertEquals(food.toString(), list.getFoodsAfterDateTime(timeNow).get(0).toString());


}

@Test
void dateFilterRangeTest() {

assertEquals(list.getPortionedFoods().toString(),
list.getPortionedFoodsInDateTimeRange(LocalDateTime.MIN, LocalDateTime.MAX).toString());
assertEquals(list.toString(), list.getInDateTimeRangeToString(LocalDateTime.MIN, LocalDateTime.MAX));

LocalDateTime timeNow = LocalDateTime.now();

if (! LocalDateTime.now().isAfter(timeNow)) { // Execution is too fast that now() = timeNow.
Expand All @@ -95,8 +104,6 @@ void dateFilterRangeTest() {

assertTrue(list.getFoodsInDateTimeRange(timeNow, LocalDateTime.MAX).size() == 0);

assertEquals(list.getPortionedFoods().toString(),
list.getPortionedFoodsInDateTimeRange(LocalDateTime.MIN, LocalDateTime.MAX).toString());
}

@Test
Expand Down