File size: 3,859 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 |
package model;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class User {
private double weightGoal;
private String name;
private String currentDate;
public User() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
currentDate = dtf.format(localDate);
name = "User";
}
public Map<String, DietLog> logHistory = new HashMap<String, DietLog>();
public void setWeight(double val){
getTodaysLog().setDailyWeight(val);
}
public double getWeight(){
return getTodaysLog().getDailyWeight();
}
public void setCaloricLimit(double val){
getTodaysLog().setDailyLimit(val);
}
public double getCaloricLimit(){
return getTodaysLog().getDailyLimit();
}
public void setDesiredWeight(double val){
this.weightGoal = val;
}
public double getDesiredWeight(){
return weightGoal;
}
public void setName(String val){
this.name = val;
}
public String getName(){
return this.name;
}
public void setDayFood(DietLog DL){
//to be implemented
}
public String getCurrentDate() {
return currentDate;
}
public void setCurrentDate(String date) {
currentDate = date;
}
public Map<String, DietLog> getLogHistory() {
return logHistory;
}
public void setLogHistory(Map<String, DietLog> logHistory) {
this.logHistory = logHistory;
}
public DietLog getTodaysLog() {
if(logHistory.get(currentDate) == null) {
logHistory.put(currentDate, new DietLog());
return logHistory.get(currentDate);
}else {
return logHistory.get(currentDate);
}
}
// not being used
// public void logFood(Food food, double servings) {
// getTodaysLog().logFood(food, servings);
// }
public void logFood(Food food, double servings, String date) {
searchLogHistory(date).logFood(food, servings);
}
public void logExercise(Exercise exer, double time, String date){
searchLogHistory(date).logExercise(exer, time);
}
public DietLog searchLogHistory(String date) {
DietLog desiredLog = logHistory.get(date);
if(desiredLog != null) {
return desiredLog;
}
else {
desiredLog = new DietLog();
// getting previous most recent log
ArrayList<String> sortedLogs = new ArrayList<>(logHistory.keySet());
sortedLogs.add(date);
Collections.sort(sortedLogs);
int previousIndex = sortedLogs.indexOf(date) - 1;
// if found, use the previous calorie and weight info
if (previousIndex >= 0 && previousIndex < sortedLogs.size()) {
DietLog previousLog = logHistory.get(sortedLogs.get(previousIndex));
desiredLog.setDailyWeight(previousLog.getDailyWeight());
desiredLog.setDailyLimit(previousLog.getDailyLimit());
}
else {
// or else if not set (value would come back as 0.0), we set default values for them
desiredLog.setDailyWeight(150.0);
desiredLog.setDailyLimit(2000.0);
}
// add it into the User's history
logHistory.put(date, desiredLog);
// return the newly created DietLog object to the user
return logHistory.get(date);
}
}
public void updateLogCalorieLimit(double calorieLimit, String date) {
searchLogHistory(date).setDailyLimit(calorieLimit);
}
public void updateLogWeight(double weight, String date) {
searchLogHistory(date).setDailyWeight(weight);
}
}
|