Ejemplo n.º 1
0
        private void buttonAccept_Click(object sender, EventArgs e)
        {
            if (test())
            {
                #region check
                if (!Double.TryParse(txtMileage.Text, out double mileage))
                {
                    Feedback.ErrorConvertMileage();
                    return;
                }

                if (!Double.TryParse(txtTankFilling.Text, out double tankFilling))
                {
                    Feedback.ErrorConvertTankFilling();
                    return;
                }

                if (!Double.TryParse(txtPower.Text, out double power))
                {
                    Feedback.ErrorConvertPower();
                    return;
                }

                if (!Double.TryParse(txtMaxTankFilling.Text, out double maxTankFilling))
                {
                    Feedback.ErrorConvertMaxTankFilling();
                    return;
                }

                if (!Double.TryParse(txtPrice.Text, out double basicPrice))
                {
                    Feedback.ErrorConvertBasicPrice();
                    return;
                }

                if (!Double.TryParse(txtPriceKilo.Text, out double pricePerKilometre))
                {
                    Feedback.ErrorConvertPriceKilo();
                    return;
                }

                if (!Double.TryParse(txtPriceMin.Text, out double pricePerMinute))
                {
                    Feedback.ErrorConvertPriceMin();
                    return;
                }

                if (!Double.TryParse(txtPosX.Text, out double posX))
                {
                    Feedback.ErrorConvertLocationX();
                    return;
                }

                if (!Double.TryParse(txtPosY.Text, out double posY))
                {
                    Feedback.ErrorConvertLocationY();
                    return;
                }

                if (!Double.TryParse(txtFuelCon.Text, out double fuelConsumption))
                {
                    Feedback.ErrorConvertFuelCon();
                    return;
                }

                if (!DateTime.TryParse(txtReg.Text, out DateTime registration))
                {
                    Feedback.ErrorConvertRegi();
                    return;
                }

                if (!Int32.TryParse(txtConstructionYear.Text, out int constructionYear))
                {
                    Feedback.ErrorConvertConstructionYear();
                    return;
                }

                if (!Int32.TryParse(txtSeats.Text, out int seats))
                {
                    Feedback.ErrorConvertSeats();
                    return;
                }

                if (tankFilling > maxTankFilling)
                {
                    Feedback.ErrorPhysikTankFilling();
                    return;
                }

                if (constructionYear < 1880)
                {
                    Feedback.ErrorConstructionYearTooEarly();
                    return;
                }

                if (constructionYear > DateTime.Today.Year)
                {
                    Feedback.ErrorConstructionYearTooLate();
                    return;
                }

                if (registration.Year < constructionYear)
                {
                    Feedback.ErrorRegiBeforeConstruction();
                    return;
                }

                if (dateTimeLastMaintenance.Value.Year < constructionYear)
                {
                    Feedback.ErrorMaintanceBeforeConstruction();
                    return;
                }

                if (!DBController.TryGetVehicleNumberPlates(out List <string> numberPlates))
                {
                    Feedback.ErrorDatabaseLoadNumberPlates();
                    return;
                }

                if (numberPlates.Contains(txtNumberplate.Text))
                {
                    if (vehicleOld != null)
                    {
                        if (vehicleOld.NumberPlate != txtNumberplate.Text)
                        {
                            Feedback.ErrorNumberPlateAlreadyExist();
                            return;
                        }
                    }
                    else
                    {
                        Feedback.ErrorNumberPlateAlreadyExist();
                        return;
                    }
                }
                #endregion

                Vehicle vehicle = new Vehicle(txtNumberplate.Text, mileage, dateTimeLastMaintenance.Value, tankFilling, new PointD(posX, posY), checkAvailable.Checked, comboBrand.Text, txtModel.Text, (int)Math.Round(power), constructionYear, comboGear.Text, maxTankFilling, basicPrice, pricePerKilometre, pricePerMinute, registration, seats, comboFuel.SelectedItem.ToString(), fuelConsumption, checkAirConditioner.Checked, checkCruiseControl.Checked, checkRadio.Checked, checkBluetooth.Checked, checkUSB.Checked, checkCDPlayer.Checked, checkNavigationDevice.Checked, checkABS.Checked, checkESP.Checked, checkHeatedSeat.Checked, checkWinter.Checked, checkSmoker.Checked);

                if (vehicleOld == null)
                {
                    if (!DBController.TryAddVehicle(vehicle))
                    {
                        Feedback.ErrorDatabaseVehicleAdd();
                        return;
                    }
                    Feedback.SuccessVehicleAdd();
                }
                else
                {
                    if (!DBController.TryUpdateVehicleInDB(vehicle, vehicleOld))
                    {
                        Feedback.ErrorDatabaseVehicleEdit();
                        return;
                    }

                    if (!DBController.TryCheckVehicleTypeIsNeeded(vehicleOld))
                    {
                        Feedback.ErrorDatabaseVehicleTypeDelete();
                    }

                    Feedback.SuccessVehicleEdit();
                }
                ((EditDataView)Parent).Close();
                FormController.MainView.UpdateVehicleList();
            }
        }