Example #1
0
        /// <summary>
        /// Handles user commands
        /// </summary>
        /// <param name="commandArray"></param>
        private void ParseUserCommand(string[] commandArray)
        {
            switch (commandArray.Length)
            {
            case 1:
                _ui.DisplayUserInfo(_s.GetUserByUsername(commandArray[0]));
                break;

            case 2:
                _ui.DisplayUserBuysProduct(
                    _s.BuyProduct(_s
                                  .GetUserByUsername(commandArray[0]), _s
                                  .GetProductByID(int.Parse(commandArray[1]))));
                break;

            case 3:
                _ui.DisplayUserBuysProduct(int.Parse(commandArray[1]), _s.BuyProduct(
                                               _s.GetUserByUsername(commandArray[0]),
                                               _s.GetProductByID(int.Parse(commandArray[2]))));

                int numberOfProducts = int.Parse(commandArray[1]) - 1;

                for (int i = 1; i < numberOfProducts; i++)
                {
                    _s.BuyProduct(
                        _s.GetUserByUsername(commandArray[0]),
                        _s.GetProductByID(int.Parse(commandArray[2])));
                }
                break;
            }
        }
        public void ParseCommand(string command)
        {
            string[] commandParts = command.Split(' ');

            if (command != string.Empty)
            {
                if (command[0] == ':')
                {
                    ExecuteAdminCommand(commandParts);
                }
                else if (ValidCommand(commandParts))
                {
                    try
                    {
                        if (commandParts.Count() == 3)
                        {
                            ui.DisplayUserBuysProduct(stregsystem.BuyProduct(Convert.ToInt32(commandParts[2]), Convert.ToInt32(commandParts[1]), commandParts[0]));
                        }
                        else if (commandParts.Count() == 2)
                        {
                            ui.DisplayUserBuysProduct(stregsystem.BuyProduct(Convert.ToInt32(commandParts[1]), commandParts[0]));
                        }
                        else if (commandParts.Count() == 1)
                        {
                            ui.DisplayUserInfo(stregsystem.GetUser(commandParts[0]));
                        }
                        else
                        {
                            ui.DisplayTooManyArgumentsError();
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is UserNotFoundException)
                        {
                            ui.DisplayUserNotFound(commandParts[0]);
                        }
                        else if (e is InsufficientCreditsException)
                        {
                            ui.DisplayError("Insufficient funds.");
                        }
                    }
                }
                else
                {
                    ui.DisplayError("Not a valid command");
                }
            }
            else
            {
                ui.DisplayError("You must type a command.");
            }
        }
Example #3
0
        private void BuyOneItem(string[] parameters)
        {
            string userName = parameters[0];
            string id       = parameters[1];

            if (id.All(c => char.IsDigit(c)))
            {
                User user = _stregsystem.GetUserByUsername(userName);
                if (user != null)
                {
                    Product product = _stregsystem.GetProductByID(int.Parse(id));
                    if (product != null)
                    {
                        try
                        {
                            if (product.IsActive)
                            {
                                BuyTransaction bt = _stregsystem.BuyProduct(user, product);
                                _ui.DisplayUserBuysProduct(bt);
                            }
                            else
                            {
                                _ui.DisplayProductNotFound(id);
                            }
                        }
                        catch (InsufficentCreditsException)
                        {
                            _ui.DisplayInsufficientCash(user, product);
                        }
                        catch (Exception message)
                        {
                            _ui.DisplayGeneralError(message.Message);
                        }
                    }
                    else
                    {
                        _ui.DisplayProductNotFound(id);
                    }
                }
                else
                {
                    _ui.DisplayUserNotFound(userName);
                }
            }
            else
            {
                _ui.DisplayGeneralError($"Product id was not a number, you wrote: {id}");
            }
        }
Example #4
0
 private void BuyProduct(User user, Product product)
 {
     if (!product.Active)
     {
         throw new NotActiveProductException($"This product is not active: {product.Name}");
     }
     if (user.Balance < product.Price && !product.CanBeBoughtOnCredit)
     {
         SUI.DisplayInsufficientCash(user, product, 1);
     }
     else
     {
         SUI.DisplayUserBuysProduct(S.BuyProduct(user, product));
     }
 }
Example #5
0
        public void UserBuysProduct(string username, string productId)
        {
            IUser        user    = stregsystem.GetUserByUsername(username);
            IProductBase product = stregsystem.GetProductById(int.Parse(productId));
            decimal      cost    = product.Price;

            ui.DisplayUserBuysProduct(user, product);
            stregsystem.BuyProduct(user, product, cost);
        }
Example #6
0
        /// <summary>
        /// Handles a user purchase based on the given product ID
        /// </summary>
        /// <param name="username"></param>
        /// <param name="productID"></param>
        /// <param name="productAmount"></param>
        public void HandlePurchase(string username, int productID, int productAmount)
        {
            User    user = null;
            Product product;

            try
            {
                user = _stregsystem.GetUserByUsername(username);
            }
            catch (UserNotFoundException)
            {
                _stregystemui.DisplayUserNotFound(username);
                return;
            }
            product = _stregsystem.GetProductByID(productID);

            BuyTransaction userTransaction = null;

            try
            {
                userTransaction = _stregsystem.BuyProduct(user, product);
            }
            catch (ProductInactiveException)
            {
                _stregystemui.DisplayuProductNotFound(Convert.ToString(product.ID));
                return;
            }
            if (productAmount == 1)
            {
                _stregystemui.DisplayUserBuysProduct(userTransaction);
            }
            else
            {
                _stregystemui.DisplayUserBuysProduct(productAmount, userTransaction);
            }
        }
        void BuyProduct <AdminMethod>(string username, string prodID, string nulc)
        {
            User    user;
            Product product;

            try
            {
                user = GetUser(username);
                int id = Convert.ToInt32(prodID);
                product = Stregsystem.GetProductByID(id);
                BuyTransaction tr = Stregsystem.BuyProduct(user, product);
                UI.DisplayUserBuysProduct(tr);
            }
            catch (UserNotFoundException e)
            {
                UI.DisplayUserNotFound(e.username);
            }
            catch (InsufficientCreditsException e)
            {
                UI.DisplayInsufficientCash(e.user, e.product);
            }
            catch (ProductNotActivatedException e)
            {
                UI.DisplayProductNotActivated(e.product);
            }
            catch (ProductNotFoundException)
            {
                UI.DisplayProductNotFound(prodID);
            }
            catch (FormatException)
            {
                UI.DisplayQuantityMustBeANumber();
            }
            catch (OverflowException)
            {
                UI.DisplayQuantityMustBeANumber();
            }
            catch (Exception e)
            {
                UI.DisplayGeneralError(e.Message);
            }
        }
Example #8
0
        void HandlePurchase(string username, int productId, int cnt)
        {
            User    user;
            Product product;

            try
            {
                user    = _stregsystem.GetUserByUsername(username);
                product = _stregsystem.GetProductByID(productId);
                for (int i = 0; i < cnt; i++)
                {
                    try
                    {
                        BuyTransaction buyTransaction = _stregsystem.BuyProduct(user, product);
                        _stregsystemUI.DisplayUserBuysProduct(buyTransaction);
                    }
                    catch (InaktivProductPurchaseExceptions)
                    {
                        _stregsystemUI.DisplayProductInactive(productId.ToString());
                        break;
                    }
                    catch (InsufficientCreditsException)
                    {
                        _stregsystemUI.DisplayInsufficientCash(user, product);
                        break;
                    }
                }
            }
            catch (ProductNotFoundException ex)
            {
                _stregsystemUI.DisplayProductNotFound(productId.ToString());
                Debug.WriteLine(ex);
            }
            catch (UserNotFoundException ex)
            {
                _stregsystemUI.DisplayUserNotFound(username);
                Debug.WriteLine(ex);
            }
        }
        private void Purchase()
        {
            var transaction = _ss.BuyProduct(_user, _product);

            _ui.DisplayUserBuysProduct(transaction);
        }
Example #10
0
        public void ParseCommand()
        {
            _command = Console.ReadLine();
            string[] command = _command.Split(" ");

            if (command[0].StartsWith(":"))
            {
                bool success = false;
                if (_adminCommands.ContainsKey(command[0]))
                {
                    _adminCommands[command[0]].Invoke();
                    success = true;
                }
                if (success == false)
                {
                    ui.DisplayAdminCommandNotFoundMessage(_command);
                }
            }
            else if (command.Length == 2)
            {
                try
                {
                    string user      = command[0];
                    int    productID = int.Parse(command[1]);
                    if (ProductSuccess(productID) && UserSucess(user))
                    {
                        BuyTransaction buy = ss.BuyProduct(ss.GetUserByUsername(user), ss.GetProductByID(productID));
                        ss.LogTransaction(buy, @"C:\Users\T-Phamz\Desktop\test.txt");
                        ui.DisplayUserBuysProduct(buy);
                        ss.OnUserBalanceWarning(ss.GetUserByUsername(user));
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine($"{(command[1])} is not a positive integer");
                }
                catch (InsufficientCreditsException)
                {
                    ui.DisplayInsufficientCash(ss.GetUserByUsername(command[0]), ss.GetProductByID(int.Parse(command[1])));
                }
                catch (ProductNotActiveException ex)
                {
                    ui.DisplayGeneralError(ex.Message);
                }
            }
            else if (command.Length == 3)
            {
                try
                {
                    string user          = command[0];
                    int    numberOfItems = int.Parse(command[1]);
                    int    productID     = int.Parse(command[2]);
                    if (ProductSuccess(productID) && UserSucess(user) && numberOfItems > 0)
                    {
                        BuyTransaction buy = null;
                        for (int i = 0; i < numberOfItems; i++)
                        {
                            buy = ss.BuyProduct(ss.GetUserByUsername(user), ss.GetProductByID(productID));
                            ss.LogTransaction(buy, @"C:\Users\T-Phamz\Desktop\test.txt");
                        }
                        ui.DisplayUserBuysProduct(numberOfItems, buy);
                        ss.OnUserBalanceWarning(ss.GetUserByUsername(user));
                    }
                    else
                    {
                        ui.DisplayGeneralError($"Number of items bought ({command[1]}) can not be under 1");
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine($"{(command[1])} or {(command[2])} " +
                                      $"is not a number");
                }
                catch (InsufficientCreditsException)
                {
                    ui.DisplayInsufficientCash(ss.GetUserByUsername(command[0]), ss.GetProductByID(int.Parse(command[1])));
                }
                catch (ProductNotActiveException ex)
                {
                    ui.DisplayGeneralError(ex.Message);
                }
            }
            else if (command.Length == 1)
            {
                if (UserSucess(command[0]))
                {
                    User u = ss.GetUserByUsername(command[0]);
                    ui.DisplayUserInfo(u);
                    foreach (Transaction item in ss.GetTransactions(u, 10))
                    {
                        ui.DisplayTransactions(item);
                    }
                    ss.OnUserBalanceWarning(u);
                }
            }
            else
            {
                ui.DisplayTooManyArgumentsError($"\"{_command}\"");
            }
            Console.WriteLine();
        }