MaltesConsolenEcke #2

Merged
PBS2H21AKA merged 4 commits from MaltesConsolenEcke into main 2023-01-12 15:59:05 +01:00
5 changed files with 114 additions and 85 deletions
Showing only changes of commit 58fb02875a - Show all commits

View File

@ -1,18 +1,15 @@
package com.bib.essensbestellungsverwaltung; 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 @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 { public class AccountMgr {
/** /**
* creates a user with createUser(...) and adds its id to the 'worker' table * 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 //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}; 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); KeySpec spec = new PBEKeySpec(pw.toCharArray(), magicSalt,310001,256);
String hashedPw = null; String hashedPw;
try { try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = factory.generateSecret(spec).getEncoded(); byte[] hash = factory.generateSecret(spec).getEncoded();

View File

@ -1,4 +1,7 @@
package com.bib.essensbestellungsverwaltung; package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -116,7 +119,9 @@ public class ConsoleLib {
System.out.print("Allergienummer: "); System.out.print("Allergienummer: ");
String allergies = sc.nextLine(); String allergies = sc.nextLine();
String[] allergyData = allergies.split(","); String[] allergyData = allergies.split(",");
FoodMgr.createFood(foodData,allergyData); if(FoodMgr.createFood(foodData,allergyData) < 1){
System.out.println("Fehler");
}
} }
public static long loginPrompt(){ public static long loginPrompt(){
@ -155,6 +160,17 @@ public class ConsoleLib {
printConsole(Database.getTable(table)); 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){ public static void printConsole(List<String> list){
for (String entry : list) { for (String entry : list) {
System.out.println(entry); System.out.println(entry);
@ -218,4 +234,32 @@ public class ConsoleLib {
} }
printConsole(food); 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; package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.List;
import java.util.Scanner; import java.util.Scanner;
public class ConsoleMain { public class ConsoleMain {
@ -38,18 +40,14 @@ public class ConsoleMain {
System.out.print("Auswahl: "); System.out.print("Auswahl: ");
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
String selection = sc.nextLine(); String selection = sc.nextLine();
switch (selection){ switch (selection) {
case "1": case "1" -> {
currentUserId = ConsoleLib.loginPrompt(); currentUserId = ConsoleLib.loginPrompt();
isWorker = AccountMgr.isWorker(String.valueOf(currentUserId)); isWorker = AccountMgr.isWorker(String.valueOf(currentUserId));
isParent = AccountMgr.isParent(String.valueOf(currentUserId)); isParent = AccountMgr.isParent(String.valueOf(currentUserId));
break; }
case "2": case "2" -> ConsoleLib.showFood_planPrompt();
ConsoleLib.showFood_planPrompt(); case "3" -> currentUserId = -2;
break;
case "3":
currentUserId = -2;
break;
} }
} }
@ -63,43 +61,31 @@ public class ConsoleMain {
System.out.println("6: Table"); System.out.println("6: Table");
System.out.println("7: Einen Essensplan erstellen"); System.out.println("7: Einen Essensplan erstellen");
System.out.println("8: Essensplan anzeigen"); System.out.println("8: Essensplan anzeigen");
System.out.println("9: Löschen");
System.out.println("10: Essen auswählen");
System.out.print("Auswahl: "); System.out.print("Auswahl: ");
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
String selection = sc.nextLine(); String selection = sc.nextLine();
switch (selection){ switch (selection) {
case "0": case "0" -> {
currentUserId = -1; currentUserId = -1;
isWorker = false; isWorker = false;
isParent = false; isParent = false;
break; }
case "1": case "1" -> ConsoleLib.createWorkerPrompt();
ConsoleLib.createWorkerPrompt(); case "2" -> ConsoleLib.createParentPrompt();
break; case "3" -> ConsoleLib.createChildPrompt(String.valueOf(currentUserId));
case "2": case "4" -> ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId));
ConsoleLib.createParentPrompt(); case "5" -> ConsoleLib.createFoodPrompt();
break; case "6" -> ConsoleLib.tablePrompt();
case "3": case "7" -> ConsoleLib.createFood_planPrompt();
ConsoleLib.createChildPrompt(String.valueOf(currentUserId)); case "8" -> ConsoleLib.showFood_planPrompt();
break; case "9" -> ConsoleLib.deletePrompt();
case "4": case "10" -> ConsoleLib.createFood_selectionPrompt();
ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId)); default -> {
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;
} }
} }
@ -113,23 +99,17 @@ public class ConsoleMain {
System.out.print("Auswahl: "); System.out.print("Auswahl: ");
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
String selection = sc.nextLine(); String selection = sc.nextLine();
switch (selection){ switch (selection) {
case "0": case "0" -> {
currentUserId = -1; currentUserId = -1;
isWorker = false; isWorker = false;
isParent = false; isParent = false;
break; }
case "3": case "3" -> ConsoleLib.createChildPrompt(String.valueOf(currentUserId));
ConsoleLib.createChildPrompt(String.valueOf(currentUserId)); case "4" -> ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId));
break; case "6" -> ConsoleLib.tablePrompt();
case "4": default -> {
ConsoleLib.matchParentChildPrompt(String.valueOf(currentUserId)); }
break;
case "6":
ConsoleLib.tablePrompt();
break;
default:
break;
} }
} }

View File

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

View File

@ -1,4 +1,7 @@
package com.bib.essensbestellungsverwaltung; package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.List; import java.util.List;
@ -43,4 +46,9 @@ public class FoodMgr {
public static List<String> getFoodById(long id){ public static List<String> getFoodById(long id){
return Database.getEntryById("food",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);
}
} }