fork download
  1. using System;
  2.  
  3. // Визначення перелічуваного типу Rank
  4. public enum Rank
  5. {
  6. First,
  7. Second,
  8. High
  9. }
  10.  
  11. // Клас License
  12. public class License
  13. {
  14. public string Organization { get; set; }
  15. public string Qualification { get; set; }
  16. public DateTime IssueDate { get; set; }
  17.  
  18. // Конструктор з параметрами
  19. public License(string organization, string qualification, DateTime issueDate)
  20. {
  21. Organization = organization;
  22. Qualification = qualification;
  23. IssueDate = issueDate;
  24. }
  25.  
  26. // Конструктор без параметрів
  27. public License()
  28. {
  29. Organization = "Default Organization";
  30. Qualification = "Default Qualification";
  31. IssueDate = DateTime.Now;
  32. }
  33.  
  34. // Перевизначення методу ToString()
  35. public override string ToString()
  36. {
  37. return $"Organization: {Organization}, Qualification: {Qualification}, Issue Date: {IssueDate:yyyy-MM-dd}";
  38. }
  39. }
  40.  
  41. // Клас Person (необхідний для класу StationWorker)
  42. public class Person
  43. {
  44. public string FirstName { get; set; }
  45. public string LastName { get; set; }
  46.  
  47. public Person(string firstName, string lastName)
  48. {
  49. FirstName = firstName;
  50. LastName = lastName;
  51. }
  52.  
  53. public Person()
  54. {
  55. FirstName = "John";
  56. LastName = "Doe";
  57. }
  58.  
  59. public override string ToString()
  60. {
  61. return $"{FirstName} {LastName}";
  62. }
  63. }
  64.  
  65. // Клас StationWorker
  66. public class StationWorker
  67. {
  68. private Person person;
  69. private string specialization;
  70. private Rank rank;
  71. private int experience;
  72. private License[] licenses;
  73.  
  74. // Властивості з get та set
  75. public Person Person
  76. {
  77. get { return person; }
  78. set { person = value; }
  79. }
  80.  
  81. public string Specialization
  82. {
  83. get { return specialization; }
  84. set { specialization = value; }
  85. }
  86.  
  87. public Rank Rank
  88. {
  89. get { return rank; }
  90. set { rank = value; }
  91. }
  92.  
  93. public int Experience
  94. {
  95. get { return experience; }
  96. set { experience = value; }
  97. }
  98.  
  99. public License[] Licenses
  100. {
  101. get { return licenses; }
  102. set { licenses = value; }
  103. }
  104.  
  105. // Властивість для першої ліцензії (тільки get)
  106. public License FirstLicense
  107. {
  108. get
  109. {
  110. if (licenses == null || licenses.Length == 0)
  111. return null;
  112.  
  113. License first = licenses[0];
  114. for (int i = 1; i < licenses.Length; i++)
  115. {
  116. if (licenses[i].IssueDate < first.IssueDate)
  117. {
  118. first = licenses[i];
  119. }
  120. }
  121. return first;
  122. }
  123. }
  124.  
  125. // Конструктор з параметрами
  126. public StationWorker(Person person, string specialization, Rank rank, int experience)
  127. {
  128. this.person = person;
  129. this.specialization = specialization;
  130. this.rank = rank;
  131. this.experience = experience;
  132. this.licenses = new License[0];
  133. }
  134.  
  135. // Конструктор без параметрів
  136. public StationWorker()
  137. {
  138. this.person = new Person();
  139. this.specialization = "General Mechanic";
  140. this.rank = Rank.First;
  141. this.experience = 0;
  142. this.licenses = new License[0];
  143. }
  144.  
  145. // Метод для додавання ліцензій
  146. public void AddLicense(params License[] newLicenses)
  147. {
  148. if (licenses == null)
  149. {
  150. licenses = newLicenses;
  151. return;
  152. }
  153.  
  154. License[] combined = new License[licenses.Length + newLicenses.Length];
  155. Array.Copy(licenses, combined, licenses.Length);
  156. Array.Copy(newLicenses, 0, combined, licenses.Length, newLicenses.Length);
  157. licenses = combined;
  158. }
  159.  
  160. // Перевизначення методу ToString()
  161. public override string ToString()
  162. {
  163. string result = $"Person: {person}, Specialization: {specialization}, " +
  164. $"Rank: {rank}, Experience: {experience} years\n";
  165.  
  166. result += "Licenses:\n";
  167. if (licenses != null && licenses.Length > 0)
  168. {
  169. foreach (var license in licenses)
  170. {
  171. result += $" {license}\n";
  172. }
  173. }
  174. else
  175. {
  176. result += " No licenses\n";
  177. }
  178.  
  179. return result;
  180. }
  181.  
  182. // Віртуальний метод ToShortString()
  183. public virtual string ToShortString()
  184. {
  185. return $"Person: {person}, Specialization: {specialization}, " +
  186. $"Rank: {rank}, Experience: {experience} years";
  187. }
  188. }
  189.  
  190. class Program
  191. {
  192. static void Main()
  193. {
  194. // 1. Створення об'єкта з використанням конструктора без параметрів
  195. StationWorker worker1 = new StationWorker();
  196. Console.WriteLine("1. Об'єкт з конструктором без параметрів:");
  197. Console.WriteLine(worker1.ToString());
  198.  
  199. // 2. Присвоєння нових значень властивостям
  200. Console.WriteLine("\n2. Після зміни властивостей:");
  201. worker1.Person = new Person("Ivan", "Petrenko");
  202. worker1.Specialization = "Engine Specialist";
  203. worker1.Rank = Rank.High;
  204. worker1.Experience = 5;
  205. Console.WriteLine(worker1.ToShortString());
  206.  
  207. // 3. Створення об'єкта з використанням конструктора з параметрами
  208. Console.WriteLine("\n3. Об'єкт з конструктором з параметрами:");
  209. Person person2 = new Person("Maria", "Ivanova");
  210. StationWorker worker2 = new StationWorker(person2, "Electric Systems", Rank.Second, 3);
  211. Console.WriteLine(worker2.ToString());
  212.  
  213. // 4. Додавання ліцензій до другого об'єкта
  214. Console.WriteLine("4. Після додавання ліцензій:");
  215. License license1 = new License("AutoTech Institute", "Advanced Engine Repair", new DateTime(2020, 5, 15));
  216. License license2 = new License("Electric Vehicles Association", "EV Systems Specialist", new DateTime(2022, 3, 10));
  217.  
  218. worker2.AddLicense(license1, license2);
  219. Console.WriteLine(worker2.ToString());
  220.  
  221. // 5. Виведення першої ліцензії
  222. Console.WriteLine("5. Перша отримана ліцензія:");
  223. License firstLicense = worker2.FirstLicense;
  224. if (firstLicense != null)
  225. {
  226. Console.WriteLine(firstLicense.ToString());
  227. }
  228. else
  229. {
  230. Console.WriteLine("Ліцензії відсутні");
  231. }
  232. }
  233. }
Success #stdin #stdout 0.07s 32544KB
stdin
Standard input is empty
stdout
1. Об'єкт з конструктором без параметрів:
Person: John Doe, Specialization: General Mechanic, Rank: First, Experience: 0 years
Licenses:
  No licenses


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

3. Об'єкт з конструктором з параметрами:
Person: Maria Ivanova, Specialization: Electric Systems, Rank: Second, Experience: 3 years
Licenses:
  No licenses

4. Після додавання ліцензій:
Person: Maria Ivanova, Specialization: Electric Systems, Rank: Second, Experience: 3 years
Licenses:
  Organization: AutoTech Institute, Qualification: Advanced Engine Repair, Issue Date: 2020-05-15
  Organization: Electric Vehicles Association, Qualification: EV Systems Specialist, Issue Date: 2022-03-10

5. Перша отримана ліцензія:
Organization: AutoTech Institute, Qualification: Advanced Engine Repair, Issue Date: 2020-05-15