Kommentare 2: electric boogaloo
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user