/** * @param drivingLicense * @param typeOfCar * @return * method determines whether the person is eligible to rent a car of the specified type and, * if there is a car available, issues and associates that car with the driving license */ public String IssueCar(DrivingLicense drivingLicense, String typeOfCar) { Calendar cal = CultureInfo.InvariantCulture.Calendar; DateTime myDT = new DateTime(); int year = 2019; int age = int.Parse(drivingLicense.GetDateOfBirth().ToString().Substring(6, 4)); int issue = int.Parse(drivingLicense.GetDateOfIssue().ToString().Substring(6, 4)); Boolean valid = drivingLicense.IsLicenseValid(); List <ICar> car = drivingLicense.CarAssociated(); //if the type of car is of type small if (typeOfCar == "small" || typeOfCar == "Small") { //if the driving license is valid, is over 21 years old and has held the license for a year if (valid == true && year - age >= 21 && year - issue >= 1) { //method call to find a free small car int i = FindFreeSmallCar(); //if size of list is less than 1 if (car.Count < MAX_NUMBER_OF_RENTED_CARS) { //add a small car to the list and set the car as rented car.Add(smallCars[i]); smallCars[i].SetRented(true); } } } //if the type of car is of type large else if (typeOfCar == "large" || typeOfCar == "Large") { //if the driving license is valid, is over 25 years old and has held the license for 5 years if (valid == true && year - age >= 25 && year - issue >= 5) { //method call to find a free large car int i = FindFreeLargeCar(); //if size of list is less than 1 if (car.Count < MAX_NUMBER_OF_RENTED_CARS) { //add a small car to the list and set the car as rented car.Add(largeCars[i]); smallCars[i].SetRented(true); } } } else { //if conditions for rental are not met then print statement return("Car cannot be issued."); } return("Car can be issued."); }
/** * @param drivingLicense * @return * method disassociates the car currently rented with the driving license and returns fuel to fill the tank */ public int TerminateRental(DrivingLicense drivingLicense) { List <ICar> car = drivingLicense.CarAssociated(); int i; //loops through maximum number of rented cars; 1 for (i = 0; i < MAX_NUMBER_OF_RENTED_CARS; i++) { //if a car is there, break if (car != null) { break; } } //change cars status to not rented and remove it from the driving license car[i].SetRented(false); int fuelConsumed = car[i].Capacity() - car[i].GetFuelInTank(); car.Remove(car[i]); //return fuel required to fill the tank return(fuelConsumed); }
public static void Main() { //create small and large car lists List <ICar> small = new List <ICar>(); List <ICar> large = new List <ICar>(); //add small cars to list small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); small.Add(new SmallCar()); //add large cars to list large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); large.Add(new LargeCar()); //add both lists to CarHire object CarHire list = new CarHire(small, large); DateTime myDT1 = new DateTime(1950, 11, 17, new GregorianCalendar()); DateTime myDT2 = new DateTime(1999, 8, 25, new GregorianCalendar()); DateTime myDT3 = new DateTime(1940, 5, 12, new GregorianCalendar()); DateTime myDT4 = new DateTime(1990, 7, 19, new GregorianCalendar()); //create list for associated car List <ICar> driv1car = new List <ICar>(); //create Name object for driving license Name name1 = new Name("James", "Peacock"); //create DrivingLicense object DrivingLicense driv1 = new DrivingLicense(name1, myDT1, myDT2, true, driv1car); //create list for associated car List <ICar> driv2car = new List <ICar>(); //create Name object for driving license Name name2 = new Name("David", "Peacock"); //create DrivingLicense object DrivingLicense driv2 = new DrivingLicense(name2, myDT3, myDT4, true, driv2car); //print available cars and cars that are currently rented out Console.WriteLine("Small cars available to rent: " + list.AvailableCars("small")); Console.WriteLine("Large cars available to rent: " + list.AvailableCars("large")); Console.WriteLine("Cars currently rented: " + list.GetRentedCars().Count); //print out driving licenses Console.WriteLine(driv1); Console.WriteLine(driv2); //issue car to driving license list.IssueCar(driv1, "small"); //show car is associated with driving license Console.WriteLine("\nCar associated with driving license: "); for (int i = 0; i < list.GetCar(driv1).Count; i++) { Console.Write(list.GetCar(driv1)[i].ToString() + " "); } //show car has been rented Console.WriteLine("\nSmall cars available to rent: " + list.AvailableCars("small")); Console.WriteLine("Large cars available to rent: " + list.AvailableCars("large")); Console.WriteLine("Cars currently rented: " + list.GetRentedCars().Count); //show fuel consumed after being driven and fuel required to fill tank driv1car[0].Drive(400); Console.WriteLine("\nFuel required to fill the tank after being driven: " + list.TerminateRental(driv1)); //show car is no long associated with the driving license after terminating rental Console.WriteLine("\nCar disassociated with driving license and returned: "); for (int i = 0; i < list.GetCar(driv1).Count; i++) { Console.Write(list.GetCar(driv1)[i].ToString() + " "); } //show all cars are now available to rent again Console.WriteLine("\nSmall cars available to rent: " + list.AvailableCars("small")); Console.WriteLine("Large cars available to rent: " + list.AvailableCars("large")); Console.WriteLine("Cars currently rented: " + list.GetRentedCars().Count); //show next car to be issued is different from first because tank is not full in first car list.IssueCar(driv2, "small"); Console.WriteLine("\nCar associated with driving license: "); for (int i = 0; i < list.GetCar(driv2).Count; i++) { Console.Write(list.GetCar(driv2)[i].ToString() + " "); } //show adding a second car to the same driving license does nothing list.IssueCar(driv2, "small"); Console.WriteLine("\nCar associated with driving license: "); for (int i = 0; i < list.GetCar(driv2).Count; i++) { Console.Write(list.GetCar(driv2)[i].ToString() + " "); } }
/** * @param drivingLicense * @return * method to return the car someone is currently renting */ public List <ICar> GetCar(DrivingLicense drivingLicense) { //returns list of single car currently associated with the driving license return(drivingLicense.CarAssociated()); }