//Execute user commands, using the amount of arguments to decide what command to run private void ParseCommand(string command) { var commandArguments = command.Split(' '); int productId; int amount; bool converted; switch (commandArguments.Length) { case 1: UserInformation(commandArguments[0].Trim()); break; case 2: converted = Int32.TryParse(commandArguments[1].Trim(), out productId); if (converted) { BuyProduct(commandArguments[0].Trim(), Convert.ToInt32(commandArguments[1].Trim())); } else { _ui.DisplayGeneralError("Invalid product id"); } break; case 3: converted = Int32.TryParse(commandArguments[1].Trim(), out amount) && Int32.TryParse(commandArguments[2].Trim(), out productId); if (converted) { BuyProduct(commandArguments[0].Trim(), Convert.ToInt32(commandArguments[1].Trim()), Convert.ToInt32(commandArguments[2].Trim())); } else { _ui.DisplayGeneralError("invalid amount or product id"); } break; default: _ui.DisplayTooManyArgumentsError(command); break; } }
public CommandParser(LineSystem lineSystem, ILineSystemUI ui) { LineSystem = lineSystem; UI = ui; adminCommands.Add(":close", args => UI.Close()); adminCommands.Add(":activate", args => { ChangeProductBoolField(":activate", args, ProductBoolField.Active, true); }); adminCommands.Add(":deactivate", args => { ChangeProductBoolField(":deactivate", args, ProductBoolField.Active, false); }); adminCommands.Add(":crediton", args => { ChangeProductBoolField(":crediton", args, ProductBoolField.CanBeBoughtOnCredit, true); }); adminCommands.Add(":creditoff", args => { ChangeProductBoolField(":creditoff", args, ProductBoolField.CanBeBoughtOnCredit, false); }); adminCommands.Add(":addcredits", args => { InsertCashTransaction transaction; User user; int amount; if (IsValidArgs(":addcredits", args, 2)) { user = LineSystem.GetUser(args[0]); if (user != null) { if (args[1].TryToCredit(out amount)) { transaction = LineSystem.AddCreditsToAccount(user, amount); UI.DisplayAdminAddedCredits(transaction); LineSystem.ExecuteTransaction(transaction); } else { UI.DisplayGeneralError(args[1] + " is not a invalid cash amount"); } } else { UI.DisplayUserNotFound(args[0]); } } }); }