diff --git a/orders.txt b/orders.txt new file mode 100644 index 0000000..14abfb1 --- /dev/null +++ b/orders.txt @@ -0,0 +1,12 @@ +12.12.2022;1234567;Vegan;Dessert V +12.12.2022;7654321;Vegan;Dessert V +12.12.2022;123;Vegetarisch;Dessert V +12.12.2022;9999999;Fleisch;Dessert +12.12.2022;1234567;Vegan;Dessert V +12.12.2022;7654321;Vegan;Dessert V +12.12.2022;123;Vegetarisch;Dessert V +12.12.2022;9999999;Fleisch;Dessert +12.12.2022;1234567;Vegan;Dessert V +12.12.2022;7654321;Vegan;Dessert V +12.12.2022;123;Vegetarisch;Dessert V +12.12.2022;1234;Fleisch;Dessert \ No newline at end of file diff --git a/orders2.txt b/orders2.txt new file mode 100644 index 0000000..d669447 --- /dev/null +++ b/orders2.txt @@ -0,0 +1,12 @@ +12.12.2022;1234567;Vegan;Dessert V +12.12.2022;7654321;Vegan;Dessert V +12.12.2022;123;Vegetarisch;Dessert V +12.12.2022;9999999;Fleisch;Dessert +12.12.2022;1234567;Vegan;Dessert V +12.12.2022;7654321;Vegan;Dessert V +12.12.2022;123;Vegetarisch;Dessert V +12.12.2022;9999999;Fleisch;Dessert +12.12.2022;1234567;Vegan;Dessert V +12.12.2022;7654321;Vegan;Dessert V +12.12.2022;123;Vegetarisch;Dessert V +12.12.2022;1234;Fleisch;Dessert diff --git a/out/production/VPR_SCRIPT/Data.class b/out/production/VPR_SCRIPT/Data.class index 32442ad..6447c1a 100644 Binary files a/out/production/VPR_SCRIPT/Data.class and b/out/production/VPR_SCRIPT/Data.class differ diff --git a/src/Data.java b/src/Data.java index 27a46ef..058ee5b 100644 --- a/src/Data.java +++ b/src/Data.java @@ -1,10 +1,14 @@ +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.sql.Array; import java.util.ArrayList; import java.util.List; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Data is a class to edit files. @@ -185,9 +189,9 @@ public class Data { String date = parts[0]; String user = parts[1]; - String mealtyp = parts[3]; - String deserttyp = parts[5]; - orderList.add(new Order(date, user, mealtyp, deserttyp)); + String mealtype = parts[3]; + String deserttype = parts[5]; + orderList.add(new Order(date, user, mealtype, deserttype)); } return null;//orderList @@ -196,14 +200,30 @@ public class Data { /** * 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 listToWrite) { - if (pathWrite.equals("users.txt")) { - - } else if (pathWrite.equals("orders.txt")) { - + 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(); } } @@ -223,5 +243,120 @@ public class Data { } return rows; } + + /** + * 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]*"); + Pattern passwordPattern = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"); + + 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; + } + + /** + * The method changes the orders.txt file by removing canceled orders + * + * @return ArrayList\ changedOrderList + * @author Felix Düsterhaus + */ + + public ArrayList changeOrder(String userLogin) { + + /* + File orderFile = new File("C:/Unterricht/VPR/orders.txt"); + if (orderFile.delete()) { + System.out.println("Datei gelöscht: " + orderFile.getName()); + } else { + System.out.println("Fehler, " + orderFile.getName() + " nicht gelöscht."); + } + */ + + ArrayList changedOrderList = new ArrayList<>(); + List rows = getRows(); + int changedEntries = 0; + + 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 Order(date, user, mealtyp, deserttyp)); + } else { + changedEntries++; + } + } + + try { + FileWriter writer = new FileWriter("orders2.txt"); + for(Order str: changedOrderList) { + writer.write(str + System.lineSeparator()); + } + writer.close(); + System.out.println("Daten gelöscht"); + System.out.println(changedEntries + " Einträge entfernt."); + } catch (IOException e) { + e.printStackTrace(); + } + return changedOrderList; + } } diff --git a/src/Execute.java b/src/Execute.java index 1d4415a..eb105e8 100644 --- a/src/Execute.java +++ b/src/Execute.java @@ -1,3 +1,5 @@ +import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Scanner; @@ -5,7 +7,7 @@ import java.util.Scanner; * The class Execute is the execution file for the whole program. */ public class Execute { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { //EXECUTE MENU OVERVIEW Data data = new Data("menue.txt"); ArrayList menu = data.readMenu(); @@ -45,5 +47,29 @@ public class Execute { String password = leser.nextLine(); login.toLogin(userData.readUser(), password, phoneNumber); + + /** + * Test zum Daten löschen + * @author Felix Düsterhaus + */ + Data orderData = new Data("orders2.txt"); + System.out.print("Möchten Sie Einträge löschen? (Y/N) "); + String orderChangeDecision = leser.nextLine(); + if (orderChangeDecision.equals("Y") || orderChangeDecision.equals("y")) { + ArrayList changedOrderList = orderData.changeOrder(phoneNumber); + } else if (orderChangeDecision.equals("N") || orderChangeDecision.equals("n")) { + System.out.println("Daten werden nicht geändert."); + } + while(!(orderChangeDecision.equals("Y") || orderChangeDecision.equals("y") || orderChangeDecision.equals("N") || orderChangeDecision.equals("n"))) { + System.out.println("Bitte eine gültige Aussage treffen.\n"); + System.out.print("Möchten Sie Einträge löschen? (Y/N) "); + orderChangeDecision = leser.nextLine(); + + if (orderChangeDecision.equals("Y") || orderChangeDecision.equals("y")) { + ArrayList changedOrderList = orderData.changeOrder(phoneNumber); + } else if (orderChangeDecision.equals("N") || orderChangeDecision.equals("n")) { + System.out.println("Daten werden nicht geändert."); + } + } } } diff --git a/src/Login.java b/src/Login.java index 4f3c92a..73e0ed9 100644 --- a/src/Login.java +++ b/src/Login.java @@ -21,16 +21,6 @@ public class Login { while (true) { - 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 = leser.nextLine(); - System.out.print("Passwort: "); - password = leser.nextLine(); - } - boolean error = false; error = isError(error, phoneNumber); diff --git a/src/Menu.java b/src/Menu.java index 71ac4f6..99276d2 100644 --- a/src/Menu.java +++ b/src/Menu.java @@ -10,7 +10,7 @@ public class Menu { String date; String dish; String sideDish; - String typ; + String type; List ingredients; @@ -20,14 +20,14 @@ public class Menu { * @param date date of meal * @param dish meal * @param sideDish side dish - * @param typ vegan, vegetarian or meat + * @param type vegan, vegetarian or meat * @param ingredients list of ingredients */ - public Menu(String date, String dish, String sideDish, String typ, List ingredients) { + public Menu(String date, String dish, String sideDish, String type, List ingredients) { this.date = date; this.dish = dish; this.sideDish = sideDish; - this.typ = typ; + this.type = type; this.ingredients = ingredients; } @@ -47,26 +47,25 @@ public class Menu { * @return typ e.g. Vegan * @author Madeleine Vigier */ - public String getTyp() { - if (typ.contains("DessertV") || typ.contains("Dessert")) { + public String getType() { + if (type.contains("DessertV") || type.contains("Dessert")) { return ""; } else { - return typ; + return type; } } /** - * The method toString() returns a String representation of an object + * The method toString() returns a String formated to save in a File * * @return a String with dish, sideDish, list of ingredients and the typ of the menu - * @author Madeleine Vigier + * @author Madeleine Vigier, Felix Wöstemeyer */ @Override public String toString() { - return dish + "\n" + sideDish - + "\n" + ingredients.toString().replace("[", "").replace("]", "") + "\n" + getTyp() + "\n"; + return date+";"+dish+";"+sideDish+";"+type+";"+ingredients; } } diff --git a/src/Order.java b/src/Order.java index 8aecf53..f0f27ca 100644 --- a/src/Order.java +++ b/src/Order.java @@ -8,15 +8,20 @@ public class Order { String date; //date for the meal, not the day it was ordered String user; //phone number of the user account - String mealtyp; //meat, vegi or vegan - String deserttyp; //desert or vegan desert + String mealtype; //meat, vegi or vegan + String desserttype; //desert or vegan desert - Order (String date, String user, String mealtyp, String deserttyp) + Order (String date, String user, String mealtype, String desserttype) { this.date = date; this.user = user; - this.mealtyp = mealtyp; - this.deserttyp = deserttyp; + this.mealtype = mealtype; + this.desserttype = desserttype; + } + + @Override + public String toString(){ + return date+";"+user+";"+mealtype+";"+desserttype; } // @Override diff --git a/src/User.java b/src/User.java index a569f70..a44bea9 100644 --- a/src/User.java +++ b/src/User.java @@ -19,7 +19,7 @@ public class User { * @param nameParent2 name of the second parent of the child * @param billAddress the address the bill should be sent to * @param phoneNumber phonenumber of one of the parents and also the username - * @param nameChildren name of the child + * @param nameChildren name of the children * @param password password of the user * @author Madeleine Vigier */ @@ -80,13 +80,6 @@ public class User { */ @Override public String toString() { - return "User{" + - "nameParent1='" + nameParent1 + '\'' + - ", nameParent2='" + nameParent2 + '\'' + - ", billAddress='" + billAddress + '\'' + - ", phonenumber='" + phoneNumber + '\'' + - ", nameChildren='" + nameChildren + '\'' + - ", password='" + password + '\'' + - '}'; + return nameParent1 + ";" + nameParent2 + ";" + billAddress + ";" + phoneNumber + ";" + nameChildren + ";" + password; } }