public static Vehicle CreateNewVehicle(eVehicleModel i_Model, string i_LicenseNumber) { Vehicle newVehicle = null; string modelStr = VehicleModelToText.AsText(i_Model); switch (i_Model) { case eVehicleModel.RegularMotorcycle: newVehicle = createRegularMotorcycle(modelStr, i_LicenseNumber); break; case eVehicleModel.ElectricMotorcycle: newVehicle = createElectricMotorcycle(modelStr, i_LicenseNumber); break; case eVehicleModel.RegularCar: newVehicle = createRegularCar(modelStr, i_LicenseNumber); break; case eVehicleModel.ElectricCar: newVehicle = createElectricCar(modelStr, i_LicenseNumber); break; case eVehicleModel.Truck: newVehicle = createTruck(modelStr, i_LicenseNumber); break; } return(newVehicle); }
private void setPercentageRemainingEnergy(eVehicleModel i_ModelName, ref Vehicle o_NewVehicle) { bool isValidInput = false; while (!isValidInput) { try { o_NewVehicle.PercentageRemainingEnergy = UI.GetRemainingPercentageEnergy( VehicleModelToText.AsText(i_ModelName), 100, 0); isValidInput = true; } catch (FormatException formatException) { UI.PrintMessage(formatException.Message); } catch (ValueOutOfRangeException valueOutOfRangeException) { UI.PrintMessage(valueOutOfRangeException.Message); } } }
//get model name from console internal static eVehicleModel GetModelName() { int numberOfOptions = Enum.GetNames(typeof(eVehicleModel)).Length; Console.WriteLine("Choose a vehicle:"); foreach (eVehicleModel model in Enum.GetValues(typeof(eVehicleModel))) { Console.WriteLine("{0}. {1}", (int)model, VehicleModelToText.AsText(model)); } int userInput; bool tryParse = int.TryParse(Console.ReadLine(), out userInput); if (!tryParse) { throw new FormatException(k_ErrInvalidInput); } if (userInput < k_MenuFirstOption || userInput > numberOfOptions) { throw new ValueOutOfRangeException(numberOfOptions, k_MenuFirstOption); } return((eVehicleModel)userInput); }