Advertisement
shady_obeyd

03.Mankind-Student.cs

Feb 27th, 2018
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Student : Human
  6. {
  7.     private string facultyNumber;
  8.  
  9.     public string FacultyNumber
  10.     {
  11.         get { return facultyNumber; }
  12.         protected set
  13.         {
  14.             if (!IsValid(value))
  15.             {
  16.                 throw new ArgumentException("Invalid faculty number!");
  17.             }
  18.             facultyNumber = value;
  19.         }
  20.     }
  21.  
  22.     public Student(string firstName, string lastName, string facultyNumber) : base(firstName, lastName)
  23.     {
  24.         FacultyNumber = facultyNumber;
  25.     }
  26.  
  27.     public override string ToString()
  28.     {
  29.         StringBuilder builder = new StringBuilder();
  30.  
  31.         builder.AppendLine(base.ToString());
  32.         builder.AppendLine($"Faculty number: {FacultyNumber}");
  33.  
  34.         return builder.ToString().TrimEnd();
  35.     }
  36.  
  37.     private bool IsValid(string value)
  38.     {
  39.         Regex pattern = new Regex(@"^[A-Za-z0-9]{5,10}$");
  40.  
  41.         if (pattern.IsMatch(value))
  42.         {
  43.             return true;
  44.         }
  45.         return false;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement