Beispiel #1
0
        /// <summary>
        /// Get median price
        /// </summary>
        /// <returns></returns>
        public double GetMedianPrice()
        {
            //The product list always needs to be sorted by price in asc order before the calculation

            if (this.NumberOfProducts == 0)
            {
                return(0);
            }
            else if (this.NumberOfProducts == 1)
            {
                return(this.productList[0].RetailPrice);
            }

            //To calculate median price, create a copy of the list and sort by price in asc order before the calculation.
            ProductCatalogue   cata       = new ProductCatalogue(string.Empty, Categories.Unknown);
            List <ProductData> medianList = new List <ProductData>();

            medianList.AddRange(this.productList);
            cata.productList = medianList;
            cata.Sort(SortType.RetailPrice, SortOrder.Ascending);

            int middle = medianList.Count / 2;

            if (medianList.Count % 2 == 0)
            {
                //When the number of product is even.
                double median1 = medianList[middle - 1].RetailPrice;
                double median2 = medianList[middle].RetailPrice;
                return((median1 + median2) / 2);
            }
            else
            {
                return(medianList[middle].RetailPrice);
            }
        }
        /// <summary>
        /// Add product button click event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddProduct_Click(object sender, EventArgs e)
        {
            if (!this.ExistCatalogue())
            {
                MessageBox.Show(AppResources.MSG_NO_CATALOGUES, AppResources.MSG_CAP_ERROR,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (LboxCatalogList.SelectedIndex < 0)
            {
                MessageBox.Show(AppResources.MSG_SELECT_CATALOGUE, AppResources.MSG_CAP_ERROR,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            FormAddProduct frmAdd = new FormAddProduct();

            if (frmAdd.ShowDialog() != DialogResult.OK)
            {
                //if user cancels
                return;
            }

            ProductCatalogue cata = this.GetSelectedCatalogue();
            ProductData      data = new ProductData(frmAdd.ProductName, frmAdd.RetailPrice);

            cata.InsertProduct(data);
            //sort product by selected sort type and order
            cata.Sort(this.GetSortType(), this.GetSortOrder());
            this.BsGridSource_DataSourceChanged(null, null);
            this.ShowMedianPrice(cata);
            //this.ClearNewProductInputPanel();
        }
        /// <summary>
        /// Create catalogue button click event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCreateCatalogue_Click(object sender, EventArgs e)
        {
            FormCreateCatalogue frmCata = new FormCreateCatalogue();

            if (frmCata.ShowDialog() != DialogResult.OK)
            {
                //if user cancels
                return;
            }

            //create new catalogue
            ProductCatalogue newCata = new ProductCatalogue(frmCata.CatalogueName, frmCata.Category);

            catalogueList.Add(newCata);
            // sort the catalogue list after added
            catalogueList.Sort(delegate(ProductCatalogue cata1, ProductCatalogue cata2)
            {
                //compare with the names
                return(cata1.CatalogueName.CompareTo(cata2.CatalogueName));
            });

            this.InitializeCatalogueListBox();

            //Find the index to get newly added item selected in the list box.
            int index = catalogueList.FindIndex(delegate(ProductCatalogue cata)
            {
                //Since multiple catalogues that have the same names can be added,
                //Equals() method needs to be used to find newly added object.
                return(cata.Equals(newCata));
            });
        }
        /// <summary>
        /// Remove catalogue button click event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRemoveCatalogue_Click(object sender, EventArgs e)
        {
            //catalogue existance check
            if (!this.ExistCatalogue())
            {
                MessageBox.Show(AppResources.MSG_NO_CATALOGUES, AppResources.MSG_CAP_ERROR,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (LboxCatalogList.SelectedIndex < 0)
            {
                MessageBox.Show(AppResources.MSG_CATE_SELECTED_TO_DELETE, AppResources.MSG_CAP_ERROR,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //ProductCatalogue cata = catalogueList[LboxCatalogList.SelectedIndex];
            ProductCatalogue cata = this.GetSelectedCatalogue();

            //confirmation message
            if (MessageBox.Show(AppResources.MSG_ASK_DELETE_CATA, AppResources.MSG_CAP_CONF,
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                //if user cancels.
                return;
            }

            //remove the catalogue and clear the data grid
            catalogueList.RemoveAt(LboxCatalogList.SelectedIndex);
            this.InitializeCatalogueListBox();

            if (LboxCatalogList.Items.Count != 0)
            {
                LboxCatalogList.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// Show median price on the window.
        /// </summary>
        /// <param name="cata"></param>
        private void ShowMedianPrice(ProductCatalogue cata)
        {
            if (cata == null)
            {
                return;
            }
            string medPerCata    = cata.GetMedianPrice().ToString();
            string medAcrossCata = this.GetMedianPriceAcrossCatalogues().ToString();

            TboxCatalogueMedian.Text = medPerCata;
            TboxAllMedian.Text       = medAcrossCata;
        }
        /// <summary>
        /// Sort_CheckedChanged event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sort_CheckedChanged(object sender, EventArgs e)
        {
            ProductCatalogue cata = GetSelectedCatalogue();

            if (cata == null)
            {
                return;
            }
            cata.Sort(this.GetSortType(), this.GetSortOrder());
            //To show sorted result, call the event.
            this.BsGridSource_DataSourceChanged(null, null);
        }
        /// <summary>
        /// Get median price across all catalogues.
        /// </summary>
        /// <returns></returns>
        private double GetMedianPriceAcrossCatalogues()
        {
            if (catalogueList == null || catalogueList.Count == 0)
            {
                return(0);
            }

            ProductCatalogue cataAll = new ProductCatalogue(AppResources.NAME_CALC_MEDIAN_CATA_LIST_NAME, Categories.Unknown);

            foreach (ProductCatalogue cata in catalogueList)
            {
                cataAll.ProductList.AddRange(cata.ProductList);
            }
            return(cataAll.GetMedianPrice());
        }
        /// <summary>
        /// Catalogue list box SelectedValueChanged event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LboxCatalogList_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.LboxCatalogList.SelectedIndex < 0)
            {
                return;
            }
            //ProductCatalogue cata = catalogueList[LboxCatalogList.SelectedIndex];
            ProductCatalogue cata = this.GetSelectedCatalogue();

            //sort product by selected sort type and order
            cata.Sort(this.GetSortType(), this.GetSortOrder());

            //Show all products in the data grid
            // When datasource is changed, the (BindingSource) event will be occured,
            // then the event shows all products in the catalogue.
            this.BsGridSource.DataSource = cata.ProductList;

            //show catalogue name and median price
            this.TboxCategory.Text      = cata.Category.ToString();
            this.TboxCatalogueName.Text = cata.CatalogueName;
            this.ShowMedianPrice(cata);
        }