Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1.  class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.  
  6.             var newPerson = new Person();
  7.             newPerson.Age = 15;
  8.             Console.WriteLine(newPerson.Age);
  9.             newPerson.Name = "kasjdajskdksadjkasjkdaksjdkajkd";
  10.             Console.WriteLine(newPerson.Name);
  11.             var newWorker = new Worker();
  12.             newWorker.Name = "doasjdjasdojaodoaodaoj";
  13.             Console.WriteLine(newWorker.Name);
  14.             newWorker.Salary = -100010;
  15.             Console.WriteLine(newWorker.Salary);
  16.  
  17.         }
  18.     }
  19. }
  20.  
  21.  class Person
  22.     {
  23.         private int age;
  24.         private string name;
  25.        
  26.  
  27.         public Person()
  28.         {
  29.  
  30.         }
  31.  
  32.         public Person(int age, string name)
  33.         {
  34.             this.Age = age;
  35.             this.Name = name;
  36.         }
  37.         public virtual void SayHello()
  38.         {
  39.             Console.WriteLine($"Hello! My name is {Name}");
  40.         }
  41.         public int Age
  42.         {
  43.             get { return this.age; }
  44.             set
  45.             {
  46.                 try
  47.                 {
  48.                     if (value < 18 || value > 60)
  49.                     {
  50.                         throw new ArgumentException();
  51.                     }
  52.                 }
  53.                 catch(ArgumentException e)
  54.                 {
  55.                     Console.WriteLine("Invalid age!");
  56.                 }
  57.                 this.age = value;
  58.             }
  59.         }
  60.         public string Name
  61.         {
  62.             get { return this.name; }
  63.             set
  64.             {
  65.                 try
  66.                 {
  67.                     if(value.Length>15)
  68.                     {
  69.                         throw new ArgumentException();
  70.                     }
  71.                    
  72.                 }
  73.                 catch(ArgumentException e)
  74.                 {
  75.                     Console.WriteLine("Invalid Name!");
  76.                 }
  77.                 this.name = value;
  78.                
  79.             }
  80.         }
  81.     }
  82. }
  83. class Worker : Person
  84.     {
  85.         private int tenure;
  86.         private decimal salary;
  87.         private int timeOff;
  88.  
  89.  
  90.         public Worker()
  91.         {
  92.  
  93.         }
  94.         public Worker(int tenure, decimal salary, int timeOff) : base()
  95.         {
  96.             this.Tenure = tenure;
  97.             this.Salary = salary;
  98.             this.TimeOff = timeOff;
  99.         }
  100.         public override void SayHello()
  101.         {
  102.             Console.WriteLine($"Hello! I am a worker and my name is {Name}");
  103.         }
  104.  
  105.         public int Tenure
  106.         {
  107.             get { return this.tenure; }
  108.             set { this.tenure = value; }
  109.         }
  110.         public decimal Salary
  111.         {
  112.             get { return this.salary; }
  113.             set
  114.             {
  115.                 try
  116.                 {
  117.                     if (value < 0)
  118.                     {
  119.                         throw new ArgumentException();
  120.                     }
  121.                 }
  122.                 catch (ArgumentException e)
  123.                 {
  124.                     Console.WriteLine("Salary can't be a negative number!");
  125.                 }
  126.                 this.salary = value;
  127.             }
  128.         }
  129.         public int TimeOff
  130.         {
  131.             get { return this.timeOff; }
  132.             set { this.timeOff = value; }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement