Пример #1
0
        public override void Execute()
        {
            if (!HasEnoughArguments(2))
            {
                return;
            }

            string  username         = Command[0];
            string  productIdString  = Command[1];
            int     amountToPurchase = 1;
            Product product;
            User    user;

            if (Command.Length == 3 && !int.TryParse(Command[2], out amountToPurchase))
            {
                barcodeCli.DisplayGeneralError($"{Command[2]} is not a valid amount.");
                return;
            }

            try
            {
                user = barcodeSystem.GetUserByUsername(username);
                if (uint.TryParse(productIdString, out uint productId))
                {
                    try
                    {
                        product = barcodeSystem.GetProductById(productId);
                        try
                        {
                            BuyTransaction transaction = barcodeSystem.BuyProduct(user, product, amountToPurchase);

                            this.transaction = transaction;
                            Succeeded        = transaction.Succeeded;

                            barcodeCli.DisplayUserBuysProduct(transaction);
                        }
                        catch (InsufficientCreditsException)
                        {
                            barcodeCli.DisplayInsufficientCash(user, product);
                        }
                    }
                    catch (ProductNotFoundException)
                    {
                        barcodeCli.DisplayProductNotFound(productIdString);
                    }
                }
            }
            catch (UserNotFoundException)
            {
                barcodeCli.DisplayUserNotFound(username);
            }

            base.Execute();
        }
Пример #2
0
        protected bool TryGetProduct()
        {
            if (!HasEnoughArguments(2))
            {
                barcodeCli.DisplayNotEnoughArguments(Command);
                return(false);
            }

            string productIdString = Command[1];

            try
            {
                uint productId = Convert.ToUInt32(productIdString);
                product = barcodeSystem.GetProductById(productId);
            }
            catch (Exception)
            {
                barcodeCli.DisplayProductNotFound(productIdString);
                return(false);
            }

            return(true);
        }