using System;
// Визначення перелічуваного типу Rank
public enum Rank
{
First,
Second,
High
}
// Клас License
public class License
{
public string Organization { get; set; }
public string Qualification { get; set; }
public DateTime IssueDate { get; set; }
// Конструктор з параметрами
public License(string organization, string qualification, DateTime issueDate)
{
Organization = organization;
Qualification = qualification;
IssueDate = issueDate;
}
// Конструктор без параметрів
public License()
{
Organization = "Default Organization";
Qualification = "Default Qualification";
IssueDate = DateTime.Now;
}
// Перевизначення методу ToString()
public override string ToString()
{
return $"Organization: {Organization}, Qualification: {Qualification}, Issue Date: {IssueDate:yyyy-MM-dd}";
}
}
// Клас Person (необхідний для класу StationWorker)
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public Person()
{
FirstName = "John";
LastName = "Doe";
}
public override string ToString()
{
return $"{FirstName} {LastName}";
}
}
// Клас StationWorker
public class StationWorker
{
private Person person;
private string specialization;
private Rank rank;
private int experience;
private License[] licenses;
// Властивості з get та set
public Person Person
{
get { return person; }
set { person = value; }
}
public string Specialization
{
get { return specialization; }
set { specialization = value; }
}
public Rank Rank
{
get { return rank; }
set { rank = value; }
}
public int Experience
{
get { return experience; }
set { experience = value; }
}
public License[] Licenses
{
get { return licenses; }
set { licenses = value; }
}
// Властивість для першої ліцензії (тільки get)
public License FirstLicense
{
get
{
if (licenses == null || licenses.Length == 0)
return null;
License first = licenses[0];
for (int i = 1; i < licenses.Length; i++)
{
if (licenses[i].IssueDate < first.IssueDate)
{
first = licenses[i];
}
}
return first;
}
}
// Конструктор з параметрами
public StationWorker(Person person, string specialization, Rank rank, int experience)
{
this.person = person;
this.specialization = specialization;
this.rank = rank;
this.experience = experience;
this.licenses = new License[0];
}
// Конструктор без параметрів
public StationWorker()
{
this.person = new Person();
this.specialization = "General Mechanic";
this.rank = Rank.First;
this.experience = 0;
this.licenses = new License[0];
}
// Метод для додавання ліцензій
public void AddLicense(params License[] newLicenses)
{
if (licenses == null)
{
licenses = newLicenses;
return;
}
License[] combined = new License[licenses.Length + newLicenses.Length];
Array.Copy(licenses, combined, licenses.Length);
Array.Copy(newLicenses, 0, combined, licenses.Length, newLicenses.Length);
licenses = combined;
}
// Перевизначення методу ToString()
public override string ToString()
{
string result = $"Person: {person}, Specialization: {specialization}, " +
$"Rank: {rank}, Experience: {experience} years\n";
result += "Licenses:\n";
if (licenses != null && licenses.Length > 0)
{
foreach (var license in licenses)
{
result += $" {license}\n";
}
}
else
{
result += " No licenses\n";
}
return result;
}
// Віртуальний метод ToShortString()
public virtual string ToShortString()
{
return $"Person: {person}, Specialization: {specialization}, " +
$"Rank: {rank}, Experience: {experience} years";
}
}
class Program
{
static void Main()
{
// 1. Створення об'єкта з використанням конструктора без параметрів
StationWorker worker1 = new StationWorker();
Console.WriteLine("1. Об'єкт з конструктором без параметрів:");
Console.WriteLine(worker1.ToString());
// 2. Присвоєння нових значень властивостям
Console.WriteLine("\n2. Після зміни властивостей:");
worker1.Person = new Person("Ivan", "Petrenko");
worker1.Specialization = "Engine Specialist";
worker1.Rank = Rank.High;
worker1.Experience = 5;
Console.WriteLine(worker1.ToShortString());
// 3. Створення об'єкта з використанням конструктора з параметрами
Console.WriteLine("\n3. Об'єкт з конструктором з параметрами:");
Person person2 = new Person("Maria", "Ivanova");
StationWorker worker2 = new StationWorker(person2, "Electric Systems", Rank.Second, 3);
Console.WriteLine(worker2.ToString());
// 4. Додавання ліцензій до другого об'єкта
Console.WriteLine("4. Після додавання ліцензій:");
License license1 = new License("AutoTech Institute", "Advanced Engine Repair", new DateTime(2020, 5, 15));
License license2 = new License("Electric Vehicles Association", "EV Systems Specialist", new DateTime(2022, 3, 10));
worker2.AddLicense(license1, license2);
Console.WriteLine(worker2.ToString());
// 5. Виведення першої ліцензії
Console.WriteLine("5. Перша отримана ліцензія:");
License firstLicense = worker2.FirstLicense;
if (firstLicense != null)
{
Console.WriteLine(firstLicense.ToString());
}
else
{
Console.WriteLine("Ліцензії відсутні");
}
}
}