From f615dcc8252a9a5630c65cefe10831f93ffd6a53 Mon Sep 17 00:00:00 2001 From: Emilie Date: Mon, 18 Sep 2023 15:23:56 -0400 Subject: [PATCH] Change food items list test to ignore order, update description of getFoodItemsForOrder Signed-off-by: Emilie --- .../codekatas/coffeeshopkata/CoffeeShopOrder.java | 8 ++++---- .../codekata/coffeeshopkata/CoffeeShopTest.java | 8 +++++--- .../codekatas/coffeeshopkata/CoffeeShopOrder.java | 13 +++++++------ .../codekatas/coffeeshopkata/CoffeeShopTest.java | 8 +++++--- .../codekatas/coffeeshopkata/CoffeeShopOrder.java | 12 ++++-------- .../codekatas/coffeeshopkata/CoffeeShopTest.java | 10 ++++++---- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/coffee-shop-kata-solutions/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java b/coffee-shop-kata-solutions/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java index 55d36b28..2e138b49 100644 --- a/coffee-shop-kata-solutions/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java +++ b/coffee-shop-kata-solutions/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java @@ -52,9 +52,9 @@ public CoffeeShopOrder(String customerName, List orderItems) * Return a list of custom strings for the customer's food items! * The string format for each food item is as follows: * If the item is a Bagel: "[bagelType] with [spreadType]" - * If the item is a Cookie: "[cookieType]" - * If the item is a Donut: "[donutType]" - * Otherwise: throw new IllegalStateException() + * If the item is a Cookie: "[cookieType] cookie" + * If the item is a Donut: "[donutType] donut" + * Otherwise: it is a beverage and should not be added to the list! *

* NOTE: This method show-cases a switch-case pattern matching. */ @@ -68,7 +68,7 @@ public List getFoodItemsForOrder() case Bagel bagel -> foodItems.add(bagel.bagelType() + " bagel with " + bagel.spreadType()); case Cookie cookie -> foodItems.add(cookie.cookieType() + " cookie"); case Donut donut -> foodItems.add(donut.donutType() + " donut"); - default -> throw new IllegalStateException("Unexpected value: " + item); + default -> {} } } return foodItems; diff --git a/coffee-shop-kata-solutions/src/test/java/bnymellon/codekata/coffeeshopkata/CoffeeShopTest.java b/coffee-shop-kata-solutions/src/test/java/bnymellon/codekata/coffeeshopkata/CoffeeShopTest.java index 24a16a2a..72a6eab9 100644 --- a/coffee-shop-kata-solutions/src/test/java/bnymellon/codekata/coffeeshopkata/CoffeeShopTest.java +++ b/coffee-shop-kata-solutions/src/test/java/bnymellon/codekata/coffeeshopkata/CoffeeShopTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Collections; import java.util.List; import static bnymellon.codekatas.coffeeshopkata.food.BagelType.EVERYTHING; @@ -59,9 +60,10 @@ public void setUp() @Test public void getFoodItemsForOrderTest() { - List expected = List.of("EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", - "CHOCOLATE_CHIP cookie", "GLAZED donut"); - assertEquals(expected, coffeeShopOrder.getFoodItemsForOrder()); + List expected = List.of("CHOCOLATE_CHIP cookie", "EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", "GLAZED donut"); + List actual = coffeeShopOrder.getFoodItemsForOrder(); + Collections.sort(actual); + assertEquals(expected, actual); } @Test diff --git a/coffee-shop-kata/jdk21/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java b/coffee-shop-kata/jdk21/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java index 36053ab3..03387b4d 100644 --- a/coffee-shop-kata/jdk21/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java +++ b/coffee-shop-kata/jdk21/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java @@ -17,6 +17,7 @@ package bnymellon.codekatas.coffeeshopkata; +import java.util.Collections; import java.util.List; public class CoffeeShopOrder @@ -34,9 +35,9 @@ public CoffeeShopOrder(String customerName, List orderItems) * Return a list of custom strings for the customer's food items! * The string format for each food item is as follows: * If the item is a Bagel: "[bagelType] with [spreadType]" - * If the item is a Cookie: "[cookieType]" - * If the item is a Donut: "[donutType]" - * Otherwise: throw new IllegalStateException() + * If the item is a Cookie: "[cookieType] cookie" + * If the item is a Donut: "[donutType] donut" + * Otherwise: it is a beverage and should not be added to the list! *

* NOTE: This method show-cases a switch-case pattern matching. * @@ -46,7 +47,7 @@ public List getFoodItemsForOrder() { // TODO: implement method // Hint: look at the Java 8 implementation in the jdk8 module, // and the link above to see how pattern matching for switch can be utilized here - return null; + return Collections.emptyList(); } /** @@ -64,7 +65,7 @@ public String generateReceipt() { // TODO: Implement the receipt generation logic here. // Hint: look at the Java 8 implementation in the jdk8 module, // and the link above to see how record patterns can be utilized here - return null; + return ""; } /** @@ -81,6 +82,6 @@ public String generateReceipt() { public List getDrinkForOrder() { // TODO: implement method logic here - return null; + return Collections.emptyList(); } } diff --git a/coffee-shop-kata/jdk21/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java b/coffee-shop-kata/jdk21/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java index 37de0cd4..51bcb148 100644 --- a/coffee-shop-kata/jdk21/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java +++ b/coffee-shop-kata/jdk21/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static bnymellon.codekatas.coffeeshopkata.food.BagelType.EVERYTHING; @@ -62,9 +63,10 @@ public void setUp() public void getFoodItemsForOrderTest() { // TODO: Complete the method getFoodItemsForOrder() in CoffeeShopOrder to make this pass - List expected = List.of("EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", - "CHOCOLATE_CHIP cookie", "GLAZED donut"); - assertEquals(expected, coffeeShopOrder.getFoodItemsForOrder()); + List expected = List.of("CHOCOLATE_CHIP cookie", "EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", "GLAZED donut"); + List actual = coffeeShopOrder.getFoodItemsForOrder(); + Collections.sort(actual); + assertEquals(expected, actual); } @Test diff --git a/coffee-shop-kata/jdk8/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java b/coffee-shop-kata/jdk8/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java index e9ba220d..5408994d 100644 --- a/coffee-shop-kata/jdk8/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java +++ b/coffee-shop-kata/jdk8/src/main/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopOrder.java @@ -40,7 +40,7 @@ public class CoffeeShopOrder private final String customerName; private final List orderItems; - public CoffeeShopOrder(String customerName, java.util.List orderItems) + public CoffeeShopOrder(String customerName, List orderItems) { this.customerName = customerName; this.orderItems = orderItems; @@ -50,9 +50,9 @@ public CoffeeShopOrder(String customerName, java.util.List orderItems) * Return a list of custom strings for the customer's food items! * The string format for each food item is as follows: * If the item is a Bagel: "[bagelType] with [spreadType]" - * If the item is a Cookie: "[cookieType]" - * If the item is a Donut: "[donutType]" - * Otherwise: throw new IllegalStateException() + * If the item is a Cookie: "[cookieType] cookie" + * If the item is a Donut: "[donutType] donut" + * Otherwise: it is a beverage and should not be added to the list! *

* NOTE: This method show-cases a switch-case pattern matching. */ @@ -76,10 +76,6 @@ else if (item instanceof Donut) Donut donut = (Donut) item; foodItems.add(donut.getDonutType() + " donut"); } - else - { - throw new IllegalStateException(); - } } return foodItems; } diff --git a/coffee-shop-kata/jdk8/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java b/coffee-shop-kata/jdk8/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java index 434643aa..8917cd39 100644 --- a/coffee-shop-kata/jdk8/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java +++ b/coffee-shop-kata/jdk8/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java @@ -22,14 +22,14 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static bnymellon.codekatas.coffeeshopkata.food.BagelType.EVERYTHING; import static bnymellon.codekatas.coffeeshopkata.food.CookieType.CHOCOLATE_CHIP; import static bnymellon.codekatas.coffeeshopkata.food.DonutType.GLAZED; import static bnymellon.codekatas.coffeeshopkata.food.SpreadType.HERB_GARLIC_CREAM_CHEESE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; class CoffeeShopTest { @@ -56,10 +56,12 @@ public void setUp() public void getFoodItemsForOrderTest() { ArrayList expected = new ArrayList<>(); - expected.add("EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE"); expected.add("CHOCOLATE_CHIP cookie"); + expected.add("EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE"); expected.add("GLAZED donut"); - assertEquals(expected, coffeeShopOrder.getFoodItemsForOrder()); + List actual = coffeeShopOrder.getFoodItemsForOrder(); + Collections.sort(actual); + assertEquals(expected, actual); } @Test