Ejemplo n.º 1
0
        /// <summary>
        /// Returns the index option selected by the user
        /// </summary>
        private int SelectOption(int optionIndex = 0)
        {
            //the following code enables option selection with arrow keys and enter
            int optIndex     = 0;
            int optionsCount = ObjectSelections.OptionsList.Count;

            Console.CursorVisible = false;
            ObjectSelections.SelectCurrentOptionAt(optionIndex);
            ConsoleKeyInfo key = Console.ReadKey(true);

            while (key.Key != ConsoleKey.Enter)
            {
                switch (key.Key)
                {
                case ConsoleKey.DownArrow:
                    ObjectSelections.DeselectCurrentOptionAt(optIndex);
                    if (optIndex + 1 >= optionsCount)
                    {
                        optIndex = (optIndex % (optionsCount - 1)) - 1;
                    }
                    ObjectSelections.SelectCurrentOptionAt(++optIndex);

                    break;

                case ConsoleKey.UpArrow:
                    ObjectSelections.DeselectCurrentOptionAt(optIndex);
                    if (optIndex - 1 < 0)
                    {
                        optIndex += optionsCount;
                    }
                    ObjectSelections.SelectCurrentOptionAt(--optIndex);
                    break;

                case ConsoleKey.Tab:
                    GInterface.SetWindowSize(GInterface.productPageWindowSize[0], GInterface.productPageWindowSize[1]);
                    GInterface.ShowCart();
                    do
                    {
                        Console.CursorLeft = 0;
                        Console.Write(' ');
                        key = Console.ReadKey(true);
                    } while (key.Key != ConsoleKey.Tab && key.Key != ConsoleKey.Escape);
                    Console.Clear();
                    GInterface.SetWindowSize(GInterface.mainPageWindowSize[0], GInterface.mainPageWindowSize[1]);
                    Console.WriteLine($"Total: {shoppingCartTotal:f2}bgn");
                    GInterface.PrintMainPageHeadder();
                    GInterface.PrintOptions(ObjectSelections.OptionsList);
                    ObjectSelections.SelectCurrentOptionAt(optionIndex);
                    break;

                case ConsoleKey.Escape:
                    Console.Clear();
                    Console.WriteLine("Visit us again :)");
                    //Console.ReadKey();
                    Environment.Exit(0);
                    break;
                }
                key = Console.ReadKey(true);
            }
            Console.CursorVisible = true;
            return(pickedOptionIndex = optIndex);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Enables option selection
        /// </summary>
        /// <param name="optionIndex"></param>
        private void SelectProduct(int optionIndex)
        {
            int productIndex  = 0;
            int categoryIndex = -1;
            int listLenght    = GInterface.GetListLenghtByCategory(optionIndex);

            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
            var key = Console.ReadKey(true);

            while (!(key.Key == ConsoleKey.Escape || key.Key == ConsoleKey.Backspace))
            {
                switch (key.Key)
                {
                case ConsoleKey.DownArrow:    //select next product
                    ObjectSelections.DeselectCurrentProductAt(productIndex, optionIndex);
                    if (productIndex + 1 >= listLenght)
                    {
                        productIndex = (productIndex % (listLenght - 1)) - 1;
                    }
                    ObjectSelections.SelectCurrentProductAt(++productIndex, optionIndex);
                    break;

                case ConsoleKey.UpArrow:    //select previous product
                    ObjectSelections.DeselectCurrentProductAt(productIndex, optionIndex);
                    if (productIndex - 1 < 0)
                    {
                        productIndex = listLenght;
                    }
                    ObjectSelections.SelectCurrentProductAt(--productIndex, optionIndex);
                    break;

                case ConsoleKey.Enter:    //add to cart
                    bool    isOutOfStock    = false;
                    dynamic categorizedList = GInterface.GetCategorizedList(optionIndex, controller);
                    string  productType     = categorizedList[productIndex].GetType().Name.ToString();
                    object  currentProduct  = categorizedList[productIndex];
                    switch (currentProduct.GetType().Name.ToString())
                    {
                    case "Supplement":
                        categoryIndex = 0;
                        Supplement supplement = (Supplement)currentProduct;
                        if (supplement.Quantity <= 0)
                        {
                            isOutOfStock = true;
                            GInterface.ShowOutOfStockMsg(productIndex);
                            Console.CursorLeft = 0;
                            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                        }
                        else        //add to cart and update database
                        {
                            controller.DecreaseQuantityOf(currentProduct, 1);
                            //controller.AddToCart(supplement, categoryIndex);
                            GInterface.SupplementList = (List <Supplement>)controller.GetAllBasedOnCategory(SupplementIndex);
                            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                            shoppingCartTotal += supplement.Price;
                            GInterface.RefreshCartTotal(shoppingCartTotal);
                        }
                        break;

                    case "Drink":
                        categoryIndex = 1;
                        Drink drink = (Drink)currentProduct;
                        if (drink.Quantity <= 0)
                        {
                            isOutOfStock = true;
                            GInterface.ShowOutOfStockMsg(productIndex);
                            Console.CursorLeft = 0;
                            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                        }
                        else        //add to cart and update database
                        {
                            controller.DecreaseQuantityOf(currentProduct, 1);
                            //controller.AddToCart(supplement, categoryIndex);
                            GInterface.DrinksList = (List <Drink>)controller.GetAllBasedOnCategory(drinksIndex);
                            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                            shoppingCartTotal += drink.Price;
                            GInterface.RefreshCartTotal(shoppingCartTotal);
                        }
                        break;

                    case "Equipment":
                        categoryIndex = 2;
                        Equipment equipment = (Equipment)currentProduct;
                        if (equipment.Quantity <= 0)
                        {
                            isOutOfStock = true;
                            GInterface.ShowOutOfStockMsg(productIndex);
                            Console.CursorLeft = 0;
                            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                        }
                        else
                        {
                            controller.DecreaseQuantityOf(currentProduct);
                            GInterface.EquipmentsList = (List <Equipment>)controller.GetAllBasedOnCategory(2);
                            ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                            GInterface.RefreshCartTotal(shoppingCartTotal);
                            shoppingCartTotal += equipment.Price;
                            GInterface.RefreshCartTotal(shoppingCartTotal);
                        }
                        break;
                    }

                    if (!isOutOfStock)
                    {
                        //if product exists in cart, increase ShoppingCartProductCounter by 1;
                        if (GInterface.ObjectListContainsProduct(GInterface.ShoppingCartList, currentProduct, productType))
                        {
                            GInterface.ShoppingCartProductCounter[GInterface.indexerOfProductsCounter]++;
                            controller.IncreaseQuantityOfCartProductIfExists(currentProduct, categoryIndex);
                            GInterface.CartList = controller.GetCart();
                        }
                        //if product does not exist in cart, create it
                        else
                        {
                            //integrated logic prohibits quantity increasing if product alreadt exists
                            if (controller.IncreaseQuantityOfCartProductIfExists(currentProduct, categoryIndex))
                            {
                                controller.AddToCart(currentProduct, categoryIndex);
                                GInterface.CartList = controller.GetCart();
                            }
                            GInterface.ShoppingCartProductCounter.Add(1);
                            GInterface.ShoppingCartList.Add(currentProduct);
                        }
                        Console.WriteLine();
                    }
                    break;

                case ConsoleKey.Tab:    //show/hide cart
                    do
                    {
                        GInterface.ShowCart();
                        Console.CursorLeft = 0;
                        key = Console.ReadKey(true);

                        /*
                         * if (key.Key == ConsoleKey.Spacebar)
                         * {
                         *  GInterface.ShowCartInTableForm(GInterface.CartList, controller.GetThePriceOfAllProductsInCart());
                         * }*/
                    } while (key.Key != ConsoleKey.Tab && key.Key != ConsoleKey.Escape);
                    Console.Clear();
                    GInterface.PrintProductsPageHeadder(shoppingCartTotal, optionIndex);
                    GInterface.PrintProductsFormated(optionIndex);
                    ObjectSelections.SelectCurrentProductAt(productIndex, optionIndex);
                    break;
                }
                key = Console.ReadKey(true);
            }
            Console.Clear();
            return;
        }