Managing a Potluck Dinner System in Kotlin: Ensuring Backward Compatibility and Introducing Advanced Features

Introduction

Welcome to today's lesson! We're exploring how to introduce complex features while maintaining backward compatibility in software systems. Our journey will take us through a Potluck Dinner organization system where participants and their dishes are tracked for each round. Get ready to dive into Kotlin programming, unveiling strategies and crafting solutions. Let's embark on this exciting adventure!

Starter Task Review

Our Potluck Dinner organization system currently lets us add and remove participants and manage their dishes for each round. Here are the essential functions:

  • fun addParticipant(memberId: String): Boolean: Adds a participant if they don't already exist. Returns true if successful, or false if the participant is already in the system.
  • fun removeParticipant(memberId: String): Boolean: Removes a participant and their dish if they exist. Returns true if successful, false otherwise.
  • fun addDish(memberId: String, dishName: String): Boolean: Allows a participant to add a dish. Returns true if added successfully, false if the participant is invalid or has already added a dish.

Let's implement these methods using Kotlin, following the initial state of our system:

class Potluck {
    private val participants = mutableSetOf<String>()
    private val dishes = mutableMapOf<String, String>()

    fun addParticipant(memberId: String): Boolean {
        return if (participants.contains(memberId)) {
            false
        } else {
            participants.add(memberId)
            true
        }
    }

    fun removeParticipant(memberId: String): Boolean {
        return if (!participants.contains(memberId)) {
            false
        } else {
            participants.remove(memberId)
            dishes.remove(memberId)
            true
        }
    }

    fun addDish(memberId: String, dishName: String): Boolean {
        return if (!participants.contains(memberId) || dishes.containsKey(memberId)) {
            false
        } else {
            dishes[memberId] = dishName
            true
        }
    }
}

With this setup, we use Kotlin's mutableSetOf for unique participant IDs and mutableMapOf to associate dishes with participant IDs. Now, let's extend our system with advanced features.

Introducing Advanced Functionalities

Currently, our Potluck Dinner organization system is simple yet effective. To enhance its capabilities, we're introducing a "Dish of the Day" feature. This allows participants to vote for a dish, with the most voted dish earning the title "Dish of the Day."

The new functionalities introduce these methods:

  • fun vote(memberId: String, voteId: String): Boolean: Allows a participant to cast a vote. Each participant can vote only once per round. Returns false if the participant or vote action is invalid.
  • fun dishOfTheDay(): String?: Calculates and returns the "Dish of the Day." In cases of ties, the earliest joining participant's dish is prioritized. Returns null if no votes exist.
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