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 BuyProduct(User user, Product product) { if (!product.Active) { throw new NotActiveProductException($"This product is not active: {product.Name}"); } if (user.Balance < product.Price && !product.CanBeBoughtOnCredit) { SUI.DisplayInsufficientCash(user, product, 1); } else { SUI.DisplayUserBuysProduct(S.BuyProduct(user, product)); } }
/// <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); } }
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); } } }
void BuyProduct <AdminMethod>(string username, string prodID, string nulc) { User user; Product product; try { user = GetUser(username); int id = Convert.ToInt32(prodID); product = Stregsystem.GetProductByID(id); BuyTransaction tr = Stregsystem.BuyProduct(user, product); UI.DisplayUserBuysProduct(tr); } catch (UserNotFoundException e) { UI.DisplayUserNotFound(e.username); } catch (InsufficientCreditsException e) { UI.DisplayInsufficientCash(e.user, e.product); } catch (ProductNotActivatedException e) { UI.DisplayProductNotActivated(e.product); } catch (ProductNotFoundException) { UI.DisplayProductNotFound(prodID); } catch (FormatException) { UI.DisplayQuantityMustBeANumber(); } catch (OverflowException) { UI.DisplayQuantityMustBeANumber(); } catch (Exception e) { UI.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); } }
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(); }