Exemple #1
0
        private void InvokeUserCommand(string commandString)
        {
            UserCommand command = new UserCommand(commandString, DashSystem);

            if (command.IsUserInfo())
            {
                UI.DisplayUserInfo(command.User);
            }
            else if (command.IsSinglePurchase())
            {
                BuyTransaction transaction = DashSystem.CreateBuyTransaction(command.User, command.Product);
                DashSystem.ExecuteTransaction(transaction);
                UI.DisplayUserBuysProduct(transaction);
            }
            else if (command.IsMultiPurchase())
            {
                BuyMultipleProducts(command);
            }
            else
            {
                UI.DisplayTooManyArgumentsError(command.CommandString);
            }
        }
Exemple #2
0
        private void BuyMultipleProducts(UserCommand command)
        {
            BuyTransaction testTransaction  = DashSystem.CreateBuyTransaction(command.User, command.Product);
            decimal        transactionPrice = Math.Abs(testTransaction.Amount) * command.PurchaseAmount;

            if (transactionPrice < testTransaction.User.Balance || testTransaction.Product.CanBeBoughtOnCredit)
            {
                List <BuyTransaction> transactions = new List <BuyTransaction>();
                for (int i = 0; i < command.PurchaseAmount; i++)
                {
                    BuyTransaction transaction = DashSystem.CreateBuyTransaction(command.User, command.Product);
                    transactions.Add(transaction);
                }

                transactions.ForEach(x => x.Execute());
                UI.DisplayUserBuysMultipleProducts(command.PurchaseAmount, transactions[0]);
            }
            else
            {
                throw new InsufficientCreditsException(
                          $"{testTransaction.User.Username} does not have enough credits ({testTransaction.User.Balance}) " +
                          $"to buy {command.PurchaseAmount} {testTransaction.Product.Name}. Price: {transactionPrice}");
            }
        }