From ba178e05fd342cae9519a469e26fdbaba8310572 Mon Sep 17 00:00:00 2001 From: Daniel Zikol <3020574@stud.hs-mannheim.de> Date: Sat, 13 Dec 2025 17:19:05 +0100 Subject: [PATCH] #Feat - addProdukt, removeProduct implementiert --- Shop/resources/quellen.txt | 7 +++- .../main/java/shop/backend/Cart.java | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Shop/resources/quellen.txt b/Shop/resources/quellen.txt index f612cc3..b7181b5 100644 --- a/Shop/resources/quellen.txt +++ b/Shop/resources/quellen.txt @@ -13,4 +13,9 @@ Code Gym - Get and Set https://codegym.cc/de/groups/posts/getter-und-setter-in-java Bro Code - How to read a File -https://www.youtube.com/watch?v=eHjbvgw4hsI \ No newline at end of file +https://www.youtube.com/watch?v=eHjbvgw4hsI + + +Baeldung - ConcurrentModificationException +https://www.baeldung.com/java-concurrentmodificationexception + 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 af36137..f04bc7c 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 @@ -1,8 +1,42 @@ package shop.backend; +import java.util.ArrayList; +import java.util.List; + public class Cart { +private List positions; +public Cart(){ + this.positions = new ArrayList<>(); + +} + +public void addProduct(Product prod, int quantity){ + //Ich überprüfe damit ob es das Produkt gibt + for(CartPosition pos : positions){ + //Ist es das Produkt? Hat es dieselbe ID? ---> Ja ---> Quantity erhöhen---->break + if(pos.getProduct().getProdID() == prod.getProdID()){ + pos.setQuantity(pos.getQuantity() + quantity); //Damit erhöhe ich quantity + break; + } + positions.add(new CartPosition(prod, quantity)); + } +} +public void removeProduct(int productID){ + CartPosition gettinYETTED = null; + for(CartPosition pos : positions){ + + if(pos.getProduct().getProdID() == productID){ + gettinYETTED = pos; + break; // Gefunden, abbrechnen + } + } + // + if(gettinYETTED != null){ + positions.remove(gettinYETTED); + } +}