Skip to content

Commit

Permalink
add step in setup file, fix methods in coffeeShopOrder class
Browse files Browse the repository at this point in the history
Signed-off-by: Aqsa Malik <aqsa.malik@bnymellon.com>
  • Loading branch information
aqsa505 committed Sep 20, 2023
1 parent 0147b30 commit 6ae53ad
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,29 @@ public List<String> getFoodItemsForOrder()
public String generateReceipt()
{
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 @@ -58,12 +58,20 @@ public void setUp()
}

@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 @@ -78,21 +86,14 @@ public void generateReceiptTest()
}

@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 the following 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 @@ -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 @@ -60,13 +60,22 @@ public void setUp()
}

@Test
public void getFoodItemsForOrderTest()
public void testBagelRecord()
{
// 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);
// TODO: Convert Bagel to record
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()
{
// TODO: Convert assertions to use Record getters
assertTrue(bagel1.isToasted());
assertEquals(bagel1.getBagelType(), EVERYTHING);
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
Expand All @@ -82,22 +91,13 @@ public void generateReceiptTest()
}

@Test
public void testBagelRecord()
{
// TODO: Convert Bagel to record
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()
public void getFoodItemsForOrderTest()
{
// TODO: Convert assertions to use Record getters
assertTrue(bagel1.isToasted());
assertEquals(bagel1.getBagelType(), EVERYTHING);
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
// 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
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 @@ -93,33 +94,35 @@ else if (item instanceof Donut)
public String generateReceipt()
{
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,19 @@ public void setUp()
}

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

@Test
public void testBagelGetters()
{
assertTrue(bagel1.isToasted());
assertEquals(bagel1.getBagelType(), EVERYTHING);
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
Expand All @@ -76,19 +80,15 @@ public void generateReceiptTest()
}

@Test
public void testBagelClass()
{
Bagel bagel2 = new Bagel(EVERYTHING, HERB_GARLIC_CREAM_CHEESE, true);
assertEquals(bagel1, bagel2);
assertEquals("Bagel[bagelType=EVERYTHING, spreadType=HERB_GARLIC_CREAM_CHEESE, toasted=true]", bagel1.toString());
}

@Test
public void testBagelGetters()
public void getFoodItemsForOrderTest()
{
assertTrue(bagel1.isToasted());
assertEquals(bagel1.getBagelType(), EVERYTHING);
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
ArrayList<String> expected = new ArrayList<>();
expected.add("CHOCOLATE_CHIP cookie");
expected.add("EVERYTHING bagel with HERB_GARLIC_CREAM_CHEESE");
expected.add("GLAZED donut");
List<String> actual = coffeeShopOrder.getFoodItemsForOrder();
Collections.sort(actual);
assertEquals(expected, actual);
}

@Test
Expand Down

0 comments on commit 6ae53ad

Please sign in to comment.