Beispiel #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;
            }
        }
Beispiel #2
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}");
            }
        }
Beispiel #3
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));
     }
 }
Beispiel #4
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);
        }
        // The transaction is handled here
        public void Receipt(string username, int productID, int quantity)
        {
            User    user;
            Product product;

            // Checks whether a username as the given, exists
            if (_stregsystem.GetUserByUsername(username) != null)
            {
                user = _stregsystem.GetUserByUsername(username);

                // Checks whether a product ID as the given, exists
                if (_stregsystem.GetProductByID(productID) != null)
                {
                    product = _stregsystem.GetProductByID(productID);

                    // Checks whether the product is active
                    if (product.Active)
                    {
                        // Checks if the user can afford the product and the quantity of it, and if not, if the product can be bought on credit
                        if (product.Price * quantity <= user.Balance || product.CanBeBoughtOnCredit)
                        {
                            BuyTransaction purchase = null;

                            for (int i = 0; i < quantity; i++)
                            {
                                purchase = _stregsystem.BuyProduct(user, product);
                            }
                            ConsoleReceipt receipt = new ConsoleReceipt(purchase, quantity, false);
                            receipt.Print();
                        }
                        else
                        {
                            _ui.DisplayInsufficientCash(user, product);
                        }
                    }
                    else
                    {
                        _ui.DisplayProductNotActive($"{product.ID}");
                    }
                }
                else
                {
                    _ui.DisplayProductNotFound();
                }
            }
            else
            {
                _ui.DisplayUserNotFound(username);
            }
        }
        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);
            }
        }
        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);
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
0
        public void ParseCommand(string command)
        {
            command = command.Trim();
            string[] commandlist = Regex.Split(command, " ");

            if (commandlist.Length > 3) // no command takes over 3 arguments
            {
                UI.DisplayTooManyArgumentsError(command);
            }
            else if (command.Contains(':')) // If command contains ':' it can ONLY be admin command, has its own
            {
                AdminstrationCommand(Regex.Split(command, " "));
            }
            else // If command does not contain ':' we know that it should be a buy or user command
            {
                if (pointsystem.GetUserByUsername(commandlist[0]) == null) // if user is null, we can disregard the command, as all commands at this point needs a user
                {
                    UI.DisplayUserNotFound(commandlist[0]);
                }
                else // we now now that first element is user, and that it is valid
                {
                    user = pointsystem.GetUserByUsername(commandlist[0]);
                    if (commandlist.Length == 1) // if lenght is 1, then it should be a User command
                    {
                        UI.DisplayUserInfo(user);
                    }
                    else // the remaining commands can either be quickbuy or multibuy
                    {
                        if (commandlist.Length == 2) // if lenght is 2 it should be a quickbuy command
                        {
                            if (pointsystem.GetProductByID(Int32.Parse(commandlist[1])) == null)
                            {
                                UI.DisplayProductNotFound(commandlist[1]);
                            }
                            else if (pointsystem.GetProductByID(Int32.Parse(commandlist[1])).Active)
                            {
                                product = pointsystem.GetProductByID(Int32.Parse(commandlist[1]));
                                BuyTransaction transaction = pointsystem.BuyProduct(user, product); // returns null if invalid funds
                                if (transaction == null)
                                {
                                    UI.DisplayInsufficientCash(user, product);
                                }
                                else
                                {
                                    UI.DisplayUserBuysProduct(transaction);
                                }
                            }
                            else
                            {
                                UI.DisplayGeneralError("Product is not active, therefore unable to be purchased");
                            }
                        }
                        else // if lenght is == 3, we know that it should be a multibuy
                        {
                            if (pointsystem.GetProductByID(Int32.Parse(commandlist[2])) == null)
                            {
                                UI.DisplayProductNotFound(commandlist[2]);
                            }
                            else // Multibuy: first element will be user, second will be count, and third will be productID
                            {
                                product = pointsystem.GetProductByID(Int32.Parse(commandlist[2]));
                                for (int i = 0; i < Double.Parse(commandlist[1]) - 1; i++)
                                {
                                    pointsystem.BuyProduct(user, product);
                                }
                                UI.DisplayUserBuysProduct(Int32.Parse(commandlist[1]), pointsystem.BuyProduct(user, product));
                            }
                        }
                    }
                }
            }
        }
        private void Purchase()
        {
            var transaction = _ss.BuyProduct(_user, _product);

            _ui.DisplayUserBuysProduct(transaction);
        }
        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();
        }