diff --git a/src/main/java/seedu/dietbook/calculator/Calculator.java b/src/main/java/seedu/dietbook/calculator/Calculator.java index e8304fe278..266ed0781a 100644 --- a/src/main/java/seedu/dietbook/calculator/Calculator.java +++ b/src/main/java/seedu/dietbook/calculator/Calculator.java @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/main/java/seedu/dietbook/list/FoodList.java b/src/main/java/seedu/dietbook/list/FoodList.java index 59a5dbabfb..3807882b12 100644 --- a/src/main/java/seedu/dietbook/list/FoodList.java +++ b/src/main/java/seedu/dietbook/list/FoodList.java @@ -13,7 +13,7 @@ * This is a stateful object. */ public class FoodList { - private static ArrayList foodEntries; + private ArrayList foodEntries; /** * Default constructor that instantiates FoodList with an empty foodentry arraylist. @@ -121,7 +121,7 @@ public List getPortionedFoods() { /** * Obtain list of foods consumed after specified timing. */ - public static List getFoodsAfterDateTime(LocalDateTime dateTime) { + public List getFoodsAfterDateTime(LocalDateTime dateTime) { List entriesAfterDateTime = FoodListManager.filterListByDate(foodEntries, dateTime); return FoodListManager.listToFoods(entriesAfterDateTime); } @@ -137,7 +137,7 @@ public List getPortionedFoodsAfterDateTime(LocalDateTime dateTime) { /** * Obtain list of foods consumed within the range of a specified timing. */ - public static List getFoodsInDateTimeRange(LocalDateTime start, LocalDateTime end) { + public List getFoodsInDateTimeRange(LocalDateTime start, LocalDateTime end) { List entriesInRange = FoodListManager.filterListByDate(foodEntries, start, end); return FoodListManager.listToFoods(entriesInRange); } @@ -158,6 +158,7 @@ public List getPortionSizes() { return FoodListManager.listToPortionSizes(foodEntries); } + /** * Obtain list of LocalDateTimes for when the entries were made. * (For storage purposes) @@ -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 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 entriesInRange = FoodListManager.filterListByDate(foodEntries, start, end); + return FoodListManager.listToString(entriesInRange); + } + } diff --git a/src/test/java/seedu/dietbook/food/Food.java b/src/test/java/seedu/dietbook/food/Food.java deleted file mode 100644 index 505588e82b..0000000000 --- a/src/test/java/seedu/dietbook/food/Food.java +++ /dev/null @@ -1,44 +0,0 @@ -package seedu.dietbook.food; - - -public class Food { - private final String name; - private final int calorie; - private final int carbohydrate; - private final int protein; - private final int fats; - - public Food(String name, int calorie, int carbohydrate, int protein, int fats) { - this.name = name; - this.calorie = calorie; - this.carbohydrate = carbohydrate; - this.protein = protein; - this.fats = fats; - } - - public int getFat() { - return fats; - } - - public String getName() { - return name; - } - - public int getCalorie() { - return calorie; - } - - public int getCarbohydrate() { - return carbohydrate; - } - - public int getProtein() { - return protein; - } - - @Override - public String toString() { - return name + " | calorie : " + calorie + " | protein : " + protein + " | carbohydrate : " + carbohydrate - + " | fats : " + fats; - } -} diff --git a/src/test/java/seedu/dietbook/list/FoodListTest.java b/src/test/java/seedu/dietbook/list/FoodListTest.java index 3b7325f856..a4072423f9 100644 --- a/src/test/java/seedu/dietbook/list/FoodListTest.java +++ b/src/test/java/seedu/dietbook/list/FoodListTest.java @@ -61,11 +61,14 @@ void dateComparisonTest() { @Test void dateFilterAfterTest() { + 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. @@ -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. @@ -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