2023-01-27 21:49:18 +01:00
package com.example.vpr_javafx ;
2023-02-01 08:52:15 +01:00
import javafx.collections.FXCollections ;
import javafx.collections.ObservableList ;
2023-01-30 12:23:17 +01:00
import javafx.scene.control.Alert ;
2023-02-01 08:52:15 +01:00
import javafx.scene.control.ListView ;
2023-01-30 15:10:54 +01:00
import javafx.scene.control.PasswordField ;
import javafx.scene.control.TextField ;
2023-01-31 21:24:48 +01:00
import org.w3c.dom.Text ;
2023-01-30 12:23:17 +01:00
2023-01-31 21:24:48 +01:00
import java.awt.* ;
2023-01-27 21:49:18 +01:00
import java.io.File ;
import java.io.FileWriter ;
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
2023-01-30 15:10:54 +01:00
import java.util.HashMap ;
2023-01-27 21:49:18 +01:00
import java.util.List ;
import java.util.Scanner ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2023-01-31 18:20:28 +01:00
import javax.swing.* ;
2023-01-27 21:49:18 +01:00
/ * *
* Data is a class to edit files .
*
* @author Madeleine Vigier
* @version 1 . 3
* /
public class Data {
private String pathRead ;
2023-01-31 14:27:24 +01:00
2023-01-27 21:49:18 +01:00
public Data ( String pathRead ) {
this . pathRead = pathRead ;
}
2023-01-31 14:27:24 +01:00
/ * *
* @author Kevin Pfannenstiel
* @return
* /
public HashMap < String , String > readLoginData ( )
{
try {
List < String > lines = Files . readAllLines ( Paths . get ( pathRead ) ) ;
HashMap < String , String > user = new HashMap < > ( ) ;
for ( int i = 0 ; i < lines . size ( ) ; i + + )
{
String [ ] parts = lines . get ( i ) . split ( " ; " ) ;
String phoneNumber = parts [ 3 ] ;
String password = parts [ 5 ] ;
user . put ( phoneNumber , password ) ;
}
return user ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
return new HashMap < > ( ) ;
}
}
2023-01-27 21:49:18 +01:00
/ * *
* readUser ( ) is a method to split user . txt into lines and save them in an arraylist
*
* @return Arraylist userList
* @author Madeleine Vigier , Sabine Gubitz
2023-01-30 14:05:25 +01:00
*
2023-01-27 21:49:18 +01:00
* /
public ArrayList < com . example . vpr_javafx . User > readUser ( ) {
List < String > rows = getRows ( ) ;
ArrayList < com . example . vpr_javafx . User > userList = new ArrayList < > ( ) ;
for ( String row : rows ) {
String [ ] parts = row . split ( " ; " ) ;
String nameParent1 = parts [ 0 ] ;
String nameParent2 = parts [ 1 ] ;
String billAddress = parts [ 2 ] ;
String phonenumber = parts [ 3 ] ;
String nameChildren = parts [ 4 ] ;
String password = parts [ 5 ] ;
userList . add ( new com . example . vpr_javafx . User ( nameParent1 , nameParent2 , billAddress , phonenumber , nameChildren , password ) ) ;
}
return userList ;
}
/ * *
* readMenue ( ) is a method to split menue . txt into lines and save them in an arraylist
*
* @return Arraylist menuelist
* @author Madeleine Vigier , Sabine Gubitz
* /
public ArrayList < com . example . vpr_javafx . Menu > readMenu ( ) {
List < String > rows = getRows ( ) ;
ArrayList < com . example . vpr_javafx . Menu > menuList = new ArrayList < > ( ) ;
for ( String row : rows ) {
String [ ] parts = row . split ( " ; " ) ;
String date = parts [ 0 ] ;
String dish = parts [ 1 ] ;
String sideDish = parts [ 2 ] ;
String typ = parts [ 3 ] ;
String ingredient = parts [ 4 ] ;
List < String > ingredients = new ArrayList < > ( ) ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " a " ) ; //eggs
}
ingredient = parts [ 5 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " d " ) ; //peanuts
}
ingredient = parts [ 6 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " m " ) ; //fish
}
ingredient = parts [ 7 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " g " ) ; //grains
}
ingredient = parts [ 8 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " f " ) ; //crustaceans
}
ingredient = parts [ 9 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " n " ) ; //lupines
}
ingredient = parts [ 10 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " c " ) ; //milk
}
ingredient = parts [ 11 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " k " ) ; //nuts
}
ingredient = parts [ 12 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " j " ) ; //sulfurDioxideAndSulfite
}
ingredient = parts [ 13 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " l " ) ; //celeriac
}
ingredient = parts [ 14 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " h " ) ; //mustards
}
ingredient = parts [ 15 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " i " ) ; //sesame
}
ingredient = parts [ 16 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " b " ) ; //soy
}
ingredient = parts [ 17 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " e " ) ; //molluscs
}
ingredient = parts [ 18 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 4 " ) ; //antioxidant
}
ingredient = parts [ 19 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 5 " ) ;
}
ingredient = parts [ 20 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 6 " ) ; //flavourEnhancer
}
ingredient = parts [ 21 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 8 " ) ; // preservatives
}
ingredient = parts [ 22 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 3 " ) ; //nitrate
}
ingredient = parts [ 23 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 1 " ) ; //picklingSalt
}
ingredient = parts [ 24 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 7 " ) ; //artificialSweetener
}
ingredient = parts [ 25 ] ;
if ( ! ingredient . isEmpty ( ) ) {
ingredients . add ( " 2 " ) ; //phosphate
}
String end = parts [ 26 ] ; //endpoint
menuList . add ( new com . example . vpr_javafx . Menu ( date , dish , sideDish , typ , ingredients ) ) ;
}
return menuList ;
}
/ * *
* readOrder ( ) is a method to split order . txt into lines and save them in an arraylist
*
* @return Arraylist orderlist
* @author Madeleine Vigier , Sabine Gubitz
* /
public ArrayList < com . example . vpr_javafx . Order > readOrder ( ) {
ArrayList < com . example . vpr_javafx . Order > orderList = new ArrayList < > ( ) ;
List < String > rows = getRows ( ) ;
for ( String row : rows ) {
String [ ] parts = row . split ( " ; " ) ;
String date = parts [ 0 ] ;
String user = parts [ 1 ] ;
String mealtype = parts [ 3 ] ;
String deserttype = parts [ 5 ] ;
orderList . add ( new com . example . vpr_javafx . Order ( date , user , mealtype , deserttype ) ) ;
}
return null ; //orderList
}
/ * *
* The method writeData writes the data of a List into a txt file .
*
* @author Felix Wöstemeyer
*
* @param pathWrite determines the filename of the file that will be written
* @param listToWrite determines which ArrayList is to be used for writing the file
* /
public void writeData ( String pathWrite , ArrayList < String > listToWrite ) {
try {
if ( new File ( pathWrite ) . exists ( ) ) {
for ( String zeile : listToWrite ) {
new FileWriter ( pathWrite ) . write ( zeile ) ;
}
} else
{
Scanner scanner = new Scanner ( System . in ) ;
System . out . println ( " Der Pfad oder die Datei " + pathWrite + " existiert nicht! \ nMöchten Sie unter dem Pfad " + pathWrite + " eine neue Datei erstellen? (Y/N) " ) ;
if ( scanner . nextLine ( ) . equals ( " Y " ) ) {
new File ( pathWrite ) . createNewFile ( ) ;
System . out . println ( " Die Datei " + pathWrite + " wurde erfolgreich erstellt " ) ;
} else if ( ! scanner . nextLine ( ) . equals ( " N " ) & & ! scanner . nextLine ( ) . equals ( " Y " ) ) {
System . out . println ( " Es ist ein Fehler mit ihrer Antwort aufgetreten! " ) ;
}
}
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
/ * *
* The method gets the rows by reading all lines of the path
*
* @return ArrayList rows
* @author Madeleine Vigier
* /
private List < String > getRows ( ) {
Path path = Paths . get ( pathRead ) ;
List < String > rows = new ArrayList < > ( ) ;
try {
rows = Files . readAllLines ( path ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
return rows ;
}
/ * *
* The method validates the user input
*
2023-01-31 21:24:48 +01:00
* @param tfPhone Phone number the user typed in
* @param pfPassword Password the user typed in
* @param controller Controller
* @return phoneNumberValid & & passwordValid
* @author Kevin Maier , Kevin Pfannenstiel
2023-01-27 21:49:18 +01:00
* /
2023-01-31 15:49:57 +01:00
public boolean validateData ( TextField tfPhone , PasswordField pfPassword , HelloController controller )
2023-01-27 21:49:18 +01:00
{
boolean phoneNumberMatchFound ;
boolean passwordMatchFound ;
boolean phoneNumberValid = false ;
boolean passwordValid = false ;
2023-01-31 21:24:48 +01:00
Alert alert = new Alert ( Alert . AlertType . WARNING ) ;
2023-01-31 14:27:24 +01:00
HashMap < String , String > users = readLoginData ( ) ;
2023-01-30 15:10:54 +01:00
String phoneNumber = tfPhone . getText ( ) ;
String password = pfPassword . getText ( ) ;
2023-01-27 21:49:18 +01:00
Pattern phoneNumberPattern = Pattern . compile ( " [0-9]* " ) ;
Pattern passwordPattern = Pattern . compile ( " ^(?=.*[A-Za-z])(?=.* \\ d)[A-Za-z \\ d]{8,}$ " ) ;
2023-01-30 12:23:17 +01:00
if ( password . isEmpty ( ) | | phoneNumber . isEmpty ( ) )
2023-01-27 21:49:18 +01:00
{
2023-01-31 21:24:48 +01:00
alert . setContentText ( " Login fehlgeschlagen. Anmeldedaten unvollständig. " ) ;
2023-01-30 12:23:17 +01:00
alert . show ( ) ;
2023-01-27 21:49:18 +01:00
}
Matcher phoneNumberMatcher = phoneNumberPattern . matcher ( phoneNumber ) ;
Matcher passwordMatcher = passwordPattern . matcher ( password ) ;
phoneNumberMatchFound = phoneNumberMatcher . find ( ) ;
passwordMatchFound = passwordMatcher . find ( ) ;
2023-01-30 15:10:54 +01:00
if ( ! users . containsKey ( phoneNumber ) | | ! phoneNumberMatchFound | | phoneNumber . length ( ) > = 15 )
2023-01-30 12:23:17 +01:00
{
2023-01-31 15:49:57 +01:00
alert . setContentText ( " Login fehlgeschlagen. Die eingegebenen Daten sind falsch. " ) ;
2023-01-30 12:23:17 +01:00
alert . show ( ) ;
}
else
{
phoneNumberValid = true ;
}
2023-01-27 21:49:18 +01:00
2023-01-31 21:24:48 +01:00
if ( ! users . get ( phoneNumber ) . equals ( password ) | | ! passwordMatchFound )
2023-01-30 12:23:17 +01:00
{
2023-01-31 15:49:57 +01:00
alert . setContentText ( " Login fehlgeschlagen. Die eingegebenen Daten sind falsch. " ) ;
2023-01-30 12:23:17 +01:00
alert . show ( ) ;
}
else
{
passwordValid = true ;
2023-01-31 15:49:57 +01:00
controller . setRadioButton ( ) ;
2023-01-27 21:49:18 +01:00
}
2023-01-30 12:23:17 +01:00
return phoneNumberValid & & passwordValid ;
2023-01-27 21:49:18 +01:00
}
2023-01-31 21:24:48 +01:00
/ * *
*
* @param tfPhone
* @param pfPassword
* @param controller
* @author Kevin Maier
* /
public boolean validateRegistration ( TextField tfName , TextField tfPhone , PasswordField pfPassword , TextField tfStreet , TextField tfHouseNumber , TextField tfPostalCode , TextField tfCity , TextField tfChild , HelloController controller )
{
boolean phoneNumberMatchFound ;
boolean passwordMatchFound ;
boolean postalCodeFound ;
boolean phoneNumberValid = false ;
boolean passwordValid = false ;
boolean postalCodeValid = false ;
Alert alert = new Alert ( Alert . AlertType . WARNING ) ;
HashMap < String , String > users = readLoginData ( ) ;
String phoneNumber = tfPhone . getText ( ) ;
String password = pfPassword . getText ( ) ;
String postalCode = tfPostalCode . getText ( ) ;
Pattern phoneNumberPattern = Pattern . compile ( " [0-9]* " ) ;
Pattern passwordPattern = Pattern . compile ( " ^(?=.*[A-Za-z])(?=.* \\ d)[A-Za-z \\ d]{8,}$ " ) ;
Pattern postalCodePattern = Pattern . compile ( " [0-9]{5} " ) ;
if ( tfName . getText ( ) . isEmpty ( ) | | tfPhone . getText ( ) . isEmpty ( ) | | pfPassword . getText ( ) . isEmpty ( ) | | tfStreet . getText ( ) . isEmpty ( ) | | tfHouseNumber . getText ( ) . isEmpty ( ) | | tfPostalCode . getText ( ) . isEmpty ( ) | | tfCity . getText ( ) . isEmpty ( ) | | tfChild . getText ( ) . isEmpty ( ) )
{
alert . setContentText ( " Registrierung fehlgeschlagen. Registrierungsdaten unvollständig. " ) ;
alert . show ( ) ;
}
Matcher phoneNumberMatcher = phoneNumberPattern . matcher ( phoneNumber ) ;
Matcher passwordMatcher = passwordPattern . matcher ( password ) ;
Matcher postalCodeMatcher = postalCodePattern . matcher ( postalCode ) ;
phoneNumberMatchFound = phoneNumberMatcher . find ( ) ;
passwordMatchFound = passwordMatcher . find ( ) ;
postalCodeFound = postalCodeMatcher . find ( ) ;
if ( users . containsKey ( phoneNumber ) | | ! phoneNumberMatchFound | | phoneNumber . length ( ) > = 15 )
{
alert . setContentText ( " Registrierung fehlgeschlagen. Die eingegebenen Telefonnummer ist bereits vergeben oder ist nicht korrekt. " ) ;
alert . show ( ) ;
}
else
{
phoneNumberValid = true ;
}
if ( ! passwordMatchFound )
{
alert . setContentText ( " Registrierung fehlgeschlagen. Das Passwort muss mindestens 8 Zeichen lang sein und mindestens 1 Ziffer und 1 Zeichen beinhalten. " ) ;
alert . show ( ) ;
}
else
{
passwordValid = true ;
}
if ( ! postalCodeFound )
{
alert . setContentText ( " Registrierung fehlgeschlagen. Die Postleitzahl ist nicht richtig. " ) ;
}
else
{
postalCodeValid = true ;
}
return phoneNumberValid & & passwordValid & & postalCodeValid ;
}
2023-01-30 12:23:17 +01:00
2023-01-27 21:49:18 +01:00
/ * *
* The method changes the orders . txt file by removing canceled orders
*
* @return ArrayList \ < Order > changedOrderList
* @author Felix Düsterhaus
* /
2023-02-01 08:52:15 +01:00
public ArrayList < com . example . vpr_javafx . Order > changeOrder ( String userLogin , ListView < String > ListViewDelete ) {
ArrayList < String > chosenMeals = new ArrayList < > ( ) ;
ObservableList < String > chosenMealsObservable = FXCollections . observableList ( chosenMeals ) ;
2023-01-27 21:49:18 +01:00
ArrayList < com . example . vpr_javafx . Order > changedOrderList = new ArrayList < > ( ) ;
List < String > rows = getRows ( ) ;
int changedEntries = 0 ;
2023-01-31 18:20:28 +01:00
//ImageIcon icon = new ImageIcon("file:target/classes/com/example/vpr_javafx/pics/vegetarisch.png");
UIManager . put ( " OptionPane.noButtonText " , " Nein " ) ;
UIManager . put ( " OptionPane.yesButtonText " , " Ja " ) ;
int result = JOptionPane . showInternalConfirmDialog ( null , " Bestellungen wirklich Löschen? " , " Bestätigung " , JOptionPane . YES_NO_OPTION ) ;
2023-02-01 08:52:15 +01:00
ListViewDelete . getSelectionModel ( ) . getSelectedItems ( ) ;
2023-01-31 18:20:28 +01:00
if ( result = = JOptionPane . YES_OPTION ) {
for ( String row : rows ) {
String [ ] parts = row . split ( " ; " ) ;
String date = parts [ 0 ] ;
String user = parts [ 1 ] ;
String mealtyp = parts [ 2 ] ;
String deserttyp = parts [ 3 ] ;
if ( ! userLogin . equals ( user ) ) {
changedOrderList . add ( new com . example . vpr_javafx . Order ( date , user , mealtyp , deserttyp ) ) ;
} else {
changedEntries + + ;
}
2023-01-27 21:49:18 +01:00
}
2023-02-01 08:52:15 +01:00
try {
FileWriter writer = new FileWriter ( " orders2.txt " ) ;
for ( com . example . vpr_javafx . Order str : changedOrderList ) {
writer . write ( str + System . lineSeparator ( ) ) ;
}
writer . close ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
2023-01-27 21:49:18 +01:00
}
2023-01-31 18:20:28 +01:00
JOptionPane . showMessageDialog ( null , changedEntries + " Bestellungen Gelöscht, Sie können eine neue Bestellung aufgeben " ) ;
} else {
JOptionPane . showMessageDialog ( null , " Bestellung nicht gelöscht. " ) ;
2023-01-27 21:49:18 +01:00
}
return changedOrderList ;
}
}