void InsertMoney <AdminMethod>(string nula, string username, string amount) { try { User user = GetUser(username); decimal _amount = Convert.ToDecimal(amount); InsertCashTransaction tr = Stregsystem.AddCreditsToAccount(user, _amount); UI.DisplayUserInfo(user); } catch (UserNotFoundException e) { UI.DisplayUserNotFound(e.username); } catch (ArgumentNullException) { UI.DisplayUserNotFound(username); } catch (FormatException) { UI.DisplayQuantityMustBeANumber(); } catch (OverflowException) { UI.DisplayQuantityMustBeANumber(); } catch (Exception e) { UI.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; } }
void HandleUserCommand(string username) { try { User user = _stregsystem.GetUserByUsername(username); IEnumerable <Transaction> transactions = _stregsystem.GetTransactions(user, 10); _stregsystemUI.DisplayUserInfo(user.ToString() + $"Saldo: {user.Balance * 0.01f}"); foreach (Transaction t in transactions) { _stregsystemUI.DisplayTransaction(t); } } catch (UserNotFoundException) { _stregsystemUI.DisplayUserNotFound(username); } }
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."); } }
/// <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); } }
public bool UserSucess(string username) { bool res = false; try { ss.GetUserByUsername(username); res = true; } catch (System.Exception) { ui.DisplayUserNotFound(username); } return(res); }
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); } } }
//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 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); } }
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; } }