/// <summary> /// Rent a car /// </summary> /// <param name="licencePlate">The licence plate of the car to rent</param> /// <param name="rentalDate">The start date for the rental</param> /// <returns>true if the car was available for renting, false otherwise.</returns> public bool RentCar(string licencePlate, SimpleDate rentalDate) { // Try to find the car with the given licence plate. Is it a Sedan? Sedan foundSedan = null; foreach (Sedan sedan in sedans) { if (sedan.LicencePlate == licencePlate) { foundSedan = sedan; break; } } // Was a sedan with the given licene plate found? Then try to rent it. if (foundSedan != null) { return(foundSedan.Rent(rentalDate)); } // No car found yet with the given licence plate. // Try to find the car with the given licence plate. Is it a Limousine? Limousine foundLimousine = null; foreach (Limousine limousine in limousines) { if (limousine.LicencePlate == licencePlate) { foundLimousine = limousine; break; } } // Was a limousine with the given licene plate found? Then try to rent it. if (foundLimousine != null) { return(foundLimousine.Rent(rentalDate)); } // No car found yet with the given licence plate. // Try to find the car with the given licence plate. Is it a Truck? Truck foundTruck = null; foreach (Truck truck in trucks) { if (truck.LicencePlate == licencePlate) { foundTruck = truck; break; } } // Was a truck with the given licene plate found? Then try to rent it. if (foundTruck != null) { return(foundTruck.Rent(rentalDate)); } return(false); // No Sedan nor Limousine nor Truck was found with the given licence plate. }
/// <summary> /// Return a car using the licence plate number /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void returnCarButton_Click(object sender, EventArgs e) { string licencePlate = licencePlateTextBox.Text; int kilometers; if (Int32.TryParse(returnKilometersTextBox.Text, out kilometers)) { SimpleDate date = new SimpleDate(returnDateTimePicker.Value.Day, returnDateTimePicker.Value.Month, returnDateTimePicker.Value.Year); decimal price = administration.ReturnCar(licencePlate, date, kilometers); if (price >= 0) { MessageBox.Show("Please Pay: '" + price + " credits'."); UpdateAvailableCarListAndRentedCarLists(); } else { MessageBox.Show("No Car with licence plate: '" + licencePlate + "'\nor the kilometers are less then when rented\nor the return date is invalid (before rental date)\nor the car was not rented and cannot be returned."); } } else { MessageBox.Show("The licence plate and/or kilometers are entered wrong format."); } }
/// <summary> /// Rents the truck. The truck will be unavailable after renting. /// It will be avaiable again after returning the truck. /// /// Only available trucks can be rented. /// </summary> /// <param name="rentalDate">The date on which the truck is rented.</param> /// <returns>true if the truck was available, false otherwise.</returns> public bool Rent(SimpleDate rentalDate) { if (IsAvailable) { RentalDate = rentalDate; return(true); } return(false); }
/// <summary> /// Returns a rented truck and calculate the costs of the rental. /// Only rented trucks can be returned. /// </summary> /// <param name="returnDate">The date on which the truck is returned.</param> /// <param name="kilometers">The total number of kilometers on the counter.</param> /// <returns>The cost of the rental, /// or a number less than zero when: /// - the truck was not rented (so it could not be returned) /// - the return date is before the rental date (so wrong return date was entered) /// - the number of kilometers when returned is less then at the start of the rental. /// Please note that returning as number less than zero for error situations is NOT(!!!) /// clean coding in this case. Later on you will learn to do this in a better fashion (Exceptions!) /// </returns> public decimal Return(SimpleDate returnDate, int kilometers) { if (!IsAvailable) { int daysRented = RentalDate.DaysDifference(returnDate); int kilometersDriven = kilometers - Kilometers; if (daysRented >= 0 && kilometersDriven >= 0) { RentalDate = null; // makes the truck available for renting again Kilometers = kilometers; // update kms for the next rental return(CalculateRentalCosts(daysRented, kilometersDriven)); } } return(-1); }
/// <summary> /// Rent the car using the licence plate /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void rentCarButton_Click(object sender, EventArgs e) { string licencePlate = licencePlateTextBox.Text; SimpleDate date = new SimpleDate(rentalDateTimePicker.Value.Day, rentalDateTimePicker.Value.Month, rentalDateTimePicker.Value.Year); bool isRented = administration.RentCar(licencePlate, date); if (isRented) { UpdateAvailableCarListAndRentedCarLists(); } else { MessageBox.Show("There's no car with licence plate: '" + licencePlate + "' or the car is already rented."); } }
/// <summary> /// Get the tumber of days between this objects date and the given date. /// </summary> /// <param name="date">The end date.</param> /// <returns>The number of days between this date and endDate.</returns> public int DaysDifference(SimpleDate date) { TimeSpan timespan = date.date.Subtract(this.date); return(timespan.Days); }