Skip to content

Commit

Permalink
Merge pull request #103 from AY2021S1-CS2113-T14-4/clean-up-branch
Browse files Browse the repository at this point in the history
[Issue: 101] Clean up and toString versions of DateTime support methods
  • Loading branch information
HengFuYuen authored Oct 28, 2020
2 parents d0ce853 + 61c0f21 commit 097fb48
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 75 deletions.
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() {
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

0 comments on commit 097fb48

Please sign in to comment.