Advertisement
silvi81

Mankind

Jul 2nd, 2016
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.54 KB | None | 0 0
  1. namespace _03.Mankind
  2. {
  3.     using System;
  4.     using System.Text;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     public class Person
  8.     {
  9.         private string firstName;
  10.         private string secondName;
  11.  
  12.         public Person(string firstName, string secondName)
  13.         {
  14.             this.FirstName = firstName;
  15.             this.SecondName = secondName;
  16.         }
  17.  
  18.         public virtual string FirstName
  19.         {
  20.             get
  21.             {
  22.                 return this.firstName;
  23.             }
  24.  
  25.             set
  26.             {
  27.                 if (char.IsLower(value[0]))
  28.                 {
  29.                     throw new ArgumentException("Expected upper case letter! Argument: firstName");
  30.                 }
  31.  
  32.                 this.firstName = value;
  33.             }
  34.         }
  35.  
  36.         public string SecondName
  37.         {
  38.             get
  39.             {
  40.                 return this.secondName;
  41.             }
  42.  
  43.             set
  44.             {
  45.                 if (char.IsLower(value[0]))
  46.                 {
  47.                     throw new ArgumentException("Expected upper case letter! Argument: lastName");
  48.                 }
  49.  
  50.                 if (value.Length < 3)
  51.                 {
  52.                     throw new ArgumentException("Expected length at least 3 symbols! Argument: lastName");
  53.                 }
  54.  
  55.                 this.secondName = value;
  56.             }
  57.         }
  58.  
  59.         public override string ToString()
  60.         {
  61.             StringBuilder result = new StringBuilder();
  62.  
  63.             result.AppendLine(string.Format(
  64.                 "First Name: {0}", this.FirstName));
  65.             result.AppendLine(string.Format(
  66.                 "Last Name: {0}", this.SecondName));
  67.  
  68.             return result.ToString();
  69.         }
  70.     }
  71.  
  72.     public class Student : Person
  73.     {
  74.         private string facultyNumber;
  75.  
  76.         public Student(string firstName, string secondName, string facultyNumber)
  77.             : base(firstName, secondName)
  78.         {
  79.             this.FacultyNumber = facultyNumber;
  80.         }
  81.  
  82.         public string FacultyNumber
  83.         {
  84.             get
  85.             {
  86.                 return this.facultyNumber;
  87.             }
  88.             set
  89.             {
  90.                 if (IsNumberInvalid(value))
  91.                 {
  92.                     throw new ArgumentException("Invalid faculty number!");
  93.                 }
  94.  
  95.                 this.facultyNumber = value;
  96.             }
  97.         }
  98.  
  99.         public override string FirstName
  100.         {
  101.             set
  102.             {
  103.                 if (value.Length < 4)
  104.                 {
  105.                     throw new ArgumentException("Expected length at least 4 symbols! Argument: firstName");
  106.                 }
  107.  
  108.                 base.FirstName = value;
  109.             }
  110.         }
  111.  
  112.         private bool IsNumberInvalid(string value)
  113.         {
  114.             bool isNumberInvalid = false;
  115.  
  116.             string numberPattern = @"^([0-9a-zA-Z]{5,10})$";
  117.  
  118.             var regex = new Regex(numberPattern);
  119.  
  120.             var match = regex.Match(value);
  121.  
  122.             if (match.Success)
  123.             {
  124.                 return isNumberInvalid;
  125.             }
  126.  
  127.             return !isNumberInvalid;
  128.         }
  129.  
  130.         public override string ToString()
  131.         {
  132.             StringBuilder result = new StringBuilder();
  133.  
  134.             result.Append(base.ToString());
  135.  
  136.             result.AppendLine(string.Format(
  137.                 "Faculty number: {0}", this.FacultyNumber));
  138.  
  139.             return result.ToString().Trim();
  140.         }
  141.     }
  142.  
  143.     public class Worker : Person
  144.     {
  145.         private double weekSalary;
  146.         private double workHoursPerDay;
  147.  
  148.         public Worker(string firstName, string secondName, double weekSalary, double workHoursPerDay)
  149.             : base(firstName, secondName)
  150.         {
  151.             this.WeekSalary = weekSalary;
  152.             this.WorkHoursPerDay = workHoursPerDay;
  153.         }
  154.  
  155.         public double WeekSalary
  156.         {
  157.             get { return this.weekSalary; }
  158.             set
  159.             {
  160.                 if (value <= 10)
  161.                 {
  162.                     throw new ArgumentException("Expected value mismatch! Argument: weekSalary");
  163.                 }
  164.  
  165.                 this.weekSalary = value;
  166.             }
  167.         }
  168.  
  169.         public double WorkHoursPerDay
  170.         {
  171.             get { return this.workHoursPerDay; }
  172.             set
  173.             {
  174.                 if (value > 12 || value < 1)
  175.                 {
  176.                     throw new ArgumentException("Expected value mismatch! Argument: workHoursPerDay");
  177.                 }
  178.  
  179.                 this.workHoursPerDay = value;
  180.             }
  181.         }
  182.  
  183.         public double CalcSalary()
  184.         {
  185.             double sal = this.weekSalary / 5 / this.WorkHoursPerDay;
  186.  
  187.             return sal;
  188.         }
  189.  
  190.         public override string ToString()
  191.         {
  192.             StringBuilder result = new StringBuilder();
  193.  
  194.             result.Append(base.ToString());
  195.  
  196.             result.AppendLine(string.Format(
  197.                 "Week Salary: {0:F2}", this.WeekSalary));
  198.             result.AppendLine(string.Format(
  199.                 "Hours per day: {0:F2}", this.WorkHoursPerDay));
  200.             result.AppendLine(string.Format(
  201.                 "Salary per hour: {0:F2}", this.CalcSalary()));
  202.  
  203.             return result.ToString().Trim();
  204.         }
  205.     }
  206.  
  207.     class Program
  208.     {
  209.         static void Main(string[] args)
  210.         {
  211.             string[] tokensStudent = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  212.  
  213.             StringBuilder result = new StringBuilder();
  214.  
  215.             try
  216.             {
  217.                 var student = new Student(tokensStudent[0], tokensStudent[1], tokensStudent[2]);
  218.                 result.AppendLine(student.ToString());
  219.                 result.AppendLine();
  220.             }
  221.             catch (Exception e)
  222.             {
  223.  
  224.                 Console.WriteLine(e.Message);
  225.  
  226.                 Environment.Exit(0);
  227.             }
  228.  
  229.             string[] tokensWorker = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  230.  
  231.             try
  232.             {
  233.                 var worker = new Worker(tokensWorker[0], tokensWorker[1], double.Parse(tokensWorker[2]), double.Parse(tokensWorker[3]));
  234.                 result.AppendLine(worker.ToString());
  235.             }
  236.             catch (Exception e)
  237.             {
  238.  
  239.                 Console.WriteLine(e.Message);
  240.  
  241.                 Environment.Exit(0);
  242.             }
  243.  
  244.             Console.WriteLine(result.ToString());
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement