diff --git a/src/main/java/de/antco/projekt/ConsoleUi.java b/src/main/java/de/antco/projekt/ConsoleUi.java index 6fbda8f..da620e3 100644 --- a/src/main/java/de/antco/projekt/ConsoleUi.java +++ b/src/main/java/de/antco/projekt/ConsoleUi.java @@ -6,18 +6,12 @@ import de.antco.projekt.model.Vehicle; import de.antco.projekt.model.WorkOrderDetails; import de.antco.projekt.model.WorkOrderItem; import de.antco.projekt.service.WorkshopService; -import de.antco.projekt.importers.IImporter; -import de.antco.projekt.importers.CsvPersonImporter; -import de.antco.projekt.exporters.IExporter; -import de.antco.projekt.exporters.CsvPersonExporter; import java.sql.SQLException; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; +import java.util.*; public class ConsoleUi { + private final WorkshopService service; private final Scanner scanner = new Scanner(System.in); @@ -27,202 +21,243 @@ public class ConsoleUi { public void start() { boolean running = true; + while (running) { - printMenu(); - String input = scanner.nextLine(); + showMenu(); + String choice = scanner.nextLine().trim(); + try { - switch (input) { + switch (choice) { case "1" -> addPerson(); - case "2" -> listPersons(); + case "2" -> showPersons(); case "3" -> addVehicle(); - case "4" -> listVehiclesOfPerson(); + case "4" -> showVehiclesOfPerson(); case "5" -> createWorkOrder(); - case "6" -> listWorkOrders(); - case "7" -> importPersons(); - case "8" -> exportPersons(); + case "6" -> showWorkOrders(); case "0" -> running = false; - default -> System.out.println("Unbekannte Auswahl"); + default -> System.out.println("Ungültige Eingabe."); } } catch (SQLException e) { - System.out.println("Fehler: " + e.getMessage()); + System.out.println("Ein Fehler ist aufgetreten: " + e.getMessage()); } } - System.out.println("Bis dann"); + + System.out.println("Programm beendet."); } - private void printMenu() { - System.out.println(); - System.out.println("==== Werkstatt Menü ===="); + private void showMenu() { + System.out.println("\n=== Werkstatt Menü ==="); System.out.println("1) Kunde anlegen"); System.out.println("2) Kunden anzeigen"); System.out.println("3) Fahrzeug anlegen"); - System.out.println("4) Fahrzeuge einer Person"); + System.out.println("4) Fahrzeuge einer Person anzeigen"); System.out.println("5) Werkstattvorgang anlegen"); System.out.println("6) Vorgänge anzeigen"); - System.out.println("7) Personen importieren (CSV)"); - System.out.println("8) Personen exportieren (CSV)"); - System.out.println("0) Ende"); + System.out.println("0) Beenden"); System.out.print("Auswahl: "); } - private void addPerson() throws SQLException { - System.out.print("Name: "); - String name = scanner.nextLine(); - System.out.print("Telefon: "); - String phone = scanner.nextLine(); - Person person = service.createPerson(name, phone); - System.out.println("Kunde gespeichert mit ID " + person.getId()); - } + // ---------------- Eingabe-Hilfen ---------------- - private void listPersons() throws SQLException { - List persons = service.listPersons(); - if (persons.isEmpty()) { - System.out.println("Keine Personen da"); - return; - } - persons.forEach(p -> System.out.println(p.toString())); - } - - private void addVehicle() throws SQLException { - System.out.print("Personen ID: "); - int personId = parseInt(scanner.nextLine()); - Person owner = service.findPerson(personId); - if (owner == null) { - System.out.println("Person nicht gefunden"); - return; - } - System.out.print("Hersteller: "); - String brand = scanner.nextLine(); - System.out.print("Modell: "); - String model = scanner.nextLine(); - System.out.print("Kennzeichen: "); - String plate = scanner.nextLine(); - Vehicle vehicle = service.createVehicle(personId, brand, model, plate); - System.out.println("Fahrzeug gespeichert mit ID " + vehicle.getId()); - } - - private void listVehiclesOfPerson() throws SQLException { - System.out.print("Personen ID: "); - int personId = parseInt(scanner.nextLine()); - Person owner = service.findPerson(personId); - if (owner == null) { - System.out.println("Person nicht gefunden"); - return; - } - List vehicles = service.listVehicles(personId); - System.out.println("Fahrzeuge von " + owner.getName() + ":"); - if (vehicles.isEmpty()) { - System.out.println("Keine Einträge"); - return; - } - vehicles.forEach(v -> System.out.println(v.toString())); - } - - private void createWorkOrder() throws SQLException { - System.out.print("Fahrzeug ID: "); - int vehicleId = parseInt(scanner.nextLine()); - Vehicle vehicle = service.findVehicle(vehicleId); - if (vehicle == null) { - System.out.println("Fahrzeug nicht gefunden"); - return; - } - System.out.print("Notiz: "); - String note = scanner.nextLine(); - List catalog = service.listServiceItems(); - Map selected = new LinkedHashMap<>(); - boolean adding = true; - while (adding) { - showCatalog(catalog); - System.out.print("Service ID (leer = fertig): "); + private int readInt(String text) { + while (true) { + System.out.print(text); String input = scanner.nextLine(); - if (input.isBlank()) { - adding = false; - } else { - int serviceId = parseInt(input); - ServiceItem item = catalog.stream().filter(s -> s.getId() == serviceId).findFirst().orElse(null); - if (item == null) { - System.out.println("Service gibt es nicht"); - continue; - } - System.out.print("Menge: "); - int qty = parseInt(scanner.nextLine()); - if (qty <= 0) { - System.out.println("Menge muss > 0 sein"); - continue; - } - selected.put(serviceId, selected.getOrDefault(serviceId, 0) + qty); + + try { + return Integer.parseInt(input.trim()); + } catch (NumberFormatException e) { + System.out.println("Bitte eine gültige Zahl eingeben."); } } - if (selected.isEmpty()) { - System.out.println("Keine Positionen gespeichert"); + } + + private String readNonEmpty(String text) { + while (true) { + System.out.print(text); + String value = scanner.nextLine().trim(); + + if (!value.isEmpty()) { + return value; + } + + System.out.println("Eingabe darf nicht leer sein."); + } + } + + private String readValidName(String text) { + while (true) { + String name = readNonEmpty(text); + + if (name.matches("[a-zA-ZäöüÄÖÜß ]+")) { + return name; + } + + System.out.println("Der Name darf nur Buchstaben enthalten."); + } + } + + private String readPhoneNumber(String text) { + while (true) { + System.out.print(text); + String phone = scanner.nextLine().trim(); + + if (phone.matches("\\d+")) { + return phone; + } + + System.out.println("Telefonnummer darf nur Zahlen enthalten."); + } + } + + // ---------------- Personen ---------------- + + private void addPerson() throws SQLException { + String name = readValidName("Name: "); + String phone = readPhoneNumber("Telefonnummer: "); + + Person person = service.createPerson(name, phone); + System.out.println("Kunde gespeichert (ID: " + person.getId() + ")"); + } + + private void showPersons() throws SQLException { + List persons = service.listPersons(); + + if (persons.isEmpty()) { + System.out.println("Keine Kunden vorhanden."); return; } - int orderId = service.createWorkOrder(vehicleId, note, selected); - System.out.println("Vorgang angelegt mit ID " + orderId); + + System.out.println("\n--- Kundenliste ---"); + persons.forEach(System.out::println); } - private void showCatalog(List items) { - System.out.println("Katalog:"); - items.forEach(item -> System.out.println(item.toString())); + // ---------------- Fahrzeuge ---------------- + + private void addVehicle() throws SQLException { + int personId = readInt("Personen-ID: "); + Person person = service.findPerson(personId); + + if (person == null) { + System.out.println("Person wurde nicht gefunden."); + return; + } + + String brand = readNonEmpty("Hersteller: "); + String model = readNonEmpty("Modell: "); + String plate = readNonEmpty("Kennzeichen: "); + + Vehicle vehicle = service.createVehicle(personId, brand, model, plate); + System.out.println("Fahrzeug angelegt (ID: " + vehicle.getId() + ")"); } - private void listWorkOrders() throws SQLException { + private void showVehiclesOfPerson() throws SQLException { + int personId = readInt("Personen-ID: "); + Person person = service.findPerson(personId); + + if (person == null) { + System.out.println("Person nicht gefunden."); + return; + } + + List vehicles = service.listVehicles(personId); + + System.out.println("Fahrzeuge von " + person.getName() + ":"); + if (vehicles.isEmpty()) { + System.out.println("Keine Fahrzeuge vorhanden."); + } else { + vehicles.forEach(System.out::println); + } + } + + // ---------------- Werkstattvorgänge ---------------- + + private void createWorkOrder() throws SQLException { + int vehicleId = readInt("Fahrzeug-ID: "); + Vehicle vehicle = service.findVehicle(vehicleId); + + if (vehicle == null) { + System.out.println("Fahrzeug nicht gefunden."); + return; + } + + String note = readNonEmpty("Notiz: "); + + List catalog = service.listServiceItems(); + Map selectedItems = new LinkedHashMap<>(); + + while (true) { + System.out.println("\n--- Service-Katalog ---"); + catalog.forEach(System.out::println); + + System.out.print("Service-ID (leer = fertig): "); + String input = scanner.nextLine().trim(); + + if (input.isEmpty()) { + break; + } + + int serviceId; + try { + serviceId = Integer.parseInt(input); + } catch (NumberFormatException e) { + System.out.println("Bitte eine gültige ID eingeben."); + continue; + } + + ServiceItem item = catalog.stream() + .filter(s -> s.getId() == serviceId) + .findFirst() + .orElse(null); + + if (item == null) { + System.out.println("Service nicht gefunden."); + continue; + } + + int qty = readInt("Menge: "); + if (qty <= 0) { + System.out.println("Menge muss größer als 0 sein."); + continue; + } + + selectedItems.merge(serviceId, qty, Integer::sum); + } + + if (selectedItems.isEmpty()) { + System.out.println("Keine Positionen ausgewählt."); + return; + } + + int orderId = service.createWorkOrder(vehicleId, note, selectedItems); + System.out.println("Vorgang wurde angelegt (ID: " + orderId + ")"); + } + + private void showWorkOrders() throws SQLException { List orders = service.listWorkOrders(); + if (orders.isEmpty()) { - System.out.println("Keine Vorgänge gespeichert"); + System.out.println("Keine Vorgänge vorhanden."); return; } + for (WorkOrderDetails order : orders) { - System.out.println("--- Vorgang " + order.getOrderId() + " ---"); + System.out.println("\n--- Vorgang " + order.getOrderId() + " ---"); System.out.println("Kunde: " + order.getPersonName()); System.out.println("Fahrzeug: " + order.getVehicleText()); System.out.println("Datum: " + order.getCreatedAt()); + if (order.getNote() != null && !order.getNote().isBlank()) { System.out.println("Notiz: " + order.getNote()); } + for (WorkOrderItem item : order.getItems()) { - System.out.println(" * " + item.getServiceItem().getTitle() + " x" + item.getQuantity() + + System.out.println(" - " + item.getServiceItem().getTitle() + + " x" + item.getQuantity() + " = " + item.getTotalPrice() + " €"); } - System.out.println("Summe: " + order.getTotal() + " €"); - } - } - private void importPersons() { - System.out.print("Pfad zur CSV-Datei: "); - String path = scanner.nextLine(); - IImporter importer = new CsvPersonImporter(); - try { - List imported = importer.importData(path); - int count = 0; - for (Person p : imported) { - service.createPerson(p.getName(), p.getPhone()); - count++; - } - System.out.println(count + " Personen erfolgreich importiert."); - } catch (Exception e) { - System.out.println("Fehler beim Import: " + e.getMessage()); - } - } - - private void exportPersons() { - System.out.print("Pfad zur Ziel-Datei: "); - String path = scanner.nextLine(); - try { - List persons = service.listPersons(); - IExporter exporter = new CsvPersonExporter(); - exporter.exportData(persons, path); - System.out.println(persons.size() + " Personen exportiert."); - } catch (Exception e) { - System.out.println("Fehler beim Export: " + e.getMessage()); - } - } - - private int parseInt(String input) { - try { - return Integer.parseInt(input.trim()); - } catch (NumberFormatException e) { - return -1; + System.out.println("Gesamtsumme: " + order.getTotal() + " €"); } } } diff --git a/target/classes/de/antco/projekt/ConsoleUi.class b/target/classes/de/antco/projekt/ConsoleUi.class index ef101c3..fa07774 100644 Binary files a/target/classes/de/antco/projekt/ConsoleUi.class and b/target/classes/de/antco/projekt/ConsoleUi.class differ diff --git a/target/classes/de/antco/projekt/Exporters/IExporter.class b/target/classes/de/antco/projekt/Exporters/IExporter.class deleted file mode 100644 index 06482d1..0000000 Binary files a/target/classes/de/antco/projekt/Exporters/IExporter.class and /dev/null differ diff --git a/target/classes/de/antco/projekt/Importers/IImporter.class b/target/classes/de/antco/projekt/Importers/IImporter.class deleted file mode 100644 index 12168a7..0000000 Binary files a/target/classes/de/antco/projekt/Importers/IImporter.class and /dev/null differ