added missing functionality

This commit is contained in:
Malte Schulze Hobeling 2023-01-06 01:51:42 +01:00
parent 89ea9ca70e
commit 58fb02875a
5 changed files with 114 additions and 85 deletions

View File

@ -1,18 +1,15 @@
package com.bib.essensbestellungsverwaltung;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
/*
@author Malte Schulze Hobeling
*/
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Base64;
public class AccountMgr {
/**
* creates a user with createUser(...) and adds its id to the 'worker' table
@ -106,7 +103,7 @@ public class AccountMgr {
//todo: find a better way to salt
byte[] magicSalt = new byte[]{96, 13, 100, 85, -37, 52, -123, 86, -123, -92, 16, 15, -110, -42, -49, 0};
KeySpec spec = new PBEKeySpec(pw.toCharArray(), magicSalt,310001,256);
String hashedPw = null;
String hashedPw;
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = factory.generateSecret(spec).getEncoded();

View File

@ -1,4 +1,7 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.ArrayList;
import java.util.List;
@ -116,7 +119,9 @@ public class ConsoleLib {
System.out.print("Allergienummer: ");
String allergies = sc.nextLine();
String[] allergyData = allergies.split(",");
FoodMgr.createFood(foodData,allergyData);
if(FoodMgr.createFood(foodData,allergyData) < 1){
System.out.println("Fehler");
}
}
public static long loginPrompt(){
@ -155,6 +160,17 @@ public class ConsoleLib {
printConsole(Database.getTable(table));
}
public static void deletePrompt(){
Scanner sc = new Scanner(System.in);
System.out.println("Löschen");
System.out.print("Tabelle: ");
String table = sc.nextLine();
System.out.print("Id: ");
long id = sc.nextLong();
sc.nextLine();
Database.delete(table,id);
}
public static void printConsole(List<String> list){
for (String entry : list) {
System.out.println(entry);
@ -218,4 +234,32 @@ public class ConsoleLib {
}
printConsole(food);
}
public static void createFood_selectionPrompt(){
System.out.println("Essensauswahl");
Scanner sc = new Scanner(System.in);
String[] food_selectionData = new String[3];
System.out.print("Kind ID: ");
food_selectionData[0] = sc.nextLine();
System.out.print("Datum: ");
String date = sc.nextLine();
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.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.print("Id: ");
food_selectionData[2] = sc.nextLine();
if(FoodMgr.createFood_selection(food_selectionData) < 1){
System.out.println("Fehler");
}
}
}

View File

@ -1,6 +1,8 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.List;
import java.util.Scanner;
public class ConsoleMain {
@ -38,18 +40,14 @@ public class ConsoleMain {
System.out.print("Auswahl: ");
Scanner sc = new Scanner(System.in);
String selection = sc.nextLine();
switch (selection){
case "1":
switch (selection) {
case "1" -> {
currentUserId = ConsoleLib.loginPrompt();
isWorker = AccountMgr.isWorker(String.valueOf(currentUserId));
isParent = AccountMgr.isParent(String.valueOf(currentUserId));
break;
case "2":
ConsoleLib.showFood_planPrompt();
break;
case "3":
currentUserId = -2;
break;
}
case "2" -> ConsoleLib.showFood_planPrompt();
case "3" -> currentUserId = -2;
}
}
@ -63,43 +61,31 @@ public class ConsoleMain {
System.out.println("6: Table");
System.out.println("7: Einen Essensplan erstellen");
System.out.println("8: Essensplan anzeigen");
System.out.println("9: Löschen");
System.out.println("10: Essen auswählen");
System.out.print("Auswahl: ");
Scanner sc = new Scanner(System.in);
String selection = sc.nextLine();
switch (selection){
case "0":
switch (selection) {
case "0" -> {
currentUserId = -1;
isWorker = false;
isParent = false;
break;
case "1":
ConsoleLib.createWorkerPrompt();
break;
case "2":
ConsoleLib.createParentPrompt();
break;
case "3":
ConsoleLib.createChildPrompt(String.valueOf(currentUserId));
break;
case "4":
ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId));
break;
case "5":
ConsoleLib.createFoodPrompt();
break;
case "6":
ConsoleLib.tablePrompt();
break;
case "7":
ConsoleLib.createFood_planPrompt();
break;
case "8":
ConsoleLib.showFood_planPrompt();
break;
default:
break;
}
case "1" -> ConsoleLib.createWorkerPrompt();
case "2" -> ConsoleLib.createParentPrompt();
case "3" -> ConsoleLib.createChildPrompt(String.valueOf(currentUserId));
case "4" -> ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId));
case "5" -> ConsoleLib.createFoodPrompt();
case "6" -> ConsoleLib.tablePrompt();
case "7" -> ConsoleLib.createFood_planPrompt();
case "8" -> ConsoleLib.showFood_planPrompt();
case "9" -> ConsoleLib.deletePrompt();
case "10" -> ConsoleLib.createFood_selectionPrompt();
default -> {
}
}
}
@ -113,23 +99,17 @@ public class ConsoleMain {
System.out.print("Auswahl: ");
Scanner sc = new Scanner(System.in);
String selection = sc.nextLine();
switch (selection){
case "0":
switch (selection) {
case "0" -> {
currentUserId = -1;
isWorker = false;
isParent = false;
break;
case "3":
ConsoleLib.createChildPrompt(String.valueOf(currentUserId));
break;
case "4":
ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId));
break;
case "6":
ConsoleLib.tablePrompt();
break;
default:
break;
}
case "3" -> ConsoleLib.createChildPrompt(String.valueOf(currentUserId));
case "4" -> ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId));
case "6" -> ConsoleLib.tablePrompt();
default -> {
}
}
}

View File

@ -114,7 +114,7 @@ public class Database {
sql[11] = """
CREATE TABLE IF NOT EXISTS food_plan (
id integer PRIMARY KEY,
date text,
date text UNIQUE,
food1 integer,
food2 integer,
dessert1 integer,
@ -137,9 +137,10 @@ public class Database {
id integer PRIMARY KEY,
childid integer,
food_planid integer,
selection integer,
foodid integer,
FOREIGN KEY(childid) REFERENCES child(id),
FOREIGN KEY(food_planid) REFERENCES food_plan(id)
FOREIGN KEY(food_planid) REFERENCES food_plan(id),
FOREIGN KEY(foodid) REFERENCES food(id)
);""";
try(Connection conn = connect(); Statement stmt = conn.createStatement()){
for(int i = 0; i < sql.length; i++){
@ -330,11 +331,10 @@ public class Database {
}
}
protected static void deleteSample(){
String sql = """
DELETE FROM user WHERE id = ?;""";
protected static void delete(String table, long id){
String sql = "DELETE FROM " + table + " WHERE id = ?;";
try(Connection conn = connect();PreparedStatement ps = conn.prepareStatement(sql)){
ps.setInt(1,1);
ps.setLong(1,id);
ps.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
@ -344,8 +344,8 @@ public class Database {
private static String queryBuilder(String type,String table, String[] header, String[] values){
String sql;
StringBuilder sb = new StringBuilder();
switch (type){
case "exists":
switch (type) {
case "exists" -> {
sb = new StringBuilder("SELECT (count(*) > 0) as found FROM ");
sb.append(table);
sb.append(" WHERE ");
@ -353,15 +353,15 @@ public class Database {
sb.append(" LIKE '");
sb.append(values[0]);
sb.append("'");
for (int i = 1; i < header.length; i++){
for (int i = 1; i < header.length; i++) {
sb.append(" AND ");
sb.append(header[i]);
sb.append(" LIKE '");
sb.append(values[i]);
sb.append("'");
}
break;
case "selectMatch":
}
case "selectMatch" -> {
sb = new StringBuilder("SELECT * FROM ");
sb.append(table);
sb.append(" WHERE ");
@ -369,33 +369,33 @@ public class Database {
sb.append(" LIKE '");
sb.append(values[0]);
sb.append("'");
for (int i = 1; i < header.length; i++){
for (int i = 1; i < header.length; i++) {
sb.append(" AND ");
sb.append(header[i]);
sb.append(" LIKE '");
sb.append(values[i]);
sb.append("'");
}
break;
case "insert":
}
case "insert" -> {
sb = new StringBuilder("INSERT OR IGNORE INTO ");
sb.append(table);
sb.append(" (");
sb.append(header[0]);
for(int i = 1; i < header.length; i++){
for (int i = 1; i < header.length; i++) {
sb.append(",");
sb.append(header[i]);
}
sb.append(") VALUES('");
sb.append(values[0]);
sb.append("'");
for (int i = 1; i < values.length; i++){
for (int i = 1; i < values.length; i++) {
sb.append(",'");
sb.append(values[i]);
sb.append("'");
}
sb.append(");");
break;
}
}
sql = sb.toString();
return sql;
@ -427,7 +427,7 @@ public class Database {
protected static List<String> select(String table,String[] header, String[] values){
List<String> data = new ArrayList<>();
StringBuilder sb = new StringBuilder();
StringBuilder sb;
String sql = queryBuilder("selectMatch",table,header,values);
try(Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);

View File

@ -1,4 +1,7 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.List;
@ -43,4 +46,9 @@ public class FoodMgr {
public static List<String> getFoodById(long id){
return Database.getEntryById("food",id);
}
public static long createFood_selection(String[] food_selectionData){
String[] food_selectionH = {"childid","foodplanid","foodid"};
return Database.insert("food_selection",food_selectionH,food_selectionData);
}
}