Shopping Cart System with Kotlin, TDD, and JUnit

Introduction to the Shopping Cart System

Welcome to your third unit of this course dedicated to practicing Test Driven Development (TDD) utilizing Kotlin and JUnit. We will begin building a new system; this time, we'll focus on creating a shopping cart system with a variety of features.

In this course, emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to write tests and implement code that makes each test pass, simulating a real-world TDD environment. Unlike previous exercises, where tests were supplied, this time you're in charge!

Remember to utilize the core concepts of the Red-Green-Refactor cycle while working through these exercises. Assistance is available, so don't hesitate to ask for help.

Requirements for ShoppingCart Class

The following requirements outline the foundational functionality of the ShoppingCart class, providing step-by-step guidance to build and test its key features while practicing TDD principles.

Each requirement focuses on a specific aspect of the shopping cart's behavior, ensuring a comprehensive and modular approach to its development and testing.

1. Starting with an Empty Cart

  • Description: When a new ShoppingCart is created in Kotlin, it should start without any items, and the total price should be zero.
  • Details
    • Initialize a new shopping cart using the ShoppingCart constructor.
    • Ensure the method getItemCount() returns 0 for an empty cart.
    • Verify the method getTotal() returns 0 for the initial state.
  • Examples: A newly created cart should have an item count of 0 and a total price of 0.
class ShoppingCart {
    private val items = mutableListOf<Item>()

    fun getItemCount(): Int = items.size

    fun getTotal(): Double = items.sumOf { it.price * it.quantity }
}

JUnit Test Example:

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class ShoppingCartTest {
    @Test
    fun newlyCreatedCartShouldHaveZeroItemsAndZeroTotal() {
        val cart = ShoppingCart()
        assertEquals(0, cart.getItemCount())
        assertEquals(0.0, cart.getTotal())
    }
}

2. Adding a Single Item

  • Description: Verify that adding a single item to the cart increases the item count and adjusts the total price to include the price of the added item.
  • Details
    • Add an item to the cart using the addItem(Item) method.
    • Confirm that getItemCount() reflects the change in the number of items.
    • Ensure getTotal() accurately accounts for the total price of items in the cart.
  • Examples: Adding an Item with id = "1", name = "Book", and price = 10 should result in an item count of 1 and a total of 10.

Kotlin Code Example:

data class Item(val id: String, val name: String, val price: Double, val quantity: Int = 1)

class ShoppingCart {
    private val items = mutableListOf<Item>()

    fun addItem(item: Item) {
        items.add(item)
    }

    fun getItemCount(): Int = items.size

    fun getTotal(): Double = items.sumOf { it.price * it.quantity }
}

JUnit Test Example:

class ShoppingCartTest {
    @Test
    fun addingASingleItemShouldIncreaseItemCountAndTotalPrice() {
        val cart = ShoppingCart()
        cart.addItem(Item("1", "Book", 10.0))
        assertEquals(1, cart.getItemCount())
        assertEquals(10.0, cart.getTotal())
    }
}
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal