feat/essensplanErstellen #8

Merged
PBS2H21ASH merged 5 commits from feat/essensplanErstellen into main 2023-02-01 09:38:31 +01:00
3 changed files with 217 additions and 84 deletions
Showing only changes of commit 324ce53bd4 - Show all commits

View File

@ -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<Food> foods;
private List<Food> desserts;
private FoodPlan currentPlan;
@FXML
public void initialize() {
foods = FoodMgr.getFood(false);
desserts = FoodMgr.getFood(true);
ObservableList<Object> foodOptions = FXCollections.observableArrayList(foods.stream().map(food -> food.getId() + ": " + food.getName()).toList().toArray(new String[0]));
firstMeal.setItems(foodOptions);
secondMeal.setItems(foodOptions);
ObservableList<Object> 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);
}
}
}

View File

@ -41,6 +41,20 @@ public class FoodMgr {
return Database.insert("food_plan",food_planH,food_planD); 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 * returns all non desserts or all desserts
* @param isDessert true for only desserts false for non desserts * @param isDessert true for only desserts false for non desserts
@ -84,6 +98,9 @@ public class FoodMgr {
String[] food_planH = {"date"}; String[] food_planH = {"date"};
String[] food_planD = {date}; String[] food_planD = {date};
List<String> entry = Database.select("food_plan",food_planH,food_planD); List<String> entry = Database.select("food_plan",food_planH,food_planD);
if(entry.size() < 1){
return null;
}
String[] parts = entry.get(0).split(":"); String[] parts = entry.get(0).split(":");
Food foodVegan = getFoodById(Long.parseLong(parts[2])); Food foodVegan = getFoodById(Long.parseLong(parts[2]));
Food foodSecond = getFoodById(Long.parseLong(parts[3])); Food foodSecond = getFoodById(Long.parseLong(parts[3]));

View File

@ -1,87 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.*?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.*?>
<?import javafx.scene.control.Label?> <?import javafx.scene.layout.*?>
<?import javafx.scene.control.TextField?> <?import javafx.scene.text.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="599.0" prefWidth="900.0" stylesheets="@createFoodplan.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1"> <HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="950.0" stylesheets="@createFoodplan.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.bib.essensbestellungsverwaltung.CreateFoodplanController">
<children> <children>
<Label layoutX="359.0" layoutY="36.0" text="Essensplan erstellen"> <VBox alignment="CENTER" prefHeight="599.0" prefWidth="357.0" spacing="20.0">
<font> <children>
<Font size="18.0" /> <Label text="Essensplan erstellen">
</font> <font>
</Label> <Font size="18.0" />
<VBox layoutX="134.0" layoutY="124.0" prefHeight="327.0" prefWidth="632.0"> </font>
<children> </Label>
<HBox prefHeight="50.0" prefWidth="632.0"> <VBox prefHeight="285.0" prefWidth="358.0">
<children> <children>
<Label prefHeight="17.0" prefWidth="133.0" text="Datum JJJJ-MMM-TT"> <HBox prefHeight="50.0" prefWidth="632.0">
<HBox.margin> <children>
<Insets top="5.0" /> <Label prefHeight="17.0" prefWidth="133.0" text="Datum JJJJ-MMM-TT">
</HBox.margin> <HBox.margin>
</Label> <Insets top="5.0" />
<TextField /> </HBox.margin>
</children> </Label>
</HBox> <DatePicker fx:id="date" onAction="#onDateChange" />
<HBox prefHeight="50.0" prefWidth="632.0"> </children>
<children> </HBox>
<Label prefHeight="17.0" prefWidth="133.0" text="Veganes Hauptgericht"> <HBox prefHeight="50.0" prefWidth="632.0">
<HBox.margin> <children>
<Insets top="5.0" /> <Label prefHeight="17.0" prefWidth="133.0" text="Veganes Hauptgericht">
</HBox.margin> <HBox.margin>
</Label> <Insets top="5.0" />
<TextField> </HBox.margin>
<HBox.margin> </Label>
<Insets /> <ChoiceBox fx:id="firstMeal" prefWidth="150.0" />
</HBox.margin> </children>
</TextField> </HBox>
</children> <HBox prefHeight="50.0" prefWidth="632.0">
</HBox> <children>
<HBox prefHeight="50.0" prefWidth="632.0"> <Label prefHeight="17.0" prefWidth="133.0" text="Zweites Hauptgericht">
<children> <HBox.margin>
<Label prefHeight="17.0" prefWidth="133.0" text="Zweites Hauptgericht"> <Insets top="5.0" />
<HBox.margin> </HBox.margin>
<Insets top="5.0" /> </Label>
</HBox.margin> <ChoiceBox fx:id="secondMeal" prefWidth="150.0" />
</Label> </children>
<TextField> </HBox>
<HBox.margin> <HBox prefHeight="50.0" prefWidth="632.0">
<Insets /> <children>
</HBox.margin> <Label prefHeight="17.0" prefWidth="133.0" text="Veganes Dessert">
</TextField> <HBox.margin>
</children> <Insets />
</HBox> </HBox.margin>
<HBox prefHeight="50.0" prefWidth="632.0"> <padding>
<children> <Insets top="5.0" />
<Label prefHeight="17.0" prefWidth="133.0" text="Veganes Dessert"> </padding>
<HBox.margin> </Label>
<Insets /> <ChoiceBox fx:id="firstDessert" prefWidth="150.0" />
</HBox.margin> </children>
<padding> </HBox>
<Insets top="5.0" /> <HBox prefHeight="50.0" prefWidth="632.0">
</padding> <children>
</Label> <Label prefHeight="17.0" prefWidth="133.0" text="Zweites Dessert">
<TextField /> <padding>
</children> <Insets top="5.0" />
</HBox> </padding>
<HBox prefHeight="50.0" prefWidth="632.0"> </Label>
<children> <ChoiceBox fx:id="secondDessert" prefWidth="150.0" />
<Label prefHeight="17.0" prefWidth="133.0" text="Zweites Dessert"> </children>
<padding> </HBox>
<Insets top="5.0" /> <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
</padding> <children>
</Label> <Button id="btCreateFoodplan" fx:id="erstellenButton" mnemonicParsing="false" onAction="#onPlanErstellen" prefHeight="68.0" prefWidth="133.0" text="Plan erstellen" />
<TextField /> <Button id="btCancelFoodplan" mnemonicParsing="false" onAction="#onAbbrechen" prefHeight="68.0" prefWidth="133.0" text="Abbrechen" />
</children> </children>
</HBox> </HBox>
</children> </children>
</VBox> </VBox>
<Button id="btCreateFoodplan" layoutX="112.0" layoutY="427.0" mnemonicParsing="false" prefHeight="68.0" prefWidth="133.0" text="Plan erstellen" /> </children>
<Button id="btCancelFoodplan" layoutX="299.0" layoutY="427.0" mnemonicParsing="false" prefHeight="68.0" prefWidth="133.0" text="Abbrechen" /> </VBox>
</children> </children>
</AnchorPane> </HBox>