Advertisement
hristo_bratanov

Untitled

May 3rd, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace InheritanceAndPolymorphism
  9. {
  10.     abstract class Course
  11.     {
  12.         private string courseName;
  13.         public string TeacherName { get; set; }
  14.         private IList<string> students;
  15.  
  16.         public string CourseName
  17.         {
  18.             get
  19.             {
  20.                 return this.courseName;
  21.             }
  22.             set
  23.             {
  24.                 if (string.IsNullOrWhiteSpace(value))
  25.                 {
  26.                     throw new ArgumentException("There is not any name of course entered.");
  27.                 }
  28.  
  29.                 this.courseName = value;
  30.             }
  31.         }
  32.  
  33.         public IList<string> Students
  34.         {
  35.             get
  36.             {
  37.                 return this.students;
  38.             }
  39.             set
  40.             {
  41.                 if (value != null)
  42.                 {
  43.                     this.students = new List<string>();
  44.  
  45.                     foreach (string student in value)
  46.                     {
  47.                         this.students.Add(student);
  48.                     }    
  49.                 }
  50.                 else
  51.                 {
  52.                     this.students = null;
  53.                 }
  54.                
  55.             }
  56.         }
  57.  
  58.         protected Course(string courseName, string teacherName, IList<string> students)
  59.         {
  60.             this.CourseName = courseName;
  61.             this.TeacherName = teacherName;
  62.             this.Students = students;
  63.         }
  64.  
  65.         private string GetStudentsAsString()
  66.         {
  67.             if (this.Students == null || this.Students.Count == 0)
  68.             {
  69.                 return "{ }";
  70.             }
  71.             else
  72.             {
  73.                 return "{ " + string.Join(", ", this.Students) + " }";
  74.             }
  75.         }
  76.  
  77.         public override string ToString()
  78.         {
  79.             StringBuilder result = new StringBuilder();
  80.  
  81.             result.Append("LocalCourse { Name = ");
  82.             result.Append(this.CourseName);
  83.  
  84.             if (this.TeacherName != null)
  85.             {
  86.                 result.Append("; Teacher = ");
  87.                 result.Append(this.TeacherName);
  88.             }
  89.  
  90.             result.Append("; Students = ");
  91.             result.Append(this.GetStudentsAsString());
  92.  
  93.             return result.ToString();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement