static void Main(string[] args) { var services = new ServiceCollection(); ConfigureServices(services); using (ServiceProvider serviceProvider = services.BuildServiceProvider()) { IBankSimulatorService _bankSimulator = serviceProvider.GetService <IBankSimulatorService>(); ILogServices _log = serviceProvider.GetService <ILogServices>(); Bank _newBank = _bankSimulator.CreateBank("Brix Test").Result; _log.WriteLine("WELCOME TO " + _newBank.Name + " Bank", ConsoleColor.Blue); _log.WriteLine("========================== ", ConsoleColor.Blue); string MenuInput = string.Empty; while (MenuInput != "E" && MenuInput != "e") { _log.WriteLine("Menu :O = open an account | R = add owner | D = Deposit | W = Withdrawal | E = Exit", ConsoleColor.Blue); MenuInput = HandleuserInput(_bankSimulator, _log, _newBank).Result; } Task.WaitAll(tasks.ToArray()); } }
public BankTransaction AddTransaction(BankTransaction _transaction) { //check that operation is done by the account owner. AccountOwner _owner = _dbContext.AccountOwners.Where(O => O.AcoountID == _transaction.AccountID && O.OwnerID == _transaction.ExcutedByOwnerID).FirstOrDefault(); if (_owner == null) { throw new Exception("The account and user provided dont match. please make sure the user provided is the account owner."); } bool _lockWasTaken = false; Monitor.Enter(Locker, ref _lockWasTaken); try { //Thread.Sleep( 10000 ); //unmark to test multiple deposit\Withdrawal at the same time. //check balance is enough for Withdrawal. Account _Account = _dbContext.Accounts.Where(A => A.ID == _transaction.AccountID).FirstOrDefault(); if (_transaction.Amount < 0 && _Account.Balance + _transaction.Amount < 0) { throw new Exception("insufficient funds. your balance is $" + _Account.Balance.ToString()); } //update balance and save transaction _dbContext.Accounts.Attach(_Account); _Account.Balance += _transaction.Amount; _dbContext.BankTransactions.Add(_transaction); _dbContext.SaveChanges(); _log.WriteLine(string.Format("Successfully {0} ${1} to account {2}. New balance is ${3}.", _transaction.Amount > 0? "Deposit": "Withdrawal", _transaction.Amount.ToString(), _transaction.AccountID.ToString(), _Account.Balance.ToString()), ConsoleColor.Green); return(_transaction); } catch (Exception ex) { _log.WriteLine("", ConsoleColor.Red); _log.WriteLine(string.Format("Error {0} account {1}. Error details: {2}", _transaction.Amount > 0 ? "Deposit to " : "Withdrawal from ", _transaction.AccountID.ToString() , ex.Message), ConsoleColor.Red); return(null); } finally { if (_lockWasTaken) { Monitor.Exit(Locker); } } }
private static async Task <string> HandleuserInput(IBankSimulatorService bankSimulator, ILogServices log, Bank bank) { string MenuInput = Console.ReadLine(); string CommanInput = string.Empty; try { switch (MenuInput) { case "O": //open account case "o": Account _newAccount = await bankSimulator.CreateAccount(bank.ID); log.WriteLine("Account number " + _newAccount.ID.ToString() + " created.", ConsoleColor.Green); break; case "R": // add owner to an account case "r": log.WriteLine("Please enter owner id (up to 9 digits), name, account id (123456789,lior gal,1). ", ConsoleColor.White); CommanInput = Console.ReadLine(); AccountOwner _newAccountOwner = await bankSimulator.AddAccountOwner(CommanInput.Split( "," )[0].Trim(), CommanInput.Split( "," )[1].Trim(), Convert.ToInt32(CommanInput.Split(",")[2].Trim())); log.WriteLine(string.Format("User id {0} was added as account {1} owner.", _newAccountOwner.OwnerID, _newAccountOwner.AcoountID.ToString()), ConsoleColor.Green); break; case "D": // Deposit case "d": log.WriteLine("Please enter account id, owner id,amount (1,123456789,100.5). ", ConsoleColor.White); CommanInput = Console.ReadLine(); Task <BankTransaction> depositTask = Task.Run(() => bankSimulator.Deposit(Convert.ToInt32(CommanInput.Split(",")[0].Trim()), CommanInput.Split(",")[1].Trim(), Convert.ToDouble(CommanInput.Split(",")[2].Trim()))); tasks.Add(depositTask); break; case "W": // Withdrawal case "w": log.WriteLine("Please enter account id, owner id,amount (1,123456789,100.5). ", ConsoleColor.White); CommanInput = Console.ReadLine(); Task <BankTransaction> withdrawalTask = Task.Run(() => bankSimulator.Withdrawal(Convert.ToInt32(CommanInput.Split(",")[0].Trim()), CommanInput.Split(",")[1].Trim(), Convert.ToDouble(CommanInput.Split(",")[2].Trim()))); tasks.Add(withdrawalTask); break; case "E": //exit program case "e": log.WriteLine("Thanks for using " + bank.Name + " bank :-)", ConsoleColor.Blue); break; default: log.WriteLine("invalid menu item.", ConsoleColor.Red); break; } } catch (Exception ex) { log.WriteLine("Error. " + ex.Message, ConsoleColor.Red); } return(MenuInput); }