Skip to content

Commit

Permalink
Merge pull request #146 from aqsa505/master
Browse files Browse the repository at this point in the history
add step in setup file, fix methods in coffeeShopOrder class
  • Loading branch information
prathasirisha authored Sep 20, 2023
2 parents 0147b30 + 4d16ab3 commit 3dceb9e
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,32 @@ public List<String> getFoodItemsForOrder()
* <p>
* NOTE: The method highlights the usage of a record deconstruction pattern
*/
public String generateReceipt()
public String generateReceiptForFoodItems()
{
double total = 0.0;
StringBuilder receiptBuilder = new StringBuilder();
List<String> receiptItems = new ArrayList<>();

for (Item item : this.orderItems)
{
if (item instanceof Donut(DonutType donutType))
{
receiptBuilder.append("Donut: ").append(donutType).append(" $").append(item.getPrice()).append("\n");
receiptItems.add("Donut: "+ donutType + " $" + item.getPrice());
total += item.getPrice();
}
else if (item instanceof Bagel(BagelType bagelType, SpreadType spreadType, boolean toasted))
{
receiptBuilder.append("Bagel: ").append(bagelType).append(" $").append(item.getPrice()).append("\n");
receiptItems.add("Bagel: "+ bagelType +" $" + item.getPrice());
total += item.getPrice();
}
else if (item instanceof Cookie(CookieType cookieType, boolean warmed))
{
receiptBuilder.append("Cookie: ").append(cookieType).append(" $").append(item.getPrice()).append("\n");
receiptItems.add("Cookie: " + cookieType +" $" + item.getPrice());
total += item.getPrice();
}
total += item.getPrice();
}
receiptItems.add("Total: $" + total);

receiptBuilder.append("Total: $").append(total);
return receiptBuilder.toString();
return String.join("\n", receiptItems);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package bnymellon.codekatas.coffeeshopkata.beverage;

public final class Americano implements CoffeeDrink
public non-sealed class Americano implements CoffeeDrink
{
private final DrinkTemperature drinkTemperature;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bnymellon.codekatas.coffeeshopkata.Item;
import bnymellon.codekatas.coffeeshopkata.beverage.Americano;
import bnymellon.codekatas.coffeeshopkata.beverage.CoffeeDrink;
import bnymellon.codekatas.coffeeshopkata.beverage.DrinkTemperature;
import bnymellon.codekatas.coffeeshopkata.beverage.Latte;
import bnymellon.codekatas.coffeeshopkata.beverage.Macchiato;
import bnymellon.codekatas.coffeeshopkata.food.Bagel;
Expand All @@ -46,24 +47,34 @@ public class CoffeeShopTest
private Bagel bagel1;
private Cookie cookie1;
private Donut donut1;
private Americano americano;

@BeforeEach
public void setUp()
{
bagel1 = new Bagel(EVERYTHING, HERB_GARLIC_CREAM_CHEESE, true);
cookie1 = new Cookie(CHOCOLATE_CHIP, true);
donut1 = new Donut(GLAZED);
itemList = List.of(bagel1, cookie1, donut1);
americano = new Americano(DrinkTemperature.HOT);
itemList = List.of(bagel1, cookie1, donut1, americano);
coffeeShopOrder = new CoffeeShopOrder("Emilie", itemList);
}

@Test
public void getFoodItemsForOrderTest()
public void testBagelRecord()
{
List<String> expected = List.of("CHOCOLATE_CHIP cookie", "EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", "GLAZED donut");
List<String> actual = coffeeShopOrder.getFoodItemsForOrder();
Collections.sort(actual);
assertEquals(expected, actual);
Bagel bagel2 = new Bagel(EVERYTHING, HERB_GARLIC_CREAM_CHEESE, true);
assertTrue(Bagel.class.isRecord());
assertEquals(bagel1, bagel2);
assertEquals("Bagel[bagelType=EVERYTHING, spreadType=HERB_GARLIC_CREAM_CHEESE, toasted=true]", bagel1.toString());
}

@Test
public void testBagelGetters()
{
assertTrue(bagel1.toasted());
assertEquals(bagel1.bagelType(), EVERYTHING);
assertEquals(bagel1.spreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
Expand All @@ -74,25 +85,18 @@ public void generateReceiptTest()
Cookie: CHOCOLATE_CHIP $1.25
Donut: GLAZED $1.75
Total: $5.5""";
assertEquals(expectedReceipt, coffeeShopOrder.generateReceipt());
assertEquals(expectedReceipt, coffeeShopOrder.generateReceiptForFoodItems());
}

@Test
public void testBagelRecord()
public void getFoodItemsForOrderTest()
{
Bagel bagel2 = new Bagel(EVERYTHING, HERB_GARLIC_CREAM_CHEESE, true);
assertTrue(Bagel.class.isRecord());
assertEquals(bagel1, bagel2);
assertEquals("Bagel[bagelType=EVERYTHING, spreadType=HERB_GARLIC_CREAM_CHEESE, toasted=true]", bagel1.toString());
List<String> expected = List.of("CHOCOLATE_CHIP cookie", "EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", "GLAZED donut");
List<String> actual = coffeeShopOrder.getFoodItemsForOrder();
Collections.sort(actual);
assertEquals(expected, actual);
}

@Test
public void testBagelGetters()
{
assertTrue(bagel1.toasted());
assertEquals(bagel1.bagelType(), EVERYTHING);
assertEquals(bagel1.spreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
public void testSealedClasses()
Expand Down
4 changes: 2 additions & 2 deletions coffee-shop-kata/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## What is the Coffee Shop Kata? ##

The Coffee Kata exercise is designed to provide a hands-on experience in learning and demonstrating the usage of latest
The Coffee Shop Kata exercise is designed to provide a hands-on experience in learning and demonstrating the usage of latest
Java features while comparing them with older.

The domain for the kata is a Coffee Shop. There are several domain
Expand Down Expand Up @@ -29,9 +29,9 @@ problems; there are no TODOs in this module. For technical setup, follow the ins

## Getting started ##
The following Java concepts will be useful in completing the kata:
* [Pattern matching for switch](https://openjdk.org/jeps/441)
* [Records](https://openjdk.org/jeps/395)
* [Record patterns](https://openjdk.org/jeps/440)
* [Pattern matching for switch](https://openjdk.org/jeps/441)
* [Sealed classes](https://openjdk.org/jeps/409)

There are failing tests in [CoffeeShopTest](jdk21/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java).
Expand Down
12 changes: 9 additions & 3 deletions coffee-shop-kata/SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
### Project setup
1. Git clone the entire [code-katas](https://github.com/BNYMellon/CodeKatas) project from GitHub or download the project
as a .zip file.
2. Launch the project in the IDE as a maven project. You can find instructions on how to do
2. Launch the project in the IDE and point to the pom.xml to be opened as a project.
You can find more instructions on how to do
that [here](https://www.jetbrains.com/idea/guide/tutorials/working-with-maven/importing-a-project/).
3. To verify that the Java 8 module is set up correctly,
3. To use Java 20 preview features in IntelliJ IDEA, follow these steps:
- Go to File | Project Structure.
- Set the Project SDK to 20.
- Set the Project language level to "20 (Preview)...".
- Make sure you have the correct JDK selected.
4. To verify that the Java 8 module is set up correctly,
run [CoffeeShopTest](jdk8/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java) in the
jdk8 module - the class should compile and all tests will pass.
4. To verify that the Java 21 module is set up correctly,
5. To verify that the Java 21 module is set up correctly,
run [CoffeeShopTest](jdk21/src/test/java/bnymellon/codekatas/coffeeshopkata/CoffeeShopTest.java) in the
jdk21 module - the class should compile but most tests will fail.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public List<String> getFoodItemsForOrder() {
*
* @see <a href="https://openjdk.org/jeps/440">...</a>
*/
public String generateReceipt() {
public String generateReceiptForFoodItems() {
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package bnymellon.codekatas.coffeeshopkata.beverage;

public final class Americano implements CoffeeDrink
public class Americano implements CoffeeDrink
{
private final DrinkTemperature drinkTemperature;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* establish a controlled hierarchy for extensions.
* Modify the following class to permit only the classes
* Latte, Macchiato, and Americano, while excluding Tea.
* Make Americano, Macchiato "non-sealed" class and Latte as a "final" class
*
* NOTE: This class hierarchy shows the usage of sealed classes
* @see <a href="https://openjdk.org/jeps/395">...</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import bnymellon.codekatas.coffeeshopkata.beverage.FlavorSyrup;
import bnymellon.codekatas.coffeeshopkata.beverage.MilkType;

public final class Latte implements CoffeeDrink
public class Latte implements CoffeeDrink
{
private final MilkType milkType;
private final boolean extraFoam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package bnymellon.codekatas.coffeeshopkata.beverage;

public final class Macchiato implements CoffeeDrink
public class Macchiato implements CoffeeDrink
{
private final MilkType milkType;
private final FlavorSyrup flavorSyrup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import bnymellon.codekatas.coffeeshopkata.beverage.Americano;
import bnymellon.codekatas.coffeeshopkata.beverage.CoffeeDrink;
import bnymellon.codekatas.coffeeshopkata.beverage.DrinkTemperature;
import bnymellon.codekatas.coffeeshopkata.beverage.Latte;
import bnymellon.codekatas.coffeeshopkata.beverage.Macchiato;
import bnymellon.codekatas.coffeeshopkata.food.Bagel;
Expand Down Expand Up @@ -45,42 +46,23 @@ public class CoffeeShopTest
private Bagel bagel1;
private Cookie cookie1;
private Donut donut1;
private Americano americano;

@BeforeEach
public void setUp()
{
bagel1 = new Bagel(EVERYTHING, HERB_GARLIC_CREAM_CHEESE, true);
cookie1 = new Cookie(CHOCOLATE_CHIP, true);
donut1 = new Donut(GLAZED);
americano = new Americano(DrinkTemperature.HOT);
itemList = new ArrayList<>();
itemList.add(bagel1);
itemList.add(cookie1);
itemList.add(donut1);
itemList.add(americano);
coffeeShopOrder = new CoffeeShopOrder("Emilie", itemList);
}

@Test
public void getFoodItemsForOrderTest()
{
// TODO: Complete the method getFoodItemsForOrder() in CoffeeShopOrder to make this pass
List<String> expected = List.of("CHOCOLATE_CHIP cookie", "EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", "GLAZED donut");
List<String> actual = coffeeShopOrder.getFoodItemsForOrder();
Collections.sort(actual);
assertEquals(expected, actual);
}

@Test
public void generateReceiptTest()
{
// TODO: Complete the method generateReceipt() in CoffeeShopOrder to make this pass
String expectedReceipt = """
Bagel: EVERYTHING $2.5
Cookie: CHOCOLATE_CHIP $1.25
Donut: GLAZED $1.75
Total: $5.5""";
assertEquals(expectedReceipt, coffeeShopOrder.generateReceipt());
}

@Test
public void testBagelRecord()
{
Expand All @@ -100,6 +82,28 @@ public void testBagelGetters()
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
public void generateReceiptForFoodItems()
{
// TODO: Complete the method generateReceipt() in CoffeeShopOrder to make this pass
String expectedReceipt = """
Bagel: EVERYTHING $2.5
Cookie: CHOCOLATE_CHIP $1.25
Donut: GLAZED $1.75
Total: $5.5""";
assertEquals(expectedReceipt, coffeeShopOrder.generateReceiptForFoodItems());
}

@Test
public void getFoodItemsForOrderTest()
{
// TODO: Complete the method getFoodItemsForOrder() in CoffeeShopOrder to make this pass
List<String> expected = List.of("CHOCOLATE_CHIP cookie", "EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE", "GLAZED donut");
List<String> actual = coffeeShopOrder.getFoodItemsForOrder();
Collections.sort(actual);
assertEquals(expected, actual);
}

@Test
public void testSealedClasses()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import bnymellon.codekatas.coffeeshopkata.food.CookieType;
import bnymellon.codekatas.coffeeshopkata.food.Donut;
import bnymellon.codekatas.coffeeshopkata.food.DonutType;
import bnymellon.codekatas.coffeeshopkata.food.SpreadType;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -90,36 +91,38 @@ else if (item instanceof Donut)
* NOTE: The method highlights the usage of a data extraction
* HINT: Use instanceOf
*/
public String generateReceipt()
public String generateReceiptForFoodItems()
{
double total = 0.0;
StringBuilder receiptBuilder = new StringBuilder();
List<String> receiptItems = new ArrayList<>();

for (Item item : this.orderItems)
{
if (item instanceof Donut)
{
Donut donut = (Donut) item;
DonutType donutType = donut.getDonutType();
receiptBuilder.append("Donut: ").append(donutType).append(" $").append(item.getPrice()).append("\n");
receiptItems.add("Donut: "+ donutType + " $" + item.getPrice());
total += item.getPrice();
}
else if (item instanceof Bagel)
{
Bagel bagel = (Bagel) item;
BagelType bagelType = bagel.getBagelType();
receiptBuilder.append("Bagel: ").append(bagelType).append(" $").append(item.getPrice()).append("\n");
receiptItems.add("Bagel: "+ bagelType +" $" + item.getPrice());
total += item.getPrice();
}
else if (item instanceof Cookie)
{
Cookie cookie = (Cookie) item;
CookieType cookieType = cookie.getCookieType();
receiptBuilder.append("Cookie: ").append(cookieType).append(" $").append(item.getPrice()).append("\n");
receiptItems.add("Cookie: " + cookieType +" $" + item.getPrice());
total += item.getPrice();
}
total += item.getPrice();
}
receiptItems.add("Total: $" + total);

receiptBuilder.append("Total: $").append(total);
return receiptBuilder.toString();
return String.join("\n", receiptItems);
}

/**
Expand Down
Loading

0 comments on commit 3dceb9e

Please sign in to comment.