most Objects done

This commit is contained in:
2023-01-30 04:26:30 +01:00
parent a0eed3ce51
commit 81bcb44d74
15 changed files with 396 additions and 121 deletions

View File

@@ -29,12 +29,16 @@ public class FoodMgr {
/**
* inserts a food_plan into the database
* @param food_planData date[YYYY-MM-DD], foodid[vegan], foodid, foodid[dessert,vegan], foodid[dessert]
* @return id of food_plan or -1
*/
public static long createFood_plan(String[] food_planData){
public static long createFood_plan(FoodPlan foodPlan){
String[] food_planH = {"date","food1","food2","dessert1","dessert2"};
return Database.insert("food_plan",food_planH,food_planData);
String[] food_planD = {foodPlan.getDate(),
String.valueOf(foodPlan.getFoodVegan().getId()),
String.valueOf(foodPlan.getFoodSecond().getId()),
String.valueOf(foodPlan.getDessertVegan().getId()),
String.valueOf(foodPlan.getDessertSecond().getId())};
return Database.insert("food_plan",food_planH,food_planD);
}
/**
@@ -76,10 +80,28 @@ public class FoodMgr {
* @param date YYYY-MM-DD one day
* @return food_plan for date
*/
public static List<String> getFood_plan(String date){
public static FoodPlan getFoodPlan(String date){
String[] food_planH = {"date"};
String[] food_planD = {date};
return Database.select("food_plan",food_planH,food_planD);
List<String> entry = Database.select("food_plan",food_planH,food_planD);
String[] parts = entry.get(0).split(":");
Food foodVegan = getFoodById(Long.parseLong(parts[2]));
Food foodSecond = getFoodById(Long.parseLong(parts[3]));
Food dessertVegan = getFoodById(Long.parseLong(parts[4]));
Food dessertSecond = getFoodById(Long.parseLong(parts[5]));
boolean isSent = !parts[6].equals("0");
return new FoodPlan(Long.parseLong(parts[0]),date,foodVegan,foodSecond,dessertVegan,dessertSecond,isSent);
}
public static FoodPlan getFoodPlanById(long id){
List<String> entry = Database.getEntryById("food_plan",id);
String[] parts = entry.get(0).split(":");
Food foodVegan = getFoodById(Long.parseLong(parts[2]));
Food foodSecond = getFoodById(Long.parseLong(parts[3]));
Food dessertVegan = getFoodById(Long.parseLong(parts[4]));
Food dessertSecond = getFoodById(Long.parseLong(parts[5]));
boolean isSent = !parts[6].equals("0");
return new FoodPlan(id,parts[1], foodVegan,foodSecond,dessertVegan,dessertSecond,isSent);
}
public static Food getFoodById(long id){
@@ -95,7 +117,7 @@ public class FoodMgr {
}
public static FoodType getFoodTypeById(long id){
List<String> entry = Database.getEntryById("foodtype",id);
List<String> entry = Database.getEntryById("food_type",id);
String[] typeParts = entry.get(0).split(":");
return new FoodType(Long.parseLong(typeParts[0]),typeParts[1]);
}
@@ -112,7 +134,7 @@ public class FoodMgr {
List<Allergy> allergies = new ArrayList<>();
String[] restrictionsH = {"foodid"};
String[] restrictionsD = {String.valueOf(foodId)};
List<String> restrictions = Database.select("food_restrictions",restrictionsH,restrictionsD);
List<String> restrictions = Database.select("food_restriction",restrictionsH,restrictionsD);
for (String restriction : restrictions) {
String[] partsRestrictions = restriction.split(":");
allergies.add(getAllergyById(Long.parseLong(partsRestrictions[2])));
@@ -127,9 +149,8 @@ public class FoodMgr {
*/
public static long createFood_selection(String[] food_selectionData){
String[] food_selectionH = {"childid","food_planid","foodid"};
List<String> food_plan = Database.getEntryById("food_plan",Long.parseLong(food_selectionData[1]));
String[] food_planParts = food_plan.get(0).split(":");
if(Long.parseLong(food_planParts[6]) == 0){
FoodPlan food_plan = getFoodPlanById(Long.parseLong(food_selectionData[1]));
if(!food_plan.isSent()){
return Database.insert("food_selection",food_selectionH,food_selectionData);
}else {
return -1;
@@ -143,18 +164,23 @@ public class FoodMgr {
*/
public static List<String> getDayOrder(String date){
List<String> orders = new ArrayList<>();
List<String> food_plan = getFood_plan(date);
String[] food_planParts = food_plan.get(0).split(":");
for(int i = 2; i < 2+4; i++){
Food food = getFoodById(Long.parseLong(food_planParts[i]));
String foodName = food.getName();
String[] food_selectionH = {"food_planid","foodid"};
String[] food_selectionD = {food_planParts[0], String.valueOf(food.getId())};
FoodPlan food_plan = getFoodPlan(date);
String sId = String.valueOf(food_plan.getId());
String[] food_selectionH = {"food_planid","foodid"};
Food[] foodArray = {
food_plan.getFoodVegan(),
food_plan.getFoodSecond(),
food_plan.getDessertVegan(),
food_plan.getDessertSecond()
};
for(int i = 0; i < 4; i++){
String foodName = foodArray[i].getName();
String[] food_selectionD = {sId, String.valueOf(foodArray[i].getId())};
int count = Database.count("food_selection",food_selectionH,food_selectionD);
orders.add(foodName+":"+count);
orders.add(count+" X "+foodName);
}
String[] updateH = {"id","issent"};
String[] updateD = {food_planParts[0],"1"};
String[] updateD = {sId,"1"};
if(Database.update("food_plan",updateH,updateD) < 0){
System.out.println("Fehler");
}