Exemple #1
0
        private void AddToInventoryButton_Click(object sender, EventArgs e)
        {
            if (orderListBox.SelectedIndex >= 0)
            {
                var article = orderList.getItemByName((string)orderListBox.SelectedItem);

                if (!inventoryList.articleExists(article.getAttributeValue("id")))
                {
                    if (radioButton1.Checked)
                    {
                        article.setAttributeValue("quantity", "1");
                    }
                    else if (radioButton2.Checked)
                    {
                        article.setAttributeValue("quantity", "5");
                    }
                    else if (radioButton3.Checked)
                    {
                        article.setAttributeValue("quantity", "10");
                    }
                    else
                    {
                        article.setAttributeValue("quantity", quantityNumeric.Value.ToString());
                    }
                    inventoryList.articleList.Add(article);
                    updateInventoryGridView();
                }
                radioButton1.Checked  = true;
                radioButton2.Checked  = false;
                radioButton3.Checked  = false;
                quantityNumeric.Text  = "0";
                quantityNumeric.Value = 0;
            }
        }
        private void removeFromCart(Article article, int index)
        {
            //removes the article from the shopping cart and restore the quantity to the store
            try
            {
                Article storeArticle = storeList.getItemByName(article.getAttributeValue("name"));
                string  tempQty      = article.getAttributeValue("quantity");
                int     Qty          = int.Parse(storeArticle.getAttributeValue("quantity")) + int.Parse(tempQty);
                storeArticle.setAttributeValue("quantity", Qty.ToString());

                var inventoryIndex = inventoryList.findArticleIndex(article.getAttributeValue("id"));
                inventoryList.articleList[inventoryIndex].setAttributeValue("quantity", Qty.ToString());

                cartList.articleList.RemoveAt(index);
            }
            catch (NullReferenceException)
            {
                //catch if the article you try to remove from the shopping cart no longer exists in the store
                //try to restore the quantity to the inventory
                Console.WriteLine("The item youre trying to remove from the cart is no longer in the store.");
                try
                {
                    Article inventoryArticle = inventoryList.getItemByName(article.getAttributeValue("name"));
                    string  tempQty          = article.getAttributeValue("quantity");
                    int     Qty = int.Parse(inventoryArticle.getAttributeValue("quantity")) + int.Parse(tempQty);
                    inventoryArticle.setAttributeValue("quantity", Qty.ToString());
                    cartList.articleList.RemoveAt(index);
                }
                //catch again if the same article does not exist in the inventory
                catch (NullReferenceException)
                {
                    Console.WriteLine("The item youre trying to remove from the cart is no longer in the store or inventory.");
                    cartList.articleList.RemoveAt(index);
                }
            }
            updateCartGridView();
            updateStoreGridView();
        }