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_3/Task_1/Methods/Gpu.cs

62 lines
1.6 KiB
C#
Raw Normal View History

2023-03-22 22:07:12 +00:00
namespace Methods;
using Classes;
using Lists;
class GpuMethods
{
public void PurchaseGpu(Gpu gpu, Computer computer)
{
computer.Gpus.Add(gpu);
computer.Price += gpu.Price;
}
public void RemoveGpu(Gpu gpu, Computer computer)
{
computer.Gpus.Remove(gpu);
computer.Price -= gpu.Price;
}
public void DisplayGpu(Gpu gpu)
{
Console.WriteLine($"{gpu.Name} manufactured by {gpu.Supplier} from {gpu.Country}, memory {gpu.Memory} GB, speed {gpu.Speed} MHz, costs {gpu.Price} ¤");
}
public void DisplayGpuList(Computer computer, List<Gpu> data, bool remove = false)
{
if (!data.Any())
{
Console.WriteLine("No data available.");
return;
}
foreach (Gpu gpu in data)
DisplayGpu(gpu);
while (true)
{
try
{
Console.Write("Enter the GPU name to purchase: ");
string? input = Console.ReadLine();
var selected = (from gpu in data where gpu.Name == input select gpu).First();
if (!remove)
{
PurchaseGpu(selected, computer);
Console.WriteLine("Purchased.");
}
else
{
RemoveGpu(selected, computer);
Console.WriteLine("Removed.");
}
return;
}
catch (InvalidOperationException)
{
Console.WriteLine("Not found.");
}
}
}
}