Skip to content
20 changes: 20 additions & 0 deletions src/main/java/com/example/shoppingcart/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.shoppingcart;

public class Product {
private final String name;
private final double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}
}

42 changes: 42 additions & 0 deletions src/main/java/com/example/shoppingcart/ShoppingCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.shoppingcart;

import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {

private final Map<Product, Integer> items = new HashMap<>();
private double discount = 0.0;

public void addProduct(Product product, int quantity) {
items.merge(product, quantity, Integer::sum);
}

public void removeProduct(Product product) {
items.remove(product);
}

public Map<Product, Integer> getItems() {
return items;
}

public void applyDiscount(double discount) {
this.discount = discount;
}

public double calculateTotal() {
double total = 0.0;
for (Map.Entry<Product, Integer> entry : items.entrySet()) {
total += entry.getKey().getPrice() * entry.getValue();
}
return total * (1 - discount); // stöd för rabatt
}
public void updateQuantity(Product product, int newQuantity) {
if (newQuantity <= 0) {
items.remove(product);
} else {
items.put(product, newQuantity);
}
}

}
92 changes: 92 additions & 0 deletions src/test/java/com/example/shoppingcart/ShoppingCartTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.example.shoppingcart;

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;

public class ShoppingCartTest {

@Test
void shouldAddProductToCart() {
ShoppingCart cart = new ShoppingCart();
Product product = new Product("Apple", 10.0);

cart.addProduct(product, 1);

assertThat(cart.getItems()).containsEntry(product, 1);
}

@Test
void shouldRemoveProductFromCart() {
ShoppingCart cart = new ShoppingCart();
Product product = new Product("Banana", 5.0);

cart.addProduct(product, 2);
cart.removeProduct(product);

assertThat(cart.getItems()).doesNotContainKey(product);
}

@Test
void shouldCalculateTotalPrice() {
ShoppingCart cart = new ShoppingCart();
Product apple = new Product("Apple", 10.0);
Product banana = new Product("Banana", 5.0);

cart.addProduct(apple, 2); // 2 x 10 = 20
cart.addProduct(banana, 3); // 3 x 5 = 15

double total = cart.calculateTotal(); // FIXAT HÄR

assertThat(total).isEqualTo(35.0);
}

@Test
void shouldApplyPercentageDiscountToTotalPrice() {
ShoppingCart cart = new ShoppingCart();
Product product1 = new Product("Apple", 10.0);
Product product2 = new Product("Banana", 5.0);

cart.addProduct(product1, 2); // 20 kr
cart.addProduct(product2, 4); // 20 kr
cart.applyDiscount(0.10); // 10% rabatt

double total = cart.calculateTotal();

assertThat(total).isEqualTo(36.0); // 40 - 10% = 36
}

@Test
void shouldCalculateTotalPriceWithNoDiscount() {
ShoppingCart cart = new ShoppingCart();
Product apple = new Product("Apple", 10.0);
Product banana = new Product("Banana", 5.0);

cart.addProduct(apple, 2); // 20.0
cart.addProduct(banana, 3); // 15.0

double total = cart.calculateTotal();

assertThat(total).isEqualTo(35.0);
}
@Test
void shouldUpdateQuantityOfExistingProduct() {
ShoppingCart cart = new ShoppingCart();
Product apple = new Product("Apple", 10.0);

cart.addProduct(apple, 2);
cart.updateQuantity(apple, 5); // ändra direkt till 5

assertThat(cart.getItems()).containsEntry(apple, 5);
}
@Test
void shouldRemoveProductWhenQuantityIsZero() {
ShoppingCart cart = new ShoppingCart();
Product apple = new Product("Apple", 10.0);
cart.addProduct(apple, 2);

cart.updateQuantity(apple, 0);

assertThat(cart.getItems()).doesNotContainKey(apple);
}

}