Esempio n. 1
0
        /// <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();
        }
Esempio n. 2
0
        /// <summary>
        /// Search vehicles to rent from user input
        /// </summary>
        /// <param name="args">List of user-input arguments</param>
        protected override void Execute(List <string> args)
        {
            // Setup variables
            string    text       = args[0];
            Fleet     fleet      = repository.Get();
            ArrayList attributes = fleet.GetAttributes();

            // Set up attribute list
            attributeSets = new SortedList();
            foreach (string attribute in attributes)
            {
                attributeSets.Add(attribute.ToUpper(), new HashSet <string>());
            }

            // Add vehicles to attribute list
            foreach (Vehicle vehicle in fleet.vehicles)
            {
                if (!fleet.IsVehicleRented(vehicle.vehicleRego))
                {
                    InsertVehicle(vehicle);
                }
            }

            // Perform lookup
            getQuery(text, out ArrayList query);
            string[] results = search(new SearchAlgorithm(query, attributes));

            // Filter results again by only those that are rentable
            List <Vehicle> resultVehicles = new List <Vehicle>();

            foreach (string vehicleRego in results)
            {
                Vehicle lookUp = fleet.vehicles.Find((vehicle) => vehicle.vehicleRego == vehicleRego);
                if (lookUp != null)
                {
                    resultVehicles.Add(lookUp);
                }
            }

            // Construct new to show to user
            SearchResultTable searchResultTable = new SearchResultTable(null, null, resultVehicles);

            searchResultTable.Refresh();
            searchResultTable.Print();
        }
Esempio n. 3
0
        /// <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();
        }
Esempio n. 4
0
        /// <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();
        }