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/Drive.cs
Stanislav Mykhailenko 843d599c90
Add Lesson 3 Task 1
2023-03-23 00:07:12 +02:00

77 lines
2.5 KiB
C#

namespace Methods;
using Classes;
using Lists;
class DriveMethods
{
public bool PurchaseDrive(Drive drive, Computer computer)
{
if ((drive.Interface == "PATA" && computer.Motherboard.UsedPataSlots != computer.Motherboard.PataSlots) || (drive.Interface == "SATA" && computer.Motherboard.UsedSataSlots != computer.Motherboard.SataSlots))
{
if (drive.Interface == "PATA")
computer.Motherboard.UsedPataSlots++;
else
computer.Motherboard.UsedSataSlots++;
computer.Drives.Add(drive);
computer.Price += drive.Price;
return true;
}
return false;
}
public void RemoveDrive(Drive drive, Computer computer)
{
if (drive.Interface == "PATA")
computer.Motherboard.UsedPataSlots--;
else
computer.Motherboard.UsedSataSlots--;
computer.Drives.Remove(drive);
computer.Price -= drive.Price;
}
public void DisplayDrive(Drive drive)
{
string? SsdAdditional = drive.Lifetime != null ? $"lifetime {drive.Lifetime} TBW, " : "";
Console.WriteLine($"{drive.Name} manufactured by {drive.Supplier} from {drive.Country}, size {drive.Size} GB, speed {drive.Speed} MB/s, interface {drive.Interface}, {SsdAdditional}costs {drive.Price} ¤");
}
public void DisplayDriveList(Computer computer, List<Drive> data, bool remove = false)
{
if (!data.Any())
{
Console.WriteLine("No data available.");
return;
}
foreach (Drive drive in data)
DisplayDrive(drive);
while (true)
{
try
{
Console.Write("Enter the drive name: ");
string? input = Console.ReadLine();
var selected = (from drive in data where drive.Name == input select drive).First();
if (!remove)
{
if (PurchaseDrive(selected, computer))
Console.WriteLine("Purchased.");
else
Console.WriteLine("This drive cannot be installed in your computer.");
}
else
{
RemoveDrive(selected, computer);
Console.WriteLine("Removed.");
}
return;
}
catch (InvalidOperationException)
{
Console.WriteLine("Not found.");
}
}
}
}