MaltesConsolenEcke #2
@ -0,0 +1,93 @@
|
||||
package com.bib.essensbestellungsverwaltung;
|
||||
/*
|
||||
@author Malte Schulze Hobeling
|
||||
*/
|
||||
public class AccountMgr {
|
||||
/**
|
||||
* creates a user with createUser(...) and adds its id to the 'worker' table
|
||||
* @param userData String[] name, firstname, password, email
|
||||
* @param addressData String[] street, number, plz, city
|
||||
* @return userid or -1
|
||||
*/
|
||||
public static long createWorker(String[] userData, String[] addressData){
|
||||
long id = createUser(userData, addressData);
|
||||
String sId = String.valueOf(id);
|
||||
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 userData String[] name, firstname, password, email
|
||||
* @param addressData String[] street, number, plz, city
|
||||
* @return userid or -1
|
||||
*/
|
||||
public static long createParent(String[] userData, String[] addressData){
|
||||
long id = createUser(userData, addressData);
|
||||
String sId = String.valueOf(id);
|
||||
Database.insert("parent", new String[]{"userid"}, new String[]{sId});
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a user to the database
|
||||
* @param userData String[] name, firstname, password, email
|
||||
* @param addressData String[] street, number, plz, city
|
||||
* @return userid or -1
|
||||
*/
|
||||
private static long createUser(String[] userData, String[] addressData) {
|
||||
String[] addressH = {"street", "number", "plz", "city"};
|
||||
String[] userH = {"name", "firstname", "addressid", "password", "email"};
|
||||
String name = userData[0];
|
||||
String firstname = userData[1];
|
||||
String pw = userData[2];
|
||||
String email = userData[3];
|
||||
|
||||
long id = Database.insert("address", addressH, addressData);
|
||||
String sId = String.valueOf(id);
|
||||
String[] userD = {name, firstname, sId, pw, email};
|
||||
id = Database.insert("user", userH, userD);
|
||||
return id;
|
||||
}
|
||||
|
||||
public static long createChild(String[] childData,String[] allergyData, String[] severityData){
|
||||
String[] childH = {"name","firstname","addressid"};
|
||||
String[] child_allergyH = {"childid","allergyid","severityid"};
|
||||
long id = Database.insert("child", childH, childData);
|
||||
if(allergyData.length > 0){
|
||||
for(int i = 0; i < allergyData.length; i++){
|
||||
String sId = String.valueOf(id);
|
||||
String[] child_allergyD = {sId,allergyData[i],severityData[i]};
|
||||
Database.insert("child_allergy",child_allergyH,child_allergyD);
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public 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);
|
||||
}
|
||||
|
||||
public static long login(String email, String pw){
|
||||
String[] userH = {"email","password"};
|
||||
String[] userD = {email,pw};
|
||||
return Database.getSingleId("user",userH,userD);
|
||||
}
|
||||
|
||||
public static boolean isWorker(String id){
|
||||
String[] workerH = {"userid"};
|
||||
String[] workerD = {id};
|
||||
long workerId = Database.getSingleId("worker",workerH,workerD);
|
||||
return workerId > 0;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isParent(String id){
|
||||
String[] parentH = {"userid"};
|
||||
String[] parentD = {id};
|
||||
long parentId = Database.getSingleId("parent",parentH,parentD);
|
||||
return parentId > 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,221 @@
|
||||
package com.bib.essensbestellungsverwaltung;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleLib {
|
||||
public static void createWorkerPrompt(){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Registrierung eines neuen Mitarbeiters");
|
||||
String[] userData = new String[4];
|
||||
String[] addressData = new String[4];
|
||||
System.out.print("Nachname: ");
|
||||
userData[0] = sc.nextLine();
|
||||
System.out.print("Vorname: ");
|
||||
userData[1] = sc.nextLine();
|
||||
System.out.print("Straße: ");
|
||||
addressData[0] = sc.nextLine();
|
||||
System.out.print("Hausnummer: ");
|
||||
addressData[1] = sc.nextLine();
|
||||
System.out.print("Postleitzahl: ");
|
||||
addressData[2] = sc.nextLine();
|
||||
System.out.print("Stadt: ");
|
||||
addressData[3] = sc.nextLine();
|
||||
System.out.print("Email: ");
|
||||
userData[3] = sc.nextLine();
|
||||
System.out.print("Passwort: ");
|
||||
userData[2] = sc.nextLine();
|
||||
long id = AccountMgr.createWorker(userData,addressData);
|
||||
if(id < 1){
|
||||
System.out.println("Fehler beim erstellen");
|
||||
}
|
||||
}
|
||||
|
||||
public static void createParentPrompt(){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Registrierung eines neuen Elternteils");
|
||||
String[] userData = new String[4];
|
||||
String[] addressData = new String[4];
|
||||
System.out.print("Nachname: ");
|
||||
userData[0] = sc.nextLine();
|
||||
System.out.print("Vorname: ");
|
||||
userData[1] = sc.nextLine();
|
||||
System.out.print("Straße: ");
|
||||
addressData[0] = sc.nextLine();
|
||||
System.out.print("Hausnummer: ");
|
||||
addressData[1] = sc.nextLine();
|
||||
System.out.print("Postleitzahl: ");
|
||||
addressData[2] = sc.nextLine();
|
||||
System.out.print("Stadt: ");
|
||||
addressData[3] = sc.nextLine();
|
||||
System.out.print("Email: ");
|
||||
userData[3] = sc.nextLine();
|
||||
System.out.print("Passwort: ");
|
||||
userData[2] = sc.nextLine();
|
||||
long id = AccountMgr.createParent(userData,addressData);
|
||||
if(id < 1){
|
||||
System.out.println("Fehler beim erstellen");
|
||||
}
|
||||
}
|
||||
|
||||
public static void createChildPrompt(String parentId){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String[] childData = new String[3];
|
||||
System.out.println("Registrierung eines neuen Kindes");
|
||||
System.out.print("Nachname: ");
|
||||
childData[0] = sc.nextLine();
|
||||
System.out.print("Vorname: ");
|
||||
childData[1] = sc.nextLine();
|
||||
System.out.println("Bitte geben Sie die Nummer der passenden Adresse an: ");
|
||||
for (String s : Database.getTable("address")) {
|
||||
String[] parts = s.split(":");
|
||||
System.out.printf("Nr.: %s Straße: %s Hausnr.: %s PLZ: %s Stadt: %s%n",parts[0],parts[1],parts[2],parts[3],parts[4]);
|
||||
}
|
||||
System.out.print("Adressnummer: ");
|
||||
childData[2] = sc.nextLine();
|
||||
for (String s : Database.getTable("allergy")) {
|
||||
String[] parts = s.split(":");
|
||||
System.out.printf("Nr. %s %s%n",parts[0],parts[1]);
|
||||
}
|
||||
System.out.println("Bitte Geben Sie die Nr der Allergien und Ihre Schwere an: ");
|
||||
System.out.print("Allergien (Nr mit , getrennt[1,4,5,16]): ");
|
||||
String allergies = sc.nextLine();
|
||||
String[] allergyData = allergies.split(",");
|
||||
System.out.print("Schweren (1 Harmlos - 3 Kritisch[2,3,1,3]): ");
|
||||
String severities = sc.nextLine();
|
||||
String[] severityData = severities.split(",");
|
||||
long id = AccountMgr.createChild(childData,allergyData,severityData);
|
||||
if(id < 1){
|
||||
System.out.println("Fehler beim erstellen");
|
||||
return;
|
||||
}
|
||||
String sId = String.valueOf(id);
|
||||
if(AccountMgr.matchParentChild(parentId,sId) == -1){
|
||||
System.out.println("Fehler beim verknüpfen");
|
||||
}
|
||||
}
|
||||
|
||||
public static void createFoodPrompt(){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String[] foodData = new String[4];
|
||||
System.out.println("Registrierung eines neuen Essens");
|
||||
System.out.print("Name: ");
|
||||
foodData[0] = sc.nextLine();
|
||||
System.out.print("Beschreibung: ");
|
||||
foodData[1] = sc.nextLine();
|
||||
System.out.print("Ist es ein Dessert?[0/1]: ");
|
||||
foodData[2] = sc.nextLine();
|
||||
System.out.print("Ist es vegan[1], vegetarisch[2] oder fleischhaltig[3]: ");
|
||||
foodData[3] = sc.nextLine();
|
||||
for (String s : Database.getTable("allergy")) {
|
||||
String[] parts = s.split(":");
|
||||
System.out.printf("Nr. %s %s%n",parts[0],parts[1]);
|
||||
}
|
||||
System.out.println("Bitte geben Sie die Nr. aller zutreffenden Allergien mit Komma getrennt an [1,3,6]");
|
||||
System.out.print("Allergienummer: ");
|
||||
String allergies = sc.nextLine();
|
||||
String[] allergyData = allergies.split(",");
|
||||
FoodMgr.createFood(foodData,allergyData);
|
||||
}
|
||||
|
||||
public static long loginPrompt(){
|
||||
System.out.println("Login");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
long id = -1;
|
||||
while (id == -1){
|
||||
System.out.print("Email: ");
|
||||
String email = sc.nextLine();
|
||||
System.out.print("Passwort: ");
|
||||
String pw = sc.nextLine();
|
||||
id = AccountMgr.login(email,pw);
|
||||
if(id == -1){
|
||||
System.out.println("Login fehlgeschlagen");
|
||||
}
|
||||
}
|
||||
System.out.println("Login erfolgreich");
|
||||
return id;
|
||||
}
|
||||
|
||||
public static void matchParentChildPrompt(String parentId){
|
||||
System.out.println("Wählen Sie ihr Kind aus: ");
|
||||
Database.getTable("child");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Nr: ");
|
||||
String childId = sc.nextLine();
|
||||
if(AccountMgr.matchParentChild(parentId,childId) == -1){
|
||||
System.out.println("Fehler");
|
||||
}
|
||||
}
|
||||
|
||||
public static void tablePrompt(){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Table: ");
|
||||
String table = sc.nextLine();
|
||||
printConsole(Database.getTable(table));
|
||||
}
|
||||
|
||||
public static void printConsole(List<String> list){
|
||||
for (String entry : list) {
|
||||
System.out.println(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public static void createFood_planPrompt(){
|
||||
System.out.println("Erstellen eines Essensplans");
|
||||
String[] food_planData = new String[5];
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Bitte geben Sie das Datum im Format YYYY-MM-DD an: ");
|
||||
food_planData[0] = sc.nextLine();
|
||||
printConsole(FoodMgr.getVeganFood(false));
|
||||
System.out.print("Veganes Hauptgericht Nr: ");
|
||||
food_planData[1] = sc.nextLine();
|
||||
printConsole(FoodMgr.getFood(false));
|
||||
System.out.print("Zweites Hauptgericht Nr: ");
|
||||
food_planData[2] = sc.nextLine();
|
||||
printConsole(FoodMgr.getVeganFood(true));
|
||||
System.out.print("Veganes Dessert Nr: ");
|
||||
food_planData[3] = sc.nextLine();
|
||||
printConsole(FoodMgr.getFood(true));
|
||||
System.out.print("Zweites Dessert Nr: ");
|
||||
food_planData[4] = sc.nextLine();
|
||||
long id = FoodMgr.createFood_plan(food_planData);
|
||||
if(id < 0){
|
||||
System.out.println("Fehler");
|
||||
}
|
||||
}
|
||||
|
||||
public static void showFood_planPrompt(){
|
||||
System.out.println("Essensplan zum Anzeigen auswählen");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Bitte geben Sie das Datum im Format YYYY-MM-DD an: ");
|
||||
String date = sc.nextLine();
|
||||
List<String> plan = FoodMgr.getFood_plan(date);
|
||||
List<String> food = new ArrayList<>();
|
||||
StringBuilder sb;
|
||||
for (String day : plan) {
|
||||
sb = new StringBuilder();
|
||||
String[] parts = day.split(":");
|
||||
sb.append("Tag: ");
|
||||
sb.append(parts[1]);
|
||||
sb.append(" Veganesgericht: ");
|
||||
food = FoodMgr.getFoodById(Long.parseLong(parts[2]));
|
||||
String[] foodParts = food.get(0).split(":");
|
||||
sb.append(foodParts[1]);
|
||||
sb.append(" Zweites Hauptgericht: ");
|
||||
food = FoodMgr.getFoodById(Long.parseLong(parts[3]));
|
||||
foodParts = food.get(0).split(":");
|
||||
sb.append(foodParts[1]);
|
||||
sb.append(" Veganesdessert: ");
|
||||
food = FoodMgr.getFoodById(Long.parseLong(parts[4]));
|
||||
foodParts = food.get(0).split(":");
|
||||
sb.append(foodParts[1]);
|
||||
sb.append(" Zweites Dessert: ");
|
||||
food = FoodMgr.getFoodById(Long.parseLong(parts[5]));
|
||||
foodParts = food.get(0).split(":");
|
||||
sb.append(foodParts[1]);
|
||||
food.add(sb.toString());
|
||||
}
|
||||
printConsole(food);
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
package com.bib.essensbestellungsverwaltung;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleMain {
|
||||
static long currentUserId = -1;
|
||||
static boolean isWorker = false;
|
||||
static boolean isParent = false;
|
||||
public static void main(String[] args) {
|
||||
boolean firstRun = Database.init();
|
||||
Database.createDb();
|
||||
Database.fillDb();
|
||||
//Database.printSampleQuery();
|
||||
if(firstRun){
|
||||
ConsoleLib.createWorkerPrompt();
|
||||
}
|
||||
while (true){
|
||||
if(currentUserId == -2){
|
||||
break;
|
||||
}else if(currentUserId < 0){
|
||||
defaultMenu();
|
||||
}else{
|
||||
if(isWorker){
|
||||
adminMenu();
|
||||
}else if(isParent){
|
||||
parentMenu();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void defaultMenu(){
|
||||
System.out.println("1: Login");
|
||||
System.out.println("2: Essensplan anzeigen");
|
||||
System.out.println("3: Programm beenden");
|
||||
|
||||
System.out.print("Auswahl: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String selection = sc.nextLine();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public static void adminMenu(){
|
||||
System.out.println("0: Ausloggen");
|
||||
System.out.println("1: Einen neuen Mitarbeiter anlegen");
|
||||
System.out.println("2: Ein neues Elternteil anlegen");
|
||||
System.out.println("3: Ein neues Kind anlegen");
|
||||
System.out.println("4: Kind einem Elternteil zuordnen");
|
||||
System.out.println("5: Ein neues Essen anlegen");
|
||||
System.out.println("6: Table");
|
||||
System.out.println("7: Einen Essensplan erstellen");
|
||||
System.out.println("8: Essensplan anzeigen");
|
||||
|
||||
|
||||
System.out.print("Auswahl: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String selection = sc.nextLine();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void parentMenu(){
|
||||
System.out.println("0: Ausloggen");
|
||||
System.out.println("3: Ein neues Kind anlegen");
|
||||
System.out.println("4: Kind einem Elternteil zuordnen");
|
||||
|
||||
|
||||
System.out.print("Auswahl: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String selection = sc.nextLine();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -13,10 +13,10 @@ import java.util.List;
|
||||
|
||||
public class Database {
|
||||
private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db";
|
||||
protected static void init(){
|
||||
protected static boolean init(){
|
||||
File db = new File(Path.of("").toAbsolutePath()+"/database.db");
|
||||
try {
|
||||
db.createNewFile();
|
||||
return db.createNewFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -44,17 +44,17 @@ public class Database {
|
||||
sql[1] = """
|
||||
CREATE TABLE IF NOT EXISTS food_type (
|
||||
id integer PRIMARY KEY,
|
||||
name text
|
||||
name text UNIQUE
|
||||
);""";
|
||||
sql[2] = """
|
||||
CREATE TABLE IF NOT EXISTS allergy (
|
||||
id integer PRIMARY KEY,
|
||||
name text
|
||||
name text UNIQUE
|
||||
);""";
|
||||
sql[3] = """
|
||||
CREATE TABLE IF NOT EXISTS severity (
|
||||
id integer PRIMARY KEY,
|
||||
severity integer
|
||||
name text UNIQUE
|
||||
);""";
|
||||
sql[4] = """
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
@ -105,7 +105,7 @@ public class Database {
|
||||
sql[10] = """
|
||||
CREATE TABLE IF NOT EXISTS food (
|
||||
id integer PRIMARY KEY,
|
||||
name text,
|
||||
name text UNIQUE,
|
||||
description text,
|
||||
isdessert integer,
|
||||
food_typeid integer,
|
||||
@ -125,7 +125,7 @@ public class Database {
|
||||
FOREIGN KEY(dessert2) REFERENCES food(id)
|
||||
);""";
|
||||
sql[12] = """
|
||||
CREATE TABLE IF NOT EXISTS food_restrictions (
|
||||
CREATE TABLE IF NOT EXISTS food_restriction (
|
||||
id integer PRIMARY KEY,
|
||||
foodid integer,
|
||||
allergyid integer,
|
||||
@ -150,7 +150,7 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
protected static void fillSampleDb(){
|
||||
protected static void fillDb(){
|
||||
List<String> sqls = new ArrayList<>();
|
||||
sqls.add("""
|
||||
INSERT OR IGNORE INTO food_type (id,name)
|
||||
@ -227,6 +227,15 @@ public class Database {
|
||||
sqls.add("""
|
||||
INSERT OR IGNORE INTO allergy (id,name)
|
||||
VALUES('22','Konservierungsstoff');""");
|
||||
sqls.add("""
|
||||
INSERT OR IGNORE INTO severity (id,name)
|
||||
VALUES('1','Harmlos');""");
|
||||
sqls.add("""
|
||||
INSERT OR IGNORE INTO severity (id,name)
|
||||
VALUES('2','Warnung');""");
|
||||
sqls.add("""
|
||||
INSERT OR IGNORE INTO severity (id,name)
|
||||
VALUES('3','Kritisch');""");
|
||||
try(Connection conn = connect(); Statement stmt = conn.createStatement()){
|
||||
for (String sql : sqls) {
|
||||
stmt.execute(sql);
|
||||
@ -236,16 +245,65 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean insert(String table, String header, String values){
|
||||
try (Connection conn = connect(); Statement stmt = conn.createStatement()){
|
||||
String sql = "INSERT OR IGNORE INTO " + table + " (" + header + ") VALUES(" + values + ");";
|
||||
stmt.execute(sql);
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
/**
|
||||
* inserts data into table and returns its id
|
||||
* @param table name of the database table
|
||||
* @param header String[] order should match with values
|
||||
* @param values String[] order should match with header
|
||||
* @return id of dataset or -1
|
||||
*/
|
||||
protected static long insert(String table, String[] header, String[] values){
|
||||
long id = -1;
|
||||
try (Connection conn = connect()){
|
||||
String query = queryBuilder("exists",table,header,values);
|
||||
PreparedStatement psQuery = conn.prepareStatement(query);
|
||||
ResultSet rsQuery = psQuery.executeQuery();
|
||||
if(rsQuery.next()){
|
||||
boolean found = rsQuery.getBoolean(1);
|
||||
if(!found){
|
||||
String sql = queryBuilder("insert",table,header,values);
|
||||
String[] rowId = {"id"};
|
||||
PreparedStatement ps = conn.prepareStatement(sql,rowId);
|
||||
ps.execute();
|
||||
ResultSet rs = ps.getGeneratedKeys();
|
||||
if(rs.next()){
|
||||
id = rs.getLong(1);
|
||||
}
|
||||
|
||||
}else{
|
||||
query = queryBuilder("selectMatch",table,header,values);
|
||||
psQuery = conn.prepareStatement(query);
|
||||
rsQuery = psQuery.executeQuery();
|
||||
if(rsQuery.next()) {
|
||||
id = rsQuery.getLong(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
return id;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
protected static long getSingleId(String table, String[] header, String[] values){
|
||||
long id = -1;
|
||||
try(Connection conn = connect()){
|
||||
String sql = queryBuilder("selectMatch",table,header,values);
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if(rs.next()){
|
||||
id = rs.getLong(1);
|
||||
if(rs.next()){
|
||||
id = -1;
|
||||
}
|
||||
}
|
||||
}catch (SQLException e){
|
||||
e.printStackTrace();
|
||||
return id;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
protected static void printSampleQuery(){
|
||||
String sql = """
|
||||
@ -253,17 +311,17 @@ public class Database {
|
||||
String sql1 = """
|
||||
SELECT * FROM allergy WHERE id > ?;""";
|
||||
try(Connection conn = connect()){
|
||||
PreparedStatement pstmt = conn.prepareStatement(sql);
|
||||
PreparedStatement pstmt1 = conn.prepareStatement(sql1);
|
||||
pstmt.setInt(1,0);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
PreparedStatement ps1 = conn.prepareStatement(sql1);
|
||||
ps.setInt(1,0);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
System.out.println("food_type");
|
||||
while (rs.next()){
|
||||
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
|
||||
}
|
||||
System.out.println("allergy");
|
||||
pstmt1.setInt(1,0);
|
||||
rs = pstmt1.executeQuery();
|
||||
ps1.setInt(1,0);
|
||||
rs = ps1.executeQuery();
|
||||
while (rs.next()){
|
||||
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
|
||||
}
|
||||
@ -275,32 +333,146 @@ public class Database {
|
||||
protected static void deleteSample(){
|
||||
String sql = """
|
||||
DELETE FROM user WHERE id = ?;""";
|
||||
try(Connection conn = connect();PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||
pstmt.setInt(1,1);
|
||||
pstmt.executeUpdate();
|
||||
try(Connection conn = connect();PreparedStatement ps = conn.prepareStatement(sql)){
|
||||
ps.setInt(1,1);
|
||||
ps.executeUpdate();
|
||||
}catch (SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/* String sql = """
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
id integer PRIMARY KEY,
|
||||
name text);""";
|
||||
String sql2 = "SELECT * FROM user WHERE id > ?";
|
||||
String sql3 = "INSERT INTO user (id,name) VALUES (1,'test1')";
|
||||
try(Connection conn = connect();
|
||||
Statement stmt = conn.createStatement()){
|
||||
stmt.execute(sql);
|
||||
stmt.execute(sql3);
|
||||
PreparedStatement pstmt = conn.prepareStatement(sql2);
|
||||
pstmt.setInt(1,0);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
private static String queryBuilder(String type,String table, String[] header, String[] values){
|
||||
String sql;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
switch (type){
|
||||
case "exists":
|
||||
sb = new StringBuilder("SELECT (count(*) > 0) as found FROM ");
|
||||
sb.append(table);
|
||||
sb.append(" WHERE ");
|
||||
sb.append(header[0]);
|
||||
sb.append(" LIKE '");
|
||||
sb.append(values[0]);
|
||||
sb.append("'");
|
||||
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":
|
||||
sb = new StringBuilder("SELECT * FROM ");
|
||||
sb.append(table);
|
||||
sb.append(" WHERE ");
|
||||
sb.append(header[0]);
|
||||
sb.append(" LIKE '");
|
||||
sb.append(values[0]);
|
||||
sb.append("'");
|
||||
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":
|
||||
sb = new StringBuilder("INSERT OR IGNORE INTO ");
|
||||
sb.append(table);
|
||||
sb.append(" (");
|
||||
sb.append(header[0]);
|
||||
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++){
|
||||
sb.append(",'");
|
||||
sb.append(values[i]);
|
||||
sb.append("'");
|
||||
}
|
||||
sb.append(");");
|
||||
break;
|
||||
}
|
||||
sql = sb.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
protected static List<String> getTable(String table){
|
||||
List<String> data = new ArrayList<>();
|
||||
StringBuilder sb;
|
||||
try(Connection conn = connect()) {
|
||||
String sql = "SELECT * FROM " + table;
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int count = rsmd.getColumnCount();
|
||||
while (rs.next()){
|
||||
System.out.println(rs.getInt("id"));
|
||||
sb = new StringBuilder();
|
||||
sb.append(rs.getString(1));
|
||||
for(int i = 2; i <= count; i++){
|
||||
sb.append(":");
|
||||
sb.append(rs.getString(i));
|
||||
}
|
||||
data.add(sb.toString());
|
||||
}
|
||||
}catch (SQLException e){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
protected static List<String> select(String table,String[] header, String[] values){
|
||||
List<String> data = new ArrayList<>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String sql = queryBuilder("selectMatch",table,header,values);
|
||||
try(Connection conn = connect()) {
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int count = rsmd.getColumnCount();
|
||||
while (rs.next()){
|
||||
sb = new StringBuilder();
|
||||
sb.append(rs.getString(1));
|
||||
for(int i = 2; i <= count; i++){
|
||||
sb.append(":");
|
||||
sb.append(rs.getString(i));
|
||||
}
|
||||
data.add(sb.toString());
|
||||
}
|
||||
}catch (SQLException e){
|
||||
e.printStackTrace();
|
||||
return;
|
||||
} */
|
||||
return data;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
protected static List<String> getEntryById(String table, long id){
|
||||
List<String> data = new ArrayList<>();
|
||||
StringBuilder sb;
|
||||
String sql = "SELECT * FROM " + table + " WHERE id = ?;";
|
||||
try (Connection conn = connect()) {
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ps.setLong(1,id);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int count = rsmd.getColumnCount();
|
||||
while (rs.next()){
|
||||
sb = new StringBuilder();
|
||||
sb.append(rs.getString(1));
|
||||
for(int i = 2; i <= count; i++){
|
||||
sb.append(":");
|
||||
sb.append(rs.getString(i));
|
||||
}
|
||||
data.add(sb.toString());
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return data;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package com.bib.essensbestellungsverwaltung;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FoodMgr {
|
||||
public static long createFood(String[] foodData, String[] allergyData){
|
||||
String[] foodH = {"name","description","isDessert","food_typeid"};
|
||||
String[] food_restrictionH = {"foodid","allergyid"};
|
||||
long id = Database.insert("food",foodH,foodData);
|
||||
if(allergyData.length > 0){
|
||||
String sId = String.valueOf(id);
|
||||
for (String allergyId : allergyData) {
|
||||
String[] food_restrictionD = {sId,allergyId};
|
||||
Database.insert("food_restriction",food_restrictionH, food_restrictionD);
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public static long createFood_plan(String[] food_planData){
|
||||
String[] food_planH = {"date","food1","food2","dessert1","dessert2"};
|
||||
return Database.insert("food_plan",food_planH,food_planData);
|
||||
}
|
||||
|
||||
public static List<String> getFood(boolean isDessert){
|
||||
String[] foodH = {"isDessert"};
|
||||
String[] foodD = {(isDessert ? "1" : "0")};
|
||||
return Database.select("food",foodH,foodD);
|
||||
}
|
||||
|
||||
public static List<String> getVeganFood(boolean isDessert){
|
||||
String[] foodH = {"isDessert","food_typeid"};
|
||||
String[] foodD = {(isDessert ? "1" : "0"),"1"};
|
||||
return Database.select("food",foodH,foodD);
|
||||
}
|
||||
|
||||
public static List<String> getFood_plan(String date){
|
||||
String[] food_planH = {"date"};
|
||||
String[] food_planD = {date};
|
||||
return Database.select("food_plan",food_planH,food_planD);
|
||||
}
|
||||
|
||||
public static List<String> getFoodById(long id){
|
||||
return Database.getEntryById("food",id);
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ public class HelloApplication extends Application {
|
||||
public static void main(String[] args) {
|
||||
Database.init();
|
||||
Database.createDb();
|
||||
Database.fillSampleDb();
|
||||
Database.fillDb();
|
||||
Database.printSampleQuery();
|
||||
//Database.deleteSample();
|
||||
launch();
|
||||
|
@ -2,6 +2,6 @@ package com.bib.essensbestellungsverwaltung;
|
||||
|
||||
public class SuperMain {
|
||||
public static void main(String[] args) {
|
||||
HelloApplication.main(args);
|
||||
ConsoleMain.main(args);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user