Kommentare 2: electric boogaloo

This commit is contained in:
Malte Schulze Hobeling 2023-02-01 22:52:04 +01:00
parent 9b6d60a571
commit c022d68b92
16 changed files with 229 additions and 58 deletions

View File

@ -1,7 +1,4 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
@ -13,12 +10,18 @@ import java.util.ArrayList;
import java.util.Base64;
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 {
protected static double price = 5.0;
/**
* 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){
long id = createUser(worker);
@ -29,7 +32,9 @@ public class AccountMgr {
/**
* 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){
long id = createUser(parent);
@ -40,7 +45,9 @@ public class AccountMgr {
/**
* 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"};
@ -56,6 +63,12 @@ public class AccountMgr {
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()};
@ -64,7 +77,9 @@ public class AccountMgr {
/**
* 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"};
@ -81,8 +96,17 @@ public class AccountMgr {
return id;
}
/**
* 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<String> 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))){
@ -100,8 +124,17 @@ public class AccountMgr {
}
}
/**
* 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<String> 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)};
@ -117,8 +150,17 @@ public class AccountMgr {
return new Child(id,parts[1],parts[2],getAddressById(Long.parseLong(parts[3])),allergySeverities);
}
/**
* 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<String> 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]);
}
@ -128,6 +170,7 @@ public class AccountMgr {
* @param parentId id of parent
* @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"};
@ -140,6 +183,7 @@ public class AccountMgr {
* @param email email
* @param pw password
* @return id or -1
* @author Malte Schulze Hobeling
*/
protected static long login(String email, String pw){
String[] pwH = {"email"};
@ -163,6 +207,7 @@ public class AccountMgr {
* 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"};
@ -175,6 +220,7 @@ public class AccountMgr {
* 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"};
@ -187,6 +233,7 @@ public class AccountMgr {
* 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){
Base64.Decoder dec = Base64.getDecoder();
@ -205,6 +252,11 @@ public class AccountMgr {
return hashedPw;
}
/**
* generates a secure random salt, Base64 encoded
* @return String Base64 encoded
* @author Malte Schulze Hobeling
*/
private static String getSalt(){
SecureRandom sec = new SecureRandom();
byte[] salt = new byte[16];
@ -218,11 +270,15 @@ public class AccountMgr {
* @param date YYYY-MM the month
* @param childId id of child
* @return the invoice as a List
* @author Malte Schulze Hobeling
*/
protected static List<String> getInvoice(String date, String childId){
List<String> invoice = new ArrayList<>();
invoice.add("Monatsabrechnung " + date);
List<String> child = Database.getEntryById("child", Long.parseLong(childId));
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"};
@ -241,18 +297,29 @@ public class AccountMgr {
invoice.add(line);
}
}
double price = getPrice();
invoice.add("Total: " + (invoice.size()-2) + " X " + price + "€ = " + ((invoice.size()-2)*price) + "");
return invoice;
}
protected static void getPriceFromDb(){
/**
* 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<String> priceEntry = Database.getEntryById("price",1);
price = Double.parseDouble(priceEntry.get(0).split(":")[1])/100.0;
return Double.parseDouble(priceEntry.get(0).split(":")[1])/100.0;
}
protected static void setPriceInDb(){
/**
* 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(price*100)};
String[] priceD = {"1", String.valueOf((int)(price*100))};
Database.update("price",priceH,priceD);
}
}

View File

@ -1,8 +1,10 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
/**
* An Address, used by User and Child and intended as a way to identify a child if you want to match it to another parent
* one constructor is used for creating new addresses the other is used to create existing addresses from database
* @author Malte Schulze Hobeling
*/
public class Address {
private long id;
private String street;

View File

@ -1,8 +1,9 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
/**
* An Allergy, used by Food and AllergySeverity, not intended to be changed
* @author Malte Schulze Hobeling
*/
public class Allergy {
private final long id;
private final String name;

View File

@ -1,8 +1,9 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
/**
* Matching an Allergy to a Severity, used by Child
* @author Malte Schulze Hobeling
*/
public class AllergySeverity {
private Allergy allergy;
private long severityId;

View File

@ -1,10 +1,12 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.List;
/**
* A Child, used by Parent
* one constructor is used to create new children the other is used to create existing children from database
* @author Malte Schulze Hobeling
*/
public class Child {
private long id;
private String name;

View File

@ -1,12 +1,13 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* library for cli functions
* @author Malte Schulze Hobeling
*/
public class ConsoleLib {
public static void createWorkerPrompt(){
Scanner sc = new Scanner(System.in);
@ -320,7 +321,6 @@ public class ConsoleLib {
Scanner sc = new Scanner(System.in);
double price = sc.nextDouble();
sc.nextLine();
AccountMgr.price = price;
AccountMgr.setPriceInDb();
AccountMgr.setPrice(price);
}
}

View File

@ -1,10 +1,11 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.Scanner;
/**
* simple cli showcase of the implemented functionality
* @author Malte Schulze Hobeling
*/
public class ConsoleMain {
static User currentUser = null;
static boolean running = true;
@ -15,7 +16,7 @@ public class ConsoleMain {
Database.fillDb();
ConsoleLib.createWorkerPrompt();
}
AccountMgr.getPriceFromDb();
AccountMgr.getPrice();
while (running){
if(currentUser == null){
defaultMenu();

View File

@ -1,7 +1,4 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.io.File;
import java.io.IOException;
@ -10,13 +7,19 @@ import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Basic operations on the database
* Use init() -> createDb() -> fillDb() to create the skeleton with some default values
* Provides select, insert, update, delete, count operations and more
* @author Malte Schulze Hobeling
*/
public class Database {
private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db";
/**
* creates new database.db if it doesn't exist
* @return true if a new database has been created
* @author Malte Schulze Hobeling
*/
protected static boolean init(){
File db = new File(Path.of("").toAbsolutePath()+"/database.db");
@ -30,6 +33,7 @@ public class Database {
/**
* connects to the database
* @return Connection to the database
* @author Malte Schulze Hobeling
*/
protected static Connection connect(){
Connection conn = null;
@ -43,6 +47,7 @@ public class Database {
/**
* creates the initial structure of the db
* @author Malte Schulze Hobeling
*/
protected static void createDb(){
String[] sql = new String[15];
@ -173,6 +178,7 @@ public class Database {
/**
* inserts fixed values into the database
* @author Malte Schulze Hobeling
*/
protected static void fillDb(){
List<String> sqls = new ArrayList<>();
@ -274,11 +280,12 @@ public class Database {
/**
* inserts data into table and returns its id
* does not insert if the exact set already exists
* simple duplication check
* @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
* @author Malte Schulze Hobeling
*/
protected static long insert(String table, String[] header, String[] values){
long id = -1;
@ -320,6 +327,7 @@ public class Database {
* @param header the header of the table, order should match with values
* @param values the data you want the id of, order should match witch values
* @return one id matching the given data or -1 if no match has been found
* @author Malte Schulze Hobeling
*/
protected static long getSingleId(String table, String[] header, String[] values){
long id = -1;
@ -372,6 +380,7 @@ public class Database {
* deletes an entry from table with matching id
* @param table the table that contains the entry you want to delete
* @param id the id of the entry you want to delete
* @author Malte Schulze Hobeling
*/
protected static void delete(String table, long id){
String sql = "DELETE FROM " + table + " WHERE id = ?;";
@ -383,6 +392,21 @@ public class Database {
}
}
/**
* its a query builder it builds queries
* exists: checks if an entry with the given parameters exists in table
* selectMatch: returns all matching rows from table
* insert: inserts or ignores into table
* count: counts exact matches from table
* update: updates table, header/values[0] is used as WHERE, using id is recommended header/values[1+] are used
* as SET
* @param type exists, selectMatch, insert, count, update
* @param table table
* @param header header
* @param values values
* @return sql statement as String
* @author Malte Schulze Hobeling
*/
private static String queryBuilder(String type,String table, String[] header, String[] values){
String sql;
StringBuilder sb = new StringBuilder();
@ -479,6 +503,7 @@ public class Database {
* returns a list of all entries
* @param table the table you want
* @return a list of all entries as String with the fields separated by ":"
* @author Malte Schulze Hobeling
*/
protected static List<String> getTable(String table){
List<String> data = new ArrayList<>();
@ -510,6 +535,7 @@ public class Database {
* @param header header for the WHERE portion, order should match with values
* @param values values for the WHERE portion, order should match with header
* @return a list of the matching data as String separated by ":"
* @author Malte Schulze Hobeling
*/
protected static List<String> select(String table,String[] header, String[] values){
List<String> data = new ArrayList<>();
@ -541,6 +567,7 @@ public class Database {
* @param table the table you want the entry from
* @param id the id of the entry you want
* @return a list of String separated by ":"
* @author Malte Schulze Hobeling
*/
protected static List<String> getEntryById(String table, long id){
List<String> data = new ArrayList<>();
@ -574,6 +601,7 @@ public class Database {
* @param header the properties you want to count on
* @param values the values for the properties
* @return the number of found rows
* @author Malte Schulze Hobeling
*/
protected static int count(String table,String[] header,String[] values){
String sql = queryBuilder("count",table,header,values);
@ -592,6 +620,7 @@ public class Database {
* @param header [0] is used as WHERE, everything else in SET
* @param values [0] is used as WHERE, everything else in SET
* @return number of rows affected or -1 on error
* @author Malte Schulze Hobeling
*/
protected static int update(String table,String[] header,String[] values){
try(Connection conn = connect()) {

View File

@ -1,10 +1,12 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.List;
/**
* Food, used by FoodPlan
* one constructor is used to create new foods the other is used to create existing foods from database
* @author Malte Schulze Hobeling
*/
public class Food {
private long id;
private String name;

View File

@ -1,16 +1,19 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
import java.util.ArrayList;
import java.util.List;
/**
* A collection of functions loosely related to food management
* Acts as an abstraction layer to the database
* @author Malte Schulze Hobeling
*/
public class FoodMgr {
/**
* inserts a food int to the database and creates the food_restriction entries
* @param food the food to be created
* @return id of food or -1
* @author Malte Schulze Hobeling
*/
public static long createFood(Food food){
String[] foodH = {"name","description","isDessert","food_typeid"};
@ -29,7 +32,9 @@ public class FoodMgr {
/**
* inserts a food_plan into the database
* @param foodPlan the foodPlan to be created
* @return id of food_plan or -1
* @author Malte Schulze Hobeling
*/
public static long createFood_plan(FoodPlan foodPlan){
String[] food_planH = {"date","food1","food2","dessert1","dessert2"};
@ -59,6 +64,7 @@ public class FoodMgr {
* returns all non desserts or all desserts
* @param isDessert true for only desserts false for non desserts
* @return a list of all non desserts or all desserts
* @author Malte Schulze Hobeling
*/
public static List<Food> getFood(boolean isDessert){
String[] foodH = {"isDessert"};
@ -76,6 +82,7 @@ public class FoodMgr {
* getFood but returns only vegan food
* @param isDessert true for only desserts false for non desserts
* @return a list of all vegan non desserts or all vegan desserts
* @author Malte Schulze Hobeling
*/
public static List<Food> getVeganFood(boolean isDessert){
String[] foodH = {"isDessert","food_typeid"};
@ -93,6 +100,7 @@ public class FoodMgr {
* returns a food_plan for a day
* @param date YYYY-MM-DD one day
* @return food_plan for date
* @author Malte Schulze Hobeling
*/
public static FoodPlan getFoodPlan(String date){
String[] food_planH = {"date"};
@ -110,8 +118,17 @@ public class FoodMgr {
return new FoodPlan(Long.parseLong(parts[0]),date,foodVegan,foodSecond,dessertVegan,dessertSecond,isSent);
}
/**
* returns a FoodPlan for a given id or null if no unique id was found
* @param id id of the FoodPlan
* @return FoodPlan or null
* @author Malte Schulze Hobeling
*/
public static FoodPlan getFoodPlanById(long id){
List<String> entry = Database.getEntryById("food_plan",id);
if(entry.size() != 1){
return null;
}
String[] parts = entry.get(0).split(":");
Food foodVegan = getFoodById(Long.parseLong(parts[2]));
Food foodSecond = getFoodById(Long.parseLong(parts[3]));
@ -121,8 +138,17 @@ public class FoodMgr {
return new FoodPlan(id,parts[1], foodVegan,foodSecond,dessertVegan,dessertSecond,isSent);
}
/**
* returns a Food for a given id or null if no unique id was found
* @param id id of the Food
* @return Food or null
* @author Malte Schulze Hobeling
*/
public static Food getFoodById(long id){
List<String> entry = Database.getEntryById("food",id);
if(entry.size() != 1){
return null;
}
String[] parts = entry.get(0).split(":");
String name = parts[1];
String description = parts[2];
@ -133,20 +159,44 @@ public class FoodMgr {
return new Food(id,name,description,isDessert,foodType,allergies);
}
/**
* returns a FoodType for a given id or null if no unique id was found
* @param id id of the FoodType
* @return FoodType or null
* @author Malte Schulze Hobeling
*/
public static FoodType getFoodTypeById(long id){
List<String> entry = Database.getEntryById("food_type",id);
if(entry.size() != 1){
return null;
}
String[] typeParts = entry.get(0).split(":");
return new FoodType(Long.parseLong(typeParts[0]),typeParts[1]);
}
/**
* returns an Allergy for a given id or null if no unique id was found
* @param id id of the Allergy
* @return Allergy or null
* @author Malte Schulze Hobeling
*/
public static Allergy getAllergyById(long id){
String[] allergyH = {"id"};
String[] allergyD = {String.valueOf(id)};
List<String> allergies = Database.select("allergy",allergyH,allergyD);
String[] allergyParts = allergies.get(0).split(":");
List<String> entry = Database.select("allergy",allergyH,allergyD);
if(entry.size() != 1){
return null;
}
String[] allergyParts = entry.get(0).split(":");
return new Allergy(id,allergyParts[1],allergyParts[2]);
}
/**
* returns all Allergies for a given foodId
* @param foodId the foodId you want the allergies from
* @return a List of Allergy, can be empty
* @author Malte Schulze Hobeling
*/
public static List<Allergy> getAllergies(long foodId){
List<Allergy> allergies = new ArrayList<>();
String[] restrictionsH = {"foodid"};
@ -163,11 +213,12 @@ public class FoodMgr {
* inserts the selected food into food_Selection if the food_plan has not been sent
* @param food_selectionData childid, food_planid, foodid
* @return id or -1
* @author Malte Schulze Hobeling
*/
public static long createFood_selection(String[] food_selectionData){
String[] food_selectionH = {"childid","food_planid","foodid"};
FoodPlan food_plan = getFoodPlanById(Long.parseLong(food_selectionData[1]));
if(!food_plan.isSent()){
if(food_plan != null && !food_plan.isSent()){
return Database.insert("food_selection",food_selectionH,food_selectionData);
}else {
return -1;
@ -178,6 +229,7 @@ public class FoodMgr {
* accumulates the selected food for a given day and locks the corresponding food_plan
* @param date YYYY-MM-DD day
* @return the accumulated orders
* @author Malte Schulze Hobeling
*/
public static List<String> getDayOrder(String date){
List<String> orders = new ArrayList<>();

View File

@ -1,5 +1,10 @@
package com.bib.essensbestellungsverwaltung;
/**
* FoodPlan
* one constructor is used to create new foodPlans the other is used to create existing foodPlans from database
* @author Malte Schulze Hobeling
*/
public class FoodPlan {
private long id;
private String date;

View File

@ -1,8 +1,9 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
/**
* FoodType used by Food, not intended to be changed
* @author Malte Schulze Hobeling
*/
public class FoodType {
private long id;
private String name;

View File

@ -1,11 +1,12 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
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
* @author Malte Schulze Hobeling
*/
public class Parent extends User{
List<Child> children;

View File

@ -1,5 +1,10 @@
package com.bib.essensbestellungsverwaltung;
/**
* Acts as a launcher required to make it a .jar
* It is also used to select either the cli or the gui depending on starting arguments
* @author Malte Schulze Hobeling
*/
public class SuperMain {
public static void main(String[] args) {
if(args.length > 0){

View File

@ -1,8 +1,9 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
/**
* one constructor is used to create new users the other is used to create existing users from database
* @author Malte Schulze Hobeling
*/
public class User {
private long id;
private String name;

View File

@ -1,8 +1,9 @@
package com.bib.essensbestellungsverwaltung;
/*
@author Malte Schulze Hobeling
*/
/**
* 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 Worker(long id, String name, String firstname, String password, String email, Address address) {
super(id, name, firstname, password, email, address);