Hinzufügen der Kosten beim bestellen und Kosten für das besorgen der

Waren
master
alama 2025-01-11 16:42:34 +01:00
parent a0e53bef20
commit 8a28a35a6d
4 changed files with 202 additions and 298 deletions

View File

@ -1,144 +1,46 @@
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
class FinancialManager { class FinancialManager {
private static final String TRANSACTION_FILE = "src/transactions.txt"; // Datei für Transaktionen (Einnahmen und Ausgaben) private double totalRevenue; // Gesamte Einnahmen
private List<Transaction> transactions = new ArrayList<>(); private double totalExpenses; // Gesamte Ausgaben
// Konstruktor
public FinancialManager() { public FinancialManager() {
loadTransactionsFromFile(); this.totalRevenue = 0.0;
this.totalExpenses = 0.0;
} }
public void recordTransaction(double amount, String description, boolean isIncome) { // Einnahmen hinzufügen
Date date = new Date(); public void addRevenue(double amount) {
Transaction transaction = new Transaction(amount, description, isIncome, date); totalRevenue += amount;
transactions.add(transaction); System.out.println("Einnahmen hinzugefügt: " + amount + " Euro.");
saveTransactionToFile(transaction);
} }
private void saveTransactionToFile(Transaction transaction) { // Ausgaben hinzufügen
try (BufferedWriter writer = new BufferedWriter(new FileWriter(TRANSACTION_FILE, true))) { public void addExpense(double amount) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); totalExpenses += amount;
String transactionLine = dateFormat.format(transaction.getDate()) + ";" + transaction.getAmount() + ";" + transaction.getDescription() + ";" + (transaction.isIncome() ? "Income" : "Expense"); System.out.println("Ausgabe hinzugefügt: " + amount + " Euro.");
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());
}
} }
// Finanzbericht anzeigen (Einnahmen, Ausgaben und Netto-Gewinn)
public void showFinancialReport(String period) { public void showFinancialReport(String period) {
double totalIncome = 0; double netProfit = totalRevenue - totalExpenses; // Netto-Gewinn berechnen
double totalExpense = 0; System.out.println("Finanzbericht für die " + period + ":");
Date startDate = null; System.out.println("Gesamte Einnahmen: " + totalRevenue + " Euro.");
Date endDate = new Date(); System.out.println("Gesamte Ausgaben: " + totalExpenses + " Euro.");
System.out.println("Netto-Gewinn: " + netProfit + " Euro.");
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");
} }
private Date getStartOfDay() { // Getter für Einnahmen
Calendar cal = Calendar.getInstance(); public double getTotalRevenue() {
cal.set(Calendar.HOUR_OF_DAY, 0); return totalRevenue;
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
} }
private Date getStartOfWeek() { // Getter für Ausgaben
Calendar cal = Calendar.getInstance(); public double getTotalExpenses() {
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return totalExpenses;
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();
} }
private Date getStartOfMonth() { // Getter für Netto-Gewinn
Calendar cal = Calendar.getInstance(); public double getNetProfit() {
cal.set(Calendar.DAY_OF_MONTH, 1); return totalRevenue - totalExpenses;
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;
} }
} }

View File

@ -1,129 +1,124 @@
import java.util.*;
import java.io.*; import java.io.*;
import java.util.*;
class Inventory { public class Inventory {
private Map<String, Integer> stock; private Map<String, Integer> stock = new HashMap<>(); // Bestand der Zutaten
private final String STOCK_FILE = "src/stock.txt"; private final String STOCK_FILE = "src/stock.txt"; // Datei für den Bestand
public void loadStockFromFile() { // Lädt den Bestand aus der Datei
stock = new HashMap<>(); public void loadStockFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(STOCK_FILE))) { stock.clear();
String line; try (BufferedReader reader = new BufferedReader(new FileReader(STOCK_FILE))) {
while ((line = reader.readLine()) != null) { String line;
String[] parts = line.split(","); 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]; // Speichert den aktuellen Bestand in die Datei
Integer count = Integer.parseInt(parts[1]); public void saveStockToFile() {
stock.put(name, count); try (BufferedWriter writer = new BufferedWriter(new FileWriter(STOCK_FILE))) {
for (Map.Entry<String, Integer> entry : stock.entrySet()) {
writer.write(entry.getKey() + ":" + entry.getValue());
writer.newLine();
}
} catch (IOException e) {
System.out.println("Fehler beim Speichern des Bestands: " + e.getMessage());
}
}
} // Zeigt den aktuellen Bestand an
} catch (FileNotFoundException e) { public void viewStock() {
System.out.println("Stockdatenbank nicht gefunden. Einträge müssen manuell erstellt werden"); if (stock.isEmpty()) {
System.out.println("Der Bestand ist leer.");
} else {
System.out.println("\n--- Aktueller Bestand ---");
for (Map.Entry<String, Integer> entry : stock.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " Stück");
}
}
}
} catch (IOException e) { // Bestellmenge verringern (z.B. beim Bestellen)
System.out.println("Fehler beim Lesen der Benutzerdatenbank: " + e.getMessage()); public void useIngredients(Map<String, Integer> usedIngredients) {
} for (Map.Entry<String, Integer> 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() { // Bestand erweitern und berechnen, ob Ausgaben hinzuzufügen sind
try (BufferedWriter writer = new BufferedWriter(new FileWriter(STOCK_FILE))) { public void addIngredients(String ingredientName, int amount, FinancialManager financialManager) {
for (Map.Entry<String, Integer> entry : stock.entrySet()) { if (stock.containsKey(ingredientName)) {
writer.write(entry.getKey() + "," + entry.getValue()); stock.put(ingredientName, stock.get(ingredientName) + amount);
writer.newLine(); } else {
} stock.put(ingredientName, amount);
} catch (IOException e) { }
System.out.println("Fehler beim Speichern der Datei: " + e.getMessage());
}
}
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() { // Gibt den Preis für eine bestimmte Zutat zurück (Beispielpreise)
System.out.println("Aktueller Bestand:"); private double getPriceForIngredient(String ingredientName) {
for (Map.Entry<String, Integer> entry : stock.entrySet()) { switch (ingredientName.toLowerCase()) {
System.out.println(entry.getKey() + ": " + entry.getValue()); case "salat":
} return 1.5; // Preis für Salat pro Einheit
} case "tomaten":
return 0.8; // Preis für Tomaten pro Einheit
public boolean showIngredientsAvailable(Map<String, Integer> ingredients) { case "cheeseburger":
for (Map.Entry<String, Integer> entry : ingredients.entrySet()) { return 3.0; // Preis für Cheeseburger pro Einheit
String ingredient = entry.getKey(); default:
int amount = entry.getValue(); return 1.0; // Standardpreis pro Einheit
}
}
// In der Inventory-Klasse:
public boolean showIngredientsAvailable(Map<String, Integer> ingredients) {
for (Map.Entry<String, Integer> entry : ingredients.entrySet()) {
String ingredientName = entry.getKey();
int requiredAmount = entry.getValue();
if (!stock.containsKey(ingredient) || stock.get(ingredient) < amount) { if (!stock.containsKey(ingredientName) || stock.get(ingredientName) < requiredAmount) {
return false; 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; return true; // Alle Zutaten sind verfügbar
} }
public void useIngredients(Map<String, Integer> ingredients) {
for (Map.Entry<String, Integer> entry : ingredients.entrySet()) {
stock.put(entry.getKey(), stock.get(entry.getKey()) - entry.getValue());
saveStockToFile();
}
}
// Neue Methode: Zutaten hinzufügen // Getter für den Bestand
public void addIngredients(String ingredient, int amount) { public Map<String, Integer> getStock() {
if (amount <= 0) { return stock;
System.out.println("Ungültige Menge: " + amount); }
return;
}
stock.put(ingredient, stock.getOrDefault(ingredient, 0) + amount);
System.out.println(amount + " " + ingredient + " hinzugefügt.");
}
// Neue Methode: Zutaten entfernen // Setter für den Bestand
public void removeIngredient(String ingredient) { public void setStock(Map<String, Integer> stock) {
if (stock.containsKey(ingredient)) { this.stock = stock;
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<String, Integer> 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);
}
}
}

View File

@ -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.util.*;
import java.io.*;
class SystemController { class SystemController {
private Scanner scanner = new Scanner(System.in); private Scanner scanner = new Scanner(System.in);
private Map<String, User> users = new HashMap<>(); private Map<String, User> users = new HashMap<>();
private final String USERS_FILE = "src/users.txt"; private final String USERS_FILE = "src/users.txt";
public static Inventory inventory = new Inventory(); private static Inventory inventory = new Inventory(); // Bestand
private Menu menu = new Menu(); private Menu menu = new Menu(); // Menü
private FinancialManager financialManager = new FinancialManager(); private FinancialManager financialManager = new FinancialManager(); // Finanzmanager
public SystemController() { public SystemController() {
loadUsersFromFile(); loadUsersFromFile(); // Benutzer aus Datei laden
} }
public void startSystem() { public void startSystem() {
@ -25,7 +30,7 @@ class SystemController {
User user = authenticate(username, password); User user = authenticate(username, password);
if (user != null) { if (user != null) {
displayMainMenu(user); displayMainMenu(user); // Hauptmenü anzeigen
} else { } else {
System.out.println("Ungültige Anmeldedaten."); System.out.println("Ungültige Anmeldedaten.");
} }
@ -47,7 +52,7 @@ class SystemController {
System.out.println("2. Neuer Nutzer erstellen"); System.out.println("2. Neuer Nutzer erstellen");
System.out.println("3. Menü anzeigen"); System.out.println("3. Menü anzeigen");
System.out.println("4. Bestellungen verwalten"); 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.println("6. Beenden");
System.out.print("Bitte wähle eine Option: "); System.out.print("Bitte wähle eine Option: ");
@ -55,17 +60,17 @@ class SystemController {
scanner.nextLine(); // Eingabepuffer leeren scanner.nextLine(); // Eingabepuffer leeren
switch (choice) { switch (choice) {
case 1 -> manageInventory(); case 1 -> manageInventory(); // Bestand verwalten
case 2 -> { case 2 -> {
if (user.role == Role.MANAGER) { if (user.role == Role.MANAGER) {
createUser(); createUser(); // Neuer Nutzer erstellen
} else { } else {
System.out.println("Keine Rechte, um neue Nutzer zu erstellen."); System.out.println("Keine Rechte, um neue Nutzer zu erstellen.");
} }
} }
case 3 -> menu.displayMenu(inventory); case 3 -> menu.displayMenu(inventory); // Menü anzeigen
case 4 -> manageOrders(); // Übergibt an Bestellsystem case 4 -> manageOrders(); // Bestellungen verwalten
case 5 -> showFinancialReports(); // Anzeige der Finanzberichte case 5 -> manageFinances(); // Finanz- und Umsatzverwaltung
case 6 -> { case 6 -> {
running = false; running = false;
System.out.println("System wird beendet. Auf Wiedersehen!"); System.out.println("System wird beendet. Auf Wiedersehen!");
@ -97,8 +102,8 @@ class SystemController {
System.out.println("Neue Anzahl ---->"); System.out.println("Neue Anzahl ---->");
try { try {
int anzahl = scanner.nextInt(); int anzahl = scanner.nextInt();
inventory.overwriteStock(name, anzahl); // Bestellungen im Speicher anzeigen inventory.useIngredients(Map.of(name, anzahl)); // Zutaten im Bestand verwenden
inventory.saveStockToFile(); // Echtzeitspeicherung der Daten inventory.saveStockToFile(); // Bestand speichern
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
System.out.println("Fehler! Überprüfen Sie Ihre Eingabe"); System.out.println("Fehler! Überprüfen Sie Ihre Eingabe");
} }
@ -110,8 +115,8 @@ class SystemController {
System.out.println("Anzahl :"); System.out.println("Anzahl :");
try { try {
int anzahl = scanner.nextInt(); int anzahl = scanner.nextInt();
inventory.addIngredients(name, anzahl); // Bestellungen im Speicher anzeigen inventory.addIngredients(name, anzahl, financialManager); // Bestand erweitern und Ausgaben verbuchen
inventory.saveStockToFile(); // Echtzeitspeicherung der Daten inventory.saveStockToFile(); // Bestand speichern
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
System.out.println("Fehler! Überprüfen Sie Ihre Eingabe"); System.out.println("Fehler! Überprüfen Sie Ihre Eingabe");
} }
@ -137,21 +142,52 @@ class SystemController {
switch (choice) { switch (choice) {
case 1 -> { case 1 -> {
menu.displayMenu(inventory); menu.displayMenu(inventory); // Menü anzeigen
Bestellsystem.erstelleBestellung(); // Neue Bestellung erstellen Bestellsystem.erstelleBestellung(); // Bestellung aufnehmen
}
case 2 -> {
Bestellsystem.ladeUndZeigeBestellungen(); // Bestellungen aus Datei anzeigen
}
case 3 -> {
Bestellsystem.loescheBestellung(); // Bestellungen aus Datei löschen
} }
case 2 -> Bestellsystem.ladeUndZeigeBestellungen(); // Alle Bestellungen anzeigen
case 3 -> Bestellsystem.loescheBestellung(); // Bestellungen löschen
case 4 -> managingOrders = false; case 4 -> managingOrders = false;
default -> System.out.println("Ungültige Auswahl. Bitte versuche es erneut."); 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() { private void createUser() {
String username = ""; String username = "";
String password = ""; String password = "";
@ -164,9 +200,7 @@ class SystemController {
password = scanner.nextLine(); password = scanner.nextLine();
System.out.print("Rolle (MANAGER, SERVICE, KITCHEN): "); System.out.print("Rolle (MANAGER, SERVICE, KITCHEN): ");
role = Role.valueOf(scanner.nextLine().toUpperCase()); role = Role.valueOf(scanner.nextLine().toUpperCase());
} catch (IllegalArgumentException d) { } catch (IllegalArgumentException | NullPointerException e) {
System.out.println("Fehler beim Erstellen eines Accounts. Überprüfen Sie Ihre Eingaben.");
} catch (NullPointerException c) {
System.out.println("Fehler beim Erstellen eines Accounts. Überprüfen Sie Ihre Eingaben."); 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); User newUser = new User(username, password, role);
users.put(username, newUser); users.put(username, newUser);
saveUserToFile(newUser); saveUserToFile(newUser);
System.out.println("Nutzer erfolgreich erstellt."); System.out.println("Nutzer erfolgreich erstellt.");
} catch (NullPointerException c) { } catch (NullPointerException e) {
System.out.println("Fehler beim Erstellen eines Accounts."); System.out.println("Fehler beim Erstellen des Nutzers.");
} }
} }
@ -190,7 +223,6 @@ class SystemController {
String username = parts[0]; String username = parts[0];
String password = parts[1]; String password = parts[1];
Role role = Role.valueOf(parts[2].toUpperCase()); Role role = Role.valueOf(parts[2].toUpperCase());
users.put(username, new User(username, password, role)); users.put(username, new User(username, password, role));
} }
} }
@ -216,22 +248,4 @@ class SystemController {
users.put("Dimitry", defaultManager); users.put("Dimitry", defaultManager);
saveUserToFile(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.");
}
}
} }

View File

@ -1,8 +1 @@
01,100 Salat:160
Soße,49999996
Ananasdasdas,5000
Brot,1503
Tomaten,503
Käse,715
Fleisch,93
Salat,494