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"); } }
/// <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!"); } }
// If command consists of one word public void commandParse01(string command, User user) { // If the word is "multi" if (command == "multi") { MultiBuyProducts01(); } // If the word contains a username else if (user != null) { if (command == user.Username) { UserInfo(command); } } else { _ui.DisplayGeneralError(); } }
//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); } }
public void HandleUserInput(List <string> commandInputs) { if ((commandInputs.Count == 1) || (Regex.IsMatch(commandInputs[1], @"^[1-9 ]\d*$"))) { switch (commandInputs.Count) { case 1: { User user = null; try { user = _stregsystem.GetUserByUsername(commandInputs[0]); } catch (UserNotFoundException) { _stregystemui.DisplayUserNotFound(commandInputs[0]); return; } _stregystemui.DisplayUserInfo(user); _stregystemui.DisplayPastTransactions(user); break; } case 2: { HandlePurchase(commandInputs[0], int.Parse(commandInputs[1]), 1); break; } case 3: { HandlePurchase(commandInputs[0], int.Parse(commandInputs[1]), int.Parse(commandInputs[2])); break; } default: { _stregystemui.DisplayGeneralError("Der skete en fejl, tjek om syntax er korrekt."); break; } } } else { _stregystemui.DisplayuProductNotFound(commandInputs[1]); return; } }
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); } } }
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}"); } }
private void Quit <AdminMethod>(string nula, string nulb, string nulc) { try { Stregsystem.WriteToLogFile(); UI.Close(); return; } catch (System.IO.IOException e) { UI.DisplayGeneralError(e.Message); } catch { //hvis der ikke er noget at gøre så luk ui og crash UI.Close(); throw new Exception(); } return; }
public void FillAdminCommands() { _adminCommands.Add(":q", () => ui.Close()); _adminCommands.Add(":quit", () => ui.Close()); _adminCommands.Add(":activate", () => { try { if (ProductSuccess(int.Parse(_command.Split(" ")[1]))) { Product p = ss.GetProductByID(int.Parse(_command.Split(" ")[1])); if (p is SeasonalProduct) { ui.DisplayGeneralError($"{p.name} is a seasonal product"); return; } p.active = true; Console.WriteLine($"{p.name} has been activated"); } } catch (FormatException) { ui.DisplayGeneralError($"{_command.Split(" ")[1]} is not a positive integer"); } }); _adminCommands.Add(":deactivate", () => { try { if (ProductSuccess(int.Parse(_command.Split(" ")[1]))) { Product p = ss.GetProductByID(int.Parse(_command.Split(" ")[1])); if (p is SeasonalProduct) { ui.DisplayGeneralError($"{p.name} is a seasonal product"); return; } p.active = false; Console.WriteLine($"{p.name} has been deactivated"); } } catch (FormatException) { ui.DisplayGeneralError($"{_command.Split(" ")[1]} is not a positive integer"); } }); _adminCommands.Add(":crediton", () => { try { if (ProductSuccess(int.Parse(_command.Split(" ")[1]))) { Product p = ss.GetProductByID(int.Parse(_command.Split(" ")[1])); if (p is SeasonalProduct) { ui.DisplayGeneralError($"{p.name} is a seasonal product"); return; } p.canBeBoughtOnCredit = true; Console.WriteLine($"{p.name} can now be bought on credit"); } } catch (FormatException) { ui.DisplayGeneralError($"{_command.Split(" ")[1]} is not a positive integer"); } }); _adminCommands.Add(":creditoff", () => { try { if (ProductSuccess(int.Parse(_command.Split(" ")[1]))) { Product p = ss.GetProductByID(int.Parse(_command.Split(" ")[1])); if (p is SeasonalProduct) { ui.DisplayGeneralError($"{p.name} is a seasonal product"); return; } p.canBeBoughtOnCredit = false; Console.WriteLine($"{p.name} can now not be bought on credit"); } } catch (FormatException) { ui.DisplayGeneralError($"{_command.Split(" ")[1]} is not a positive integer"); } }); _adminCommands.Add(":addcredits", () => { try { if (UserSucess(_command.Split(" ")[1])) { User u = ss.GetUserByUsername(_command.Split(" ")[1]); ss.LogTransaction(ss.AddCreditsToAccount(u, int.Parse(_command.Split(" ")[2])), @"C:\Users\T-Phamz\Desktop\test.txt"); Console.WriteLine($"{_command.Split(" ")[2]} credits has been added to {u.username}"); } } catch (SystemException) { ui.DisplayGeneralError($"{_command.Split(" ")[2]} is not a positive integer"); } }); }