Nackey

Mankind

Feb 26th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using System;
  2.  
  3. public class StartUp
  4. {
  5.     public static void Main()
  6.     {
  7.         try
  8.         {
  9.             string[] studentTokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  10.             string[] workerTokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  11.  
  12.             Student student = new Student(studentTokens[0], studentTokens[1], studentTokens[2]);
  13.  
  14.             Worker worker = new Worker
  15.                 (workerTokens[0]
  16.                 , workerTokens[1]
  17.                 , decimal.Parse(workerTokens[2])
  18.                 , decimal.Parse(workerTokens[3]));
  19.  
  20.             Console.WriteLine(student.ToString());
  21.             Console.WriteLine(worker.ToString());
  22.         }
  23.         catch (ArgumentException argEx)
  24.         {
  25.             Console.WriteLine(argEx.Message);
  26.         }
  27.        
  28.     }
  29. }
  30.  
  31. using System;
  32. using System.Text;
  33.  
  34. public class Human
  35. {
  36.     private string firstName;
  37.     private string lastName;
  38.  
  39.     public Human(string firstName, string lastName)
  40.     {
  41.         this.FirstName = firstName;
  42.         this.LastName = lastName;
  43.     }
  44.  
  45.     public string FirstName
  46.     {
  47.         get { return this.firstName; }
  48.         set
  49.         {
  50.             if (char.IsLower(value[0]))
  51.             {
  52.                 throw new ArgumentException($"Expected upper case letter! Argument: firstName");
  53.             }
  54.             if (value.Length < 4)
  55.             {
  56.                 throw new ArgumentException($"Expected upper case letter! Argument: firstName");
  57.             }
  58.             this.firstName = value;
  59.         }
  60.     }
  61.  
  62.     public string LastName
  63.     {
  64.         get { return this.lastName; }
  65.         set
  66.         {
  67.             if (char.IsLower(value[0]))
  68.             {
  69.                 throw new ArgumentException($"Expected upper case letter! Argument: lastName");
  70.             }
  71.             if (value.Length < 3)
  72.             {
  73.                 throw new ArgumentException($"Expected upper case letter! Argument: lastName");
  74.             }
  75.             this.lastName = value;
  76.         }
  77.     }
  78.  
  79.     public override string ToString()
  80.     {
  81.         var sb = new StringBuilder();
  82.         sb
  83.             .AppendLine($"First Name: {this.FirstName}")
  84.             .AppendLine($"Last Name: {this.LastName}");
  85.  
  86.         return sb.ToString();
  87.     }
  88. }
  89.  
  90. using System;
  91. using System.Text;
  92. using System.Text.RegularExpressions;
  93.  
  94. public class Student : Human
  95. {
  96.     private const string facultyValidation = @"^[A-Za-z\d]{5,10}$";
  97.     private string facultyNumber;
  98.  
  99.     public Student(string firstName, string lastName, string facultyNumber)
  100.         : base(firstName, lastName)
  101.     {
  102.         this.FacultyNumber = facultyNumber;
  103.     }
  104.  
  105.     public string FacultyNumber
  106.     {
  107.         get { return this.facultyNumber; }
  108.         set
  109.         {
  110.             if (value.Length < 5 || value.Length > 10)
  111.             {
  112.                 throw new ArgumentException("Invalid faculty number!");
  113.             }
  114.             if (!Regex.IsMatch(value, facultyValidation))
  115.             {
  116.                 throw new ArgumentException("Invalid faculty number!");
  117.             }
  118.             this.facultyNumber = value;
  119.         }
  120.     }
  121.  
  122.     public override string ToString()
  123.     {
  124.         var sb = new StringBuilder();
  125.         sb.Append(base.ToString())
  126.         .AppendLine($"Faculty number: {this.FacultyNumber}");
  127.  
  128.         return sb.ToString();
  129.     }
  130. }
  131.  
  132. using System;
  133. using System.Text;
  134.  
  135. public class Worker : Human
  136. {
  137.     private const int workDays = 5;
  138.  
  139.     private decimal weekSalary;
  140.     private decimal workHours;
  141.  
  142.     public Worker(string firstName, string lastName, decimal weekSalary, decimal workHours)
  143.         : base(firstName, lastName)
  144.     {
  145.         this.WeekSalary = weekSalary;
  146.         this.WorkHours = workHours;
  147.     }
  148.  
  149.     public decimal WeekSalary
  150.     {
  151.         get { return this.weekSalary; }
  152.         set
  153.         {
  154.             if (value <= 10)
  155.             {
  156.                 throw new ArgumentException($"Expected value mismatch! Argument: weekSalary");
  157.             }
  158.             this.weekSalary = value;
  159.         }
  160.     }
  161.  
  162.     public decimal WorkHours
  163.     {
  164.         get { return this.workHours; }
  165.         set
  166.         {
  167.             if (value < 1 || value > 12)
  168.             {
  169.                 throw new ArgumentException($"Expected value mismatch! Argument: workHoursPerDay");
  170.             }
  171.             this.workHours = value;
  172.         }
  173.     }
  174.  
  175.     private decimal CalculateSalaryPerHour()
  176.     {
  177.         return this.WeekSalary / (workDays * this.WorkHours);
  178.     }
  179.  
  180.     public override string ToString()
  181.     {
  182.         var sb = new StringBuilder();
  183.         sb.Append(base.ToString())
  184.             .AppendLine($"Week Salary: {this.WeekSalary:f2}")
  185.             .AppendLine($"Hours per day: {this.WorkHours:f2}")
  186.             .AppendLine($"Salary per hour: {this.CalculateSalaryPerHour():f2}");
  187.         return sb.ToString();
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment