fork download
  1. using System;
  2.  
  3. enum Rank { First, Second, High }
  4.  
  5. class License
  6. {
  7. public string Organization { get; set; }
  8. public string Qualification { get; set; }
  9. public DateTime IssueDate { get; set; }
  10.  
  11. public License(string org, string qual, DateTime date)
  12. {
  13. Organization = org;
  14. Qualification = qual;
  15. IssueDate = date;
  16. }
  17.  
  18. public License() : this("Default Org", "Default Qual", DateTime.Now) { }
  19.  
  20. public override string ToString() =>
  21. $"Org: {Organization}, Qual: {Qualification}, Date: {IssueDate:yyyy-MM-dd}";
  22. }
  23.  
  24. class Person
  25. {
  26. public string FirstName { get; set; }
  27. public string LastName { get; set; }
  28.  
  29. public Person(string first, string last)
  30. {
  31. FirstName = first;
  32. LastName = last;
  33. }
  34.  
  35. public Person() : this("John", "Doe") { }
  36.  
  37. public override string ToString() => $"{FirstName} {LastName}";
  38. }
  39.  
  40. class StationWorker
  41. {
  42. public Person Person { get; set; }
  43. public string Specialization { get; set; }
  44. public Rank Rank { get; set; }
  45. public int Experience { get; set; }
  46. public License[] Licenses { get; set; }
  47.  
  48. public License FirstLicense
  49. {
  50. get
  51. {
  52. if (Licenses == null || Licenses.Length == 0) return null;
  53.  
  54. License first = Licenses[0];
  55. foreach (var license in Licenses)
  56. if (license.IssueDate < first.IssueDate)
  57. first = license;
  58. return first;
  59. }
  60. }
  61.  
  62. public StationWorker(Person person, string spec, Rank rank, int exp)
  63. {
  64. Person = person;
  65. Specialization = spec;
  66. Rank = rank;
  67. Experience = exp;
  68. Licenses = Array.Empty<License>();
  69. }
  70.  
  71. public StationWorker() : this(new Person(), "Mechanic", Rank.First, 0) { }
  72.  
  73. public void AddLicense(params License[] newLicenses)
  74. {
  75. if (Licenses == null)
  76. {
  77. Licenses = newLicenses;
  78. return;
  79. }
  80.  
  81. var combined = new License[Licenses.Length + newLicenses.Length];
  82. Array.Copy(Licenses, combined, Licenses.Length);
  83. Array.Copy(newLicenses, 0, combined, Licenses.Length, newLicenses.Length);
  84. Licenses = combined;
  85. }
  86.  
  87. public override string ToString()
  88. {
  89. string result = $"Person: {Person}, Spec: {Specialization}, " +
  90. $"Rank: {Rank}, Exp: {Experience} years\nLicenses:\n";
  91.  
  92. if (Licenses.Length > 0)
  93. foreach (var license in Licenses)
  94. result += $" {license}\n";
  95. else
  96. result += " No licenses\n";
  97.  
  98. return result;
  99. }
  100.  
  101. public virtual string ToShortString() =>
  102. $"Person: {Person}, Spec: {Specialization}, Rank: {Rank}, Exp: {Experience} years";
  103. }
  104.  
  105. class Program
  106. {
  107. static void Main()
  108. {
  109. // 1
  110. var worker1 = new StationWorker();
  111. Console.WriteLine("1. Конструктор без параметрів:");
  112. Console.WriteLine(worker1.ToString());
  113.  
  114. // 2
  115. worker1.Person = new Person("Ivan", "Petrenko");
  116. worker1.Specialization = "Engine Specialist";
  117. worker1.Rank = Rank.High;
  118. worker1.Experience = 5;
  119. Console.WriteLine("\n2. Після зміни властивостей:");
  120. Console.WriteLine(worker1.ToShortString());
  121.  
  122. // 3
  123. var worker2 = new StationWorker(
  124. new Person("Maria", "Ivanova"),
  125. "Electric Systems",
  126. Rank.Second,
  127. 3
  128. );
  129. Console.WriteLine("\n3. Конструктор з параметрами:");
  130. Console.WriteLine(worker2.ToString());
  131.  
  132. // 4
  133. worker2.AddLicense(
  134. new License("AutoTech", "Engine Repair", new DateTime(2020, 5, 15)),
  135. new License("EVA", "EV Specialist", new DateTime(2022, 3, 10))
  136. );
  137. Console.WriteLine("4. Після додавання ліцензій:");
  138. Console.WriteLine(worker2.ToString());
  139.  
  140. // 5
  141. Console.WriteLine("5. Перша ліцензія:");
  142. Console.WriteLine(worker2.FirstLicense?.ToString() ?? "Ліцензії відсутні");
  143. }
  144. }
Success #stdin #stdout 0.07s 30512KB
stdin
Standard input is empty
stdout
1. Конструктор без параметрів:
Person: John Doe, Spec: Mechanic, Rank: First, Exp: 0 years
Licenses:
  No licenses


2. Після зміни властивостей:
Person: Ivan Petrenko, Spec: Engine Specialist, Rank: High, Exp: 5 years

3. Конструктор з параметрами:
Person: Maria Ivanova, Spec: Electric Systems, Rank: Second, Exp: 3 years
Licenses:
  No licenses

4. Після додавання ліцензій:
Person: Maria Ivanova, Spec: Electric Systems, Rank: Second, Exp: 3 years
Licenses:
  Org: AutoTech, Qual: Engine Repair, Date: 2020-05-15
  Org: EVA, Qual: EV Specialist, Date: 2022-03-10

5. Перша ліцензія:
Org: AutoTech, Qual: Engine Repair, Date: 2020-05-15