diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java b/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java index bc85970..558517f 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java @@ -8,9 +8,12 @@ import javax.crypto.spec.PBEKeySpec; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; +import java.util.ArrayList; import java.util.Base64; +import java.util.List; public class AccountMgr { + protected static double price = 5.0; /** * creates a user with createUser(...) and adds its id to the 'worker' table * @param userData String[] name, firstname, password, email @@ -91,7 +94,6 @@ public class AccountMgr { return workerId > 0; } - public static boolean isParent(String id){ String[] parentH = {"userid"}; String[] parentD = {id}; @@ -114,4 +116,30 @@ public class AccountMgr { } return hashedPw; } + + public static List getInvoice(String date, String childId){ + List invoice = new ArrayList<>(); + invoice.add("Monatsabrechnung " + date); + List child = Database.getEntryById("child", Long.parseLong(childId)); + 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); + 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); + for (String food_select : food_selection) { + String[] food_selectParts = food_select.split(":"); + 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); + } + } + invoice.add("Total: " + (invoice.size()-2) + " X " + price + "€ = " + ((invoice.size()-2)*price) + "€"); + return invoice; + } } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleLib.java b/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleLib.java index a63fe50..fa9775f 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleLib.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleLib.java @@ -246,20 +246,50 @@ public class ConsoleLib { String[] foodPlanParts = Database.select("food_plan", new String[]{"date"}, new String[]{date}).get(0).split(":"); food_selectionData[1] = foodPlanParts[0]; System.out.println("Hauptspeisen: "); - System.out.println(Database.select("food",new String[]{"food"},new String[]{foodPlanParts[2]}).get(0)); - System.out.println(Database.select("food",new String[]{"food"},new String[]{foodPlanParts[3]}).get(0)); + System.out.println(Database.select("food",new String[]{"id"},new String[]{foodPlanParts[2]}).get(0)); + System.out.println(Database.select("food",new String[]{"id"},new String[]{foodPlanParts[3]}).get(0)); System.out.print("Id: "); food_selectionData[2] = sc.nextLine(); if(FoodMgr.createFood_selection(food_selectionData) < 1){ System.out.println("Fehler"); } System.out.println("Nachspeisen: "); - System.out.println(Database.select("food",new String[]{"food"},new String[]{foodPlanParts[4]}).get(0)); - System.out.println(Database.select("food",new String[]{"food"},new String[]{foodPlanParts[5]}).get(0)); + System.out.println(Database.select("food",new String[]{"id"},new String[]{foodPlanParts[4]}).get(0)); + System.out.println(Database.select("food",new String[]{"id"},new String[]{foodPlanParts[5]}).get(0)); System.out.print("Id: "); food_selectionData[2] = sc.nextLine(); if(FoodMgr.createFood_selection(food_selectionData) < 1){ System.out.println("Fehler"); } } + + public static void dayOrderPrompt(){ + System.out.println("Zusammenfassung des Tages"); + System.out.print("Datum eingeben: "); + Scanner sc = new Scanner(System.in); + String date = sc.nextLine(); + List dayOrder = FoodMgr.getDayOrder(date); + for (String food : dayOrder) { + System.out.println(food); + } + } + + public static void invoicePrompt(){ + System.out.println("Monatsabrechnung"); + System.out.print("Monat(YYYY-MM): "); + Scanner sc = new Scanner(System.in); + String date = sc.nextLine(); + System.out.print("ID des Kindes: "); + String id = sc.nextLine(); + List invoice = AccountMgr.getInvoice(date,id); + printConsole(invoice); + } + + public static void changePricePrompt(){ + System.out.print("Neuer Preis: "); + Scanner sc = new Scanner(System.in); + double price = sc.nextDouble(); + sc.nextLine(); + AccountMgr.price = price; + } } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleMain.java b/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleMain.java index c280e99..6d823e5 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleMain.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/ConsoleMain.java @@ -11,10 +11,9 @@ public class ConsoleMain { static boolean isParent = false; public static void main(String[] args) { boolean firstRun = Database.init(); - Database.createDb(); - Database.fillDb(); - //Database.printSampleQuery(); if(firstRun){ + Database.createDb(); + Database.fillDb(); ConsoleLib.createWorkerPrompt(); } while (true){ @@ -63,6 +62,9 @@ public class ConsoleMain { System.out.println("8: Essensplan anzeigen"); System.out.println("9: Löschen"); System.out.println("10: Essen auswählen"); + System.out.println("11: Bestellungen des Tages sammeln"); + System.out.println("12: Monatsabrechnung"); + System.out.println("13: Preis ändern"); System.out.print("Auswahl: "); @@ -84,8 +86,9 @@ public class ConsoleMain { case "8" -> ConsoleLib.showFood_planPrompt(); case "9" -> ConsoleLib.deletePrompt(); case "10" -> ConsoleLib.createFood_selectionPrompt(); - default -> { - } + case "11" -> ConsoleLib.dayOrderPrompt(); + case "12" -> ConsoleLib.invoicePrompt(); + case "13" -> ConsoleLib.changePricePrompt(); } } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java index 8ec8b8a..0e5d949 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -119,6 +119,7 @@ public class Database { food2 integer, dessert1 integer, dessert2 integer, + issent integer DEFAULT '0', FOREIGN KEY(food1) REFERENCES food(id), FOREIGN KEY(food2) REFERENCES food(id), FOREIGN KEY(dessert1) REFERENCES food(id), @@ -396,6 +397,38 @@ public class Database { } sb.append(");"); } + case "count" -> { + sb = new StringBuilder("SELECT COUNT(*) FROM "); + sb.append(table); + sb.append(" WHERE "); + sb.append(header[0]); + sb.append(" = "); + sb.append(values[0]); + for(int i = 1; i < header.length; i++){ + sb.append(" AND "); + sb.append(header[i]); + sb.append(" = "); + sb.append(values[i]); + } + } + case "update" -> { + sb = new StringBuilder("UPDATE "); + sb.append(table); + sb.append(" SET "); + sb.append(header[1]); + sb.append(" = "); + 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(" WHERE "); + sb.append(header[0]); + sb.append(" = "); + sb.append(values[0]); + } } sql = sb.toString(); return sql; @@ -475,4 +508,25 @@ public class Database { } return data; } + + protected static int count(String table,String[] header,String[] values){ + String sql = queryBuilder("count",table,header,values); + try(Connection conn = connect()) { + PreparedStatement ps = conn.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + return rs.getInt(1); + }catch (SQLException e){ + return -1; + } + } + + protected static int update(String table,String[] header,String[] values){ + try(Connection conn = connect()) { + String sql = queryBuilder("update",table,header,values); + PreparedStatement ps = conn.prepareStatement(sql); + return ps.executeUpdate(); + }catch (SQLException e){ + return -1; + } + } } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java b/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java index 065d0d5..50d791b 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java @@ -3,6 +3,7 @@ package com.bib.essensbestellungsverwaltung; @author Malte Schulze Hobeling */ +import java.util.ArrayList; import java.util.List; public class FoodMgr { @@ -48,7 +49,34 @@ public class FoodMgr { } public static long createFood_selection(String[] food_selectionData){ - String[] food_selectionH = {"childid","foodplanid","foodid"}; - return Database.insert("food_selection",food_selectionH,food_selectionData); + String[] food_selectionH = {"childid","food_planid","foodid"}; + List 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){ + return Database.insert("food_selection",food_selectionH,food_selectionData); + }else { + return -1; + } + } + + public static List getDayOrder(String date){ + List orders = new ArrayList<>(); + List food_plan = getFood_plan(date); + String[] food_planParts = food_plan.get(0).split(":"); + for(int i = 2; i < 2+4; i++){ + List food = getFoodById(Long.parseLong(food_planParts[i])); + String[] foodParts = food.get(0).split(":"); + String foodName = foodParts[1]; + String[] food_selectionH = {"food_planid","foodid"}; + String[] food_selectionD = {food_planParts[0],foodParts[0]}; + int count = Database.count("food_selection",food_selectionH,food_selectionD); + orders.add(foodName+":"+count); + } + String[] updateH = {"id","issent"}; + String[] updateD = {food_planParts[0],"1"}; + if(Database.update("food_plan",updateH,updateD) < 0){ + System.out.println("Fehler"); + } + return orders; } } diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/SuperMain.java b/src/main/java/com/bib/essensbestellungsverwaltung/SuperMain.java index e45c8f0..9fb076c 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/SuperMain.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/SuperMain.java @@ -2,6 +2,10 @@ package com.bib.essensbestellungsverwaltung; public class SuperMain { public static void main(String[] args) { - ConsoleMain.main(args); + if(args.length > 0){ + ConsoleMain.main(args); + }else { + HelloApplication.main(args); + } } }