Ejemplo n.º 1
0
        private void ApplyButton_Click(object sender, EventArgs e)
        {
            int        age        = int.Parse(AgeTextBox.Text);
            bool       isStudent  = StudentCheckBox.Checked;
            int        income     = int.Parse(IncomeTextBox.Text);
            BundleItem bundleItem = (BundleItem)BundleSelection.SelectedItem;

            if (!RulesValidator.Validate(_config.Bundles[bundleItem.BundleID].Rules, age, isStudent, income, null))
            {
                Bundle bundle       = _config.Bundles[bundleItem.BundleID];
                string errorMessage = GetErrorMessage(bundle.Rules, string.Format(BUNDLE_SENDER_FORMAT, bundle.Name));
                MessageBox.Show(errorMessage, ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                List <string> selectedProducts = new List <string>();
                for (int i = 0; i < AccountsContainer.Controls.Count; ++i)
                {
                    RadioButton radioButton = (RadioButton)AccountsContainer.Controls[i];
                    if (radioButton.Checked)
                    {
                        ProductItem productItem = (ProductItem)radioButton.Tag;
                        selectedProducts.Add(productItem.ProductID);
                        break;
                    }
                }
                var checkedItems = ProductList.CheckedItems.GetEnumerator();
                while (checkedItems.MoveNext())
                {
                    ProductItem productItem = (ProductItem)checkedItems.Current;
                    selectedProducts.Add(productItem.ProductID);
                }
                for (int i = 0; i < selectedProducts.Count; ++i)
                {
                    Product product = _config.Products[selectedProducts[i]];
                    if (!RulesValidator.Validate(product.Rules, age, isStudent, income, selectedProducts))
                    {
                        string errorMessage = GetErrorMessage(product.Rules, string.Format(PRODUCT_SENDER_FORMAT, product.Name));
                        MessageBox.Show(errorMessage, ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            UpdateCurrentBundle();
        }
        public string Evaluate(int age, bool isStudent, int income)
        {
            var           enumerator      = _config.Bundles.GetEnumerator();
            List <string> eligibleBundles = new List <string>();

            while (enumerator.MoveNext())
            {
                Bundle bundle = enumerator.Current.Value;
                if (RulesValidator.Validate(bundle.Rules, age, isStudent, income, null))
                {
                    eligibleBundles.Add(enumerator.Current.Key);
                }
            }
            if (eligibleBundles.Count > 1)
            {
                return(GetBestValueBundle(eligibleBundles));
            }
            else if (eligibleBundles.Count == 1)
            {
                return(eligibleBundles[0]);
            }
            return(string.Empty);
        }