File size: 5,107 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 |
package controller;
import model.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
public class DietController {
private FoodList foodList;
private ExerciseList exerciseList;
private CSVParser csv = new CSVParser();
private User user = new User();
public DietController() {
foodList = csv.readFoodCSV();
exerciseList = csv.readExerciseCSV();
user.setLogHistory(csv.readLogCSV(foodList, exerciseList));
}
/*
Given a BasicFood, add it to the global FoodList.
*/
public void addBasicFood(String name, int calories, double carbs, double proteins, double fats) {
BasicFood food = new BasicFood(name, calories, carbs, proteins, fats);
foodList.addFood(food);
}
/**
* Adds an Exercise to the global Exercise list
* @param name - the name of the exercise
* @param calsBurned - the number of calories burned for a 100lb person
*/
public void addExercise(String name, double calsBurned){
try {
if(name.contains(",")) {
throw new Exception("Sorry, you cannot have commas in your exercise name");
}
else {
Exercise exercise = new Exercise(name, calsBurned);
exerciseList.addExercise(exercise);
System.out.println("Your exercise has been added!");
}
}
catch(Exception e ) {
System.out.println(e.getMessage());
}
}
/*
Given a Recipe, add it to the global FoodList.
*/
public void addRecipe(String name, ArrayList<String> recipeFoods, ArrayList<String> recipeServings) {
// Creates a new recipe object
Recipe recipe = new Recipe(name);
// Loops through arrayList and adds each food object as a child
for (int i = 0; i < recipeFoods.size(); i++ ) {
recipe.addChild( foodList.findFood( recipeFoods.get(i) ), recipeServings.get(i) );
}
recipe.addNutritionalInfo();
// Adds the recipe to the list of food
foodList.addFood(recipe);
System.out.println(recipe.getCalories());
}
/**
* Logs a food to the food log for the current day
* @param foodName - name of the food being logged
* @param servings - number of servings of the food
* @return - true if successful, false on failure
*/
public boolean logFood(String foodName, double servings, String date) {
Food loggedFood = foodList.findFood(foodName);
user.logFood(loggedFood, servings, date);
return true;
}
public boolean logExercise(String name, double time, String date){
Exercise loggedExer = exerciseList.getExercise(name);
user.logExercise(loggedExer, time, date);
return true;
}
// searches for food in foodlist
public Food searchFoodList(String nameFood) {
return foodList.findFood(nameFood);
}
public Exercise searchExerciseList(String exerName) {
return exerciseList.getExercise(exerName);
}
public Set<String> getFoodListKeys() {
return foodList.getFoods().keySet();
}
public Set<String> getExerciseNames(){
return exerciseList.getAllExercises().keySet();
}
public void setUserGoals(double calorieLimit, double weightLimit, String date) {
user.setCaloricLimit(calorieLimit);
user.setDesiredWeight(weightLimit);
user.updateLogCalorieLimit(calorieLimit, date);
}
// set user's weight
public void setPersonalInfo(double weight, String date) {
user.setWeight(weight);
user.updateLogWeight(weight, date);
}
// attempts to find desired DietLog object and returns it
public DietLog getDesiredDietLog(String date) {
return user.searchLogHistory(date).calcTotals();
}
// returns history of user's dietlog objects
public Map<String, DietLog> getUserHistory() {
return user.getLogHistory();
}
public String getCurrentDate() {
return user.getCurrentDate();
}
public void setCurrentDate(String date) {
user.setCurrentDate(date);
}
private DietLog getTodaysLog() {
return user.getTodaysLog();
}
public ArrayList<Food> getTodaysLogList() {
return getTodaysLog().getDailyFood();
}
public ArrayList<Exercise> getTodaysExerciseLogList(){
return getTodaysLog().getDailyExercise();
}
public void removeFoodFromLog(int index) {
getTodaysLog().removeFoodByIndex(index);
}
public void removeExerciseFromLog(int index) {
getTodaysLog().removeExerciseByIndex(index);
}
public void setUserName(String name) {
user.setName(name);
}
public String getUserName() {
return user.getName();
}
/*
Write the entire FoodList and Daily DietLog to CSV.
*/
public void writeCSV() {
csv.writeFoodList(foodList);
csv.writeExerciseList(exerciseList);
csv.writeToLog(user);
}
} |