public static double GetValueDiscount(DiscountCode discountCode)
        {
            if (discountCode.Type == "Value")
            {
                return(discountCode.Value);
            }
            else if (discountCode.Type == "2for1")
            {
                List <Cart> canonOrderedByPrice = Cart.CartItems.Where(x => x.Product.Name.Contains("Canon")).OrderBy(z => z.Product.Price).ToList();

                if (canonOrderedByPrice.Count() > 1 || canonOrderedByPrice[0].Amount > 1)
                {
                    return(canonOrderedByPrice[0].Product.Price);
                }
                else
                {
                    throw new Exception("Discount not valid! Add another canon!");
                }
            }
            else if (discountCode.Type == "Percent")
            {
                return(CheckoutSummaryValue * (discountCode.Value / 100.0));
            }
            else
            {
                throw new Exception("Discount not valid");
            }
        }
        public static void AddDiscountCodesToList()
        {
            try
            {
                string[] fileContent;
                fileContent = File.ReadAllLines("discountCodes.csv");

                foreach (string s in fileContent)
                {
                    string[] split = s.Split(';');

                    if (split.Length == 3)
                    {
                        DiscountCode newDiscount = new DiscountCode
                        {
                            Name  = split[0],
                            Type  = split[1],
                            Value = int.Parse(split[2])
                        };

                        DiscountCodeList.Add(newDiscount);
                    }
                    else
                    {
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#3
0
        public ShopWindow()
        {
            ClientSize    = new Size(1000, 700);
            StartPosition = FormStartPosition.CenterScreen;
            Text          = "Camera store";
            Icon          = new Icon("MainFormIcon.ico");
            Product.AddProductsToList();
            Cart.GetSavedCartItems();
            DiscountCode.AddDiscountCodesToList();
            UpdateCartAndTotalCost();
            //Updates total cost if you have saved products in your cart since last visit
            foreach (Cart item in Cart.CartItems)
            {
                if (item.Amount > 1)
                {
                    for (int i = 0; i < item.Amount; i++)
                    {
                        CartSummaryValue += item.Product.Price;
                    }
                }
                else
                {
                    CartSummaryValue += item.Product.Price;
                }
            }
            PriceSummaryTextFormatting = CartPriceSummaryLabel.Text;

            MainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 70));
            MainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30));
            MainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 60));
            MainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 40));
            ButtonLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));

            Controls.Add(MainLayout);
            MainLayout.Controls.Add(ShoppingCartGridView);
            MainLayout.Controls.Add(ButtonLayout);
            ButtonLayout.Controls.Add(CartPriceSummaryLabel);
            ButtonLayout.Controls.Add(RemoveButton);
            ButtonLayout.Controls.Add(ClearCartButton);
            ButtonLayout.Controls.Add(CheckOutButton);

            ShoppingCartGridView.Columns[0].Name  = "Product";
            ShoppingCartGridView.Columns[1].Name  = "Price";
            ShoppingCartGridView.Columns[2].Name  = "Quantity";
            ShoppingCartGridView.Columns[0].Width = 150;

            ShoppingCartGridView.CellClick += DataGridCellClick;
            RemoveButton.Click             += RemoveFromCartClick;
            CheckOutButton.Click           += CheckoutButtonClick;
            CheckOutButton.EnabledChanged  += CheckOutButton_EnabledChanged;
            ClearCartButton.Click          += ClearCartClick;
            FormClosing += SaveCartWhenExit;
            Load        += FormOpenEventHandler;
        }