File size: 6,238 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
package model;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.time.*;
public class DietLog {
protected int totalCalories;
protected double totalCaloriesBurned;
protected double totalFat;
protected double totalProtein;
protected double totalCarbs;
protected double dailyLimit;
protected double dailyWeight;
protected String currentDate;
private int fatPerc;
private int carbPerc;
private int proteinPerc;
// Used to format date string
protected DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
protected Date date = new Date();
private ArrayList<Food> dailyFood = new ArrayList<>();
private ArrayList<Exercise> dailyExercise = new ArrayList<>();
// Constructor for daily log object
public DietLog() {
currentDate = dateFormat.format(date);
}
// Complies the total amount of calories
public int getTotalCalories() {
return totalCalories;
}
public double getTotalCaloriesBurned(){ return totalCaloriesBurned;}
// Compiles the total amount of fat
public double getTotalFat() {
return totalFat;
}
// Compiles the total amount of protein
public double getTotalProtein() {
return totalProtein;
}
// Compiles the total amount of carbs
public double getTotalCarbs() {
return totalCarbs;
}
public double getDailyWeight() {
return dailyWeight;
}
public double getDailyLimit() { return dailyLimit; }
public double getFatPerc() {
return fatPerc;
}
public double getCarbPerc() {
return carbPerc;
}
public double getProteinPerc() {
return proteinPerc;
}
// Sets the user's calorie limit for the day
public void setDailyLimit(double calorieLimit) {
dailyLimit = calorieLimit;
}
// Sets the user's weight for the day
public void setDailyWeight(double weight) {
dailyWeight = weight;
}
//gets the net difference of calories consumed vs burned
public double getNetCals(){
return totalCalories - totalCaloriesBurned;
}
// Adds a food to the list of foods consumed
public void logFood(Food foodItem, double servings) {
foodItem.setServing(servings);
dailyFood.add(foodItem);
}
/**
* The math to calculate the amount of calories burned for the user given their weight and the duration
* @param exer The exercise performed
* @param time The duration of the exercise in minutes
* @return The total number of calories burned by the user
*/
public double caloriesBurned(Exercise exer, double time){
double burnedCals = exer.getCaloriesBurned() * (this.dailyWeight / 100.0) * (time / 60);
totalCaloriesBurned += burnedCals;
return burnedCals;
}
/**
* Adds an exercise to the list of exercises performed
* @param exer the Exercise object
* @param time The duration of the exercise
*/
public void logExercise(Exercise exer, double time){
double burnedCalories = caloriesBurned(exer, time);
exer.setCaloriesBurned(burnedCalories);
exer.setTime(time);
dailyExercise.add(exer);
}
/**
* Removes an exercise from the exercises performed
* @param index The position of the exercise object that eneds to be removed
*/
public void removeExerciseByIndex(int index){
dailyExercise.remove(index);
}
/**
* Returns all of the exercises performed by the user for the current date
* @return an ArrayList on Exercise Objects
*/
public ArrayList<Exercise> getDailyExercise() {
return dailyExercise;
}
/**
* Builds an ArrayList of strings for all the Exercises
* @return An ArrayList of Strings containing exercise information
*/
public ArrayList<String> getExerciseInfo() {
ArrayList<String> ExerciseInfoList = new ArrayList<>();
for(Exercise e: dailyExercise){
String eName = e.getName();
double calsBurned = e.getCaloriesBurned();
String exerInfo = "Name: " + eName + "\nCalories: " + calsBurned;
ExerciseInfoList.add(exerInfo);
}
return ExerciseInfoList;
}
// Removes a food from the list of foods consumed
public void removeFood(Food foodItem) {
dailyFood.remove(foodItem);
}
public void removeFoodByIndex(int index) {
dailyFood.remove(index);
}
public ArrayList<Food> getDailyFood() {
return dailyFood;
}
// this is simply going to be extracting information for each food object in the food arraylist
// and building a new arraylist of Strings
public ArrayList<String> getFoodInfo() {
ArrayList<String> foodInfoList = new ArrayList<>();
for(Food food : dailyFood) {
String foodName = food.getName();
int foodCalories = food.getCalories();
double foodServings = food.getServing();
String foodInfo = "Name: " + foodName + "\nCalories: " + foodCalories + "\nServings: " + foodServings;
foodInfoList.add(foodInfo);
}
return foodInfoList;
}
public DietLog calcTotals() {
int totalCals = 0;
double totalBurned = 0;
double totalFats = 0;
double totalProtein = 0;
double totalCarbs = 0;
for(Food food : dailyFood) {
double foodServing = food.getServing();
totalCals += (food.getCalories() * foodServing);
totalFats += (food.getFat() * foodServing);
totalProtein += (food.getProtein() * foodServing);
totalCarbs += (food.getCarbs() * foodServing);
}
for(Exercise e: dailyExercise){
totalBurned += e.getCaloriesBurned();
}
this.totalCalories = totalCals;
this.totalCaloriesBurned = totalBurned;
this.totalFat = totalFats;
this.totalProtein = totalProtein;
this.totalCarbs = totalCarbs;
calcPercentages();
return this;
}
public double calcCalDiff() {
int caloriesEaten = this.totalCalories;
double caloriesBurned = this.totalCaloriesBurned;
double caloriesLeft = dailyLimit - caloriesEaten + caloriesBurned;
return caloriesLeft;
}
public void calcPercentages() {
double totalNutrients = this.totalCarbs + this.totalProtein + this.totalFat;
// sets to nearest integer
this.fatPerc = (int) Math.rint(this.totalFat/totalNutrients * 100);
this.carbPerc = (int) Math.rint(this.totalCarbs/totalNutrients * 100);
this.proteinPerc = (int) Math.rint(this.totalProtein/totalNutrients * 100);
}
// private void getPreviousDay(LocalDate start, LocalDate end) {
// for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
// processDate(date);
// }
// }
} |