File size: 2,608 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
package model;
import java.util.ArrayList;
import java.util.List;
public class Recipe extends Food {
/**
* Food constructor
*
* @param name - name of the food
*/
public Recipe(String name) {
super(name);
this.calories = 0;
this.protein = 0;
this.carbs = 0;
this.fat = 0;
}
@Override
public List<Food> getChildren() {
return this.children;
}
@Override
public Food getChild(int index) {
return children.get(index);
}
@Override
public void addChild(Food food, String serving) {
children.add(food);
recipeServings.add(serving);
addNutritionalInfo();
}
@Override
public void removeChild(Food foodToRemove) {
children.remove(foodToRemove);
}
@Override
public String buildCSVFormat() {
String csv = "r," + name;
for (int i = 0; i < children.size(); i++) {
csv = csv + "," + children.get(i).getName() + "," + recipeServings.get(i);
}
return csv;
}
// Adds up the nutritional info and adds those values to the recipe object
public void addNutritionalInfo() {
for (int i = 0; i < children.size(); i++) {
this.calories = this.calories + children.get(i).getCalories();
this.protein = this.protein + children.get(i).getProtein();
this.carbs = this.carbs + children.get(i).getCarbs();
this.fat = this.fat + children.get(i).getFat();
}
}
@Override
public double getProtein() {
double val = 0;
for (int i = 0; i < children.size(); i++) {
val += children.get(i).getProtein();
}
return val;
}
@Override
public double getCarbs() {
double val = 0;
for (int i = 0; i < children.size(); i++) {
val += children.get(i).getCarbs();
}
return val;
}
@Override
public int getCalories() {
int val = 0;
for (int i = 0; i < children.size(); i++) {
val += children.get(i).getCalories();
}
return val;
}
@Override
public double getFat() {
double val = 0;
for (int i = 0; i < children.size(); i++) {
val += children.get(i).getFat();
}
return val;
}
@Override
public Food copy() {
Recipe copyRecipe = new Recipe(name);
copyRecipe.serving = serving;
copyRecipe.children = new ArrayList<>(children);
return copyRecipe;
}
} // end of Recipe Class
|