TheBulgarianWolf

School OOP

Aug 23rd, 2021
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.96 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace SchoolOOP
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<string> input = ReadInput();
  15.             List<Person> people = ParseInput(input);
  16.  
  17.             people.Sort();
  18.             people.ForEach(p => Console.WriteLine(p));
  19.         }
  20.  
  21.         private static List<string> ReadInput()
  22.         {
  23.             List<string> input = new List<string>();
  24.             Int32 numberOfLines;
  25.             string firstLineOfInput = Console.ReadLine();
  26.             numberOfLines = Int32.Parse(firstLineOfInput);
  27.             for(int lineCounter = 0;lineCounter < numberOfLines; lineCounter++)
  28.             {
  29.                 string line = Console.ReadLine();
  30.                 input.Add(line);
  31.             }
  32.  
  33.             return input;
  34.         }
  35.  
  36.         private static List<Person> ParseInput(List<string> input)
  37.         {
  38.             const Int32 firstNameIndex = 1, lastNameIndex = 2,
  39.                 studentNumberIndex = 3, disciplinesStartIndex = 3;
  40.             Char[] separators = new Char[] { ' ' };
  41.             List<Person> people = new List<Person>();
  42.  
  43.             foreach (String line in input)
  44.             {
  45.                 String[] words = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  46.  
  47.                 switch (words[0].ToLower())
  48.                 {
  49.                     case "teacher":
  50.                         List<Discipline> disciplines = ParseDisciplines(words, disciplinesStartIndex);
  51.                         Teacher teacher = new Teacher(words[firstNameIndex], words[lastNameIndex], disciplines);
  52.                         people.Add(teacher);
  53.                         break;
  54.                     case "student":
  55.                         Student student = new Student(words[firstNameIndex], words[lastNameIndex], Int32.Parse(words[studentNumberIndex]));
  56.                         people.Add(student);
  57.                         break;
  58.                     default:
  59.                         throw new ArgumentException("Unknown type!");
  60.                 }
  61.             }
  62.             return people;
  63.         }
  64.  
  65.         private static List<Discipline> ParseDisciplines(String[] words,Int32 startIndex)
  66.         {
  67.             List<Discipline> disciplines = new List<Discipline>();
  68.  
  69.             for(int i = startIndex; i < words.Length; i++)
  70.             {
  71.                 Discipline discipline = new Discipline(words[i]);
  72.                 disciplines.Add(discipline);
  73.             }
  74.  
  75.             return disciplines;
  76.         }
  77.  
  78.         class Class : IComparable, IEnumerable
  79.         {
  80.             private List<Student> students;
  81.             public String ID { get; set; }
  82.  
  83.             public Class(string id)
  84.             {
  85.                 this.ID = id;
  86.                 this.students = new List<Student>();
  87.             }
  88.  
  89.             public void Add(Student student)
  90.             {
  91.                 if(student == null)
  92.                 {
  93.                     throw new NullReferenceException();
  94.                 }
  95.  
  96.                 if (this.students.Contains(student))
  97.                 {
  98.                     throw new ArgumentException("The class has a student with the same number!");
  99.                 }
  100.  
  101.                 this.students.Add(student);
  102.             }
  103.  
  104.             public bool Contains(Student student)
  105.             {
  106.                 if(student == null)
  107.                 {
  108.                     throw new NullReferenceException();
  109.                 }
  110.  
  111.                 return this.students.Contains(student);
  112.             }
  113.  
  114.             public void Remove(Student student)
  115.             {
  116.                 if (student == null)
  117.                 {
  118.                     throw new NullReferenceException();
  119.                 }
  120.  
  121.                 if (!this.Contains(student))
  122.                 {
  123.                     throw new Exception("Class does not contain a student with this number!");
  124.                 }
  125.  
  126.                 this.students.Remove(student);
  127.             }
  128.  
  129.             public override bool Equals(object obj)
  130.             {
  131.                 if(obj == null)
  132.                 {
  133.                     return false;
  134.                 }
  135.  
  136.                 if(this.GetType() != obj.GetType())
  137.                 {
  138.                     return false;
  139.                 }
  140.  
  141.                 Class otherClass = (Class)obj;
  142.  
  143.                 return this.ID.Equals(otherClass.ID);
  144.             }
  145.  
  146.             public override string ToString()
  147.             {
  148.                 return this.ID;
  149.             }
  150.  
  151.             public override int GetHashCode()
  152.             {
  153.                 return this.ID.GetHashCode();
  154.             }
  155.  
  156.             public int CompareTo(object obj)
  157.             {
  158.                 if (null == obj) return 1;
  159.  
  160.                 Class otherClass = obj as Class;
  161.  
  162.                 if (otherClass != null)
  163.                 {
  164.                     return this.ID.CompareTo(otherClass.ID);
  165.                 }
  166.                 else
  167.                 {
  168.                     throw new ArgumentException("Object is not a Cell");
  169.                 }
  170.             }
  171.  
  172.             public IEnumerator GetEnumerator()
  173.             {
  174.                 return new StudentEnum(this.students);
  175.             }
  176.         }
  177.  
  178.         class StudentEnum : IEnumerator
  179.         {
  180.             List<Student> students;
  181.  
  182.             int position = -1;
  183.  
  184.             public StudentEnum(List<Student> students)
  185.             {
  186.                 this.students = students;
  187.             }
  188.  
  189.             public bool MoveNext()
  190.             {
  191.                 position++;
  192.                 return (position < students.Count);
  193.             }
  194.  
  195.             public void Reset()
  196.             {
  197.                 position = -1;
  198.             }
  199.  
  200.             object IEnumerator.Current
  201.             {
  202.                 get
  203.                 {
  204.                     return Current;
  205.                 }
  206.             }
  207.  
  208.             public Student Current
  209.             {
  210.                 get
  211.                 {
  212.                     try
  213.                     {
  214.                         return students[position];
  215.                     }
  216.                     catch(IndexOutOfRangeException)
  217.                     {
  218.                         throw new InvalidOperationException();
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.  
  224.         class Discipline
  225.         {
  226.             const Int32 defaultNumberOfLectures = 25;
  227.             const Int32 defaultNumberOfExercises = 25;
  228.  
  229.             public string Name { get; set; }
  230.             public Int32 NumberOfLectures { get; set; }
  231.             public Int32 NumberOfExercises { get; set; }
  232.  
  233.             public Discipline(string name) : this(name, defaultNumberOfLectures, defaultNumberOfExercises)
  234.             {
  235.  
  236.             }
  237.  
  238.             public Discipline(string name,Int32 numberOfLectures,Int32 numberOfExercises)
  239.             {
  240.                 this.Name = name;
  241.                 this.NumberOfLectures = numberOfLectures;
  242.                 this.NumberOfExercises = numberOfExercises;
  243.             }
  244.  
  245.             public override string ToString()
  246.             {
  247.                 return this.Name;
  248.             }
  249.  
  250.         }
  251.  
  252.         class Person : IComparable
  253.         {
  254.             public String FirstName { get; set; }
  255.             public String LastName { get; set; }
  256.  
  257.             public Person(string firstName,string lastName)
  258.             {
  259.                 this.FirstName = firstName;
  260.                 this.LastName = lastName;
  261.             }
  262.  
  263.             public override string ToString()
  264.             {
  265.                 return String.Format("{0} {1}", this.FirstName, this.LastName);
  266.             }
  267.  
  268.             public int CompareTo(object obj)
  269.             {
  270.                 if (null == obj) return 1;
  271.  
  272.                 Person otherPerson = obj as Person;
  273.  
  274.                 if(otherPerson != null)
  275.                 {
  276.                     Int32 firstNameComparisson = this.FirstName.CompareTo(otherPerson.FirstName);
  277.                     if(firstNameComparisson == 0)
  278.                     {
  279.                         firstNameComparisson = this.LastName.CompareTo(otherPerson.LastName);
  280.                     }
  281.  
  282.                     return firstNameComparisson;
  283.                 }
  284.                 else
  285.                 {
  286.                     throw new ArgumentException("Object is not a person");
  287.                 }
  288.             }
  289.  
  290.             public override bool Equals(object obj)
  291.             {
  292.                 if (obj == null) return false;
  293.  
  294.                 if (this.GetType() != obj.GetType()) return false;
  295.  
  296.                 Person otherPerson = (Person)obj;
  297.  
  298.                 bool firstNamesAreEqual = this.FirstName == otherPerson.FirstName;
  299.                 bool lastNamesAreEqual = this.LastName == otherPerson.LastName;
  300.  
  301.                 return firstNamesAreEqual && lastNamesAreEqual;
  302.             }
  303.  
  304.             public override int GetHashCode()
  305.             {
  306.                 return (this.FirstName + this.LastName).GetHashCode();
  307.             }
  308.         }
  309.  
  310.         class Teacher : Person
  311.         {
  312.             public List<Discipline> Disciplines { get; set; }
  313.  
  314.             public Teacher(string firstName,string lastName) : base(firstName, lastName)
  315.             {
  316.                 this.Disciplines = new List<Discipline>();
  317.             }
  318.  
  319.             public Teacher(string firstName,string lastName,List<Discipline> disciplines) : base(firstName,lastName)
  320.             {
  321.                 this.Disciplines = disciplines;
  322.             }
  323.  
  324.             public override string ToString()
  325.             {
  326.                 return String.Format("{0} {1}", base.ToString(), this.ConvertDisciplineToString());
  327.             }
  328.  
  329.             private string ConvertDisciplineToString()
  330.             {
  331.                 StringBuilder outputString = new StringBuilder();
  332.                 foreach(Discipline discipline in this.Disciplines)
  333.                 {
  334.                     outputString.Append(discipline.ToString() + " ");
  335.                 }
  336.  
  337.                 return outputString.ToString().Trim();
  338.             }
  339.         }
  340.  
  341.         class Student : Person
  342.         {
  343.             public Int32 Number { get; set; }
  344.             public Student(string firstName,string lastName,Int32 number) : base(firstName, lastName)
  345.             {
  346.                 this.Number = number;
  347.             }
  348.  
  349.             public override bool Equals(object obj)
  350.             {
  351.                 if (obj == null) return false;
  352.  
  353.                 if (this.GetType() != obj.GetType()) return false;
  354.  
  355.                 Student otherPerson = (Student)obj;
  356.                 return this.Number.Equals(otherPerson.Number);
  357.             }
  358.  
  359.             public override int GetHashCode()
  360.             {
  361.                 return this.Number.GetHashCode();
  362.             }
  363.  
  364.             public override string ToString()
  365.             {
  366.                 return String.Format("{0} {1}", base.ToString(), this.Number);
  367.             }
  368.         }
  369.     }
  370. }
Add Comment
Please, Sign In to add comment