Exemplo n.º 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();
        }
Exemplo n.º 2
0
        public void GetTransactionsForUser_ReturnsCorrectTransactions_True()
        {
            IBarcodeSystem barcodeSystem = Substitute.For <IBarcodeSystem>();
            User           user          = Substitute.For <User>(userArgs);
            Product        product       = Substitute.For <Product>(productArgs);

            user.Balance = 1000m;
            const int amountOfPurchases = 5;

            for (int i = 0; i < amountOfPurchases; i++)
            {
                barcodeSystem.BuyProduct(user, product);
            }
            IEnumerable <Transaction> transactionsForUser =
                barcodeSystem.GetTransactionsForUser(user, amountOfPurchases);

            Assert.That(transactionsForUser, Is.All.Property("User").EqualTo(user));
        }