From 681f039b5c784648b94ac724362737b52f57a400 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:28:09 +0100 Subject: [PATCH 01/23] add: ControlsFX --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index b3e3131..3e6af89 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,11 @@ sqlite-jdbc 3.40.0.0 + + org.controlsfx + controlsfx + 11.1.2 + From e87c7b480aaba9c202379b61d085c8786a8d6693 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:28:31 +0100 Subject: [PATCH 02/23] fix: scene Change --- .../com/bib/essensbestellungsverwaltung/SingUpController.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java b/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java index a53fcfd..557012e 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java @@ -93,8 +93,6 @@ public class SingUpController { @FXML private void onAnmeldenBtClick() throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(StartViewApplication.class.getResource("login-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 950,480); - StartViewApplication.primary.setScene(scene); + StartViewApplication.changeScene("login-view.fxml"); } } From 9d3e035a20454f910373ca2cf68228c04d72f02f Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:28:48 +0100 Subject: [PATCH 03/23] add: ControlsFX --- src/main/java/module-info.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 79a355a..bc620dc 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -3,6 +3,7 @@ module com.bib.essensbestellungsverwaltung { requires javafx.fxml; requires java.sql; requires org.xerial.sqlitejdbc; + requires org.controlsfx.controls; opens com.bib.essensbestellungsverwaltung to javafx.fxml; From 7534df32637f25d9e421dc44183b46bc595a4ec7 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:29:26 +0100 Subject: [PATCH 04/23] fix: update query quotes --- .../java/com/bib/essensbestellungsverwaltung/Database.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java index 64fbd60..1cbb805 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -482,12 +482,12 @@ public class Database { sb.append(" SET "); sb.append(header[1]); sb.append(" = "); - sb.append(values[1]); + sb.append("'" + values[1] + "'"); for(int i = 2; i < header.length; i++){ sb.append(", "); sb.append(header[i]); sb.append(" = "); - sb.append(values[i]); + sb.append("'" + values[i] + "'"); } sb.append(" WHERE "); sb.append(header[0]); From 944ddbff60d59c22d20989032e9854e97f17e55b Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:30:14 +0100 Subject: [PATCH 05/23] add: child methods --- .../AccountMgr.java | 307 +++++++++++++----- 1 file changed, 220 insertions(+), 87 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java b/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java index 6e16b8d..ca6a3cf 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java @@ -13,6 +13,7 @@ import java.util.List; /** * A collection of functions loosely related to account management * Acts as an abstraction layer to the database + * * @author Malte Schulze Hobeling */ public class AccountMgr { @@ -21,226 +22,354 @@ public class AccountMgr { /** * creates a user with createUser(...) and adds its id to the 'worker' table + * * @param worker the worker to be created * @return userid or -1 * @author Malte Schulze Hobeling */ - protected static long createWorker(Worker worker){ + protected static long createWorker(Worker worker) { long id = createUser(worker); String sId = String.valueOf(id); - Database.insert("worker", new String[]{"userid"}, new String[]{sId}); + Database.insert("worker", new String[] { "userid" }, new String[] { sId }); return id; } /** * creates a user with createUser(...) and adds its id to the 'parent' table + * * @param parent the parent to be created * @return userid or -1 * @author Malte Schulze Hobeling */ - protected static long createParent(Parent parent){ + protected static long createParent(Parent parent) { long id = createUser(parent); String sId = String.valueOf(id); - Database.insert("parent", new String[]{"userid"}, new String[]{sId}); + Database.insert("parent", new String[] { "userid" }, new String[] { sId }); return id; } /** * adds a user to the database + * * @param user the user to be created * @return userid or -1 * @author Malte Schulze Hobeling */ protected static long createUser(User user) { - String[] userH = {"name", "firstname", "addressid", "password", "email"}; + String[] userH = { "name", "firstname", "addressid", "password", "email" }; String name = user.getName(); String firstname = user.getFirstname(); String pw = hashAndSalt(user.getPassword(), getSalt()); String email = user.getEmail(); long addressId = user.getAddress().getId(); - if(addressId < 1){ + if (addressId < 1) { addressId = createAddress(user.getAddress()); } - String[] userD = {name, firstname, String.valueOf(addressId), pw, email}; + String[] userD = { name, firstname, String.valueOf(addressId), pw, email }; return Database.insert("user", userH, userD); } /** * adds an address to the database + * * @param address the address to be created * @return id or -1 * @author Malte Schulze Hobeling */ - protected static long createAddress(Address address){ - String[] addressH = {"street", "number", "plz", "city"}; - String[] addressD = {address.getStreet(),address.getNumber(),address.getPlz(),address.getCity()}; - return Database.insert("address",addressH,addressD); + protected static long createAddress(Address address) { + String[] addressH = { "street", "number", "plz", "city" }; + String[] addressD = { address.getStreet(), address.getNumber(), address.getPlz(), address.getCity() }; + return Database.insert("address", addressH, addressD); } /** * adds a child and allergies to the database + * * @param child the child to be created * @return id of child or -1 * @author Malte Schulze Hobeling */ - protected static long createChild(Child child){ - String[] childH = {"name","firstname","addressid"}; - String[] childD = {child.getName(), child.getFirstname(), String.valueOf(child.getAddress().getId())}; + protected static long createChild(Child child) { + String[] childH = { "name", "firstname", "addressid" }; + String[] childD = { child.getName(), child.getFirstname(), String.valueOf(child.getAddress().getId()) }; long id = Database.insert("child", childH, childD); - String[] child_allergyH = {"childid","allergyid","severityid"}; - for (AllergySeverity allergy: child.getAllergies()) { + String[] child_allergyH = { "childid", "allergyid", "severityid" }; + for (AllergySeverity allergy : child.getAllergies()) { String sId = String.valueOf(id); String sAllergyId = String.valueOf(allergy.getAllergy().getId()); String sSeverityId = String.valueOf(allergy.getSeverityId()); - String[] child_allergyD = {sId,sAllergyId,sSeverityId}; - Database.insert("child_allergy",child_allergyH,child_allergyD); + String[] child_allergyD = { sId, sAllergyId, sSeverityId }; + Database.insert("child_allergy", child_allergyH, child_allergyD); } return id; } /** - * returns a User(Worker | Parent) for a given id or null if no unique id was found + * update Child + * + * @param child the child to be updated + * @return id of child + * @author Johannes Kantz + */ + protected static long updateChild(Child child) { + String[] childH = { "id", "name", "firstname", "addressid" }; + String[] childD = { String.valueOf(child.getId()), child.getName(), child.getFirstname(), + String.valueOf(child.getAddress().getId()) }; + long updates = Database.update("child", childH, childD); + String[] child_allergyH = { "childid", "allergyid", "severityid" }; + Database.select("child_allergy", new String[] { "childid" }, new String[] { String.valueOf(child.getId()) }) + .stream().forEach(row -> { + String allergyId = row.split(":")[0]; + Database.delete("child_allergy", Integer.parseInt(allergyId)); + }); + for (AllergySeverity allergy : child.getAllergies()) { + String sId = String.valueOf(child.getId()); + String sAllergyId = String.valueOf(allergy.getAllergy().getId()); + String sSeverityId = String.valueOf(allergy.getSeverityId()); + String[] child_allergyD = { sId, sAllergyId, sSeverityId }; + Database.insert("child_allergy", child_allergyH, child_allergyD); + } + return updates; + } + + /** + * delete Child + * + * @author Johannes Kantz + */ + protected static void deleteChildWithId(long id) { + Database.delete("child", id); + Database.select("child_allergy", new String[] { "childid" }, new String[] { String.valueOf(id) }).stream() + .forEach(row -> { + String allergyId = row.split(":")[0]; + Database.delete("child_allergy", Integer.parseInt(allergyId)); + }); + } + + /** + * returns a User(Worker | Parent) for a given id or null if no unique id was + * found + * * @param id id of the User * @return User(Worker | Parent) or null * @author Malte Schulze Hobeling */ - protected static User getUserById(long id){ - List entry = Database.getEntryById("user",id); - if(entry.size() != 1){ + protected static User getUserById(long id) { + List entry = Database.getEntryById("user", id); + if (entry.size() != 1) { return null; } String[] parts = entry.get(0).split(":"); Address address = getAddressById(id); - if(isWorker(String.valueOf(id))){ - return new Worker(id,parts[1],parts[2],parts[4],parts[5],address); - }else{ - String[] parent_childH = {"parentuserid"}; - String[] parent_childD = {String.valueOf(id)}; + if (isWorker(String.valueOf(id))) { + return new Worker(id, parts[1], parts[2], parts[4], parts[5], address); + } else { + String[] parent_childH = { "parentuserid" }; + String[] parent_childD = { String.valueOf(id) }; List children = new ArrayList<>(); - List parent_childEntries = Database.select("parent_child",parent_childH,parent_childD); - for (String parent_childEntry: parent_childEntries) { + List parent_childEntries = Database.select("parent_child", parent_childH, parent_childD); + for (String parent_childEntry : parent_childEntries) { String[] parent_childParts = parent_childEntry.split(":"); children.add(getChildById(Long.parseLong(parent_childParts[2]))); } - return new Parent(id,parts[1],parts[2],parts[4],parts[5],address,children); + return new Parent(id, parts[1], parts[2], parts[4], parts[5], address, children); } } /** * returns a Child for a given id or null if no unique id was found + * * @param id id of child * @return Child or null * @author Malte Schulze Hobeling */ - protected static Child getChildById(long id){ - List entry = Database.getEntryById("child",id); - if(entry.size() != 1){ + protected static Child getChildById(long id) { + List entry = Database.getEntryById("child", id); + if (entry.size() != 1) { return null; } String[] parts = entry.get(0).split(":"); - String[] child_allergyH = {"childid"}; - String[] child_allergyD = {String.valueOf(id)}; - List entriesAllergy = Database.select("child_allergy",child_allergyH,child_allergyD); + String[] child_allergyH = { "childid" }; + String[] child_allergyD = { String.valueOf(id) }; + List entriesAllergy = Database.select("child_allergy", child_allergyH, child_allergyD); List allergySeverities = new ArrayList<>(); for (String entryAllergy : entriesAllergy) { String[] allergyParts = entryAllergy.split(":"); List severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3])); String sSeverity = severity.get(0).split(":")[1]; long lSeverity = Long.parseLong(severity.get(0).split(":")[0]); - allergySeverities.add(new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])),lSeverity,sSeverity)); + allergySeverities.add( + new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])), lSeverity, sSeverity)); } - return new Child(id,parts[1],parts[2],getAddressById(Long.parseLong(parts[3])),allergySeverities); + return new Child(id, parts[1], parts[2], getAddressById(Long.parseLong(parts[3])), allergySeverities); + } + + /** + * returns all Children + * + * @return {List getAllChildren() { + List entry = Database.getTable("child"); + if (entry.size() < 1) { + return new ArrayList<>(); + } + List children = new ArrayList<>(); + for (String s : entry) { + String[] parts = s.split(":"); + String[] child_allergyH = { "childid" }; + String[] child_allergyD = { String.valueOf(parts[0]) }; + List entriesAllergy = Database.select("child_allergy", child_allergyH, child_allergyD); + List allergySeverities = new ArrayList<>(); + for (String entryAllergy : entriesAllergy) { + String[] allergyParts = entryAllergy.split(":"); + List severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3])); + String sSeverity = severity.get(0).split(":")[1]; + long lSeverity = Long.parseLong(severity.get(0).split(":")[0]); + allergySeverities.add(new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])), + lSeverity, sSeverity)); + } + children.add(new Child(Long.parseLong(parts[0]), parts[1], parts[2], + getAddressById(Long.parseLong(parts[3])), allergySeverities)); + } + return children; + } + + /** + * returns all Children from parent + * @param id parentid + * @return {List getAllChildrenFromParentWithId(long id) { + List entry = Database.select("parent_child", new String[] { "parentuserid" }, + new String[] { String.valueOf(id) }); + if (entry.size() < 1) { + return new ArrayList<>(); + } + List childIds = new ArrayList<>(); + for (String s : entry) { + String[] parts = s.split(":"); + childIds.add(parts[2]); + } + List children = new ArrayList<>(); + for (String s : childIds) { + List child = Database.getEntryById("child", Long.parseLong(s)); + String[] parts = child.get(0).split(":"); + String[] child_allergyH = { "childid" }; + String[] child_allergyD = { String.valueOf(parts[0]) }; + List entriesAllergy = Database.select("child_allergy", child_allergyH, child_allergyD); + List allergySeverities = new ArrayList<>(); + for (String entryAllergy : entriesAllergy) { + String[] allergyParts = entryAllergy.split(":"); + List severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3])); + String sSeverity = severity.get(0).split(":")[1]; + long lSeverity = Long.parseLong(severity.get(0).split(":")[0]); + allergySeverities.add(new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])), + lSeverity, sSeverity)); + } + children.add(new Child(Long.parseLong(parts[0]), parts[1], parts[2], + getAddressById(Long.parseLong(parts[3])), allergySeverities)); + } + return children; } /** * returns an Address for a given id or null if no unique id was found + * * @param id id of the address * @return Address or null * @author Malte Schulze Hobeling */ - protected static Address getAddressById(long id){ - List entry = Database.getEntryById("address",id); - if(entry.size() != 1){ + protected static Address getAddressById(long id) { + List entry = Database.getEntryById("address", id); + if (entry.size() != 1) { return null; } String[] parts = entry.get(0).split(":"); - return new Address(Long.parseLong(parts[0]),parts[1],parts[2],parts[3],parts[4]); + return new Address(Long.parseLong(parts[0]), parts[1], parts[2], parts[3], parts[4]); } /** * creates entries in the database to match parent to child + * * @param parentId id of parent - * @param childId id of child + * @param childId id of child * @return id of parent_child or -1 * @author Malte Schulze Hobeling */ - protected static long matchParentChild(String parentId, String childId){ - String[] parent_childH = {"parentuserid","childid"}; - String[] parent_childD = {parentId,childId}; - return Database.insert("parent_child", parent_childH,parent_childD); + protected static long matchParentChild(String parentId, String childId) { + String[] parent_childH = { "parentuserid", "childid" }; + String[] parent_childD = { parentId, childId }; + return Database.insert("parent_child", parent_childH, parent_childD); } /** * a simple login to check if a given email matches a password + * * @param email email - * @param pw password + * @param pw password * @return id or -1 * @author Malte Schulze Hobeling */ - protected static long login(String email, String pw){ - String[] pwH = {"email"}; - String[] pwD = {email}; - List foundEmail = Database.select("user",pwH,pwD); + protected static long login(String email, String pw) { + String[] pwH = { "email" }; + String[] pwD = { email }; + List foundEmail = Database.select("user", pwH, pwD); String salt; - if(foundEmail.size() == 1){ + if (foundEmail.size() == 1) { String[] userParts = foundEmail.get(0).split(":"); String[] pwParts = userParts[4].split("\\."); salt = pwParts[1]; - }else{ - //no unique user found; still calculating a hash for security reasons + } else { + // no unique user found; still calculating a hash for security reasons salt = getSalt(); } - String[] userH = {"email","password"}; - String[] userD = {email,hashAndSalt(pw,salt)}; - return Database.getSingleId("user",userH,userD); + String[] userH = { "email", "password" }; + String[] userD = { email, hashAndSalt(pw, salt) }; + return Database.getSingleId("user", userH, userD); } /** * checks if id is in worker table + * * @param id userid * @return true if id is in worker table * @author Malte Schulze Hobeling */ - protected static boolean isWorker(String id){ - String[] workerH = {"userid"}; - String[] workerD = {id}; - long workerId = Database.getSingleId("worker",workerH,workerD); + protected static boolean isWorker(String id) { + String[] workerH = { "userid" }; + String[] workerD = { id }; + long workerId = Database.getSingleId("worker", workerH, workerD); return workerId > 0; } /** * checks if id is in parent table + * * @param id userid * @return true if id is in parent table * @author Malte Schulze Hobeling */ - protected static boolean isParent(String id){ - String[] parentH = {"userid"}; - String[] parentD = {id}; - long parentId = Database.getSingleId("parent",parentH,parentD); + protected static boolean isParent(String id) { + String[] parentH = { "userid" }; + String[] parentD = { id }; + long parentId = Database.getSingleId("parent", parentH, parentD); return parentId > 0; } /** * returns a hashed and salted password + * * @param pw the password to hash * @return hashed and salted password * @author Malte Schulze Hobeling */ - private static String hashAndSalt(String pw, String salt){ + private static String hashAndSalt(String pw, String salt) { Base64.Decoder dec = Base64.getDecoder(); byte[] bySalt = dec.decode(salt); - KeySpec spec = new PBEKeySpec(pw.toCharArray(), bySalt,310001,256); + KeySpec spec = new PBEKeySpec(pw.toCharArray(), bySalt, 310001, 256); String hashedPw; try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); @@ -256,10 +385,11 @@ public class AccountMgr { /** * generates a secure random salt, Base64 encoded + * * @return String Base64 encoded * @author Malte Schulze Hobeling */ - private static String getSalt(){ + private static String getSalt() { SecureRandom sec = new SecureRandom(); byte[] salt = new byte[16]; sec.nextBytes(salt); @@ -269,59 +399,62 @@ public class AccountMgr { /** * gives the invoice for one month and one child - * @param date YYYY-MM the month + * + * @param date YYYY-MM the month * @param childId id of child * @return the invoice as a List * @author Malte Schulze Hobeling */ - protected static List getInvoice(String date, String childId){ + protected static List getInvoice(String date, String childId) { List invoice = new ArrayList<>(); List child = Database.getEntryById("child", Long.parseLong(childId)); - if(child.size() != 1){ + if (child.size() != 1) { return invoice; } invoice.add("Monatsabrechnung " + date); String[] childParts = child.get(0).split(":"); invoice.add(childParts[1] + ", " + childParts[2]); - String[] food_planH = {"date"}; - String[] food_planD = {date+"%"}; - List food_plan = Database.select("food_plan",food_planH,food_planD); + String[] food_planH = { "date" }; + String[] food_planD = { date + "%" }; + List food_plan = Database.select("food_plan", food_planH, food_planD); for (String day : food_plan) { String[] food_planParts = day.split(":"); - String[] food_selectionH = {"childid","food_planid"}; - String[] food_selectionD = {childId,food_planParts[0]}; - List food_selection = Database.select("food_selection",food_selectionH,food_selectionD); + String[] food_selectionH = { "childid", "food_planid" }; + String[] food_selectionD = { childId, food_planParts[0] }; + List food_selection = Database.select("food_selection", food_selectionH, food_selectionD); for (String food_select : food_selection) { String[] food_selectParts = food_select.split(":"); - List food = Database.getEntryById("food",Long.parseLong(food_selectParts[3])); + List food = Database.getEntryById("food", Long.parseLong(food_selectParts[3])); String[] foodParts = food.get(0).split(":"); String line = food_planParts[1] + ": " + foodParts[1]; invoice.add(line); } } double price = getPrice(); - invoice.add("Total: " + (invoice.size()-2) + " X " + price + "€ = " + ((invoice.size()-2)*price) + "€"); + invoice.add("Total: " + (invoice.size() - 2) + " X " + price + "€ = " + ((invoice.size() - 2) * price) + "€"); return invoice; } /** * gets the price per meal from the database and converts it to double + * * @return double price * @author Malte Schulze Hobeling */ - protected static double getPrice(){ - List priceEntry = Database.getEntryById("price",1); - return Double.parseDouble(priceEntry.get(0).split(":")[1])/100.0; + protected static double getPrice() { + List priceEntry = Database.getEntryById("price", 1); + return Double.parseDouble(priceEntry.get(0).split(":")[1]) / 100.0; } /** * converts the price per meal to integer and updates it in the database + * * @param price double * @author Malte Schulze Hobeling */ - protected static void setPrice(double price){ - String[] priceH = {"id","price"}; - String[] priceD = {"1", String.valueOf((int)(price*100))}; - Database.update("price",priceH,priceD); + protected static void setPrice(double price) { + String[] priceH = { "id", "price" }; + String[] priceD = { "1", String.valueOf((int) (price * 100)) }; + Database.update("price", priceH, priceD); } } From 07050a58090557e5c650295b03a8a8185c3443c9 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:31:08 +0100 Subject: [PATCH 06/23] fix: show only vegan food in vegan food dropdown --- .../CreateFoodplanController.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java b/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java index e7290c7..2dc41f2 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodplanController.java @@ -31,20 +31,28 @@ public class CreateFoodplanController { ChoiceBox secondDessert; private List foods; + private List veganFoods; private List desserts; + private List veganDesserts; private FoodPlan currentPlan; @FXML public void initialize() { + veganFoods = FoodMgr.getVeganFood(false); foods = FoodMgr.getFood(false); + veganDesserts = FoodMgr.getVeganFood(true); desserts = FoodMgr.getFood(true); + ObservableList foodOptions = FXCollections.observableArrayList(foods.stream().map(food -> food.getId() + ": " + food.getName()).toList().toArray(new String[0])); - firstMeal.setItems(foodOptions); + ObservableList veganFoodOptions = FXCollections.observableArrayList(veganFoods.stream().map(food -> food.getId() + ": " + food.getName()).toList().toArray(new String[0])); + ObservableList veganDessertOptions = FXCollections.observableArrayList(veganDesserts.stream().map(dessert -> dessert.getId() + ": " + dessert.getName()).toList().toArray(new String[0])); + ObservableList dessertOptions = FXCollections.observableArrayList(desserts.stream().map(dessert -> dessert.getId() + ": " + dessert.getName()).toList().toArray(new String[0])); + firstMeal.setItems(veganFoodOptions); secondMeal.setItems(foodOptions); - ObservableList dessertOptions = FXCollections.observableArrayList(desserts.stream().map(dessert -> dessert.getId() + ": " + dessert.getName()).toList().toArray(new String[0])); - firstDessert.setItems(dessertOptions); + + firstDessert.setItems(veganDessertOptions); secondDessert.setItems(dessertOptions); date.setValue(LocalDate.now()); From 5f5480bf5cff51135b433e36b6bf97d97a516250 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:31:22 +0100 Subject: [PATCH 07/23] fix: Error messages --- .../CreateFoodController.java | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodController.java b/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodController.java index 834040a..089ffdc 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/CreateFoodController.java @@ -2,6 +2,7 @@ package com.bib.essensbestellungsverwaltung; 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; @@ -39,12 +40,40 @@ public class CreateFoodController { 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()){ - // art auswähelen + 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()){ - // Typ auswählen + 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"); @@ -52,6 +81,14 @@ public class CreateFoodController { // TODO: allergien hinzufügen 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(); From b37cd2ad38b4b40425856a6575065fd0be06fc0a Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:32:19 +0100 Subject: [PATCH 08/23] add: create and edit children --- .../ChildController.java | 142 ++++++++++++++++++ .../WorkerMenuController.java | 1 - .../child-view.fxml | 81 +++------- .../workerMenu-view.fxml | 74 ++++----- 4 files changed, 196 insertions(+), 102 deletions(-) create mode 100644 src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java b/src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java new file mode 100644 index 0000000..0d11b50 --- /dev/null +++ b/src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java @@ -0,0 +1,142 @@ +package com.bib.essensbestellungsverwaltung; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.*; +import org.controlsfx.control.CheckComboBox; + +import java.util.ArrayList; +import java.util.List; + +public class ChildController { + public TextField firstName; + public ChoiceBox childChoiceBox; + public TextField lastName; + public Button kindHinzufügenButton; + public CheckComboBox allergienComboBox; + public Button kindLoeschenButton; + + private Child currentChild = null; + + private long selectedChildId; + + @FXML + public void initialize() { + kindLoeschenButton.setDisable(true); + updateChildChoiceBoxItems(); + + List a = Database.getTable("allergy"); + ObservableList allergies = FXCollections.observableArrayList(); + for (String allergie : a) { + allergies.add(allergie.split(":")[0] + ": " + allergie.split(":")[1]); + } + allergienComboBox.getItems().addAll(allergies); + } + + public void onSelectChild(ActionEvent mouseEvent) { + selectedChildId = Integer.parseInt(childChoiceBox.getValue().toString().split(":")[0]); + System.out.println("Selected Child: " + selectedChildId); + + clearInputs(); + + if(selectedChildId == 0) { + kindHinzufügenButton.setText("Kind Hinzufügen"); + kindLoeschenButton.setDisable(true); + return; + } + + // Get selected child and update fields + currentChild = AccountMgr.getChildById(selectedChildId); + firstName.setText(currentChild.getFirstname()); + lastName.setText(currentChild.getName()); + for (AllergySeverity a : currentChild.getAllergies()){ + allergienComboBox.getCheckModel().check((int) a.getAllergy().getId() - 1); + } + kindHinzufügenButton.setText("Kind updaten"); + kindLoeschenButton.setDisable(false); + } + + public void onKindHinzufügen(ActionEvent actionEvent) { + if(childChoiceBox.getValue() == null){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Ungültige Eingabe"); + alert.setHeaderText("Sie müssen ein Kind auswählen"); + alert.showAndWait(); + return; + } + + if(lastName.getText().isBlank() || firstName.getText().isBlank()){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Ungültige Eingabe"); + alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt"); + alert.showAndWait(); + return; + } + + Address adress = AccountMgr.currentUser.getAddress(); + List allergies = new ArrayList<>(); + allergienComboBox.getCheckModel().getCheckedItems().stream().forEach(a -> { + long id = Integer.parseInt(a.toString().split(":")[0]); + String name = a.toString().split(":")[1].trim(); + // TODO: Allergy Severity + allergies.add(new AllergySeverity(new Allergy(id, name, ""), 2, null)); + }); + + if(selectedChildId == 0){ + // TODO: Create child + long id = AccountMgr.createChild(new Child(lastName.getText(), firstName.getText(), adress, allergies)); + System.out.println("Child created: " + id); + + if(id <= 0){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Es ist ein fehler beim erstellen des Kinds aufgetreten"); + alert.showAndWait(); + return; + } + AccountMgr.matchParentChild(String.valueOf(AccountMgr.currentUser.getId()), String.valueOf(id)); + + childChoiceBox.getItems().add(id + ": " + firstName.getText() + " " + lastName.getText()); + childChoiceBox.setValue(childChoiceBox.getItems().get(childChoiceBox.getItems().size() - 1)); + clearInputs(); + onSelectChild(null); + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Kind erstellt"); + alert.setHeaderText("Es wurde ein Kind erstellt"); + alert.showAndWait(); + }else { + long id = AccountMgr.updateChild(new Child(currentChild.getId(), lastName.getText(), firstName.getText(), adress, allergies)); + System.out.println("Updated Child: " + id); + } + } + + private void clearInputs(){ + firstName.setText(""); + lastName.setText(""); + allergienComboBox.getCheckModel().clearChecks(); + } + + public void updateChildChoiceBoxItems(){ + List childList = AccountMgr.getAllChildrenFromParentWithId(AccountMgr.currentUser.getId()); + + ObservableList childOptions = FXCollections.observableArrayList(childList.stream().map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0])); + childOptions.add(0, "0: neues Kind erstellen"); + childChoiceBox.setItems(childOptions); + } + + public void onKindLoeschen(ActionEvent actionEvent) { + AccountMgr.deleteChildWithId(currentChild.getId()); + System.out.println("Deleted Child: " + currentChild.getId()); + + childChoiceBox.getItems().remove(childChoiceBox.getItems().stream().filter(v -> v.equals(currentChild.getId() + ": " + currentChild.getFirstname() + " " + currentChild.getName())).findFirst().get()); + childChoiceBox.setValue(childChoiceBox.getItems().get(1)); + + onSelectChild(null); + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Kind wurde gelöscht"); + alert.showAndWait(); + } +} diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java b/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java index 42d7bcb..a9a854d 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java @@ -12,7 +12,6 @@ import java.io.IOException; public class WorkerMenuController { @FXML public BorderPane contentView; - @FXML Button tagesbestellungButton; @FXML diff --git a/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml b/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml index dab0830..5c0893d 100644 --- a/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml +++ b/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml @@ -1,48 +1,32 @@ - - - - - - - - - - - + + + + + - + - + - + - + - - - - - - - - - - @@ -50,44 +34,16 @@ - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -95,6 +51,19 @@ - - - - - - + - - + - + - - +
- +
From cd8e4c9b3dd38323f4d53e627515061262bb10a1 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 18:49:22 +0100 Subject: [PATCH 09/23] add: create worker account on first launch --- .../essensbestellungsverwaltung/Parent.java | 15 +++++++++--- .../SingUpController.java | 24 ++++++++++++++----- .../StartViewApplication.java | 5 ++-- .../essensbestellungsverwaltung/Worker.java | 11 +++++++-- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Parent.java b/src/main/java/com/bib/essensbestellungsverwaltung/Parent.java index 1f3f7df..8b9178b 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Parent.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Parent.java @@ -4,21 +4,30 @@ import java.util.ArrayList; import java.util.List; /** - * one constructor is used to create new parents the other is used to create existing parents from database + * one constructor is used to create new parents the other is used to create + * existing parents from database + * * @author Malte Schulze Hobeling */ -public class Parent extends User{ +public class Parent extends User { List children; - public Parent(long id, String name, String firstname, String password, String email, Address address, List children) { + public Parent(long id, String name, String firstname, String password, String email, Address address, + List children) { super(id, name, firstname, password, email, address); this.children = children; } + public Parent(String name, String firstname, String password, String email, Address address) { super(name, firstname, password, email, address); this.children = new ArrayList<>(); } + public Parent(User user) { + super(user.getId(), user.getName(), user.getFirstname(), user.getPassword(), user.getEmail(), user.getAddress()); + this.children = new ArrayList<>(); + } + public List getChildren() { return children; } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java b/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java index 557012e..891eac6 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/SingUpController.java @@ -4,14 +4,11 @@ package com.bib.essensbestellungsverwaltung; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import java.io.IOException; -import java.util.HashMap; public class SingUpController { @FXML @@ -74,9 +71,24 @@ public class SingUpController { }else { Address newAdresse = new Address(strasse,hausnummer,plz,stadt); User newUser = new User(name,vorname,passwort,email,newAdresse); - long creatNewUser = AccountMgr.createUser(newUser); - if (creatNewUser > 0){ - alert = new Alert(Alert.AlertType.CONFIRMATION,"Ihrer Daten wurde gespeichert."); + if(StartViewApplication.firstLaunch){ + long id = AccountMgr.createWorker(new Worker(newUser)); + if(id < 1) { + Alert a = new Alert(Alert.AlertType.ERROR,"Es ist ein Fehler bei der Erstellung Ihres Accounts aufgetreten."); + a.showAndWait(); + return; + } + alert = new Alert(Alert.AlertType.CONFIRMATION,"Mitarbeiter Account erfolgreich erstellt"); + alert.showAndWait(); + StartViewApplication.firstLaunch = false; + }else { + long id = AccountMgr.createParent(new Parent(newUser)); + if(id < 1) { + Alert a = new Alert(Alert.AlertType.ERROR,"Es ist ein Fehler bei der Erstellung Ihres Accounts aufgetreten."); + a.showAndWait(); + return; + } + alert = new Alert(Alert.AlertType.CONFIRMATION,"Eltern Account erfolgreich erstellt"); alert.showAndWait(); } tfName.setText(""); diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/StartViewApplication.java b/src/main/java/com/bib/essensbestellungsverwaltung/StartViewApplication.java index 911705f..3dae544 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/StartViewApplication.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/StartViewApplication.java @@ -15,10 +15,11 @@ import java.io.IOException; public class StartViewApplication extends Application { public static Stage primary; + public static boolean firstLaunch; @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(StartViewApplication.class.getResource("login-view.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(StartViewApplication.class.getResource((firstLaunch) ? "signUp-view.fxml" : "login-view.fxml")); Scene scene = new Scene(fxmlLoader.load(), 1300, 750); primary = stage; stage.setTitle("Essen Bestellung im Kindergarten"); @@ -29,7 +30,7 @@ public class StartViewApplication extends Application { } public static void main(String[] args) { - Database.init(); + firstLaunch = Database.init(); Database.createDb(); Database.fillDb(); //Database.printSampleQuery(); diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Worker.java b/src/main/java/com/bib/essensbestellungsverwaltung/Worker.java index 45161f3..60f02f8 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Worker.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Worker.java @@ -1,14 +1,21 @@ package com.bib.essensbestellungsverwaltung; /** - * one constructor is used to create new worker the other is used to create existing worker from database + * one constructor is used to create new worker the other is used to create + * existing worker from database + * * @author Malte Schulze Hobeling */ -public class Worker extends User{ +public class Worker extends User { public Worker(long id, String name, String firstname, String password, String email, Address address) { super(id, name, firstname, password, email, address); } + public Worker(String name, String firstname, String password, String email, Address address) { super(name, firstname, password, email, address); } + + public Worker(User user) { + super(user.getId(), user.getName(), user.getFirstname(), user.getPassword(), user.getEmail(), user.getAddress()); + } } From 745eddea319add6b52092439478962d8e2454430 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 19:07:14 +0100 Subject: [PATCH 10/23] add: select child for invoice and show total --- .../InvoiceController.java | 31 +++++++++++++++++-- .../invoice-view.fxml | 20 +++++++----- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java b/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java index aebd7ba..58af018 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java @@ -1,12 +1,16 @@ package com.bib.essensbestellungsverwaltung; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.fxml.FXML; +import javafx.scene.control.Alert; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Spinner; import javafx.scene.input.MouseEvent; import javafx.scene.text.Text; import java.util.Calendar; +import java.util.List; public class InvoiceController { @FXML @@ -14,6 +18,9 @@ public class InvoiceController { @FXML ChoiceBox monatChoiceBox; + @FXML + ChoiceBox childChoiceBox; + @FXML Spinner jahrSpinner; @@ -21,12 +28,32 @@ public class InvoiceController { public void initialize() { monatChoiceBox.setValue(intToMonth(Calendar.getInstance().get(Calendar.MONTH) + 1)); jahrSpinner.getValueFactory().setValue(Calendar.getInstance().get(Calendar.YEAR)); + + List childList = AccountMgr.getAllChildren(); + ObservableList childOptions = FXCollections.observableArrayList(childList.stream().map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0])); + childChoiceBox.setItems(childOptions); } @FXML void onRechnungErstellenClick(MouseEvent mouseEvent) { - responseText.setText(monatChoiceBox.getValue().toString() + jahrSpinner.getValue()); - // hier rechnung erstellen + if(childChoiceBox.getValue() == null){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Es wurde kein Kind ausgewählt"); + alert.setHeaderText("Bitte wählen sie ein Kind aus"); + alert.showAndWait(); + } + + String childId = childChoiceBox.getValue().toString().split(":")[0]; + String date = String.format("%d-%02d", Integer.parseInt(jahrSpinner.getValue().toString()), monthToInt(monatChoiceBox.getValue().toString())); + + System.out.println("Invoice (" + date + ") from child: " + childId); + + List invoice = AccountMgr.getInvoice(date, childId); + + responseText.setText(invoice.get(invoice.size() - 1)); + + //TODO: show invoice + //TODO: export invoice as word or pdf } private int monthToInt(String month) { diff --git a/src/main/resources/com/bib/essensbestellungsverwaltung/invoice-view.fxml b/src/main/resources/com/bib/essensbestellungsverwaltung/invoice-view.fxml index 5b2a05c..99c1d22 100644 --- a/src/main/resources/com/bib/essensbestellungsverwaltung/invoice-view.fxml +++ b/src/main/resources/com/bib/essensbestellungsverwaltung/invoice-view.fxml @@ -17,14 +17,10 @@ - + - @@ -50,6 +46,16 @@ + + + +