/// <summary> /// Add energy to an Gasoline Vehicle /// </summary> /// <param name="gasolineAmount">amount of gasoline to add</param> /// <param name="fuelType">fuel type Enum</param> public override void AddEnergy(float gasolineAmount, E_FuelType fuelType) { if ((CurrentAmount + gasolineAmount) <= MaxAmount) { CurrentAmount += gasolineAmount; } else { throw new ValueOutOfRangeException("Select less amount", MaxAmount, 0, (CurrentAmount + gasolineAmount)); } }
/// <summary> /// Constractor for Gasoline type Engine /// </summary> /// <param name="fuelType">type of fuel for the vehicle</param> /// <param name="maxAmount">max amount of fuel can be</param> /// <param name="currentAmount">current amount of fuel</param> public Gasoline(E_FuelType fuelType, float maxAmount, float currentAmount) { EngineType = E_EngineType.Fuel; if (currentAmount > maxAmount) { CurrentAmount = maxAmount; } else { CurrentAmount = currentAmount; } MaxAmount = maxAmount; FuelType = fuelType; }
/// <summary> /// the try to add energy type (fuel/electric) /// </summary> /// <param name="vehicle">vehicle value</param> private static void tryAddEnergy(Vehicle vehicle) { ///using try/catch to identify the engine type switch (vehicle.VehicleEngine.EngineType) { case E_EngineType.Fuel: E_FuelType fuelType = Utils.Parsers.getFuelTypeFromUser(); float amountToAdd = Utils.Parsers.GetFloatFromUser($"Enter amount of {fuelType} to add:"); try { vehicle.VehicleEngine.AddEnergy(amountToAdd, fuelType); Console.WriteLine($"Added {amountToAdd} of type {fuelType} to the {vehicle.VehicleType}"); } catch (ValueOutOfRangeException ex) { Console.WriteLine(ex.Message); tryAddEnergy(vehicle); } break; case E_EngineType.Electric: float timeToCharge = Utils.Parsers.GetFloatFromUser("Enter time amount to add"); try { vehicle.VehicleEngine.AddEnergy(timeToCharge); Console.WriteLine($"Added {timeToCharge} to the {vehicle.VehicleType}"); } catch (ValueOutOfRangeException ex) { Console.WriteLine(ex.Message); tryAddEnergy(vehicle); } break; default: throw new InvalidDataException("Unknown engine type"); } }
public static Engine CreateNewEngine(E_FuelType fuelType, float maxAmount, float currentAmount) { return(new Gasoline(fuelType, maxAmount, currentAmount)); }
/// <summary> /// Add energy to an Gasoline Vehicle /// </summary> /// <param name="GasolineAmount">amount of gasoline to add</param> /// <param name="FuelType">fuel type Enum</param> public virtual void AddEnergy(float GasolineAmount, E_FuelType FuelType) { throw new ArgumentException("This is an Electric Vehicle"); }