125 lines
4.6 KiB
Java
125 lines
4.6 KiB
Java
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.RadioButton;
|
|
import javafx.scene.control.TextArea;
|
|
import javafx.scene.control.TextField;
|
|
import javafx.scene.text.Text;
|
|
import org.controlsfx.control.CheckComboBox;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class CreateFoodController {
|
|
@FXML
|
|
public TextField name;
|
|
@FXML
|
|
public TextArea description;
|
|
@FXML
|
|
public RadioButton isHauptgerichtRadio;
|
|
@FXML
|
|
public RadioButton isDessertRadio;
|
|
@FXML
|
|
public RadioButton isVegetarischRadio;
|
|
@FXML
|
|
public RadioButton isVeganRadio;
|
|
@FXML
|
|
public RadioButton isFleischRadio;
|
|
@FXML
|
|
public Text responseText;
|
|
public CheckComboBox allergienComboBox;
|
|
|
|
@FXML
|
|
public void initialize() {
|
|
List<String> a = Database.getTable("allergy");
|
|
ObservableList<String> allergies = FXCollections.observableArrayList();
|
|
for (String allergie : a) {
|
|
allergies.add(allergie.split(":")[0] + ": " + allergie.split(":")[1]);
|
|
}
|
|
allergienComboBox.getItems().addAll(allergies);
|
|
}
|
|
|
|
@FXML
|
|
public void onAbbrechen(ActionEvent actionEvent) {
|
|
clearInputs();
|
|
}
|
|
|
|
@FXML
|
|
public void onHinzufuegen(ActionEvent actionEvent) {
|
|
String gerichtName = name.getText();
|
|
String beschreibung = description.getText();
|
|
|
|
if(gerichtName.isBlank()){
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
alert.setTitle("Ungültige Eingabe");
|
|
alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt");
|
|
alert.setContentText("Das Feld 'Name' ist nicht ausgefüllt");
|
|
alert.showAndWait();
|
|
return;
|
|
}
|
|
if(beschreibung.isBlank()){
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
alert.setTitle("Ungültige Eingabe");
|
|
alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt");
|
|
alert.setContentText("Das Feld 'Beschreibung' ist nicht ausgefüllt");
|
|
alert.showAndWait();
|
|
return;
|
|
}
|
|
|
|
if(!isHauptgerichtRadio.isSelected() && !isDessertRadio.isSelected()){
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
alert.setTitle("Ungültige Eingabe");
|
|
alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt");
|
|
alert.setContentText("Das Feld 'Art' ist nicht ausgefüllt");
|
|
alert.showAndWait();
|
|
return;
|
|
}
|
|
boolean isNachtisch = !isHauptgerichtRadio.isSelected();
|
|
if(!isVegetarischRadio.isSelected() && !isVeganRadio.isSelected() && !isFleischRadio.isSelected()){
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
alert.setTitle("Ungültige Eingabe");
|
|
alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt");
|
|
alert.setContentText("Das Feld 'Typ' ist nicht ausgefüllt");
|
|
alert.showAndWait();
|
|
return;
|
|
}
|
|
int ft = isVeganRadio.isSelected() ? 1 : isVeganRadio.isSelected() ? 2 : 3;
|
|
FoodType foodType = new FoodType(ft, "Vegan");
|
|
List<Allergy> allergies = new ArrayList<>();
|
|
allergienComboBox.getCheckModel().getCheckedItems().stream().forEach(a -> {
|
|
long id = Integer.parseInt(a.toString().split(":")[0]);
|
|
String name = a.toString().split(":")[1].trim();
|
|
allergies.add((new Allergy(id, name, "")));
|
|
});
|
|
System.out.println(allergies.get(0).getName());
|
|
|
|
long id = FoodMgr.createFood(new Food(gerichtName, beschreibung, isNachtisch, foodType, allergies));
|
|
if(id <= 0){
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
alert.setTitle("Es ist ein Problem beim Erstellen des Gerichts aufgetreten");
|
|
alert.setHeaderText("Bitte überprüfen Sie ihre Eingabe");
|
|
alert.setContentText("Es besteht die Möglichkeit, dass dieses Gericht bereits existiert");
|
|
alert.showAndWait();
|
|
return;
|
|
}
|
|
System.out.println("Food created with id: " + id);
|
|
responseText.setText("New Food Created");
|
|
clearInputs();
|
|
}
|
|
|
|
private void clearInputs(){
|
|
name.setText("");
|
|
description.setText("");
|
|
isHauptgerichtRadio.setSelected(false);
|
|
isDessertRadio.setSelected(false);
|
|
isVeganRadio.setSelected(false);
|
|
isVegetarischRadio.setSelected(false);
|
|
isFleischRadio.setSelected(false);
|
|
allergienComboBox.getCheckModel().clearChecks();
|
|
}
|
|
}
|