Change Lesson 3 Task 1 to remove goto

This commit is contained in:
Stanislav Mykhailenko 2023-03-23 10:39:39 +02:00
parent 1380c6a583
commit 60f5d40b8d
GPG key ID: 1E95E66A9C9D6A36
2 changed files with 36 additions and 35 deletions

View file

@ -65,6 +65,39 @@ class Computer
Price = motherboard.Price; Price = motherboard.Price;
} }
public void HardwareSelection(int money)
{
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(this, (new RamsForSale()).Hardware);
break;
case 2:
(new CpuMethods()).DisplayCpuList(this, (new CpusForSale()).Hardware);
break;
case 3:
(new GpuMethods()).DisplayGpuList(this, (new GpusForSale()).Hardware);
break;
case 4:
(new DriveMethods()).DisplayDriveList(this, (new DrivesForSale()).Hardware);
break;
case 5:
Checkout(money);
break;
case 6:
return;
}
}
}
}
public void Checkout(int money) public void Checkout(int money)
{ {
while (true) while (true)
@ -80,6 +113,7 @@ class Computer
Console.WriteLine("You cannot afford this computer."); 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]"); Console.WriteLine("Remove RAMs [1]\nRemove CPUs [2]\nRemove GPUs [3]\nRemove Drives [4]\nReplace motherboard (removes everything else) [5]\nGo back [6]");
Console.Write("Enter a number: ");
if (int.TryParse(Console.ReadLine(), out choice)) if (int.TryParse(Console.ReadLine(), out choice))
{ {
switch (choice) switch (choice)
@ -101,12 +135,10 @@ class Computer
this.Replace(motherboard); this.Replace(motherboard);
break; break;
case 6: case 6:
goto Back; return;
} }
} }
} }
Back:
Console.WriteLine("Going back…");
} }
public Computer(Motherboard motherboard) public Computer(Motherboard motherboard)

View file

@ -15,35 +15,4 @@ Console.WriteLine("Choose a motherboard first.");
Motherboard motherboard = (new MotherboardMethods()).DisplayMotherboardList(); Motherboard motherboard = (new MotherboardMethods()).DisplayMotherboardList();
Computer computer = new Computer(motherboard); Computer computer = new Computer(motherboard);
while (true) computer.HardwareSelection(money);
{
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.");