Advertisement
Guest User

Untitled

a guest
Jun 19th, 2021
239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4.  
  5. namespace ClassroomProject
  6. {
  7.     public class Classroom
  8.     {
  9.         public List<Student> Students { get; set; }
  10.         public int Capacity { get; set; }
  11.         public int Count => Students.Count;
  12.  
  13.         public Classroom(int capacity)
  14.         {
  15.             Capacity = capacity;
  16.             Students = new List<Student>(capacity);
  17.         }
  18.  
  19.         public string RegisterStudent(Student student)
  20.         {
  21.             if (this.Count < this.Capacity)
  22.             {
  23.                 Students.Add(student);
  24.                 return $"Added student {student.FirstName} {student.LastName}";
  25.             }
  26.             else
  27.             {
  28.                 return "No seats in the classroom";
  29.             }
  30.         }
  31.  
  32.         public string DismissStudent(string firstName, string lastName)
  33.         {
  34.             if (Students.Any(f => f.FirstName == firstName && f.LastName == lastName))
  35.             {
  36.                 Student toRemove = Students.FirstOrDefault(f => f.FirstName == firstName && f.LastName == lastName);
  37.                 Students.Remove(toRemove);
  38.                 return $"Dismissed student {toRemove.FirstName} {toRemove.LastName}";
  39.             }
  40.             else
  41.             {
  42.                 return "Student not found";
  43.             }
  44.  
  45.         }
  46.  
  47.         public string GetSubjectInfo(string subject)
  48.         {
  49.             int counter = 0;
  50.  
  51.             foreach (Student student in Students)
  52.             {
  53.                 if (student.Subject == subject)
  54.                 {
  55.                     counter++;
  56.                     break;
  57.                 }
  58.             }
  59.  
  60.             if (counter > 0)
  61.             {
  62.                 StringBuilder result = new StringBuilder();
  63.  
  64.                 result.AppendLine($"Subject: {subject}");
  65.                 result.AppendLine("Students:");
  66.  
  67.                 foreach (var student in Students.Where(student => student.Subject == subject))
  68.                 {
  69.                     result.AppendLine($"{student.FirstName} {student.LastName}");
  70.                 }
  71.  
  72.                 return result.ToString().TrimEnd();
  73.             }
  74.  
  75.             else
  76.             {
  77.                 return "No students enrolled for the subject";
  78.             }
  79.         }
  80.  
  81.         public string GetStudentsCount()
  82.         {
  83.             return $"{Students.Count}";
  84.         }
  85.  
  86.         public Student GetStudent(string firstName, string lastName)
  87.         {
  88.             Student student = Students.FirstOrDefault(
  89.                 x => x.FirstName == firstName
  90.                 && x.LastName == lastName);
  91.             return student;
  92.         }
  93.     }
  94. }
  95.  
Advertisement
RAW Paste Data Copied
Advertisement