Exemplo n.º 1
0
        void ParseCommand(string command)
        {
            if (!string.IsNullOrEmpty(command))
            {
                command = command.ToLower();
                List <string> inputs = command.Split(new char[] { ' ' }).ToList();

                try
                {
                    if (command[0] == ':')
                    {
                        try
                        {
                            _admincommands[inputs[0]](inputs.ToList());
                        }
                        catch (KeyNotFoundException)
                        {
                            _stregsystemUI.DisplayAdminCommandNotFoundMessage(inputs.First());
                        }
                    }
                    else
                    {
                        HandleUserCMD(inputs);
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    _stregsystemUI.DisplayArgumentCountError(inputs.ElementAt(0));
                }
            }
            else
            {
                _stregsystemUI.DisplayGeneralError("Ugyldigt input");
            }
        }
Exemplo n.º 2
0
        private void ParseCommand(string command)
        {
            string[] split = command.Split(' ');
            if (command.StartsWith(":"))
            {
                try
                {
                    _adminCommands[split[0]].Invoke(split);
                }
                catch (KeyNotFoundException)

                {
                    SUI.DisplayAdminCommandNotFoundMessage($"{split[0]}");
                }
            }
            else
            {
                if (split.Length == 3) //Multi BUY
                {
                    MultiBuyProduct(S.GetUserByUsername(split[0]), S.GetProductByID(Convert.ToInt32(split[1])), Convert.ToInt32(split[2]));
                }
                else if (split.Length == 2) //Normal
                {
                    BuyProduct(S.GetUserByUsername(split[0]), S.GetProductByID(Convert.ToInt32(split[1])));
                }
                else if (split.Length == 1) //User info
                {
                    TypedInUsername(S.GetUserByUsername(split[0]));
                }
                else if (split.Length > 3)
                {
                    throw new TooManyArgumentsException($"{split.Length} arguments entered, a maximum of 3 expected");
                }
            }
        }
Exemplo n.º 3
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 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();
                    }
                }
            }
        }
        /// <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);
            }
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
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;
            }
        }