From 8a28a35a6ddaa0ae02dd266ea9d3e2320788e0d8 Mon Sep 17 00:00:00 2001 From: alama Date: Sat, 11 Jan 2025 16:42:34 +0100 Subject: [PATCH] =?UTF-8?q?Hinzuf=C3=BCgen=20der=20Kosten=20beim=20bestell?= =?UTF-8?q?en=20und=20Kosten=20f=C3=BCr=20das=20besorgen=20der=20Waren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/FinancialManager.java | 154 +++--------- Restaurantverwaltung/src/Inventory.java | 225 +++++++++--------- .../src/SystemController.java | 112 +++++---- Restaurantverwaltung/src/stock.txt | 9 +- 4 files changed, 202 insertions(+), 298 deletions(-) diff --git a/Restaurantverwaltung/src/FinancialManager.java b/Restaurantverwaltung/src/FinancialManager.java index 8085e87..755ce01 100644 --- a/Restaurantverwaltung/src/FinancialManager.java +++ b/Restaurantverwaltung/src/FinancialManager.java @@ -1,144 +1,46 @@ -import java.io.*; -import java.text.SimpleDateFormat; -import java.util.*; - class FinancialManager { - private static final String TRANSACTION_FILE = "src/transactions.txt"; // Datei für Transaktionen (Einnahmen und Ausgaben) - private List transactions = new ArrayList<>(); + private double totalRevenue; // Gesamte Einnahmen + private double totalExpenses; // Gesamte Ausgaben + // Konstruktor public FinancialManager() { - loadTransactionsFromFile(); + this.totalRevenue = 0.0; + this.totalExpenses = 0.0; } - public void recordTransaction(double amount, String description, boolean isIncome) { - Date date = new Date(); - Transaction transaction = new Transaction(amount, description, isIncome, date); - transactions.add(transaction); - saveTransactionToFile(transaction); + // Einnahmen hinzufügen + public void addRevenue(double amount) { + totalRevenue += amount; + System.out.println("Einnahmen hinzugefügt: " + amount + " Euro."); } - private void saveTransactionToFile(Transaction transaction) { - try (BufferedWriter writer = new BufferedWriter(new FileWriter(TRANSACTION_FILE, true))) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String transactionLine = dateFormat.format(transaction.getDate()) + ";" + transaction.getAmount() + ";" + transaction.getDescription() + ";" + (transaction.isIncome() ? "Income" : "Expense"); - writer.write(transactionLine); - writer.newLine(); - } catch (IOException e) { - System.out.println("Fehler beim Speichern der Transaktion: " + e.getMessage()); - } - } - - private void loadTransactionsFromFile() { - try (BufferedReader reader = new BufferedReader(new FileReader(TRANSACTION_FILE))) { - String line; - while ((line = reader.readLine()) != null) { - String[] parts = line.split(";"); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date = dateFormat.parse(parts[0]); - double amount = Double.parseDouble(parts[1]); - String description = parts[2]; - boolean isIncome = "Income".equals(parts[3]); - transactions.add(new Transaction(amount, description, isIncome, date)); - } - } catch (IOException | java.text.ParseException e) { - System.out.println("Fehler beim Laden der Transaktionen: " + e.getMessage()); - } + // Ausgaben hinzufügen + public void addExpense(double amount) { + totalExpenses += amount; + System.out.println("Ausgabe hinzugefügt: " + amount + " Euro."); } + // Finanzbericht anzeigen (Einnahmen, Ausgaben und Netto-Gewinn) public void showFinancialReport(String period) { - double totalIncome = 0; - double totalExpense = 0; - Date startDate = null; - Date endDate = new Date(); - - switch (period.toLowerCase()) { - case "daily": - startDate = getStartOfDay(); - break; - case "weekly": - startDate = getStartOfWeek(); - break; - case "monthly": - startDate = getStartOfMonth(); - break; - default: - System.out.println("Ungültige Zeitraumoption!"); - return; - } - - // Berechnen der Einnahmen und Ausgaben im angegebenen Zeitraum - for (Transaction transaction : transactions) { - if (!transaction.getDate().before(startDate) && !transaction.getDate().after(endDate)) { - if (transaction.isIncome()) { - totalIncome += transaction.getAmount(); - } else { - totalExpense += transaction.getAmount(); - } - } - } - - System.out.println("\n--- Finanzbericht für " + period + " ---"); - System.out.println("Gesamte Einnahmen: " + totalIncome + " EUR"); - System.out.println("Gesamte Ausgaben: " + totalExpense + " EUR"); - System.out.println("Nettogewinn: " + (totalIncome - totalExpense) + " EUR"); + double netProfit = totalRevenue - totalExpenses; // Netto-Gewinn berechnen + System.out.println("Finanzbericht für die " + period + ":"); + System.out.println("Gesamte Einnahmen: " + totalRevenue + " Euro."); + System.out.println("Gesamte Ausgaben: " + totalExpenses + " Euro."); + System.out.println("Netto-Gewinn: " + netProfit + " Euro."); } - private Date getStartOfDay() { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - return cal.getTime(); + // Getter für Einnahmen + public double getTotalRevenue() { + return totalRevenue; } - private Date getStartOfWeek() { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - return cal.getTime(); + // Getter für Ausgaben + public double getTotalExpenses() { + return totalExpenses; } - private Date getStartOfMonth() { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.DAY_OF_MONTH, 1); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - return cal.getTime(); - } -} - -class Transaction { - private double amount; - private String description; - private boolean isIncome; - private Date date; - - public Transaction(double amount, String description, boolean isIncome, Date date) { - this.amount = amount; - this.description = description; - this.isIncome = isIncome; - this.date = date; - } - - public double getAmount() { - return amount; - } - - public String getDescription() { - return description; - } - - public boolean isIncome() { - return isIncome; - } - - public Date getDate() { - return date; + // Getter für Netto-Gewinn + public double getNetProfit() { + return totalRevenue - totalExpenses; } } diff --git a/Restaurantverwaltung/src/Inventory.java b/Restaurantverwaltung/src/Inventory.java index ba6ff17..ac91a1e 100644 --- a/Restaurantverwaltung/src/Inventory.java +++ b/Restaurantverwaltung/src/Inventory.java @@ -1,129 +1,124 @@ -import java.util.*; import java.io.*; +import java.util.*; -class Inventory { - private Map stock; - private final String STOCK_FILE = "src/stock.txt"; - - +public class Inventory { + private Map stock = new HashMap<>(); // Bestand der Zutaten + private final String STOCK_FILE = "src/stock.txt"; // Datei für den Bestand - public void loadStockFromFile() { - stock = new HashMap<>(); - try (BufferedReader reader = new BufferedReader(new FileReader(STOCK_FILE))) { - String line; - while ((line = reader.readLine()) != null) { - String[] parts = line.split(","); + // Lädt den Bestand aus der Datei + public void loadStockFromFile() { + stock.clear(); + try (BufferedReader reader = new BufferedReader(new FileReader(STOCK_FILE))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(":"); + if (parts.length == 2) { + String ingredientName = parts[0]; + int quantity = Integer.parseInt(parts[1]); + stock.put(ingredientName, quantity); + } + } + } catch (FileNotFoundException e) { + System.out.println("Bestand-Datei nicht gefunden, neue Datei wird erstellt."); + } catch (IOException e) { + System.out.println("Fehler beim Laden des Bestands: " + e.getMessage()); + } + } - String name = parts[0]; - Integer count = Integer.parseInt(parts[1]); - stock.put(name, count); + // Speichert den aktuellen Bestand in die Datei + public void saveStockToFile() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(STOCK_FILE))) { + for (Map.Entry entry : stock.entrySet()) { + writer.write(entry.getKey() + ":" + entry.getValue()); + writer.newLine(); + } + } catch (IOException e) { + System.out.println("Fehler beim Speichern des Bestands: " + e.getMessage()); + } + } - } - } catch (FileNotFoundException e) { - System.out.println("Stockdatenbank nicht gefunden. Einträge müssen manuell erstellt werden"); + // Zeigt den aktuellen Bestand an + public void viewStock() { + if (stock.isEmpty()) { + System.out.println("Der Bestand ist leer."); + } else { + System.out.println("\n--- Aktueller Bestand ---"); + for (Map.Entry entry : stock.entrySet()) { + System.out.println(entry.getKey() + ": " + entry.getValue() + " Stück"); + } + } + } - } catch (IOException e) { - System.out.println("Fehler beim Lesen der Benutzerdatenbank: " + e.getMessage()); - } - } + // Bestellmenge verringern (z.B. beim Bestellen) + public void useIngredients(Map usedIngredients) { + for (Map.Entry entry : usedIngredients.entrySet()) { + String ingredientName = entry.getKey(); + int amountUsed = entry.getValue(); + if (stock.containsKey(ingredientName)) { + int currentStock = stock.get(ingredientName); + if (currentStock >= amountUsed) { + stock.put(ingredientName, currentStock - amountUsed); + } else { + System.out.println("Nicht genügend Bestand für " + ingredientName); + } + } else { + System.out.println("Zutat " + ingredientName + " existiert nicht im Bestand."); + } + } + } - public void saveStockToFile() { - try (BufferedWriter writer = new BufferedWriter(new FileWriter(STOCK_FILE))) { - for (Map.Entry entry : stock.entrySet()) { - writer.write(entry.getKey() + "," + entry.getValue()); - writer.newLine(); - } - } catch (IOException e) { - System.out.println("Fehler beim Speichern der Datei: " + e.getMessage()); - } - } + // Bestand erweitern und berechnen, ob Ausgaben hinzuzufügen sind + public void addIngredients(String ingredientName, int amount, FinancialManager financialManager) { + if (stock.containsKey(ingredientName)) { + stock.put(ingredientName, stock.get(ingredientName) + amount); + } else { + stock.put(ingredientName, amount); + } - public Inventory() { + // Preis für den Einkauf von Zutaten (hier Beispielwerte für Salat und Tomaten) + double pricePerUnit = getPriceForIngredient(ingredientName); + double totalCost = pricePerUnit * amount; - } + // Finanzmanager über die Ausgaben informieren + financialManager.addExpense(totalCost); + } - public void viewStock() { - System.out.println("Aktueller Bestand:"); - for (Map.Entry entry : stock.entrySet()) { - System.out.println(entry.getKey() + ": " + entry.getValue()); - } - } - - public boolean showIngredientsAvailable(Map ingredients) { - for (Map.Entry entry : ingredients.entrySet()) { - String ingredient = entry.getKey(); - int amount = entry.getValue(); + // Gibt den Preis für eine bestimmte Zutat zurück (Beispielpreise) + private double getPriceForIngredient(String ingredientName) { + switch (ingredientName.toLowerCase()) { + case "salat": + return 1.5; // Preis für Salat pro Einheit + case "tomaten": + return 0.8; // Preis für Tomaten pro Einheit + case "cheeseburger": + return 3.0; // Preis für Cheeseburger pro Einheit + default: + return 1.0; // Standardpreis pro Einheit + } + } + + // In der Inventory-Klasse: + public boolean showIngredientsAvailable(Map ingredients) { + for (Map.Entry entry : ingredients.entrySet()) { + String ingredientName = entry.getKey(); + int requiredAmount = entry.getValue(); - if (!stock.containsKey(ingredient) || stock.get(ingredient) < amount) { - return false; - } - } - - return true; - } + if (!stock.containsKey(ingredientName) || stock.get(ingredientName) < requiredAmount) { + System.out.println("Nicht genügend Bestand für: " + ingredientName); + return false; // Wenn eine Zutat nicht genug vorhanden ist, gibt die Methode false zurück + } + } + return true; // Alle Zutaten sind verfügbar + } - public void useIngredients(Map ingredients) { - - for (Map.Entry entry : ingredients.entrySet()) { - stock.put(entry.getKey(), stock.get(entry.getKey()) - entry.getValue()); - saveStockToFile(); - } - - } - // Neue Methode: Zutaten hinzufügen - public void addIngredients(String ingredient, int amount) { - if (amount <= 0) { - System.out.println("Ungültige Menge: " + amount); - return; - } - stock.put(ingredient, stock.getOrDefault(ingredient, 0) + amount); - System.out.println(amount + " " + ingredient + " hinzugefügt."); - } + // Getter für den Bestand + public Map getStock() { + return stock; + } - // Neue Methode: Zutaten entfernen - public void removeIngredient(String ingredient) { - if (stock.containsKey(ingredient)) { - stock.remove(ingredient); - System.out.println(ingredient + " wurde aus dem Lager entfernt."); - } else { - System.out.println(ingredient + " ist nicht im Lager vorhanden."); - } - } - - // Neue Methode: Gesamten Bestand prüfen - public int totalStock() { - int total = 0; - for (int amount : stock.values()) { - total += amount; - } - return total; - } - - // Neue Methode: Warnungen bei niedrigem Bestand - public void lowStockWarning(int threshold) { - System.out.println("Warnung: Niedriger Bestand bei folgenden Zutaten:"); - for (Map.Entry entry : stock.entrySet()) { - if (entry.getValue() < threshold) { - System.out.println(entry.getKey() + ": " + entry.getValue()); - } - } - } - - - - // Neue Methode: Zutatenbestand überschreiben - public void overwriteStock(String ingredient, int amount) { - if (amount < 0) { - System.out.println("Ungültige Menge: " + amount); - return; - } - if (stock.containsKey(ingredient)) { - stock.put(ingredient, amount); - System.out.println(ingredient + " Bestand auf " + amount + " gesetzt."); - } else { - System.out.println(ingredient + " ist nicht im Lager vorhanden. Hinzufügen mit neuer Menge."); - stock.put(ingredient, amount); - } - } -} \ No newline at end of file + // Setter für den Bestand + public void setStock(Map stock) { + this.stock = stock; + } +} diff --git a/Restaurantverwaltung/src/SystemController.java b/Restaurantverwaltung/src/SystemController.java index 84ab99f..641dbca 100644 --- a/Restaurantverwaltung/src/SystemController.java +++ b/Restaurantverwaltung/src/SystemController.java @@ -1,16 +1,21 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.*; -import java.io.*; class SystemController { private Scanner scanner = new Scanner(System.in); private Map users = new HashMap<>(); private final String USERS_FILE = "src/users.txt"; - public static Inventory inventory = new Inventory(); - private Menu menu = new Menu(); - private FinancialManager financialManager = new FinancialManager(); + private static Inventory inventory = new Inventory(); // Bestand + private Menu menu = new Menu(); // Menü + private FinancialManager financialManager = new FinancialManager(); // Finanzmanager public SystemController() { - loadUsersFromFile(); + loadUsersFromFile(); // Benutzer aus Datei laden } public void startSystem() { @@ -25,7 +30,7 @@ class SystemController { User user = authenticate(username, password); if (user != null) { - displayMainMenu(user); + displayMainMenu(user); // Hauptmenü anzeigen } else { System.out.println("Ungültige Anmeldedaten."); } @@ -47,7 +52,7 @@ class SystemController { System.out.println("2. Neuer Nutzer erstellen"); System.out.println("3. Menü anzeigen"); System.out.println("4. Bestellungen verwalten"); - System.out.println("5. Finanzbericht anzeigen"); + System.out.println("5. Finanz- und Umsatzverwaltung"); System.out.println("6. Beenden"); System.out.print("Bitte wähle eine Option: "); @@ -55,17 +60,17 @@ class SystemController { scanner.nextLine(); // Eingabepuffer leeren switch (choice) { - case 1 -> manageInventory(); + case 1 -> manageInventory(); // Bestand verwalten case 2 -> { if (user.role == Role.MANAGER) { - createUser(); + createUser(); // Neuer Nutzer erstellen } else { System.out.println("Keine Rechte, um neue Nutzer zu erstellen."); } } - case 3 -> menu.displayMenu(inventory); - case 4 -> manageOrders(); // Übergibt an Bestellsystem - case 5 -> showFinancialReports(); // Anzeige der Finanzberichte + case 3 -> menu.displayMenu(inventory); // Menü anzeigen + case 4 -> manageOrders(); // Bestellungen verwalten + case 5 -> manageFinances(); // Finanz- und Umsatzverwaltung case 6 -> { running = false; System.out.println("System wird beendet. Auf Wiedersehen!"); @@ -97,8 +102,8 @@ class SystemController { System.out.println("Neue Anzahl ---->"); try { int anzahl = scanner.nextInt(); - inventory.overwriteStock(name, anzahl); // Bestellungen im Speicher anzeigen - inventory.saveStockToFile(); // Echtzeitspeicherung der Daten + inventory.useIngredients(Map.of(name, anzahl)); // Zutaten im Bestand verwenden + inventory.saveStockToFile(); // Bestand speichern } catch (IllegalArgumentException e) { System.out.println("Fehler! Überprüfen Sie Ihre Eingabe"); } @@ -110,8 +115,8 @@ class SystemController { System.out.println("Anzahl :"); try { int anzahl = scanner.nextInt(); - inventory.addIngredients(name, anzahl); // Bestellungen im Speicher anzeigen - inventory.saveStockToFile(); // Echtzeitspeicherung der Daten + inventory.addIngredients(name, anzahl, financialManager); // Bestand erweitern und Ausgaben verbuchen + inventory.saveStockToFile(); // Bestand speichern } catch (IllegalArgumentException e) { System.out.println("Fehler! Überprüfen Sie Ihre Eingabe"); } @@ -137,21 +142,52 @@ class SystemController { switch (choice) { case 1 -> { - menu.displayMenu(inventory); - Bestellsystem.erstelleBestellung(); // Neue Bestellung erstellen - } - case 2 -> { - Bestellsystem.ladeUndZeigeBestellungen(); // Bestellungen aus Datei anzeigen - } - case 3 -> { - Bestellsystem.loescheBestellung(); // Bestellungen aus Datei löschen + menu.displayMenu(inventory); // Menü anzeigen + Bestellsystem.erstelleBestellung(); // Bestellung aufnehmen } + case 2 -> Bestellsystem.ladeUndZeigeBestellungen(); // Alle Bestellungen anzeigen + case 3 -> Bestellsystem.loescheBestellung(); // Bestellungen löschen case 4 -> managingOrders = false; default -> System.out.println("Ungültige Auswahl. Bitte versuche es erneut."); } } } + private void manageFinances() { + boolean managingFinances = true; + while (managingFinances) { + System.out.println("\n--- Finanz- und Umsatzverwaltung ---"); + System.out.println("1. Einnahmen und Ausgaben anzeigen"); + System.out.println("2. Umsatzübersicht (Täglich, Wöchentlich, Monatlich)"); + System.out.println("3. Zurück zum Hauptmenü"); + + System.out.print("Bitte wähle eine Option: "); + int choice = scanner.nextInt(); + scanner.nextLine(); // Eingabepuffer leeren + + switch (choice) { + case 1 -> financialManager.showFinancialReport("heute"); // Finanzbericht anzeigen + case 2 -> { + System.out.println("Bitte wählen Sie den Zeitraum:"); + System.out.println("1. Täglich"); + System.out.println("2. Wöchentlich"); + System.out.println("3. Monatlich"); + int periodChoice = scanner.nextInt(); + scanner.nextLine(); // Eingabepuffer leeren + + switch (periodChoice) { + case 1 -> financialManager.showFinancialReport("täglich"); // Täglicher Bericht + case 2 -> financialManager.showFinancialReport("wöchentlich"); // Wöchentlicher Bericht + case 3 -> financialManager.showFinancialReport("monatlich"); // Monatlicher Bericht + default -> System.out.println("Ungültige Auswahl."); + } + } + case 3 -> managingFinances = false; + default -> System.out.println("Ungültige Auswahl. Bitte versuche es erneut."); + } + } + } + private void createUser() { String username = ""; String password = ""; @@ -164,9 +200,7 @@ class SystemController { password = scanner.nextLine(); System.out.print("Rolle (MANAGER, SERVICE, KITCHEN): "); role = Role.valueOf(scanner.nextLine().toUpperCase()); - } catch (IllegalArgumentException d) { - System.out.println("Fehler beim Erstellen eines Accounts. Überprüfen Sie Ihre Eingaben."); - } catch (NullPointerException c) { + } catch (IllegalArgumentException | NullPointerException e) { System.out.println("Fehler beim Erstellen eines Accounts. Überprüfen Sie Ihre Eingaben."); } @@ -174,10 +208,9 @@ class SystemController { User newUser = new User(username, password, role); users.put(username, newUser); saveUserToFile(newUser); - System.out.println("Nutzer erfolgreich erstellt."); - } catch (NullPointerException c) { - System.out.println("Fehler beim Erstellen eines Accounts."); + } catch (NullPointerException e) { + System.out.println("Fehler beim Erstellen des Nutzers."); } } @@ -190,7 +223,6 @@ class SystemController { String username = parts[0]; String password = parts[1]; Role role = Role.valueOf(parts[2].toUpperCase()); - users.put(username, new User(username, password, role)); } } @@ -216,22 +248,4 @@ class SystemController { users.put("Dimitry", defaultManager); saveUserToFile(defaultManager); } - - private void showFinancialReports() { - System.out.println("\n--- Finanzbericht ---"); - System.out.println("Wählen Sie den Zeitraum:"); - System.out.println("1. Täglich"); - System.out.println("2. Wöchentlich"); - System.out.println("3. Monatlich"); - - int periodChoice = scanner.nextInt(); - scanner.nextLine(); // Eingabepuffer leeren - - switch (periodChoice) { - case 1 -> financialManager.showFinancialReport("daily"); - case 2 -> financialManager.showFinancialReport("weekly"); - case 3 -> financialManager.showFinancialReport("monthly"); - default -> System.out.println("Ungültige Auswahl."); - } - } } diff --git a/Restaurantverwaltung/src/stock.txt b/Restaurantverwaltung/src/stock.txt index 7c6ef85..2f66960 100644 --- a/Restaurantverwaltung/src/stock.txt +++ b/Restaurantverwaltung/src/stock.txt @@ -1,8 +1 @@ -01,100 -Soße,49999996 -Ananasdasdas,5000 -Brot,1503 -Tomaten,503 -Käse,715 -Fleisch,93 -Salat,494 +Salat:160