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

changes in calculator #162

Merged
19 changes: 11 additions & 8 deletions src/main/java/seedu/dietbook/Manager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.dietbook;

import seedu.dietbook.calculator.Calculator;
import seedu.dietbook.calculator.CalculatorData;
import seedu.dietbook.command.AddCommand;
import seedu.dietbook.command.CalculateCommand;
import seedu.dietbook.command.ClearCommand;
Expand All @@ -14,16 +16,14 @@
import seedu.dietbook.command.NameCommand;
import seedu.dietbook.command.RecommendCommand;
import seedu.dietbook.command.UserinfoCommand;
import seedu.dietbook.list.FoodList;
import seedu.dietbook.person.ActivityLevel;
import seedu.dietbook.person.Person;
import seedu.dietbook.calculator.Calculator;
import seedu.dietbook.database.DataBase;
import seedu.dietbook.person.Gender;
import seedu.dietbook.exception.DietException;
import seedu.dietbook.list.FoodList;
import seedu.dietbook.parser.Parser;
import seedu.dietbook.person.ActivityLevel;
import seedu.dietbook.person.Gender;
import seedu.dietbook.person.Person;

import java.util.ArrayList;

/**
* Manager class of the program.
Expand All @@ -39,6 +39,7 @@ public class Manager {
private String name;
private int commandCount = 1;
private DataBase dataBase;
private CalculatorData data = new CalculatorData();
private Calculator calculator;

public static final String COMMAND_ADD = "add";
Expand All @@ -61,7 +62,8 @@ public Manager(FoodList foodlist, DataBase dataBase) {
1, ActivityLevel.LOW);
this.foodList = foodlist;
this.dataBase = dataBase;
this.calculator = new Calculator(foodList.getFoods());
this.data.inputData(this.foodList);
this.calculator = new Calculator(this.data);
}

public FoodList getFoodList() {
Expand All @@ -86,7 +88,8 @@ public Calculator getCalculator() {
}

public void setCalculator() {
this.calculator = new Calculator(foodList.getFoods());
this.data.inputData(foodList);
this.calculator.update(this.data);
}

public DataBase getDataBase() {
Expand Down
137 changes: 82 additions & 55 deletions src/main/java/seedu/dietbook/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.dietbook.calculator;

import seedu.dietbook.food.Food;
import seedu.dietbook.list.FoodList;
import seedu.dietbook.person.Gender;
import seedu.dietbook.person.Person;

Expand All @@ -13,27 +11,28 @@
*/
public class Calculator {
private int totalCalorie = 0;
private int totalCarbohydrate = 0;
private int totalCarb = 0;
private int totalProtein = 0;
private int totalFat = 0;
CalculatorData data = null;

public Calculator() {
}

/**
* Construct a calculator taking in a foodList. Add up calories,
* carbs, protein, and fats in each food item.
*
* @param foodList foodList containing food items to calculate.
* @param data a CalculatorData class instance containing data
* of food items to calculate.
*/
public Calculator(List<Food> foodList) {
assert foodList != null : "The foodList should not be null.";

for (int i = 0; i < foodList.size(); i++) {
assert foodList.get(i).getName().trim().length() != 0 : "Food names should not be empty.";
public Calculator(CalculatorData data) {
assert data != null : "The foodList should not be null.";
this.data = data;
}

totalCalorie += foodList.get(i).getCalorie();
totalCarbohydrate += foodList.get(i).getCarbohydrate();
totalProtein += foodList.get(i).getProtein();
totalFat += foodList.get(i).getFat();
}
public void update(CalculatorData data) {
this.data = data;
}

/**
Expand All @@ -42,6 +41,11 @@ public Calculator(List<Food> foodList) {
* @return the value of total calorie of food items in foodList.
*/
public int calculateCalorie() {
totalCalorie = 0;
List<Integer> calories = data.getTotalCalorie();
for (int calorie : calories) {
totalCalorie += calorie;
}
return totalCalorie;
}

Expand All @@ -54,12 +58,13 @@ public int calculateCalorie() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateCalorie(LocalDateTime startTime) {
totalCalorie = 0;
List<Integer> calories = data.getTotalCalorie(startTime);
for (int calorie : calories) {
totalCalorie += calorie;
}
return calorie;
return totalCalorie;
}

/**
Expand All @@ -72,12 +77,13 @@ public int calculateCalorie(FoodList foodList, LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateCalorie(LocalDateTime startTime, LocalDateTime endTime) {
totalCalorie = 0;
List<Integer> calories = data.getTotalCalorie(startTime, endTime);
for (int calorie : calories) {
totalCalorie += calorie;
}
return calorie;
return totalCalorie;
}

/**
Expand All @@ -86,7 +92,12 @@ public int calculateCalorie(FoodList foodList, LocalDateTime startTime, LocalDat
* @return the value of total carbs of food items in foodList.
*/
public int calculateCarb() {
return totalCarbohydrate;
totalCarb = 0;
List<Integer> carbs = data.getTotalCarb();
for (int carb : carbs) {
totalCarb += carb;
}
return totalCarb;
}

/**
Expand All @@ -98,12 +109,13 @@ public int calculateCarb() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateCarb(LocalDateTime startTime) {
totalCarb = 0;
List<Integer> carbs = data.getTotalCarb(startTime);
for (int carb : carbs) {
totalCarb += carb;
}
return carb;
return totalCarb;
}

/**
Expand All @@ -116,12 +128,13 @@ public int calculateCarb(FoodList foodList, LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateCarb(LocalDateTime startTime, LocalDateTime endTime) {
totalCarb = 0;
List<Integer> carbs = data.getTotalCarb(startTime, endTime);
for (int carb : carbs) {
totalCarb += carb;
}
return carb;
return totalCarb;
}

/**
Expand All @@ -130,6 +143,11 @@ public int calculateCarb(FoodList foodList, LocalDateTime startTime, LocalDateTi
* @return the value of total protein of food items in foodList.
*/
public int calculateProtein() {
totalProtein = 0;
List<Integer> proteins = data.getTotalProtein();
for (int protein : proteins) {
totalProtein += protein;
}
return totalProtein;
}

Expand All @@ -142,12 +160,13 @@ public int calculateProtein() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateProtein(LocalDateTime startTime) {
totalProtein = 0;
List<Integer> proteins = data.getTotalProtein(startTime);
for (int protein : proteins) {
totalProtein += protein;
}
return protein;
return totalProtein;
}

/**
Expand All @@ -160,12 +179,13 @@ public int calculateProtein(FoodList foodList, LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateProtein(LocalDateTime startTime, LocalDateTime endTime) {
totalProtein = 0;
List<Integer> proteins = data.getTotalProtein(startTime, endTime);
for (int protein : proteins) {
totalProtein += protein;
}
return protein;
return totalProtein;
}

/**
Expand All @@ -174,6 +194,11 @@ public int calculateProtein(FoodList foodList, LocalDateTime startTime, LocalDat
* @return the value of total fats of food items in foodList.
*/
public int calculateFat() {
totalFat = 0;
List<Integer> fats = data.getTotalFat();
for (int fat : fats) {
totalFat += fat;
}
return totalFat;
}

Expand All @@ -186,12 +211,13 @@ public int calculateFat() {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateFat(LocalDateTime startTime) {
totalFat = 0;
List<Integer> fats = data.getTotalFat(startTime);
for (int fat : fats) {
totalFat += fat;
}
return fat;
return totalFat;
}

/**
Expand All @@ -204,12 +230,13 @@ public int calculateFat(FoodList foodList, LocalDateTime startTime) {
* @return the value of total calorie of food items with time after
* startTime in foodList.
*/
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();
public int calculateFat(LocalDateTime startTime, LocalDateTime endTime) {
totalFat = 0;
List<Integer> fats = data.getTotalFat(startTime, endTime);
for (int fat : fats) {
totalFat += fat;
}
return fat;
return totalFat;
}

/**
Expand Down
Loading