2023-01-17 16:09:55 +01:00
import java.io.File ;
import java.io.FileWriter ;
2022-12-07 12:02:02 +01:00
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
import java.util.List ;
2023-01-16 16:53:30 +01:00
import java.util.Scanner ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2022-12-07 12:02:02 +01:00
/ * *
* Data is a class to edit files .
2022-12-15 19:38:24 +01:00
*
2022-12-07 12:02:02 +01:00
* @author Madeleine Vigier
2022-12-14 20:38:33 +01:00
* @version 1 . 3
2022-12-07 12:02:02 +01:00
* /
public class Data {
private String pathRead ;
public Data ( String pathRead ) {
this . pathRead = pathRead ;
}
2022-12-15 19:38:24 +01:00
2022-12-14 20:38:33 +01:00
/ * *
* readUser ( ) is a method to split user . txt into lines and save them in an arraylist
2022-12-15 19:38:24 +01:00
*
2022-12-14 20:38:33 +01:00
* @return Arraylist userList
* @author Madeleine Vigier , Sabine Gubitz
* /
2022-12-15 19:38:24 +01:00
public ArrayList < User > readUser ( ) {
2022-12-14 20:38:33 +01:00
List < String > rows = getRows ( ) ;
ArrayList < 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 ] ;
2022-12-15 19:38:24 +01:00
userList . add ( new User ( nameParent1 , nameParent2 , billAddress , phonenumber , nameChildren , password ) ) ;
2022-12-14 20:38:33 +01:00
}
return userList ;
2022-12-15 19:38:24 +01:00
2022-12-14 20:38:33 +01:00
}
2022-12-07 12:02:02 +01:00
/ * *
2022-12-14 20:38:33 +01:00
* readMenue ( ) is a method to split menue . txt into lines and save them in an arraylist
2022-12-15 19:38:24 +01:00
*
2022-12-14 20:38:33 +01:00
* @return Arraylist menuelist
2022-12-14 13:17:46 +01:00
* @author Madeleine Vigier , Sabine Gubitz
2022-12-07 12:02:02 +01:00
* /
2022-12-19 15:28:14 +01:00
public ArrayList < Menu > readMenu ( ) {
2022-12-07 12:02:02 +01:00
2022-12-15 19:38:24 +01:00
List < String > rows = getRows ( ) ;
2022-12-19 15:28:14 +01:00
ArrayList < Menu > menuList = new ArrayList < > ( ) ;
2022-12-17 13:31:21 +01:00
2022-12-14 20:38:33 +01:00
for ( String row : rows ) {
2022-12-17 13:31:21 +01:00
2022-12-14 20:38:33 +01:00
String [ ] parts = row . split ( " ; " ) ;
2022-12-07 12:02:02 +01:00
2022-12-17 13:31:21 +01:00
2022-12-15 19:38:24 +01:00
String date = parts [ 0 ] ;
String dish = parts [ 1 ] ;
String sideDish = parts [ 2 ] ;
String typ = parts [ 3 ] ;
2022-12-17 13:31:21 +01:00
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
2022-12-19 15:28:14 +01:00
menuList . add ( new Menu ( date , dish , sideDish , typ , ingredients ) ) ;
2022-12-14 20:38:33 +01:00
}
2022-12-19 15:28:14 +01:00
return menuList ;
2022-12-14 20:38:33 +01:00
}
2022-12-14 20:50:57 +01:00
2022-12-14 20:38:33 +01:00
/ * *
2022-12-14 20:50:57 +01:00
* readOrder ( ) is a method to split order . txt into lines and save them in an arraylist
2022-12-15 19:38:24 +01:00
*
2022-12-14 20:38:33 +01:00
* @return Arraylist orderlist
* @author Madeleine Vigier , Sabine Gubitz
* /
2022-12-15 19:38:24 +01:00
public ArrayList < Order > readOrder ( ) {
2022-12-07 12:02:02 +01:00
2023-01-10 15:59:16 +01:00
ArrayList < Order > orderList = new ArrayList < > ( ) ;
2022-12-14 20:38:33 +01:00
List < String > rows = getRows ( ) ;
2022-12-07 12:02:02 +01:00
2022-12-15 19:38:24 +01:00
for ( String row : rows ) {
String [ ] parts = row . split ( " ; " ) ;
String date = parts [ 0 ] ;
2023-01-10 16:23:10 +01:00
String user = parts [ 1 ] ;
2023-01-17 15:43:56 +01:00
String mealtype = parts [ 3 ] ;
String deserttype = parts [ 5 ] ;
orderList . add ( new Order ( date , user , mealtype , deserttype ) ) ;
2022-12-15 19:38:24 +01:00
}
return null ; //orderList
2022-12-07 12:02:02 +01:00
}
/ * *
* The method writeData writes the data of a List into a txt file .
2022-12-15 19:38:24 +01:00
*
2023-01-17 16:09:55 +01:00
* @author Felix Wöstemeyer
*
2022-12-15 19:38:24 +01:00
* @param pathWrite determines the filename of the file that will be written
2022-12-07 12:02:02 +01:00
* @param listToWrite determines which ArrayList is to be used for writing the file
* /
2022-12-15 19:38:24 +01:00
public void writeData ( String pathWrite , ArrayList < String > listToWrite ) {
2023-01-17 16:09:55 +01:00
try {
if ( new File ( pathWrite ) . exists ( ) ) {
for ( String zeile : listToWrite ) {
new FileWriter ( pathWrite ) . write ( zeile ) ;
}
2023-01-17 16:37:39 +01:00
} 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. " ) ;
}
2023-01-17 16:09:55 +01:00
}
} catch ( IOException e ) {
e . printStackTrace ( ) ;
2022-12-07 12:02:02 +01:00
}
}
2022-12-14 20:38:33 +01:00
/ * *
* The method gets the rows by reading all lines of the path
2022-12-15 19:38:24 +01:00
*
2022-12-14 20:38:33 +01:00
* @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 ;
}
2023-01-16 16:53:30 +01:00
/ * *
* The method validates the user input
*
* @return boolean inputValid
* @author Kevin Maier
* /
private boolean validateData ( String password , String phoneNumber )
{
boolean inputValid = false ;
boolean phoneNumberMatchFound ;
boolean passwordMatchFound ;
boolean phoneNumberValid = false ;
boolean passwordValid = false ;
Pattern phoneNumberPattern = Pattern . compile ( " [0-9]* " ) ;
2023-01-17 16:02:29 +01:00
Pattern passwordPattern = Pattern . compile ( " ^(?=.*[A-Za-z])(?=.* \\ d)[A-Za-z \\ d]{8,}$ " ) ;
2023-01-16 16:53:30 +01:00
Scanner reader = new Scanner ( System . in ) ;
while ( password . isEmpty ( ) | | phoneNumber . isEmpty ( ) )
{
System . out . println ( " Login fehlgeschlagen. Eingabe ist leer, versuchen Sie es erneut. " ) ;
System . out . println ( " " ) ;
System . out . print ( " Telefonnummer: " ) ;
phoneNumber = reader . nextLine ( ) ;
System . out . print ( " Passwort: " ) ;
password = reader . nextLine ( ) ;
}
Matcher phoneNumberMatcher = phoneNumberPattern . matcher ( phoneNumber ) ;
Matcher passwordMatcher = passwordPattern . matcher ( password ) ;
phoneNumberMatchFound = phoneNumberMatcher . find ( ) ;
passwordMatchFound = passwordMatcher . find ( ) ;
while ( ! inputValid ) {
if ( ! phoneNumberMatchFound | | phoneNumber . length ( ) ! = 15 ) {
System . out . println ( " Login fehlgeschlagen. Die eingegebene Handynummer ist nicht valide. " ) ;
System . out . println ( " " ) ;
System . out . print ( " Telefonnummer: " ) ;
phoneNumber = reader . nextLine ( ) ;
System . out . print ( " Passwort: " ) ;
password = reader . nextLine ( ) ;
} else {
phoneNumberValid = true ;
}
if ( ! passwordMatchFound | | password . length ( ) < 6 | | password . length ( ) > 20 ) {
System . out . println ( " Login fehlgeschlagen. Das eingegebene Passwort ist nicht valide. " ) ;
System . out . println ( " " ) ;
System . out . print ( " Telefonnummer: " ) ;
phoneNumber = reader . nextLine ( ) ;
System . out . print ( " Passwort: " ) ;
password = reader . nextLine ( ) ;
} else {
passwordValid = true ;
}
if ( phoneNumberValid & & passwordValid ) {
inputValid = true ;
}
}
return inputValid ;
}
2022-12-07 12:02:02 +01:00
}
2022-12-14 20:38:33 +01:00