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

46 lines
964 B
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
*/
2023-03-20 00:14:49 +00:00
using CarClass;
using OwnerClass;
2023-03-15 18:07:52 +00:00
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;
2023-03-15 18:07:52 +00:00
2023-03-19 12:23:47 +00:00
while (true)
2023-03-15 18:07:52 +00:00
{
2023-03-19 12:23:47 +00:00
Console.Write("Enter number: ");
string? input = Console.ReadLine();
if (!string.IsNullOrEmpty(input))
{
number = input;
2023-03-19 12:23:47 +00:00
break;
}
2023-03-19 12:23:47 +00:00
}
2023-03-15 18:07:52 +00:00
2023-03-20 15:09:43 +00:00
try
{
var selected = owners.First(owner => cars.Any(car => car.Number == number && car.OwnerId == owner.Id));
Console.WriteLine($"Name: {selected.Name}, Address: {selected.Address}");
}
catch (InvalidOperationException)
{
Console.WriteLine("Not found.");
}