Practice

Time to practice!

You will put this session’s Kotlin course to use with a code kata: a short, self-contained exercise meant to be solved from scratch, on your own or in pairs, to practice a language or a design idea rather than to produce "real" production code.

This session’s kata is Cupcake, originally designed to practice the decorator design pattern. We will use it to practice idiomatic Kotlin instead: class hierarchies and composition, as in Object-oriented programming's Discountable example.

Rules

  • Timebox: 1 hour, split roughly as suggested below. Don’t worry if you don’t reach the stretch goal; a kata is about the journey, not the finish line.

  • Start from a fresh Kotlin project (see Project creation).

  • Write a CakeTest class with JUnit 5 (see the testing module of the Spring Boot course) and let the suggested test cases below drive your implementation, one at a time.

  • No peeking at a reference solution until you’ve given it an honest try!

Part 1 (~35 min): cakes and toppings

Design a Cake abstraction with a name() and a price(), two base cakes, and composable toppings, so that you can build things like "Cupcake with chocolate and nuts" and get both its name and its price.

  • A base 🧁 Cupcake costs 1.0, a base 🍪 Cookie costs 2.0.

  • Chocolate adds 0.1, Nuts adds 0.2.

  • Beware: the order of toppings matters for the name. Nuts(Chocolate(Cookie)) should read "🍪 with 🍫 and 🥜", while Chocolate(Nuts(Cookie)) should read "🍪 with 🥜 and 🍫".

Suggested test cases:

 1import org.junit.jupiter.api.Test
 2import org.junit.jupiter.api.Assertions.assertEquals
 3
 4class CakeTest {
 5
 6    @Test
 7    fun aCupcakeIsNamedCupcake() {
 8        assertEquals("🧁", Cupcake().name())
 9    }
10
11    @Test
12    fun aCookieCostsTwoDollars() {
13        assertEquals(2.0, Cookie().price())
14    }
15
16    @Test
17    fun aCupcakeWithChocolateIsNamedAccordingly() {
18        assertEquals("🧁 with 🍫", Chocolate(Cupcake()).name())
19    }
20
21    @Test
22    fun aCupcakeWithChocolateCostsOnePointOneDollars() {
23        assertEquals(1.1, Chocolate(Cupcake()).price(), 0.0001)
24    }
25
26    @Test
27    fun toppingOrderMattersForTheName() {
28        assertEquals("🍪 with 🍫 and 🥜", Nuts(Chocolate(Cookie())).name())
29        assertEquals("🍪 with 🥜 and 🍫", Chocolate(Nuts(Cookie())).name())
30    }
31}

Part 2 (~25 min, stretch goal): bundles

Once a single cake works, introduce a Bundle that groups several cakes (possibly containing other bundles) and whose price is 10% less than the sum of the prices of the cakes it contains.

  • A Bundle should expose a price() just like a Cake does — can a Bundle be a Cake?

  • A bundle of 1 Cupcake and 1 Cookie should cost 0.9 * (1.0 + 2.0).

  • A bundle can contain another bundle: a bundle of (a bundle of 2 cakes) and 1 more cake applies the 10% discount at each level.

Tip
This is a great place to reach for fold or sumOf instead of a manual loop.

Going further

If you finish early, or want to practice at home, the Coding Dojo katalog has plenty more; Manhattan Distance and Pagination Seven are good, shorter follow-ups.