private void bt_remove_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) { for (int i = 0; i < cars.Count; i++) { if (cars[i].LicensePlate == rentalCar.LicensePlate) { cars.RemoveAt(i); break; } } RentalSerivce.SerializeCars(cars); DataGrid_cars.Rows.Clear(); foreach (var car in cars) { AddCarToDataGrid(car); } MsgBox msgBox = new MsgBox("Removing car", "The car is successfully removed"); msgBox.ShowDialog(); } else { MsgBox msgBox = new MsgBox("Removing error", "The selected car is not available"); msgBox.ShowDialog(); } } else { MsgBox msgBox = new MsgBox("Removing error", "You have not chosen the car you want to remove"); msgBox.ShowDialog(); } }
private void bt_addcar_Click(object sender, EventArgs e) { bool brand_valide = false, model_valide = false, license_plate_valide = false, amount_valide = false; if (txtbox_carBrand.Text.Length > 2) { brand_valide = true; for (int i = 0; i < txtbox_carBrand.Text.Length; i++) { if (char.IsDigit(txtbox_carBrand.Text[i])) { brand_valide = false; break; } } } if (!brand_valide) { MsgBox msgBox = new MsgBox("Error adding car", "Entered the wrong car brand. The car brand must be longer than 2 letters and not contain other characters!"); msgBox.ShowDialog(); } if (txtbox_carBrand.Text.Length > 2) { model_valide = true; } else { MsgBox msgBox = new MsgBox("Error adding car", "Entered the wrong car model. The car model must be longer than 2 characters!"); msgBox.ShowDialog(); } var num_reg = new Regex(@"^[A-Z]{2}\d{4}[A-Z]{2}$"); if (num_reg.IsMatch(txtbox_licensePlate.Text)) { license_plate_valide = true; for (int i = 0; i < cars.Count; i++) { if (cars[i].LicensePlate == txtbox_licensePlate.Text) { license_plate_valide = false; break; } } if (!license_plate_valide) { MsgBox msgBox = new MsgBox("Error adding car", "A car with such a license plate is already there!"); msgBox.ShowDialog(); } } else { MsgBox msgBox = new MsgBox("Error adding car", "The license plate of the car is incorrect"); msgBox.ShowDialog(); } if (brand_valide && model_valide && license_plate_valide) { cars.Add(new RentalCar() { Available = true, AvailableFrom = DateTime.Now, Brand = txtbox_carBrand.Text, LicensePlate = txtbox_licensePlate.Text, Model = txtbox_model.Text, RentedID = 0, Type = comboBox_carType.SelectedItem.ToString(), PricePerDay = (int)numeric_pricePerDay.Value }); RentalSerivce.SerializeCars(cars); MsgBox msgBox = new MsgBox("Adding car", "Car added successfully"); msgBox.ShowDialog(); Desktop.BringToFront(); this.Close(); } }
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(); } }