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"); } } }
/// <summary> /// Handles user commands /// </summary> /// <param name="commandArray"></param> private void ParseUserCommand(string[] commandArray) { switch (commandArray.Length) { case 1: _ui.DisplayUserInfo(_s.GetUserByUsername(commandArray[0])); break; case 2: _ui.DisplayUserBuysProduct( _s.BuyProduct(_s .GetUserByUsername(commandArray[0]), _s .GetProductByID(int.Parse(commandArray[1])))); break; case 3: _ui.DisplayUserBuysProduct(int.Parse(commandArray[1]), _s.BuyProduct( _s.GetUserByUsername(commandArray[0]), _s.GetProductByID(int.Parse(commandArray[2])))); int numberOfProducts = int.Parse(commandArray[1]) - 1; for (int i = 1; i < numberOfProducts; i++) { _s.BuyProduct( _s.GetUserByUsername(commandArray[0]), _s.GetProductByID(int.Parse(commandArray[2]))); } break; } }
public void AddCreditToUser(string[] args) { if (args[0] != null) { IUser user = stregsystem.GetUserByUsername(args[1]); decimal amount = decimal.Parse(args[2]); user.Balance += amount; Console.WriteLine($"User {user.UserName} now has {user.Balance} credits"); } }
// All the admin commands and what they do public void AddAdminCommands() { AdminCommands.Add(":activate", args => _stregsystem.GetProductByID(int.Parse(args[1])).Active = true); AdminCommands.Add(":addcredits", args => _stregsystem.AddCreditsToAccount(_stregsystem.GetUserByUsername(args[1]), decimal.Parse(args[2]))); AdminCommands.Add(":deactivate", args => _stregsystem.GetProductByID(int.Parse(args[1])).Active = false); AdminCommands.Add(":creditoff", args => _stregsystem.GetProductByID(int.Parse(args[1])).CanBeBoughtOnCredit = false); AdminCommands.Add(":crediton", args => _stregsystem.GetProductByID(int.Parse(args[1])).CanBeBoughtOnCredit = true); AdminCommands.Add(":q", args => _ui.Close()); AdminCommands.Add(":quit", args => _ui.Close()); }
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); } }
private commandType CommandPattern() { if (Regex.IsMatch(_commandString, @"[\w]\s[0-9]\s[0-9]")) { _user = _ss.GetUserByUsername(_commandStrings[0]); _product = _ss.GetProductByID(Int32.Parse(_commandStrings[2])); return(commandType.Multibuy); } if (Regex.IsMatch(_commandString, @"[\w]\s[0-9]")) { _user = _ss.GetUserByUsername(_commandStrings[0]); _product = _ss.GetProductByID(Int32.Parse(_commandStrings[1])); return(commandType.Purchase); } if (Regex.IsMatch(_commandString, @"[\w]")) { _user = _ss.GetUserByUsername(_commandStrings[0]); return(commandType.User); } return(0); }
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); } }
User GetUser(string username) { User user = Stregsystem.GetUserByUsername(username); return(user); }
public void ParseCommand(string command) { command = command.Trim(); string[] commandlist = Regex.Split(command, " "); if (commandlist.Length > 3) // no command takes over 3 arguments { UI.DisplayTooManyArgumentsError(command); } else if (command.Contains(':')) // If command contains ':' it can ONLY be admin command, has its own { AdminstrationCommand(Regex.Split(command, " ")); } else // If command does not contain ':' we know that it should be a buy or user command { if (pointsystem.GetUserByUsername(commandlist[0]) == null) // if user is null, we can disregard the command, as all commands at this point needs a user { UI.DisplayUserNotFound(commandlist[0]); } else // we now now that first element is user, and that it is valid { user = pointsystem.GetUserByUsername(commandlist[0]); if (commandlist.Length == 1) // if lenght is 1, then it should be a User command { UI.DisplayUserInfo(user); } else // the remaining commands can either be quickbuy or multibuy { if (commandlist.Length == 2) // if lenght is 2 it should be a quickbuy command { if (pointsystem.GetProductByID(Int32.Parse(commandlist[1])) == null) { UI.DisplayProductNotFound(commandlist[1]); } else if (pointsystem.GetProductByID(Int32.Parse(commandlist[1])).Active) { product = pointsystem.GetProductByID(Int32.Parse(commandlist[1])); BuyTransaction transaction = pointsystem.BuyProduct(user, product); // returns null if invalid funds if (transaction == null) { UI.DisplayInsufficientCash(user, product); } else { UI.DisplayUserBuysProduct(transaction); } } else { UI.DisplayGeneralError("Product is not active, therefore unable to be purchased"); } } else // if lenght is == 3, we know that it should be a multibuy { if (pointsystem.GetProductByID(Int32.Parse(commandlist[2])) == null) { UI.DisplayProductNotFound(commandlist[2]); } else // Multibuy: first element will be user, second will be count, and third will be productID { product = pointsystem.GetProductByID(Int32.Parse(commandlist[2])); for (int i = 0; i < Double.Parse(commandlist[1]) - 1; i++) { pointsystem.BuyProduct(user, product); } UI.DisplayUserBuysProduct(Int32.Parse(commandlist[1]), pointsystem.BuyProduct(user, product)); } } } } } }
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"); } }); }
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; } }