コード例 #1
0
ファイル: Form1.cs プロジェクト: jtneumann/CS4B
        private bool AddVehicle(IVehicle vehicle)
        {
            bool returnValue = false;

            try
            {
                //Check the controls for erroneous data
                string errMsg = String.Empty;
                if (txtRegNo.TextLength == 0)
                {
                    errMsg = "Incorrect vehicle registration number" +
                             Environment.NewLine;
                }
                if (cboTypes.SelectedIndex < 0)
                {
                    errMsg += "No vehicle type selected" +
                              Environment.NewLine;
                }
                if (!IsNumeric(txtMeter.Text) ||
                    (IsNumeric(txtMeter.Text) && Double.Parse(txtMeter.Text) < 0))
                {
                    errMsg += "The meter value is incorrect" +
                              Environment.NewLine;
                }
                if (!IsNumeric(txtMeter.Text) ||
                    Double.Parse(txtMeter.Text) < 0 || txtRegNo.TextLength == 0 ||
                    cboTypes.SelectedIndex < 0)
                {
                    MessageBox.Show(errMsg);
                    // this is set as (return false;) in example but gives an comp error.
                    return(returnValue);
                }
                // assign data to the vehicle object
                var type = ((VehicleType)cboTypes.SelectedItem);
                var car  = new Car()
                {
                    Meter = Double.Parse(txtMeter.Text),
                    RegistrationNumber = txtRegNo.Text,
                    TypeId             = type.Id,
                    BasePricePerDay    = type.BasePricePerDay,
                    BasePricePerKm     = type.BasePricePerKm,
                    DayTariff          = type.DayTariff,
                    KmTariff           = type.KmTariff
                };
                // Add the vehicle obj to the Vehicles collection
                processor.AddVehicle(car);

                // Update the vehicle list on the Rent Vehicles tab
                FillAvailableVehicles();

                txtRegNo.Text = String.Empty;
                txtMeter.Text = String.Empty;

                returnValue = true;
            }
            catch (Exception)
            {
                returnValue = false;
            }
            return(returnValue);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: jtneumann/CS4B
        private bool AddVehicle(IVehicle vehicle)
        {
            bool returnValue = false;

            try
            {
                //Check the controls for erroneous data
                string errMsg = String.Empty;
                if (txtRegNo.TextLength == 0)
                {
                    errMsg = "Incorrect vehicle registration number" +
                             Environment.NewLine;
                }
                if (cboTypes.SelectedIndex < 0)
                {
                    errMsg += "No vehicle type selected" +
                              Environment.NewLine;
                }
                if (!IsNumeric(txtMeter.Text) ||
                    (IsNumeric(txtMeter.Text) && Double.Parse(txtMeter.Text) < 0))
                {
                    errMsg += "The meter value is incorrect" +
                              Environment.NewLine;
                }
                if (!IsNumeric(txtMeter.Text) ||
                    Double.Parse(txtMeter.Text) < 0 || txtRegNo.TextLength == 0 ||
                    cboTypes.SelectedIndex < 0)
                {
                    MessageBox.Show(errMsg);
                    // this is set as (return false;) in example but gives an comp error.
                    return(returnValue);
                }
                // assign data to the vehicle object
                var type = ((VehicleType)cboTypes.SelectedItem);
                var car  = new Car()
                {
                    Meter = Double.Parse(txtMeter.Text),
                    RegistrationNumber = txtRegNo.Text,
                    TypeId             = type.Id,
                    BasePricePerDay    = type.BasePricePerDay,
                    BasePricePerKm     = type.BasePricePerKm,
                    DayTariff          = type.DayTariff,
                    KmTariff           = type.KmTariff
                };
                // Adding business rules
                var rules = new List <BusinessRule <IVehicle> >()
                {
                    new BusinessRule <IVehicle>
                    {
                        Property = "Id",
                        Value    = 1,
                        Comparer = RuleOperator.LessThan
                    },
                    new BusinessRule <IVehicle>
                    {
                        Property = "Meter",
                        Value    = -1,
                        Comparer = RuleOperator.GreaterThan
                    },
                    new BusinessRule <IVehicle>
                    {
                        Property = "RegistrationNumber",
                        Value    = String.Empty,
                        Comparer = RuleOperator.NotEqual
                    },
                    new BusinessRule <IVehicle>
                    {
                        Property = "TypeId",
                        Value    = 0,
                        Comparer = RuleOperator.GreaterThan
                    }
                };

                // Test - page 637 in kindle
                //            var success = new RuleComparer()
                //.EvaluateRules(car, rules);

                // Add the vehicle obj to the Vehicles collection
                processor.AddVehicle(car);



                // Update the vehicle list on the Rent Vehicles tab
                FillAvailableVehicles();

                txtRegNo.Text = String.Empty;
                txtMeter.Text = String.Empty;

                returnValue = true;
            }
            catch (Exception)
            {
                returnValue = false;
            }
            return(returnValue);
        }