public EditRentingForm(IBL bl, int rentingId) { this.bl = bl; this.rentingToEdit = bl.SelectRenting(rentingId); InitializeComponent(); // Setting form variables rentingIdLabel.Text = "Renting #" + rentingToEdit.IdNumber; comboBox1.DataSource = bl.SelectAllCars().Select(c => c.Name).ToList(); comboBox1.SelectedItem = bl.SelectCar(rentingToEdit.CarId).Name; comboBox2.DataSource = bl.SelectAllClients().Select(c => c.Name).ToList(); comboBox2.SelectedItem = bl.SelectClient(rentingToEdit.DriversId()[0]).Name; dateTimePicker1.Value = rentingToEdit.RentalStartDate; if (rentingToEdit.DriversNumber == 2) { checkBox1.Checked = true; comboBox3.DataSource = bl.SelectAllClients().Select(c => c.Name).ToList(); comboBox3.SelectedItem = bl.SelectClient(rentingToEdit.DriversId()[1]).Name; checkBox1.Text = "Change the Second driver:"; } else { comboBox3.Enabled = false; } SummaryUpdate(); }
public bool InsertRenting(Renting r) { // Checking existing client and car int clientMatching = MyDAL.SelectAllClients().Where(c => (r.DriversId()[0] == c.IdNumber) || (r.DriversId()[1] == c.IdNumber)).Count(); int carMatching = MyDAL.SelectAllCars().Where(c => r.CarId == c.IdNumber).Count(); // Checking faults responsibility of the driver/drivers bool faultsNbTooBig = false; for (int i = 0; i < r.DriversNumber; i++) { if (MyDAL.SelectClient(r.DriversId()[i]).FaultResponsible > 2) { faultsNbTooBig = true; } } // Handling exceptions if (clientMatching < 1) // there must be the client1 and the default client or the client2 { throw new Exception("The renting's clients haven't been registered yet."); } if (carMatching == 0) { throw new Exception("The car selected hasn't been registered yet."); } if (faultsNbTooBig) { throw new Exception("One of the renting's driver has been responsible for 2 faults or more. The renting can't be done."); } try { return(MyDAL.InsertRenting(r)); } catch (Exception) { throw; } }
/// <summary> /// Returns the final price for the given renting /// </summary> /// <param name="rentingId"></param> /// <returns></returns> public double FinalRentingPrice(int rentingId) { // we get the renting price based on the number of days, and other criterias Renting rent = MyDAL.SelectRenting(rentingId); if (rent.RentalPriceDaily == 0) { throw new ArgumentNullException("The renting must be started before calculating the final price."); } // getting the properties of the rent double price = rent.RentalPriceDaily; int driversNb = rent.DriversNumber; DateTime today = Convert.ToDateTime(DateTime.Now.ToString()); // we take fee of 50 NIS for a second driver int taxSecondDriver = driversNb == 2 ? 50 : 0; /* * * * * * * * * * * * Additional fees for * recently obtained * driver license * * * * * * * * * * */ int taxRecentDriver = 0; for (int i = 0; i < driversNb; i++) { if (IsNewDriver(rent.DriversId()[i])) { taxRecentDriver += 100; } } /* * * * * * * * * * * * Additional fees for * young driver * under 25 years old * * * * * * * * * * */ int taxYoungDriver = 0; for (int i = 0; i < driversNb; i++) { if (IsYoungDriver(rent.DriversId()[i])) { taxYoungDriver += 100; } } // If he is responsible of a takala so he has to pay for double taxFaults = 0; if (ThereWasFault(rentingId)) { // select all faults that happened during the renting var thereWasNewFault = MyDAL.SelectAllCar_Faults(). Where(cf => cf.CarId == rent.CarId). Where(cf => cf.FaultDate > rent.RentalStartDate && cf.FaultDate < rent.RentalEndDate). ToList(); foreach (Car_Fault item in thereWasNewFault) { Fault currentFault = MyDAL.SelectFault(item.FaultId); if (currentFault.Responsible) { taxFaults += currentFault.RepairCost; } } } // Calculating final price double finalPrice = price + taxSecondDriver + taxRecentDriver + taxYoungDriver + taxFaults; return(finalPrice); }