Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Command that runs when the event is raised from stregsystemCLI
        /// It splits the command and calls either user commands or admin commands.
        /// Exceptions are handled at the buttom
        /// </summary>
        /// <param name="command"></param>
        private void ParseCommand(string command)
        {
            string[] commandArray = command.Split(' ');

            if (string.IsNullOrEmpty(command))
            {
                return;
            }
            else if (commandArray.Length > 3)
            {
                _ui.DisplayTooManyArgumentsError(command);
                return;
            }

            try
            {
                if (command[0].Equals(':'))
                {
                    ParseAdminCommand(commandArray);
                }
                else
                {
                    ParseUserCommand(commandArray);
                }
            }
            catch (InsufficientCreditsException e)
            {
                _ui.DisplayInsufficientCash(e.TransactionUser, e.ProductToBuy);
            }
            catch (UserNotFoundException)
            {
                _ui.DisplayUserNotFound(commandArray.Length == 1 ? commandArray[0] : commandArray[1]);
            }
            catch (ProductNotFoundException)
            {
                _ui.DisplayProductNotFound(commandArray.Length == 2 ? commandArray[1] : commandArray[2]);
            }
            catch (KeyNotFoundException)
            {
                _ui.DisplayAdminCommandNotFoundMessage(commandArray[0]);
            }
            catch (FormatException)
            {
                _ui.DisplayGeneralError($"{command} must consist of either a productid or amount of buys");
            }
            catch (OverflowException)
            {
                _ui.DisplayGeneralError($"The numbers in: {command} must not be that high!");
            }
        }
        // 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);
            }
        }
Exemplo n.º 4
0
        public bool ProductSuccess(int id)
        {
            bool res = false;

            try
            {
                ss.GetProductByID(id);
                res = true;
            }
            catch (System.Exception)
            {
                ui.DisplayProductNotFound(id.ToString());
            }
            return(res);
        }
Exemplo n.º 5
0
        private void CommandParser(string commandStrings)
        {
            _commandString  = commandStrings;
            _commandStrings = _commandString.Split(' ');

            if (commandStrings[0].Equals(':'))
            {
                Console.WriteLine("[ADMIN]");
                AdminCommand();
            }
            else
            {
                try
                {
                    switch (CommandPattern())
                    {
                    case commandType.User:
                        DisplayUser();
                        break;

                    case commandType.Purchase:
                        Purchase();
                        break;

                    case commandType.Multibuy:
                        MultiBuy();
                        break;
                    }
                }
                catch (UserNotFoundException e)
                {
                    _ui.DisplayUserNotFound(e.UserName);
                }
                catch (ProductNotFoundException e)
                {
                    _ui.DisplayProductNotFound(e.Product);
                }
                catch (InsufficientCreditsException e)
                {
                    _ui.DisplayInsufficientCash(e.User, e.Product);
                }

                catch (Exception e)
                {
                    _ui.DisplayGeneralError(e.Message);
                }
            }
        }
Exemplo n.º 6
0
 //Handle most of the throws
 private void Controller(string command)
 {
     try
     {
         ParseCommand(command);
     }
     catch (NonExistingUserException e)
     {
         SUI.DisplayUserNotFound(e.Message);
     }
     catch (NonExistingProductException e)
     {
         SUI.DisplayProductNotFound(e.Message);
     }
     catch (InsufficientCreditsException e)
     {
         SUI.DisplayGeneralError(e.Message);
     }
     catch (NotActiveProductException e)
     {
         SUI.DisplayGeneralError(e.Message);
     }
     catch (FormatException e)
     {
         SUI.DisplayGeneralError(e.Message);
     }
     catch (IndexOutOfRangeException)
     {
         SUI.DisplayGeneralError($"Not enough arguments were entered");
     }
     catch (TooManyArgumentsException e)
     {
         SUI.DisplayTooManyArgumentsError(e.Message);
     }
     catch (Exception e)
     {
         SUI.DisplayGeneralError(e.Message);
     }
 }
        void Activate <AdminMethod>(string activate, string productID, string nulc)
        {
            bool isActivated = activate == ":activate" ? true : false;

            try
            {
                int     id = Convert.ToInt32(productID);
                Product p  = Stregsystem.GetProductByID(id: id);
                p.Active = isActivated;
                UI.DisplayProducts(Stregsystem.ActiveProducts);
            }
            catch (FormatException)
            {
                UI.DisplayProductNotFound(productID);
            }
            catch (ProductNotFoundException)
            {
                UI.DisplayProductNotFound(productID);
            }
            catch (Exception)
            {
                UI.DisplayProductNotFound(productID);
            }
        }
Exemplo n.º 8
0
        private void AdminCommand(string commandparameters)
        {
            string[] parameters = commandparameters.Split(' ');
            string   command    = parameters[0];



            parameters[0] = parameters[0].ToLower();
            Dictionary <string, Action> adminCommands = new Dictionary <string, Action>();

            switch (parameters.Length)
            {
            case 1:
                adminCommands.Add(":q", () => _ui.Close());
                adminCommands.Add(":quit", () => _ui.Close());
                adminCommands.Add(":help", () => _ui.ShowHelp());
                if (adminCommands.ContainsKey(command))
                {
                    adminCommands[command]();
                }
                else
                {
                    _ui.DisplayAdminCommandNotFoundMessage(commandparameters);
                }
                break;

            case 2:
                int id = int.Parse(parameters[1]);
                adminCommands.Add(":active", () => SetProductActive(id, true));
                adminCommands.Add(":deactive", () => SetProductActive(id, false));
                adminCommands.Add(":crediton", () => SetProductCredit(id, true));
                adminCommands.Add(":creditoff", () => SetProductCredit(id, false));
                Product product = _stregsystem.GetProductByID(id);
                if (product != null)
                {
                    if (adminCommands.ContainsKey(command))
                    {
                        adminCommands[command]();
                    }
                    else
                    {
                        _ui.DisplayAdminCommandNotFoundMessage(commandparameters);
                    }
                }
                else
                {
                    _ui.DisplayProductNotFound(id.ToString());
                }
                break;

            case 3:
                string userName = parameters[1];
                int    credit   = int.Parse(parameters[2]);
                adminCommands.Add(":addcredits", () => InsertCashTransaction(userName, credit));
                User user = _stregsystem.GetUserByUsername(userName);
                if (user != null)
                {
                    if (adminCommands.ContainsKey(command))
                    {
                        adminCommands[command]();
                    }
                    else
                    {
                        _ui.DisplayAdminCommandNotFoundMessage(commandparameters);
                    }
                }
                else
                {
                    _ui.DisplayUserNotFound(userName);
                }
                break;

            default:
                _ui.DisplayAdminCommandNotFoundMessage(commandparameters);
                break;
            }
        }