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.");
            }
        }
Ejemplo n.º 2
0
        public void ParseCommand(object source, EventArgs args, string command)
        {
            command = command.Trim(); // fjerner whitesplace fra start og s**t
            if (command.StartsWith(":"))
            {
                AdminCommand(command);
            }
            else
            {
                string[] parameters = command.Split(' ');

                if (parameters.Length == 1)
                {
                    FindUserInfo(parameters[0]);
                }
                else if (parameters.Length == 2)
                {
                    BuyOneItem(parameters);
                }
                else if (parameters.Length == 3)
                {
                    MultibuyItem(parameters);
                }
                else
                {
                    _ui.DisplayTooManyArgumentsError(command);
                }
            }
        }
        // The one input is handled here, splitted and putted into a stringarray
        public void HandleInput(string command)
        {
            string[] commandSplitted = null;
            User     user            = _stregsystem.GetUserByUsername(command);

            //Checks if the command is empty
            if (command != null)
            {
                commandSplitted = command.Split(' ');

                // Checks if the command if it starts with ":", if <true>, it is an admin command.
                if (command.StartsWith(":"))
                {
                    try
                    {
                        AdminCommands[commandSplitted[0]](commandSplitted);
                    }
                    catch (Exception)
                    {
                        _ui.DisplayAdminCommandNotFoundMessage();
                    }
                }
                // If it is not an admin command, it should be handled differently.
                else
                {
                    if (commandSplitted.Length == 1)
                    {
                        commandParse01(command, user);
                    }
                    else if (commandSplitted.Length == 2)
                    {
                        commandParse02(commandSplitted);
                    }
                    else
                    {
                        _ui.DisplayTooManyArgumentsError();
                    }
                }
            }
        }
Ejemplo n.º 4
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!");
            }
        }
Ejemplo n.º 5
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);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Parses a string into a commandcall from _adminCommands or _userCommands
        /// </summary>
        /// <param name="command"></param>
        public void ParseCommand(string command)
        {
            //Opret et stringarray til at holde de tre parametre som en kommando kan indeholde
            string[] parts = new string[MAX_ALLOWED_ARGUMENTS];

            string[] countedParts = command.Split(" ");

            if (countedParts.Length <= MAX_ALLOWED_ARGUMENTS)
            {
                int i = 0;
                foreach (string s in countedParts)
                {
                    parts[i] = s;
                    i++;
                }
                //fyld resten af argumanterne med ""
                for (; i < MAX_ALLOWED_ARGUMENTS; i++)
                {
                    parts[i] = "";
                }
            }
            else
            {
                UI.DisplayTooManyArgumentsError(command);
                return;
                //throw new ToManyArgumentsException();
            }

            //Kald den parsede command  //split evt i to funktioner
            try
            {
                if (isAdminCommand(parts[0]))
                {
                    _adminCommands[parts[0]]?.DynamicInvoke(parts[0], parts[1], parts[2]);
                }
                else
                {
                    switch (countedParts.Length)
                    {
                    case 1:
                        _userCommands["getUser"]?.DynamicInvoke(parts[0], parts[1], parts[2]);
                        break;

                    case 2:
                        _userCommands["buyProduct"]?.DynamicInvoke(parts[0], parts[1], parts[2]);
                        break;

                    case 3:
                        _userCommands["BuyProductQuantity"]?.DynamicInvoke(parts[0], parts[1], parts[2]);
                        break;

                    default:
                        UI.DisplayTooManyArgumentsError(command);
                        break;
                    }
                }
            }
            catch (KeyNotFoundException)
            {
                UI.DisplayAdminCommandNotFoundMessage(command);
            }
        }
Ejemplo n.º 7
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();
        }