From 20700448af21b3f79071b9a31e2ce87259bbf514 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Wed, 1 Feb 2023 07:22:09 +0100 Subject: [PATCH] add: createFoodplan --- .../CreateFoodplanController.java | 120 +++++++++++++ .../essensbestellungsverwaltung/FoodMgr.java | 17 ++ .../createFoodplan-view.fxml | 164 +++++++++--------- 3 files changed, 217 insertions(+), 84 deletions(-) create mode 100644 src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java b/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java new file mode 100644 index 0000000..b0f31d1 --- /dev/null +++ b/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java @@ -0,0 +1,120 @@ +package com.bib.essensbestellungsverwaltung; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.ChoiceBox; +import javafx.scene.control.DatePicker; + +import java.time.LocalDate; +import java.util.List; + +public class CreateFoodplanController { + @FXML + Button erstellenButton; + @FXML + DatePicker date; + + @FXML + ChoiceBox firstMeal; + + @FXML + ChoiceBox secondMeal; + + @FXML + ChoiceBox firstDessert; + + @FXML + ChoiceBox secondDessert; + + private List foods; + private List desserts; + private FoodPlan currentPlan; + + + @FXML + public void initialize() { + foods = FoodMgr.getFood(false); + desserts = FoodMgr.getFood(true); + ObservableList foodOptions = FXCollections.observableArrayList(foods.stream().map(food -> food.getId() + ": " + food.getName()).toList().toArray(new String[0])); + firstMeal.setItems(foodOptions); + secondMeal.setItems(foodOptions); + + ObservableList dessertOptions = FXCollections.observableArrayList(desserts.stream().map(dessert -> dessert.getId() + ": " + dessert.getName()).toList().toArray(new String[0])); + firstDessert.setItems(dessertOptions); + secondDessert.setItems(dessertOptions); + + date.setValue(LocalDate.now()); + onDateChange(null); + } + + @FXML + public void onPlanErstellen(ActionEvent actionEvent) { + if (date.getValue() == null || + firstMeal.getValue().toString().isBlank() || + secondMeal.getValue().toString().isBlank() || + firstDessert.getValue().toString().isBlank() || + secondDessert.getValue().toString().isBlank() + ) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Ungültige Eingabe"); + alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt"); + alert.setContentText("Es kann sein, dass Sie erst Gerichte erstellen müssen, um alle Felder auszufüllen"); + alert.showAndWait(); + return; + } + + + int firstMealId = Integer.parseInt(firstMeal.getValue().toString().split(":")[0]); + int secondMealId = Integer.parseInt(secondMeal.getValue().toString().split(":")[0]); + int firstDessertId = Integer.parseInt(firstDessert.getValue().toString().split(":")[0]); + int secondDessertId = Integer.parseInt(secondDessert.getValue().toString().split(":")[0]); + + + + Food f1 = foods.stream().filter(food -> food.getId() == firstMealId).findFirst().get(); + Food f2 = foods.stream().filter(food -> food.getId() == secondMealId).findFirst().get(); + Food d1 = desserts.stream().filter(dessert -> dessert.getId() == firstDessertId).findFirst().get(); + Food d2 = desserts.stream().filter(dessert -> dessert.getId() == secondDessertId).findFirst().get(); + + String d = date.getValue().toString(); + System.out.println(d); + + if (currentPlan != null) { // update Foodplan + long id = currentPlan.getId(); + FoodPlan plan = new FoodPlan(id, d, f1, f2, d1, d2, false); + long i = FoodMgr.updateFood_plan(plan); + System.out.println("Foodplan updated: " + i); + } else { // create new Foodplan + FoodPlan plan = new FoodPlan(d, f1, f2, d1, d2); + long id = FoodMgr.createFood_plan(plan); + System.out.println("Foodplan created with id: " + id); + } + } + + @FXML + public void onAbbrechen(ActionEvent actionEvent) { + } + + @FXML + public void onDateChange(ActionEvent actionEvent) { + currentPlan = FoodMgr.getFoodPlan(date.getValue().toString()); + if (currentPlan != null) { // current FoodPlan already exists and can be edited + firstMeal.setValue(currentPlan.getFoodVegan().getId() + ": " + currentPlan.getFoodVegan().getName()); + secondMeal.setValue(currentPlan.getFoodSecond().getId() + ": " + currentPlan.getFoodSecond().getName()); + firstDessert.setValue(currentPlan.getDessertVegan().getId() + ": " + currentPlan.getDessertVegan().getName()); + secondDessert.setValue(currentPlan.getDessertSecond().getId() + ": " + currentPlan.getDessertSecond().getName()); + erstellenButton.setText("Plan updaten"); + } else { // current Foodplan does not exist + erstellenButton.setText("Plan erstellen"); + firstMeal.setValue(null); + secondMeal.setValue(null); + firstDessert.setValue(null); + secondDessert.setValue(null); + } + + } +} diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java b/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java index ac1bcc1..b1f612e 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java @@ -41,6 +41,20 @@ public class FoodMgr { return Database.insert("food_plan",food_planH,food_planD); } + /** + * updates a food_plan into the database + * @return number of rows affected or -1 on error + */ + public static long updateFood_plan(FoodPlan foodPlan){ + String[] food_planH = {"id","food1","food2","dessert1","dessert2"}; + String[] food_planD = {String.valueOf(foodPlan.getId()), + String.valueOf(foodPlan.getFoodVegan().getId()), + String.valueOf(foodPlan.getFoodSecond().getId()), + String.valueOf(foodPlan.getDessertVegan().getId()), + String.valueOf(foodPlan.getDessertSecond().getId())}; + return Database.update("food_plan",food_planH,food_planD); + } + /** * returns all non desserts or all desserts * @param isDessert true for only desserts false for non desserts @@ -84,6 +98,9 @@ public class FoodMgr { String[] food_planH = {"date"}; String[] food_planD = {date}; List entry = Database.select("food_plan",food_planH,food_planD); + if(entry.size() < 1){ + return null; + } String[] parts = entry.get(0).split(":"); Food foodVegan = getFoodById(Long.parseLong(parts[2])); Food foodSecond = getFoodById(Long.parseLong(parts[3])); diff --git a/src/main/resources/com/bib/essensbestellungsverwaltung/createFoodplan-view.fxml b/src/main/resources/com/bib/essensbestellungsverwaltung/createFoodplan-view.fxml index 6b93e3e..8e69006 100644 --- a/src/main/resources/com/bib/essensbestellungsverwaltung/createFoodplan-view.fxml +++ b/src/main/resources/com/bib/essensbestellungsverwaltung/createFoodplan-view.fxml @@ -1,87 +1,83 @@ - - - - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -