/// <summary> /// The actual execution of editing a vehicle. /// Resolve and check for business logic constraints before equalising values and letting the user know it was successful. /// </summary> /// <param name="args">List of user-defined inputs</param> protected override void Execute(List <string> args) { PropertyInfo[] vehicleProperties = typeof(Vehicle).GetProperties(); Vehicle existingVehicle = ResolveVehicle(args); List <string> trimmedArgs = TrimArguments(args); DefaultArguments(ref trimmedArgs, vehicleProperties, existingVehicle); Vehicle validatedVehicle = CreateNewVehicle(trimmedArgs); // Ensure vehicle isn't being rented Fleet fleet = repository.Get(); Vehicle vehicle = fleet.GetVehicle(validatedVehicle.vehicleRego); if (fleet.IsVehicleRented(vehicle.vehicleRego)) { throw new VehicleCurrentlyRentingException(string.Format("{0} is currently being rented and cannot be edited.", vehicle.vehicleRego)); } ValidateConstraints(existingVehicle, validatedVehicle); EqualiseVehicles(existingVehicle, validatedVehicle); Console.WriteLine("Successfully edited vehicle {0} {1} {2} (Registration = {3}).", validatedVehicle.year, validatedVehicle.make, validatedVehicle.model, validatedVehicle.vehicleRego); Console.WriteLine(); }
// This method creates the rental data table for displaying rentals public static void RentalsToTable(Fleet fleet, DataTable Name) { // String array containing all fields string[] Fields = { "Registration", "CustomerID", "DailyRate" }; // Add columns to rentals datatable foreach (string field in Fields) { Name.Columns.Add(new DataColumn(field)); } // list which will contain all the lines List <string> totalLines = new List <string>(); // Loop through dictionary and store all values into total lines, including dailyRate foreach (KeyValuePair <string, int> kvp in fleet.Rentals) { totalLines.Add(kvp.Key + "," + kvp.Value.ToString() + "," + fleet.GetVehicle(kvp.Key).dailyRate.ToString("C")); } // implement loop to add data lines to the row data for (int row = 0; row < totalLines.Count; row++) { string[] dataWords = totalLines[row].Split(','); DataRow dataRow = Name.NewRow(); int columnIndex = 0; foreach (string Field in Fields) { dataRow[Field] = dataWords[columnIndex++]; } // add row to datatable Name.Rows.Add(dataRow); } }
/// <summary> /// Hard resolve vehicle from fleet, with an exception throwing if it cannot be resolveds /// </summary> /// <param name="args">List of user-defined inputs</param> /// <returns>Vehicle if can be resolved from arguments</returns> private Vehicle ResolveVehicle(List <string> args) { Fleet fleet = repository.Get(); string vehicleRego = args[Index_To_Remove]; return(fleet.GetVehicle(vehicleRego)); }
/// <summary> /// Rents a vehicle based on user input /// </summary> /// <param name="args">List of user-input arguments</param> protected override void Execute(List <string> args) { // Setup variables int customerID = int.Parse(args[0]); string vehicleRego = args[1]; int days = int.Parse(args[2]); FleetRepository fleetRepository = (FleetRepository)repository; Fleet fleet = fleetRepository.Get(); CRM crm = crmRepository.Get(); // Resolve Vehicle vehicle = fleet.GetVehicle(vehicleRego); Customer customer = crm.GetCustomer(customerID); // Inform user of cost with lots of details Console.WriteLine("Rent {0} to {1} {2} (ID: {3}) for {4} days.", vehicle.vehicleRego, customer.firstName, customer.lastName, customer.ID, days); Console.WriteLine("This vehicle has a daily cost of ${0}, bringing the total to ${1:0.00}.", vehicle.dailyRate, (vehicle.dailyRate * days)); Console.WriteLine(); // Make user confirm rental at price string input = ""; do { Console.Write("Confirm you want to go ahead with this rental (y/n): "); input = Console.ReadLine().Trim().ToLower(); if (input == "y") { break; } if (input == "n") { return; } } while (input != "y" || input != "n"); // Let user know it was successful Console.WriteLine(); fleet.RentVehicle(vehicle.vehicleRego, customer.ID); Console.WriteLine("Successfully rented a {0} {1} {2} to {3} {4} (ID: {5})", vehicle.year, vehicle.make, vehicle.model, customer.firstName, customer.lastName, customer.ID); Console.WriteLine(); }
/// <summary> /// Removes a vehicle from the fleet using user input /// </summary> /// <param name="args">List of user-input arguments</param> protected override void Execute(List <string> args) { Fleet fleet = repository.Get(); string vehicleRego = args[Index_To_Use]; // Disallow deleting vehicles being rented Vehicle vehicle = fleet.GetVehicle(vehicleRego); if (fleet.IsVehicleRented(vehicle.vehicleRego)) { throw new VehicleCurrentlyRentingException(string.Format("{0} is currently being rented and cannot be deleted.", vehicle.vehicleRego)); } fleet.RemoveVehicle(vehicle); Console.WriteLine("Successfully removed a {0} {1} {2} with registration {3} from the MRRC fleet.", vehicle.year, vehicle.make, vehicle.model, vehicle.vehicleRego); Console.WriteLine(); }
/// <summary> /// Return a vehicle from user input /// </summary> /// <param name="args">List of user-input arguments</param> protected override void Execute(List <string> args) { // Setup variables Fleet fleet = repository.Get(); string vehicleRego = args[0]; fleet.GetVehicle(vehicleRego); // Call this to verify vehicle exists (will throw exception) // Disallow returning vehicles not being rented if (!fleet.IsVehicleRented(vehicleRego)) { throw new Exception(string.Format("Vehicle {0} is not currently being rented. No need to return it.", vehicleRego)); } fleet.ReturnVehicle(vehicleRego); // Let user know that it has been successful Console.WriteLine(string.Format("Successfully returned vehicle {0} to the fleet. It can now be rented to other customers.", vehicleRego)); Console.WriteLine(); }