Skip to content

Lobby thoughts #2

@vandorjw

Description

@vandorjw

Remove the "Loopyness" from Lobby/Rooms/User

This example uses Student and School to try to keep the conversation generic.
Wanted to dump my thoughts before I forget by next session.

using System;
using System.Collections.Generic;

class Student  // User
{
    public string Name { get; set; }
    public School School { get; set; }
}

class School  // Room
{
    public string Name { get; set; }
    public List<Student> Students { get; } = new List<Student>();

    public void AddStudent(Student student)
    {
        if (student.School != null)
        {
            Console.WriteLine($"{student.Name} already belongs to {student.School.Name}. Cannot add to {this.Name}.");
            return;
        }

        Students.Add(student);
        student.School = this;
    }

    public void RemoveStudent(Student student)
    {
        if (!Students.Contains(student))
        {
            Console.WriteLine($"{student.Name} does not belong to {this.Name}. Cannot remove");
            return;
        }

        Students.Remove(student);
        student.School = null;
    }

    public List<Student> GetStudents()
    {
        return Students;
    }
}

class Program
{
    static void Main()
    {
        // Creating schools and students
        School school1 = new School { Name = "School 1" };
        School school2 = new School { Name = "School 2" };
        Student student1 = new Student { Name = "Alice" };

        // Adding students to schools
        school1.AddStudent(student1);
        school1.AddStudent(student1); // Attempting to add the same student to the same school again
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions