Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.3</version>
<version>3.27.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.15.2</version>
<version>5.21.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/example/shop/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.shop;

public class Item {
private String name;
private double price;
private int quantity;

Item(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/example/shop/ShoppingCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.shop;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
private List<Item> items = new ArrayList<>();
private double discount = 0.0;

public boolean addItem(String name, double price, int quantity) {
if (name == null || price < 0 || quantity <=0) {
return false;
}
items.add(new Item(name, price, quantity));
return true;
}

public boolean removeItem(String name) {
return items.removeIf(item -> item.getName().equals(name));
}
public double getTotalPrice() {
double totalPrice = 0.0;
for(Item item : items) {
totalPrice += item.getPrice() * item.getQuantity();
}
return totalPrice * (1 - discount);
}
public boolean updateQuantity(String name, int quantity) {
boolean changedQty = false;
for(Item item : items) {
if(item.getName().equals(name)) {
changedQty = true;
item.setQuantity(quantity);
}
}
return changedQty;
}
public boolean applyDiscount(double discount) {
if (discount < 0 || discount > 1) {
return false;
}
this.discount = discount;
return true;
}

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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.*;

class ShoppingCartTest {
@Test
void addItem_returnsTrue_whenItemIsAdded() {
ShoppingCart cart = new ShoppingCart();

boolean result = cart.addItem("T-Shirt", 129.0, 1);

assertTrue(result);
}

@Test
void removeItem_returnsTrue_whenItemExists() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100.0, 1);

boolean result = cart.removeItem("T-Shirt");

assertTrue(result);
}
@Test
void getTotalPrice_returnsSumOfPrices() {
ShoppingCart cart = new ShoppingCart();

cart.addItem("T-Shirt", 100.0, 2);
cart.addItem("Jeans", 300, 1);

assertEquals(500, cart.getTotalPrice());
}
@Test
void updateQuantity_returnsTrue_andUpdatesTotalPrice() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100, 1);

boolean result = cart.updateQuantity("T-Shirt", 3);
assertTrue(result);
assertEquals(300.0, cart.getTotalPrice());
}
@Test
void applyDiscount_returnsTrue_andReducesTotal() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100.0, 1);
boolean result = cart.applyDiscount(0.3);
assertTrue(result);
assertEquals(70.0, cart.getTotalPrice());
}
@Test
void removeItemThatDoesNotExist_returnsFalse() {
ShoppingCart cart = new ShoppingCart();

Assertions.assertFalse(cart.removeItem("Hat"));
}
@Test
void removeItemWhenNameIsNull_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.removeItem(null));
}
@ParameterizedTest
@CsvSource({
"-1.0, 1",
"10.0, -1",
"10.0, 0",
"-10.0, -1"
})
void addItemWithNegativePriceOrQty_returnsFalse(double price, int quantity) {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.addItem("T-Shirt", price, quantity));
}
@Test
void addItemWhereNameIsNull_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.addItem(null, 100,1));
}

@Test
void updateQuantityWhereNameIsNull_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.updateQuantity(null, 1));
}
@ParameterizedTest
@CsvSource({
"-1",
"0"
})
void updateQuantityWhereQtyIsNull_returnsFalse(int quantity) {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.updateQuantity("T-Shirt", quantity ));
}

@ParameterizedTest
@CsvSource({
"-0.1",
"1.1"
})
void applyDiscountWhereDiscountIsNotAllowed_returnsFalse(double discount) {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.applyDiscount(discount));
}
}