Developing a Potluck Dinner System with C# for Backward Compatibility

Introduction

Welcome to today's lesson, where we will tackle a common challenge in software engineering: the introduction of complex features while preserving backward compatibility. Our focus will be on a Potluck Dinner organization system, where we will manage participants and their respective dishes for each round. Get ready for an exciting journey through C# programming, step-by-step analysis, and strategic thinking. Let's dive into our adventure!

Starter Task Review

Initially, our Potluck Dinner organization system allows us to add and remove participants and manage their respective dishes for each round. There are three essential methods:

  • bool AddParticipant(string memberId): This method adds a participant. If a participant with the given memberId already exists, it won't create a new one but will return false. Otherwise, it will add the member and return true.
  • bool RemoveParticipant(string memberId): This method removes a participant with the given memberId. If the participant exists, the system will remove them and return true. Otherwise, it will return false. When removing a participant, you need to remove their dish if they brought one.
  • bool AddDish(string memberId, string dishName): This method enables each participant to add their dishes for every round. If a participant has already added a dish for this round OR if the memberId isn't valid, the method will return false. Otherwise, it will add the dish for the respective participant's round and return true.

Let's write our C# code, which implements the functions as per our initial state:

using System;
using System.Collections.Generic;

public class Potluck
{
    private readonly HashSet<string> participants;
    private readonly Dictionary<string, string> dishes;

    public Potluck()
    {
        participants = new HashSet<string>();
        dishes = new Dictionary<string, string>();
    }

    public bool AddParticipant(string memberId)
    {
        if (participants.Contains(memberId))
        {
            return false;
        }
        else
        {
            participants.Add(memberId);
            return true;
        }
    }

    public bool RemoveParticipant(string memberId)
    {
        if (!participants.Contains(memberId))
        {
            return false;
        }
        else
        {
            participants.Remove(memberId);
            dishes.Remove(memberId);
            return true;
        }
    }

    public bool AddDish(string memberId, string dishName)
    {
        if (!participants.Contains(memberId) || dishes.ContainsKey(memberId))
        {
            return false;
        }
        else
        {
            dishes[memberId] = dishName;
            return true;
        }
    }
}

In this code, we employed a C# HashSet<string> to store unique participant IDs and a Dictionary<string, string> to store the participant's ID and their respective dish name. With this foundation laid, let's introduce some advanced functionalities.

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