Skip to content

added some tests to catalog and shopping cart tests #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: java-challenge-start
Choose a base branch
from
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: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
<artifactId>assertj-core</artifactId>
<version>3.18.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>


<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/serenitydojo/fruitmarket/Catalog.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.serenitydojo.fruitmarket;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Catalog {

Expand All @@ -11,8 +13,19 @@ public PriceSetter setPriceOf(Fruit fruit) {
return new PriceSetter(this, fruit);
}

public List<String> getAvailableFruit() {
return pricePerKilo.keySet()
.stream()
.map(Enum::name)
.sorted()
.collect(Collectors.toList());
}

public Double getPriceOf(Fruit fruit) {
return pricePerKilo.get(fruit);
if (pricePerKilo.containsKey(fruit)) {
return pricePerKilo.get(fruit);
}
throw new FruitUnavailableException(fruit.name() + " currently unavailable");
}

public static class PriceSetter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.serenitydojo.fruitmarket;

public class FruitUnavailableException extends RuntimeException {
public FruitUnavailableException(String message) {
super(message);
}


}
32 changes: 32 additions & 0 deletions src/main/java/com/serenitydojo/fruitmarket/ShoppingCart.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,36 @@ public ShoppingCart(Catalog catalog) {
this.catalog = catalog;
this.items = new ArrayList<>();
}

public ShoppingCartAdder add(Double amount) {
return new ShoppingCartAdder(this, amount);
}


public List<ShoppingCartItem> getItems() {
return new ArrayList<>(items);
}

public Double getTotalPrice() {
return items.stream().mapToDouble(ShoppingCartItem::getTotalCost).sum();
}

public class ShoppingCartAdder {
private final ShoppingCart shoppingCart;
private final Double amount;

public ShoppingCartAdder(ShoppingCart shoppingCart, Double amount) {
this.shoppingCart = shoppingCart;
this.amount = amount;
}

public ShoppingCart kilosOf(Fruit fruit) {
double basePrice = shoppingCart.catalog.getPriceOf(fruit);
double discountedPrice = (amount >= 5) ? basePrice * 0.9 : basePrice;
ShoppingCartItem item = new ShoppingCartItem(fruit, amount, discountedPrice * amount);
shoppingCart.items.add(item);
return shoppingCart;
}
}

}
63 changes: 63 additions & 0 deletions src/test/java/com/serenitydojo/fruitmarket/TheCatalog.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package com.serenitydojo.fruitmarket;

import org.junit.Before;
import org.junit.Test;

import static com.serenitydojo.fruitmarket.Fruit.*;
import static org.assertj.core.api.Assertions.assertThat;

public class TheCatalog {
Catalog catalog;

@Before
public void createANewCatalog(){
catalog = new Catalog();
catalog.setPriceOf(Apple).to(4.00)
.setPriceOf(Orange).to(5.50)
.setPriceOf(Banana).to(4.50)
.setPriceOf(Pear).to(4.50);
}

@Test
public void shouldBeAbleToUpdateTheCurrentPriceOfAFruit() {
Expand All @@ -14,4 +25,56 @@ public void shouldBeAbleToUpdateTheCurrentPriceOfAFruit() {
catalog.setPriceOf(Apple).to(4.00);
assertThat(catalog.getPriceOf(Apple)).isEqualTo(4.00);
}

@Test
public void shouldListTheAvailableFruitInAlphabeticalOrder() {
assertThat(catalog.getAvailableFruit()).containsExactly("Apple", "Banana", "Orange", "Pear");

}

@Test
public void shouldReturnTheCorrectPricesOfEachFruitInSeason(){
assertThat(catalog.getPriceOf(Apple)).isEqualTo(4.00);
assertThat(catalog.getPriceOf(Orange)).isEqualTo(5.50);
}

@Test(expected = FruitUnavailableException.class)
public void shouldReportAnExceptionIfAFruitIsNotAvailable() {
catalog.getPriceOf(Strawberries);
}

//Test I added
@Test
public void shouldNotBeAbleToAddFruitWithWrongPrice(){
Catalog catalog = new Catalog();

catalog.setPriceOf(Banana).to(4.25);

assertThat(catalog.getPriceOf(Banana)).isNotEqualTo(4.50);
}

//Test I added
@Test
public void shouldListAllAvailableFruits(){
catalog = new Catalog();
catalog.setPriceOf(Apple).to(4.00)
.setPriceOf(Orange).to(5.50)
.setPriceOf(Banana).to(4.50)
.setPriceOf(Pear).to(4.50)
.setPriceOf(Peach).to(3.25)
.setPriceOf(Strawberries).to(4.25)
.setPriceOf(Mulberries).to(3.0);



assertThat(catalog.getAvailableFruit()).containsExactlyInAnyOrder("Apple", "Banana", "Pear","Orange","Mulberries","Peach", "Strawberries");







}

}
87 changes: 87 additions & 0 deletions src/test/java/com/serenitydojo/fruitmarket/TheShoppingCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.serenitydojo.fruitmarket;

import org.junit.Before;
import org.junit.Test;

import static com.serenitydojo.fruitmarket.Fruit.*;
import static org.assertj.core.api.Assertions.assertThat;


public class TheShoppingCart {

Catalog catalog;
ShoppingCart cart;

@Before
public void setupCatalog() {
catalog = new Catalog();
catalog.setPriceOf(Apple).to(4.00)
.setPriceOf(Orange).to(5.50)
.setPriceOf(Banana).to(4.50)
.setPriceOf(Pear).to(4.50);

cart = new ShoppingCart(catalog);
}

@Test
public void shouldStartWithNoItems() {
assertThat(cart.getItems()).isEmpty();
}

@Test
public void shouldKeepTrackOfItemsAddedToTheCart() {
cart.add(2.0).kilosOf(Apple)
.add(3.0).kilosOf(Orange);

assertThat(cart.getItems()).hasSize(2);
}

@Test
public void shouldUseTheCatalogToCalculateThePriceOfItemsAddedToTheCart() {
cart.add(2.0).kilosOf(Apple)
.add(2.0).kilosOf(Orange)
.add(1.0).kilosOf(Pear);

assertThat(cart.getTotalPrice()).isEqualTo(23.50);
}

@Test
public void shouldKeepTrackOfTheTotalPrice() {
cart.add(2.0).kilosOf(Apple);

ShoppingCartItem apples = cart.getItems().get(0);

assertThat(apples.getFruit()).isEqualTo(Apple);
assertThat(apples.getQuantity()).isEqualTo(2.0);
assertThat(apples.getTotalCost()).isEqualTo(8.00);
}

@Test
public void shouldGiveBulkDiscountsDiscount() {
cart.add(10.0).kilosOf(Apple);

assertThat(cart.getTotalPrice()).isEqualTo(36.00);
}

@Test
public void buildDiscountsOnlyApplyToQuantitiesOverFiveKgs() {
cart.add(10.0).kilosOf(Apple);
cart.add(2.00).kilosOf(Orange);

assertThat(cart.getTotalPrice()).isEqualTo(47.00);
}

// Test I added
@Test
public void shouldNotAllowNegativeBalance(){
cart.add(10.0).kilosOf(Apple);
cart.add(2.00).kilosOf(Orange);

assertThat(cart.getTotalPrice()).isNotEqualTo(-51.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of checking for negative balances but I'm not sure if this test is very useful in it's current form. Maybe have the code throw an exception if you try to add a negative quantity to the cart or a negative price in the catalog, and have tests that check for those?


}




}