This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2023_Stanislav_Mykhailenko/Lesson_2/Task_4/Program.cs

68 lines
1.2 KiB
C#
Raw Normal View History

2023-03-15 18:07:52 +00:00
/*
* Lesson 2 Task 4: get two collections with cars and their owners and ask car number, then return its owner's name and address
* Author: Stanislav Mykhailenko
* License: Unlicense
*/
List<Owner> owners = new List<Owner>()
{
new Owner(1, "Tom", "Street 1"),
new Owner(2, "Bob", "Street 2"),
new Owner(3, "Sam", "Street 3"),
};
List<Car> cars = new List<Car>()
{
new Car("123ABC", 1),
new Car("456DEF", 2),
new Car("789GHI", 3),
};
string? number;
do
{
Console.Write("Enter number: ");
number = Console.ReadLine();
} while (number == null);
2023-03-19 11:15:54 +00:00
var selected = owners.FirstOrDefault(owner => cars.Any(car => car.Number == number && car.OwnerId == owner.Id));
2023-03-15 18:07:52 +00:00
Console.WriteLine(selected);
class Owner
{
2023-03-19 11:15:54 +00:00
public int Id { get;}
2023-03-15 18:07:52 +00:00
public string Name { get;}
public string Address { get;}
public override string ToString()
{
return "Name: " + Name + ", Address: " + Address;
}
public Owner(int id, string name, string address)
{
2023-03-19 11:15:54 +00:00
Id = id;
2023-03-15 18:07:52 +00:00
Name = name;
Address = address;
}
}
class Car
{
public string Number { get;}
2023-03-19 11:15:54 +00:00
public int OwnerId { get;}
2023-03-15 18:07:52 +00:00
public override string ToString()
{
2023-03-19 11:15:54 +00:00
return "Number: " + Number + ", Owner ID: " + OwnerId;
2023-03-15 18:07:52 +00:00
}
2023-03-19 11:15:54 +00:00
public Car(string number, int ownerId)
2023-03-15 18:07:52 +00:00
{
Number = number;
2023-03-19 11:15:54 +00:00
OwnerId = ownerId;
2023-03-15 18:07:52 +00:00
}
}