From 61252aae03761842f68a28fc958ccc00e34b6277 Mon Sep 17 00:00:00 2001 From: Daniel Zikol <3020574@stud.hs-mannheim.de> Date: Mon, 15 Dec 2025 05:53:10 +0100 Subject: [PATCH] #Refactored MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Order erweitert aus cart rechnungen entfernt. Trchnungen waren aus KI prompt ü 7 Zeilen <--- musste also entfernen TUI angepasst --- .../main/java/shop/backend/Cart.java | 78 +-------- .../main/java/shop/backend/Order.java | 94 +++++++++- .../main/java/shop/backend/Product.java | 14 +- .../main/java/shop/frontend/ShopTUI.java | 164 +++++++++++------- 4 files changed, 191 insertions(+), 159 deletions(-) diff --git a/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Cart.java b/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Cart.java index fdfc946..22ae69d 100644 --- a/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Cart.java +++ b/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Cart.java @@ -64,14 +64,7 @@ public void removeProduct(int productID){ positions.remove(gettinYETTED); } } - //Price of all pos without them taxes :p - public double getTotalPrice() { - double sum = 0; - for(CartPosition pos : positions){ - sum += pos.getTotalPrice(); - } - return sum; - } + //Auto completion hat eingegriffen lol public boolean isEmpty(){ return positions.isEmpty(); @@ -84,77 +77,8 @@ public void removeProduct(int productID){ public void clear(){ positions.clear(); } - //Brutto aller pos mit mwst - public double getThemBruttoPrices() { - double sum = 0; - for (CartPosition pos : positions) { - sum += pos.getTotalPrice(); - } - return sum; - } - public double getWeight(){ - double weight = 0; - for(CartPosition pos : positions){ - weight += pos.getProductWeight(); - } - return weight; - } - public double getShippingCost(){ - double value = getThemBruttoPrices(); - if(value >= 500) return 0.0; - double weight = getWeight(); - if(weight <= 0.1) return 3.95; - if(weight <= 1.0) return 4.95; - if(weight <= 5.0) return 5.95; - return 19.95; - } - public double getMwstRateOf7(){ - double sum = 0; - for(CartPosition pos : positions){ - if(pos.getMwstRate() == 7); - sum += pos.getMwstAmount(); - } - return sum; - } - public double getMwstRateOf19(){ - double sum = 0; - for(CartPosition pos : positions){ - if(pos.getMwstRate() == 19); - sum += pos.getMwstAmount(); - } - return sum; - } - //Formel und prinzip nach KI-Prompt - public double getFinalTotal(){ - double total7 = 0; - double total19 = 0; - for(CartPosition pos : positions){ - if(pos.getMwstRate() == 7){ - total7 += pos.getTotalBruttoPrice(); - - }else { - total19 += pos.getTotalBruttoPrice(); - } - } - double totalBrutto = total7 + total19; - double shipbrutto = getShippingCost(); - if(shipbrutto > 0){ - if(totalBrutto > 0){ - //Verhältnis und wenn Waren mit unterschiedlichen mwst im warenkorb sind - double ratio7 = total7 / totalBrutto; - double ratio19 = total19 / totalBrutto; - - double shippingBy7 = totalBrutto * ratio7; - double shippingBy19 = totalBrutto * ratio19; - // Hier wird nur der Gesamtbetrag berechnet, nicht die MwSt-Aufteilung - // Die anteiligen Versandkosten werden einfach addiert - return totalBrutto + shippingBy7 + shippingBy19; - } - } - return totalBrutto; - } diff --git a/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Order.java b/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Order.java index 5ca0006..22ef133 100644 --- a/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Order.java +++ b/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Order.java @@ -1,19 +1,95 @@ package shop.backend; -/* -TODO Kassenbon System - */ + +import shop.backend.CartPosition; + +import java.util.ArrayList; +import java.util.List; + public class Order { + private String customerName; + private String address; + private List positions; + private double totalNet; + private double totalBrutto; + private double shippingCost; + private double finalTotal; - Cart cart; - String customerName; - String address; - public Order(Cart cart, String customerName, String address){ - this.cart = cart; + public Order(String customerName, String address, List positions) { this.customerName = customerName; this.address = address; + this.positions = new ArrayList<>(); + + //KI Bug fix + for (CartPosition pos : positions) { + this.positions.add(new CartPosition(pos.getProduct(), pos.getQuantity())); + } + } + private void calculateShippingCost() { + //wenn ü 500 dann ist free + if (totalBrutto >= 500) { + shippingCost = 0.0; + return; + } + double weight = 0; + for (CartPosition pos : positions) { + weight += pos.getProductWeight(); + } -} + //KI Ansatz + if (weight <= 0.1) { + shippingCost = 3.95; + } else if (weight <= 1.0) { + shippingCost = 4.95; + } else if (weight <= 5.0) { + shippingCost = 5.95; + } else { + shippingCost = 19.95; + } + } + + public void printConfirmation() { + IO.println("----------------------------------------"); + IO.println(" ORDER CONFIRMATION "); + IO.println("----------------------------------------"); + IO.println("Customer: " + customerName); + IO.println("Delivery address: " + address); + IO.println("----------------------------------------"); + IO.println("Your order:"); + + for (CartPosition pos : positions) { + IO.println(" " + pos.getProduct().getName() + " x " + pos.getQuantity()); + } + + IO.println("----------------------------------------"); + IO.println(String.format("Product value: %6.2f €", totalNet)); + IO.println("----------------------------------------"); + IO.println("Thank you for your order, lil bro!"); + IO.println("----------------------------------------"); + } + + + public String getCustomerName() { + return customerName; + } + + public String getAddress() { + return address; + } + + public List getPositions() { + return positions; + } + + public double getTotalNet() { + return totalNet; + } + + public double getFinalTotal() { + return finalTotal; + } + +} \ No newline at end of file diff --git a/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Product.java b/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Product.java index 0dadbf2..0ef8968 100644 --- a/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Product.java +++ b/Shop/src/de/th_mannheim/informatik/main/java/shop/backend/Product.java @@ -12,7 +12,7 @@ public class Product { private String name; private double transportWeight; private double netPrice; - private double bruttoPrice; + private double mwst; // <----- Maybe final setzen private int stock; @@ -54,8 +54,9 @@ public class Product { public double getNetPrice() { return netPrice; } + //Änderung aus ansatz azs vl public double getBruttoPrice() { - return bruttoPrice; + return Math.round(netPrice * (1 + mwst/100)); } public double getMwst() { @@ -71,15 +72,6 @@ public class Product { return String.format("[%d] %-25s | %6.2f € Netprice | Stock: %2d", prodID, name, netPrice, stock); } - //mwst berechnung ----> gibt nru die Steuer zurück - public double mwstCalc() { - return Math.round(netPrice * (mwst / 100)); - } - //Gibt preis versteuert zurück - public double mwstOnPrice(){ - return Math.round(netPrice * (mwst / 100)); - } - //Lagerbestand kann sich ändern. Einzige setter Methode public void setStock(int neuerLagerbestand) { this.stock = neuerLagerbestand; diff --git a/Shop/src/de/th_mannheim/informatik/main/java/shop/frontend/ShopTUI.java b/Shop/src/de/th_mannheim/informatik/main/java/shop/frontend/ShopTUI.java index 3f0c3b2..8e31a72 100644 --- a/Shop/src/de/th_mannheim/informatik/main/java/shop/frontend/ShopTUI.java +++ b/Shop/src/de/th_mannheim/informatik/main/java/shop/frontend/ShopTUI.java @@ -3,66 +3,53 @@ package shop.frontend; import shop.backend.Cart; import shop.backend.CartPosition; import shop.backend.OnlineShop; +import shop.backend.Order; import shop.backend.Product; import java.io.FileNotFoundException; -import java.util.Scanner; +import java.util.ArrayList; -/* -IO Only und Darstellungslogik (Nur Anzeigen, bekommen, nichts verändern) -keine Business logik <----- Ein einziger Scanner für TUI -ruft Methoden aus Onlineshop auf oder auch CartPos und product <---Ungewiss -getName ect.. - - - */ public class ShopTUI { - static Scanner scanner = new Scanner(System.in); private static OnlineShop onlineShop; private static Cart cart; - void main() throws FileNotFoundException { - //KI - IO.println("\n+--------------------------------------+\n| " + - " WELCOME TO THE TH MANNHEIM |\n| ONLINE SHOP " + - "|\n+--------------------------------------+"); - IO.println("| |"); + public static void main(String[] args) throws FileNotFoundException { + IO.println("\n+--------------------------------------+"); + IO.println("| WELCOME TO THE ONLINE SHOP |"); + IO.println("+--------------------------------------+"); onlineShop = new OnlineShop(); cart = new Cart(); onlineShop.initShop("Shop/resources/produkte.csv"); - run(onlineShop); - + run(); } public static void menu() { - - IO.println("+--------------------------------------+"); - IO.println("|1. Show products |"); - IO.println("|2. Search |"); - IO.println("|3. Add to cart |"); - IO.println("|4. Show cart |"); - IO.println("|5. Edit cart |"); - IO.println("|6. Check out |"); - IO.println("|7. Exit |"); - IO.println("|Your choice: |"); + IO.println("\n+--------------------------------------+"); + IO.println("|1. Show all products |"); + IO.println("|2. Search products |"); + IO.println("|3. Add to cart |"); + IO.println("|4. Show cart |"); + IO.println("|5. Edit cart |"); + IO.println("|6. Checkout |"); + IO.println("|7. Exit |"); IO.println("+--------------------------------------+"); + IO.print("Your choice: "); } public static void showProducts() { - IO.println(); - IO.println("Product showcase"); + IO.println("\nProduct overview:"); + for (Product prod : onlineShop.getLager()) { IO.println(prod); - } + } - public static void run(OnlineShop shop) { + public static void run() { int choice; do { menu(); - - choice = scanner.nextInt(); + choice = Integer.parseInt(IO.readln()); switch (choice) { case 1: showProducts(); @@ -79,79 +66,132 @@ public class ShopTUI { case 5: editCart(); break; - case 6: - checkinOut(); + checkout(); break; case 7: exit(); break; + } } while (choice != 7); - } public static void exit() { - IO.println("Thank you for visiting....."); + IO.println("Thank you for your visit!"); } public static void search() { - String keyWord = IO.readln("Keyword ur lookin for: "); - for (Product pr : onlineShop.seachByName(keyWord)) { - IO.println(pr); + String keyWord = IO.readln("Search term: "); + ArrayList results = onlineShop.seachByName(keyWord); + if (results.isEmpty()) { + IO.println("No products found!"); + } else { + IO.println("\nSearch results:"); + + for (Product pr : results) { + IO.println(pr); + } + IO.println(); } } public static void addToCart() { - String strId = IO.readln("Prod ID ur lookin for: "); + String strId = IO.readln("Product ID: "); int id = Integer.parseInt(strId); Product p = onlineShop.getProdId(id); - if (p == null) { //Pointer auf leere referenz + + if (p == null) { IO.println("Product not found!"); return; } - String strQuantity = IO.readln("Enter quantity: "); + String strQuantity = IO.readln("Quantity: "); int quantity = Integer.parseInt(strQuantity); + if (quantity > p.getStock()) { - IO.println("Not enough in stock, sorry!"); + IO.println("Not enough in stock!"); IO.println("Available: " + p.getStock()); } else { cart.addProduct(p, quantity); - IO.println("Added to cart: "); + IO.println("Added: " + p.getName() + " x " + quantity); } - } public static void showCart() { - if (!cart.isEmpty()) { - for (CartPosition pos : cart.getPositions()) { - IO.println(pos); - } - IO.println("Summe ist: (NotImplemented yet....)"); //TODO CartBerechnungen + if (cart.isEmpty()) { + IO.println("Cart is empty."); + return; } - IO.println("Cart is empty."); + + Order tempOrder = new Order("", "", cart.getPositions()); + + IO.println("\nYour shopping cart:"); + IO.println("----------------------------------------"); + for (CartPosition pos : cart.getPositions()) { + IO.println(pos); + } + IO.println("----------------------------------------"); + IO.println(String.format("Product value (net): %6.2f €", tempOrder.getTotalNet())); + IO.println(String.format("TOTAL: %6.2f €", tempOrder.getFinalTotal())); + IO.println("----------------------------------------"); } public static void editCart() { - showCart(); if (cart.isEmpty()) { + IO.println("Cart is empty!"); return; } - String strId = IO.readln("ID to change: "); - int id = Integer.parseInt(strId); - String strQuantity = IO.readln("Quantity you want to change: "); - int quantity = Integer.parseInt(strQuantity); - cart.updateProductQuantity(id, quantity); - IO.println("Cart has changed!"); + showCart(); + String strId = IO.readln("Product ID to change: "); + int id = Integer.parseInt(strId); + for (CartPosition pos : cart.getPositions()) { + if (pos.getProduct().getProdID() == id) { + String strQuantity = IO.readln("New quantity (0 to remove): "); + int quantity = Integer.parseInt(strQuantity); + cart.updateProductQuantity(id, quantity); + IO.println("Cart updated!"); + break; + } + } } - public static void checkinOut() { + public static void checkout() { if (cart.isEmpty()) { - IO.println("Cart empty, you can't order yet!"); + IO.println("Cart is empty!"); return; } + + IO.println("\n--- CHECKOUT ---"); + String name = IO.readln("Name: "); + String address = IO.readln("Delivery address: "); + + + + for (CartPosition pos : cart.getPositions()) { + Product prod = pos.getProduct(); + int quantity = pos.getQuantity(); + + if (prod.getStock() < quantity) { + IO.println("Error: " + prod.getName() + " not enough in stock!"); + IO.println("Available: " + prod.getStock() + ", Requested: " + quantity); + IO.println("Order cannot be completed!"); + return; + } + } + + for (CartPosition pos : cart.getPositions()) { + Product prod = pos.getProduct(); + int quantity = pos.getQuantity(); + onlineShop.reduceStock(prod, quantity); + } + + Order order = new Order(name, address, cart.getPositions()); + order.printConfirmation(); + + cart.clear(); + IO.println("Cart has been cleared. You can continue shopping!"); } } \ No newline at end of file