Advertisement
yanchevilian

03. Classroom(only class classroom) / C# Advanced Exam - 25 October 2020

Sep 20th, 2021
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Linq;
  5. using System.Reflection.Metadata.Ecma335;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Text;
  8.  
  9. namespace ClassroomProject
  10. {
  11.     public class Classroom
  12.     {
  13.         public Classroom(int capacity)
  14.         {
  15.             this.Capacity = capacity;
  16.             this.AllStudents = new List<Student>(capacity);
  17.         }
  18.         public int Capacity { get; set; }
  19.         public List<Student> AllStudents { get; set; }
  20.         public int Count => AllStudents.Count;
  21.  
  22.         //Method RegisterStudent(Student student) – adds an entity to the students if there is an empty seat for the student.
  23.         //Returns "Added student {firstName} {lastName}" if the student is successfully added
  24.         //Returns "No seats in the classroom" – if there are no more seats in the classroom
  25.  
  26.         public string RegisterStudent(Student student)
  27.         {
  28.             if (this.Capacity > this.Count)
  29.             {
  30.                 AllStudents.Add(student);
  31.                 return $"Added student {student.FirstName} {student.LastName}";
  32.             }
  33.             return "No seats in the classroom";
  34.         }
  35.         // Method DismissStudent(string firstName, string lastName) – removes the student by the given names
  36.         // Returns "Dismissed student {firstName} {lastName}" if the student is successfully dismissed
  37.         // Returns "Student not found" if the student is not in the classroom
  38.         public string DismissStudent(string firstName, string lastName)
  39.         {
  40.             if (AllStudents.Any(x => x.FirstName == firstName && x.LastName == lastName))
  41.             {
  42.                 var removedStudent = AllStudents.FirstOrDefault(x => x.FirstName == firstName && x.LastName == lastName);
  43.                 AllStudents.Remove(removedStudent);
  44.                 return $"Dismissed student {removedStudent?.FirstName} {removedStudent?.LastName}";
  45.             }
  46.             return "Student not found";
  47.         }
  48.         //•   Method GetSubjectInfo(string subject) – returns all the students with the given subject in the following format:
  49.         //"Subject: {subjectName}
  50.         //Students:
  51.         //{firstName {lastName}
  52.         //{ firstName} { lastName}"
  53.         public string GetSubjectInfo(string subject)
  54.         {
  55.             if (AllStudents.Any(s => s.Subject == subject))
  56.             {
  57.                 StringBuilder result = new StringBuilder();
  58.  
  59.                 result.AppendLine($"Subject: {subject}");
  60.                 result.AppendLine("Students:");
  61.  
  62.                 foreach (var student in AllStudents.Where(student => student.Subject == subject))
  63.                 {
  64.                     result.AppendLine($"{student.FirstName} {student.LastName}");
  65.                 }
  66.  
  67.                 return result.ToString().TrimEnd();
  68.             }
  69.             return "No students enrolled for the subject";
  70.         }
  71.         //•   Method GetStudentsCount() – returns the count of the students in the classroom.
  72.         public string GetStudentsCount()
  73.         {
  74.             return $"{AllStudents.Count}";
  75.         }
  76.         //•   Method GetStudent(string firstName, string lastName) – returns the student with the given names.
  77.         public Student GetStudent(string firstName, string lastName)
  78.         {
  79.             var student = AllStudents.FirstOrDefault(x => x.FirstName == firstName && x.LastName == lastName);
  80.             return student;
  81.         }
  82.  
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement