Add Lesson 3 Task 1

This commit is contained in:
Stanislav Mykhailenko 2023-03-23 00:07:12 +02:00
parent a7ada9c683
commit 843d599c90
GPG key ID: 1E95E66A9C9D6A36
19 changed files with 754 additions and 0 deletions

View file

@ -0,0 +1,117 @@
namespace Classes;
using Lists;
using Methods;
class Computer
{
public Motherboard Motherboard { get; set; }
public List<Ram> Rams { get; set; } = new List<Ram>();
public List<Cpu> Cpus { get; set; } = new List<Cpu>();
public List<Gpu> Gpus { get; set; } = new List<Gpu>();
public List<Drive> Drives { get; set; } = new List<Drive>();
public int Price { get; set; } = 0;
public void DisplayInstalledMotherboard()
{
Console.WriteLine("Motherboard: ");
(new MotherboardMethods()).DisplayMotherboard(Motherboard);
}
public void DisplayInstalledRams()
{
Console.WriteLine($"RAM ({Motherboard.RamType}): {Motherboard.UsedRamSlots}/{Motherboard.RamSlots}");
foreach (Ram ram in Rams)
(new RamMethods()).DisplayRam(ram);
}
public void DisplayInstalledCpus()
{
Console.WriteLine($"CPUs ({Motherboard.Socket}): {Motherboard.UsedCpuSlots}/{Motherboard.CpuSlots}");
foreach (Cpu cpu in Cpus)
(new CpuMethods()).DisplayCpu(cpu);
}
public void DisplayInstalledGpus()
{
Console.WriteLine("GPUs:");
foreach (Gpu gpu in Gpus)
(new GpuMethods()).DisplayGpu(gpu);
}
public void DisplayInstalledDrives()
{
Console.WriteLine($"Drives (PATA {Motherboard.UsedPataSlots}/{Motherboard.PataSlots}, SATA {Motherboard.UsedSataSlots}/{Motherboard.SataSlots}):");
foreach (Drive drive in Drives)
(new DriveMethods()).DisplayDrive(drive);
}
public void Display()
{
DisplayInstalledMotherboard();
DisplayInstalledRams();
DisplayInstalledCpus();
DisplayInstalledGpus();
DisplayInstalledDrives();
}
public void Replace(Motherboard motherboard)
{
Motherboard = motherboard;
Rams.Clear();
Cpus.Clear();
Gpus.Clear();
Drives.Clear();
Price = motherboard.Price;
}
public void Checkout(int money)
{
while (true)
{
int choice;
Display();
if (!Rams.Any() || !Cpus.Any() || !Gpus.Any() || !Drives.Any())
Console.WriteLine("\nThis computer lacks some details.");
Console.WriteLine($"\nTotal cost: {Price}");
if (money - Price < 0)
Console.WriteLine("You cannot afford this computer.");
Console.WriteLine("Remove RAMs [1]\nRemove CPUs [2]\nRemove GPUs [3]\nRemove Drives [4]\nReplace motherboard (removes everything else) [5]\nGo back [6]");
if (int.TryParse(Console.ReadLine(), out choice))
{
switch (choice)
{
case 1:
(new RamMethods()).DisplayRamList(this, Rams, true);
break;
case 2:
(new CpuMethods()).DisplayCpuList(this, Cpus, true);
break;
case 3:
(new GpuMethods()).DisplayGpuList(this, Gpus, true);
break;
case 4:
(new DriveMethods()).DisplayDriveList(this, Drives, true);
break;
case 5:
Motherboard motherboard = (new MotherboardMethods()).DisplayMotherboardList();
this.Replace(motherboard);
break;
case 6:
goto Back;
}
}
}
Back:
Console.WriteLine("Going back…");
}
public Computer(Motherboard motherboard)
{
Motherboard = motherboard;
Price = motherboard.Price;
}
}

View file

@ -0,0 +1,8 @@
namespace Classes;
class Cpu : Detail
{
public string Socket { get; set; }
public int Cores { get; set; }
public int Frequency { get; set; }
}

View file

@ -0,0 +1,9 @@
namespace Classes;
class Detail
{
public int Price { get; set; }
public string Supplier { get; set; }
public string Country { get; set; }
public string Name { get; set; }
}

View file

@ -0,0 +1,9 @@
namespace Classes;
class Drive : Detail
{
public int Size { get; set; }
public int Speed { get; set; }
public string Interface { get; set; }
public int? Lifetime { get; set; }
}

View file

@ -0,0 +1,7 @@
namespace Classes;
class Gpu : Detail
{
public int Memory { get; set; }
public int Speed { get; set; }
}

View file

@ -0,0 +1,15 @@
namespace Classes;
class Motherboard : Detail
{
public string RamType { get; set; }
public int RamSlots { get; set; }
public int UsedRamSlots {get; set; }
public string Socket { get; set; }
public int CpuSlots {get; set; }
public int UsedCpuSlots {get; set; }
public int PataSlots { get; set; }
public int UsedPataSlots {get; set; }
public int SataSlots { get; set; }
public int UsedSataSlots {get; set; }
}

View file

@ -0,0 +1,7 @@
namespace Classes;
class Ram : Detail
{
public string Type { get; set; }
public int Size { get; set; }
}

View file

@ -0,0 +1,40 @@
namespace Lists;
using Classes;
class CpusForSale
{
public List<Cpu> Hardware { get; set; } = new List<Cpu>
{
new Cpu
{
Price = 1000,
Supplier = "Company 1",
Country = "Country 1",
Name = "CPU 1",
Socket = "Socket 1",
Cores = 1,
Frequency = 2000
},
new Cpu
{
Price = 2000,
Supplier = "Company 2",
Country = "Country 2",
Name = "CPU 2",
Socket = "Socket 1",
Cores = 2,
Frequency = 3000
},
new Cpu
{
Price = 3000,
Supplier = "Company 3",
Country = "Country 3",
Name = "CPU 3",
Socket = "Socket 2",
Cores = 4,
Frequency = 2000
}
};
}

View file

@ -0,0 +1,41 @@
namespace Lists;
using Classes;
class DrivesForSale
{
public List<Drive> Hardware { get; set; } = new List<Drive>
{
new Drive
{
Price = 1000,
Supplier = "Company 1",
Country = "Country 1",
Name = "Drive 1",
Size = 240,
Speed = 100,
Interface = "PATA"
},
new Drive
{
Price = 2000,
Supplier = "Company 2",
Country = "Country 2",
Name = "Drive 2",
Size = 480,
Speed = 200,
Interface = "SATA"
},
new Drive
{
Price = 3000,
Supplier = "Company 3",
Country = "Country 3",
Name = "Drive 3",
Size = 250,
Speed = 400,
Interface = "SATA",
Lifetime = 70
}
};
}

View file

@ -0,0 +1,37 @@
namespace Lists;
using Classes;
class GpusForSale
{
public List<Gpu> Hardware { get; set; } = new List<Gpu>
{
new Gpu
{
Price = 1000,
Supplier = "Company 1",
Country = "Country 1",
Name = "GPU 1",
Memory = 2048,
Speed = 2000
},
new Gpu
{
Price = 2000,
Supplier = "Company 2",
Country = "Country 2",
Name = "GPU 2",
Memory = 4096,
Speed = 3000
},
new Gpu
{
Price = 3000,
Supplier = "Company 3",
Country = "Country 3",
Name = "GPU 3",
Memory = 8192,
Speed = 4000
}
};
}

View file

@ -0,0 +1,49 @@
namespace Lists;
using Classes;
class MotherboardsForSale
{
public List<Motherboard> Hardware { get; set; } = new List<Motherboard>
{
new Motherboard
{
Price = 1000,
Supplier = "Company 1",
Country = "Country 1",
Name = "Motherboard 1",
RamType = "DDR2",
RamSlots = 2,
Socket = "Socket 1",
CpuSlots = 1,
PataSlots = 2,
SataSlots = 2
},
new Motherboard
{
Price = 2000,
Supplier = "Company 2",
Country = "Country 2",
Name = "Motherboard 2",
RamType = "DDR3",
RamSlots = 4,
Socket = "Socket 1",
CpuSlots = 1,
PataSlots = 1,
SataSlots = 4
},
new Motherboard
{
Price = 3000,
Supplier = "Company 3",
Country = "Country 3",
Name = "Motherboard 3",
RamType = "DDR4",
RamSlots = 4,
Socket = "Socket 2",
CpuSlots = 1,
PataSlots = 0,
SataSlots = 4
}
};
}

View file

@ -0,0 +1,37 @@
namespace Lists;
using Classes;
class RamsForSale
{
public List<Ram> Hardware { get; set; } = new List<Ram>
{
new Ram
{
Price = 1000,
Supplier = "Company 1",
Country = "Country 1",
Name = "RAM 1",
Type = "DDR2",
Size = 2048
},
new Ram
{
Price = 2000,
Supplier = "Company 2",
Country = "Country 2",
Name = "RAM 2",
Type = "DDR3",
Size = 4096
},
new Ram
{
Price = 4000,
Supplier = "Company 3",
Country = "Country 3",
Name = "RAM 3",
Type = "DDR4",
Size = 8192
}
};
}

View file

@ -0,0 +1,70 @@
namespace Methods;
using Classes;
using Lists;
class CpuMethods
{
public bool PurchaseCpu(Cpu cpu, Computer computer)
{
if (cpu.Socket == computer.Motherboard.Socket && computer.Motherboard.UsedCpuSlots != computer.Motherboard.CpuSlots)
{
computer.Motherboard.UsedCpuSlots++;
computer.Cpus.Add(cpu);
computer.Price += cpu.Price;
return true;
}
return false;
}
public void RemoveCpu(Cpu cpu, Computer computer)
{
computer.Motherboard.UsedCpuSlots--;
computer.Cpus.Remove(cpu);
computer.Price -= cpu.Price;
}
public void DisplayCpu(Cpu cpu)
{
Console.WriteLine($"{cpu.Name} manufactured by {cpu.Supplier} from {cpu.Country}, socket {cpu.Socket}, cores: {cpu.Cores}, frequency {cpu.Frequency} MHz, costs {cpu.Price} ¤");
}
public void DisplayCpuList(Computer computer, List<Cpu> data, bool remove = false)
{
if (!data.Any())
{
Console.WriteLine("No data available.");
return;
}
foreach (Cpu cpu in data)
DisplayCpu(cpu);
while (true)
{
try
{
Console.Write("Enter the CPU name: ");
string? input = Console.ReadLine();
var selected = (from cpu in data where cpu.Name == input select cpu).First();
if (!remove)
{
if (PurchaseCpu(selected, computer))
Console.WriteLine("Purchased.");
else
Console.WriteLine("This CPU cannot be installed in your computer.");
}
else
{
RemoveCpu(selected, computer);
Console.WriteLine("Removed.");
}
return;
}
catch (InvalidOperationException)
{
Console.WriteLine("Not found.");
}
}
}
}

View file

@ -0,0 +1,77 @@
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.");
}
}
}
}

View file

@ -0,0 +1,61 @@
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.");
}
}
}
}

View file

@ -0,0 +1,41 @@
namespace Methods;
using Classes;
using Lists;
class MotherboardMethods
{
public void PurchaseMotherboard(Motherboard motherboard, Computer computer)
{
computer.Motherboard = motherboard;
computer.Price += motherboard.Price;
}
public void DisplayMotherboard(Motherboard motherboard)
{
Console.WriteLine($"{motherboard.Name} manufactured by {motherboard.Supplier} from {motherboard.Country}, RAM type {motherboard.RamType}, slots {motherboard.RamSlots}, socket {motherboard.Socket}, CPU slots {motherboard.CpuSlots}, PATA slots {motherboard.PataSlots}, SATA slots {motherboard.SataSlots}, costs {motherboard.Price} ¤");
}
public Motherboard DisplayMotherboardList()
{
List<Motherboard> motherboards = (new MotherboardsForSale()).Hardware;
foreach (Motherboard motherboard in motherboards)
DisplayMotherboard(motherboard);
while (true)
{
try
{
Console.Write("Enter the motherboard name to purchase: ");
string? input = Console.ReadLine();
var selected = (from motherboard in motherboards where motherboard.Name == input select motherboard).First();
Console.WriteLine("Purchased.");
return selected;
}
catch (InvalidOperationException)
{
Console.WriteLine("Not found.");
}
}
}
}

View file

@ -0,0 +1,70 @@
namespace Methods;
using Classes;
using Lists;
class RamMethods
{
public bool PurchaseRam(Ram ram, Computer computer)
{
if (ram.Type == computer.Motherboard.RamType && computer.Motherboard.UsedRamSlots != computer.Motherboard.RamSlots)
{
computer.Motherboard.UsedRamSlots++;
computer.Rams.Add(ram);
computer.Price += ram.Price;
return true;
}
return false;
}
public void RemoveRam(Ram ram, Computer computer)
{
computer.Motherboard.UsedRamSlots--;
computer.Rams.Remove(ram);
computer.Price -= ram.Price;
}
public void DisplayRam(Ram ram)
{
Console.WriteLine($"{ram.Name} manufactured by {ram.Supplier} from {ram.Country}, type {ram.Type}, size {ram.Size} GB, costs {ram.Price} ¤");
}
public void DisplayRamList(Computer computer, List<Ram> data, bool remove = false)
{
if (!data.Any())
{
Console.WriteLine("No data available.");
return;
}
foreach (Ram ram in data)
DisplayRam(ram);
while (true)
{
try
{
Console.Write("Enter the RAM name: ");
string? input = Console.ReadLine();
var selected = (from ram in data where ram.Name == input select ram).First();
if (!remove)
{
if (PurchaseRam(selected, computer))
Console.WriteLine("Purchased.");
else
Console.WriteLine("This RAM cannot be installed in your computer.");
}
else
{
RemoveRam(selected, computer);
Console.WriteLine("Removed.");
}
return;
}
catch (InvalidOperationException)
{
Console.WriteLine("Not found.");
}
}
}
}

View file

@ -0,0 +1,49 @@
using Classes;
using Lists;
using Methods;
int money;
while (true)
{
Console.Write("Enter how much money you can afford to spend: ");
if (int.TryParse(Console.ReadLine(), out money))
break;
}
Console.WriteLine("Choose a motherboard first.");
Motherboard motherboard = (new MotherboardMethods()).DisplayMotherboardList();
Computer computer = new Computer(motherboard);
while (true)
{
int choice;
Console.WriteLine("RAMs [1]\nCPUs [2]\nGPUs [3]\nDrives [4]\nCheckout [5]\nQuit [6]");
Console.Write("Enter a number: ");
if (int.TryParse(Console.ReadLine(), out choice))
{
switch (choice)
{
case 1:
(new RamMethods()).DisplayRamList(computer, (new RamsForSale()).Hardware);
break;
case 2:
(new CpuMethods()).DisplayCpuList(computer, (new CpusForSale()).Hardware);
break;
case 3:
(new GpuMethods()).DisplayGpuList(computer, (new GpusForSale()).Hardware);
break;
case 4:
(new DriveMethods()).DisplayDriveList(computer, (new DrivesForSale()).Hardware);
break;
case 5:
computer.Checkout(money);
break;
case 6:
goto Exit;
}
}
}
Exit:
Console.WriteLine("Goodbye.");

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>