示例#1
0
 private void bt_rent_Click(object sender, EventArgs e)
 {
     if (DataGrid_cars.Rows.Count > 0)
     {
         RentalCar rentalCar = new RentalCar()
         {
             Available     = (bool)(DataGrid_cars.SelectedRows[0]).Cells[0].Value,
             AvailableFrom = (DateTime)(DataGrid_cars.SelectedRows[0]).Cells[1].Value,
             PricePerDay   = (int)(DataGrid_cars.SelectedRows[0]).Cells[2].Value,
             Brand         = (string)(DataGrid_cars.SelectedRows[0]).Cells[3].Value,
             Model         = (string)(DataGrid_cars.SelectedRows[0]).Cells[4].Value,
             LicensePlate  = (string)(DataGrid_cars.SelectedRows[0]).Cells[5].Value,
             Type          = (string)(DataGrid_cars.SelectedRows[0]).Cells[6].Value,
         };
         if (rentalCar.Available)
         {
             if (user.Balance >= numeric_daysCount.Value * rentalCar.PricePerDay)
             {
                 int index = 0;
                 for (int i = 0; i < cars.Count; i++)
                 {
                     if (cars[i].LicensePlate == rentalCar.LicensePlate)
                     {
                         index = i;
                         break;
                     }
                 }
                 cars.RemoveAt(index);
                 rentalCar.AvailableFrom = DateTime.Now;
                 rentalCar.AvailableFrom = rentalCar.AvailableFrom.AddDays((int)numeric_daysCount.Value);
                 rentalCar.Available     = false;
                 rentalCar.RentedID      = user.ID;
                 user.Pay(rentalCar.PricePerDay * numeric_daysCount.Value);
                 LB_balance.Text = user.Balance.ToString();
                 cars.Insert(index, rentalCar);
                 RentalSerivce.SerializeCars(cars);
                 user.Serialize();
                 DataGrid_cars.Rows.Clear();
                 foreach (var car in cars)
                 {
                     AddCarToDataGrid(car);
                 }
                 MsgBox msgBox = new MsgBox("Rental success", "The car is successfully rented");
                 msgBox.ShowDialog();
             }
             else
             {
                 MsgBox msgBox = new MsgBox("Rental error", "There are not enough funds on your balance for this rent");
                 msgBox.ShowDialog();
             }
         }
         else
         {
             MsgBox msgBox = new MsgBox("Rental error", "The selected car is not available");
             msgBox.ShowDialog();
         }
     }
     else
     {
         MsgBox msgBox = new MsgBox("Rental error", "You have not chosen the car you want to rent");
         msgBox.ShowDialog();
     }
 }