private void fuelVehicleUi(string i_LicenseNumber) { bool isValidInput = false; do { try { while (!r_CurrentGarage.AllVehicles.ContainsKey(i_LicenseNumber)) { Console.WriteLine(@"This vehicle is not in the garage, please enter a valid License Number Please chose a vehicle from the list:"); printVehiclesByEngineType(typeof(FuelEngine).ToString()); i_LicenseNumber = getLicenseNumber(); } Console.WriteLine("Please enter how many litters to fill"); string littersToFillString = Console.ReadLine(); float littersToFill; while (!float.TryParse(littersToFillString, out littersToFill)) { Console.WriteLine("Please enter a number"); littersToFillString = Console.ReadLine(); } Console.WriteLine( @"Please enter the full type to fill Octan 95 press 1 Octan 96 press 2 Octan 98 press 3 Soler press 4"); string fuelTypeString = Console.ReadLine(); int.TryParse(fuelTypeString, out int fuelType); while (fuelTypeString == null || !Enum.IsDefined(typeof(FuelEngine.eGasType), fuelType)) { Console.WriteLine(@"Please enter a valid fuel type"); fuelTypeString = Console.ReadLine(); int.TryParse(fuelTypeString, out fuelType); } FuelEngine.eGasType gasTypeToFill = (FuelEngine.eGasType)Enum.Parse( typeof(FuelEngine.eGasType), fuelTypeString); r_CurrentGarage.FillTank(i_LicenseNumber, gasTypeToFill, littersToFill); isValidInput = true; } catch (KeyNotFoundException keyNotFound) { Console.WriteLine(keyNotFound.Message); isValidInput = true; } catch (Exception exception) { Console.WriteLine(exception.Message); } }while(!isValidInput); }
public void FillTank(string i_LicenseNumber, FuelEngine.eGasType i_GasType, float i_AmountToFill) { bool vehicleExists = r_AllVehicles.TryGetValue(i_LicenseNumber, out Vehicle requestedVehicle); if (vehicleExists) { FuelEngine thisFuelEngine = requestedVehicle.VehicleEngine as FuelEngine; if (thisFuelEngine != null) { if (thisFuelEngine.MaxEnergyCapacity > thisFuelEngine.CurrentEnergyCapacity) { if (thisFuelEngine.GasType != i_GasType) { string errorMessage = string.Format( @"This car takes {0} fuel", thisFuelEngine.GasType); throw new ArgumentException(errorMessage); } else if (thisFuelEngine.MaxEnergyCapacity <= thisFuelEngine.CurrentEnergyCapacity + i_AmountToFill || i_AmountToFill <= 0) { float maxAmountToFill = thisFuelEngine.MaxEnergyCapacity - thisFuelEngine.CurrentEnergyCapacity; throw new ValueOutOfRangeException(@"Invalid amount to fill", 0, maxAmountToFill); } else { thisFuelEngine.AddFuel(i_AmountToFill, i_GasType); } } } else { throw new KeyNotFoundException(@"This is not an fuel vehicle"); } } else { throw new KeyNotFoundException(@"This vehicle is not it the garage"); } }