From ad85a4aebae950dd8dcaac03687427aa9092f98a Mon Sep 17 00:00:00 2001 From: Fatima Zehra Ulu <3026753@stud.hs-mannheim.de> Date: Mon, 15 Dec 2025 06:46:41 +0100 Subject: [PATCH] Dateien nach "/" hochladen implementierung clear des warenkorbs und bestellung des Kunden --- Cart.java | 11 ++-- OnlineShop.java | 157 +++++++++++++++++++++++++++++++++--------------- Products.java | 13 ++-- Shop.java | 5 +- 4 files changed, 126 insertions(+), 60 deletions(-) diff --git a/Cart.java b/Cart.java index 62af260..d9919eb 100644 --- a/Cart.java +++ b/Cart.java @@ -3,7 +3,7 @@ import Shop.Products; import java.util.ArrayList; public class Cart { - private ArrayList cartContents; + private static ArrayList cartContents; public Cart() { cartContents = new ArrayList(); @@ -11,10 +11,10 @@ public class Cart { //Adding stuff to Cart (adding stuff to cart means removing from stock) - public boolean AddCart(Products prod){ + public static boolean AddCart(Products prod, int amount){ if (prod.getStock() > 0) { // checking if product is in stock cartContents.add(prod); // adding to cart - prod.setStock(prod.getStock() - 1); // removing from stock + prod.setStock(prod.getStock() - amount); // removing from stock return true; } return false; @@ -31,10 +31,10 @@ public class Cart { } //removing stuff from cart (means adding back to stock) - public boolean RemoveCart(Products prod){ + public boolean RemoveCart(Products prod, int amount){ if ( NumberOfArticels(prod) > 0) { // checking if product is in stock if(cartContents.remove(prod)){ // removing from cart - prod.setStock(prod.getStock() + 1); // adding back to stock + prod.setStock(prod.getStock() + amount); // adding back to stock return true; } } @@ -140,4 +140,5 @@ public class Cart { return amount; } + } \ No newline at end of file diff --git a/OnlineShop.java b/OnlineShop.java index 2506221..9fd3886 100644 --- a/OnlineShop.java +++ b/OnlineShop.java @@ -1,16 +1,17 @@ package Shop; -import Shop.Cart; -import Shop.Products; + import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; +import static Shop.Shop.cart; + public class OnlineShop { ArrayList warehouse; - public OnlineShop throws FileNotFoundException { //readFile-Methode + public Object OnlineShop() throws FileNotFoundException { //readFile-Methode warehouse = new ArrayList<>(); //Einlesen @@ -20,63 +21,121 @@ public class OnlineShop { while (scan.hasNextLine()) { //Solange da noch Zeilen im Dokument sind... String Line = scan.nextLine().trim(); - if (Line.length() == 5) { - String[] Parts = Line.split(","); + while (scan.hasNextLine()) { + String line = scan.nextLine().trim(); + String[] Parts = line.split(","); - int productID = Integer.parseInt(Parts[0]); - String Name = Parts[1]; - double weight = Double.parseDouble(Parts[2]); - double netWorth = Double.parseDouble(Parts[3]); - double tax = Double.parseDouble(Parts[4]); - int stock = Integer.parseInt(Parts[5]); - - Products prod = new Products(Name, weight, netWorth, tax, stock, productID); - warehouse.add(prod); - }} + // ANNAHME: Die CSV-Zeile hat 6 Spalten (ID, Name, Gewicht, NetWorth, Tax, Stock) + if (Parts.length == 6) { + try { + int productID = Integer.parseInt(Parts[0].trim()); // ID ist jetzt da + String Name = Parts[1].trim(); + double weight = Double.parseDouble(Parts[2].trim()); + double netWorth = Double.parseDouble(Parts[3].trim()); + double tax = Double.parseDouble(Parts[4].trim()); // z.B. 7.0 oder 19.0 + int stock = Integer.parseInt(Parts[5].trim()); + Products prod = new Products(Name, weight, netWorth, tax, stock, productID); + warehouse.add(prod); + } catch (NumberFormatException e) { + System.err.println("Fehler beim Parsen numerischer Daten in Zeile: " + Line); + } + } + } scan.close(); } - public String[] ProductList() { - String[] productList = new String [warehouse.size()]; - for (int i = 0; i < warehouse.size(); i++) { - productList[i] = warehouse.get(i).toString(); + + class OrderCustomer { + private String CustomerName; + private String Adress; + private Cart cart; + private double wholeAmount; + + public OrderCustomer(String Name, String Adress, Cart cart, double wholeAmount) { + this.CustomerName = Name; + this.Adress = Adress; + this.cart = cart; + this.wholeAmount = wholeAmount; + } + + public String getName() { + return CustomerName; + } + + public String getAdresse() { + return Adress; + } + + public Cart getCart() { + return cart; + } + + public double wholeAmount() { + return wholeAmount; + } } + return 0; - return productList; + + Products getProductById(int ProductID) { + int i = 0; + while (i < warehouse.size()) { + if (warehouse.get(i).getProductID() == id) { + return warehouse.get(i); + } + i++; } + return null; + } - - - public class Order { - private String Name; - private String Adress; - private Cart cart; - private double wholeAmount; - - public Order(String Name, String Adress, Cart cart, double wholeAmount) { - this.Name = Name; - this.Adress = Adress; - this.cart = cart; - this.wholeAmount = wholeAmount; - } - - public String getName() { - return Name; - } - - public String getAdresse() { - return Adress; - } - - public Cart getCart() { - return cart; - } - - public double wholeAmount() { - return wholeAmount; + public void showProducts() { + int i = 0; + while (i < warehouse.size()) { + Products p = warehouse.get(i); + System.out.println(p.getProductID() + " | " + p.getName() + " | " + + String.format("%.2f €", p.getGrossPrice()) + " | Bestand: " + p.getStock()); + i++; } } + OrderCustomer createOrder(String customerName, String Adress, Cart cart) { + double net = Cart.getnetWorth(); + double tax = Cart.getTaxDelivery(); + double gross = Cart.getGrossTotal(); + + Order order = new Order(name, Address, cart, net, tax, gross); + cart.clear(); + return order; + } + public static class Order { + private String customerName; + private String address; + private Cart cart; + private double net; + private double tax; + private double gross; + + public Order(String customerName, String address, Cart cart, + double net, double tax, double gross) { + this.customerName = customerName; + this.address = address; + this.cart = cart.copy(); + this.net = net; + this.tax = tax; + this.gross = gross; + } + + public void printConfirmation() { + System.out.println("===== BESTELLBESTÄTIGUNG ====="); + System.out.println("Name: " + customerName); + System.out.println("Adresse: " + address); + cart.printCart(); + System.out.println("Netto: " + net + " €"); + System.out.println("MwSt: " + tax + " €"); + System.out.println("Brutto: " + gross + " €"); + } + } +} } \ No newline at end of file diff --git a/Products.java b/Products.java index 099c236..8780c2c 100644 --- a/Products.java +++ b/Products.java @@ -19,7 +19,7 @@ import java.util.*; this.stock = stock; this.productID = productID; } - public int getProductID() { + public String getProductID() { return productID; } public void setProductID(int productID) { @@ -57,7 +57,7 @@ import java.util.*; this.tax = newTax; } - public int getStock() { + public static int getStock() { return this.stock; } //neuer Bestand setzen @@ -65,11 +65,16 @@ import java.util.*; this.stock = newStock; } - // Brutto Preis public double getGrossPrice() { - return this.netWorth * (1 + this.tax / 100); + return this.netWorth * (1 + this.tax); } + public double getTaxAmount() { + return this.getGrossPrice() - this.netWorth; + } + + + public String toString() { return diff --git a/Shop.java b/Shop.java index 0d624bc..78a12ec 100644 --- a/Shop.java +++ b/Shop.java @@ -11,7 +11,7 @@ import java.io.FileNotFoundException; public class Shop { private static OnlineShop shop; - private static Cart cart = new Cart(); + static Cart cart = new Cart(); private static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws FileNotFoundException { @@ -40,7 +40,7 @@ public class Shop { } else if (eingabe.equals("produktsuche") || eingabe.equals("suche")) { Search(); } else if (eingabe.equals("warenkorb")) { - cart; + Cart(); } else if (eingabe.equals("beenden")) { System.out.println(" ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄"); System.out.println(" Auf Wiedersehen! "); @@ -143,4 +143,5 @@ public class Shop { } } } +}