public void FillPetrol(string carSerial, string petrolType, float petrolAmount) { Vehicle vehicleToFill = null; if (GarageDictionary.TryGetValue(carSerial, out vehicleToFill)) { try { PetrolEngine petrolEngineToFill = (PetrolEngine)vehicleToFill.Engine; if (petrolType != petrolEngineToFill.PetrolType) { throw new Exception("ERROR: Wrong petrol type, this vehicle runs on " + petrolEngineToFill.PetrolType + ", and the input was " + petrolType); } vehicleToFill.Engine.FillVehicle(float.Parse(Console.ReadLine())); } catch (FormatException) { throw new FormatException("ERROR: tried to fill petrol to an electric vehicle"); } catch (Exception otherException) { throw otherException; } } else { throw new Exception("ERROR: Invalid vehicle serial, this vehicle doesn't exist in the garage"); } }
public Truck(bool isElectric) { m_VehicleWheels = VehicleCreator.createWheels(k_NumberOfWheels, k_TruckMaxWheelPressure); m_VehicleEngine = VehicleCreator.CreateEngine(isElectric); PetrolEngine petrolEngine = m_VehicleEngine as PetrolEngine; petrolEngine.MaximumPetrolAmount = k_MaxPetrolLiterCapacity; petrolEngine.PetrolType = k_PetrolType; }
public Car(bool isElectric) { m_VehicleWheels = VehicleCreator.createWheels(k_NumberOfWheels, k_CarMaxWheelPressure); m_VehicleEngine = VehicleCreator.CreateEngine(isElectric); if (isElectric) { ElectricEngine electricEngine = m_VehicleEngine as ElectricEngine; electricEngine.MaximumBatteryHours = k_MaxBatteryHoursCapacity; } else { PetrolEngine petrolEngine = m_VehicleEngine as PetrolEngine; petrolEngine.MaximumPetrolAmount = k_MaxPetrolLiterCapacity; petrolEngine.PetrolType = k_PetrolType; } }
public static Engine CreateEngine(bool i_IsElectric) { Engine createdEngine = null; return(i_IsElectric ? createdEngine = new ElectricEngine() : createdEngine = new PetrolEngine()); }