public double DriveCar(int distance, double currentFuelAmount) { FuelGauge fuel = new FuelGauge(); if (currentFuelAmount < 0.12) { Console.WriteLine("Please add more fuel"); } else { for (int i = 1; i <= distance; i++) { currentKM = currentKM + 1; if (currentKM > 999) { currentKM = 0; } currentFuelAmount = fuel.BurnFuel(currentFuelAmount); Console.WriteLine($"Distance Travelled: {currentKM}. Fuel right now: {currentFuelAmount:n2}."); if (currentFuelAmount < 0.12) { Console.WriteLine("Please add more fuel"); i = distance; } } Console.WriteLine($"Total distance travelled after this journey {currentKM}km."); } return(currentFuelAmount); }
static void Main(string[] args) { FuelGauge fuel = new FuelGauge(); fuel.currentFuel = 0; double currentFuelAmount = 0; Odometer km = new Odometer(); km.currentKM = 0; bool again = true; while (again) { try { Console.WriteLine("|------------------------|"); Console.WriteLine("| Car Simulator |"); Console.WriteLine("|------------------------|"); Console.WriteLine("| 1. Add Fuel |"); Console.WriteLine("| 2. Drive Car |"); Console.WriteLine("| 0. Exit |"); Console.WriteLine("|------------------------|"); int option = int.Parse(Console.ReadLine()); if (option == 1) { Console.Write("Enter amount of fuel: "); int amount = int.Parse(Console.ReadLine()); currentFuelAmount = fuel.AddFuel(amount, currentFuelAmount); fuel.FuelAmount("Initial fuel amount: ", currentFuelAmount); } else if (option == 2) { Console.Write("Enter distance: "); int distance = int.Parse(Console.ReadLine()); currentFuelAmount = km.DriveCar(distance, currentFuelAmount); fuel.FuelAmount("Fuel left after journey: ", currentFuelAmount); } else if (option == 0) { Console.WriteLine("Goodbye!"); again = false; } else { Console.WriteLine("Invalid input. Enter a number from 0-2"); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}. Try again."); } } }