public AccountWithdrawalResponse Withdraw(string accountNumber, decimal amount) { AccountWithdrawalResponse response = new AccountWithdrawalResponse { Success = false, Account = _accountRepository.LoadAccount(accountNumber) }; if (response.Account == null) { response.Message = $"{accountNumber} is not a valid account."; } else { //var someThing = DIContainer.Kernel.GetBindings(DIContainer.Kernel.GetService().Withdraw() IWithdrawal depositRule = WithdrawalRulesFactory.Create(response.Account.Type); response = depositRule.Withdraw(response.Account, amount); } if (response.Success) { _accountRepository.SaveAccount(response.Account); } return(response); }
public void Execute() { Console.Clear(); AccountManager accountManager = DIContainer.Kernel.Get <AccountManager>(); Console.Write("\nEnter an account number: "); string accountNumber = Console.ReadLine(); decimal amount = 0; do { Console.Write("\nEnter the amount to withdraw amount: "); if (decimal.TryParse(Console.ReadLine(), out amount)) { break; } else { Console.WriteLine("Invalid amount entered. Try again..."); } } while (true); AccountWithdrawalResponse response = accountManager.Withdraw(accountNumber, amount); if (response.Success) { var line = "\t{0,-20} {1,10:c}"; Console.WriteLine("Withdrawal completed!"); Console.WriteLine("\n"); Console.WriteLine($"Account number : {response.Account.AccountNumber}\n"); Console.WriteLine(line, " Old balance : ", response.OldBalance); Console.WriteLine(line, "- Amount withdrawn: ", response.Amount); if (response.OverdraftFees > 0) { Console.WriteLine(line, "- Overdraft fees : ", response.OverdraftFees); } Console.WriteLine("--------------------------------------------------"); Console.WriteLine(line, "= New balance : ", response.Account.Balance); Console.WriteLine(); } else { Console.WriteLine("An error occurred: "); Console.WriteLine(response.Message); } Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); }
public AccountWithdrawalResponse Withdraw(Account account, decimal amount) { AccountWithdrawalResponse response = new AccountWithdrawalResponse { Success = false }; if (account.Type != AccountType.Basic) { response.Message = "Error: A non Basic account hit the Basic Withdrawal Rule. Contact IT"; return(response); } if (amount > 500) { response.Message = "Basic accounts cannot withdraw more than $500 at a time"; return(response); } if (amount <= 0) { response.Message = "Withdrawal amount must be greater than 0"; return(response); } if (account.Balance - amount < -100M) { response.Message = "This amount will overdraw your account by more than your $100 limit."; return(response); } response.OldBalance = account.Balance; account.Balance -= amount; if (account.Balance < 0) { response.OverdraftFees = 10M; account.Balance -= response.OverdraftFees; } else { response.OverdraftFees = 0; } response.Account = account; response.Amount = amount; response.Success = true; return(response); }
[TestCase("33333", "BasicAccount", 100, AccountType.Basic, 150, -60, true)] //Pass, Overdraft public void BasicAccount_WithdrawalRuleTest(string accountNumber, string name, decimal balance, AccountType accountType, decimal amount, decimal newBalance, bool expectedResult) { IWithdrawal withdrawalRule = WithdrawalRulesFactory.Create(AccountType.Basic); Account account = new Account() { AccountNumber = accountNumber, Balance = balance, Name = name, Type = accountType }; AccountWithdrawalResponse response = withdrawalRule.Withdraw(account, amount); Assert.AreEqual(expectedResult, response.Success); }
public AccountWithdrawalResponse Withdraw(Account account, decimal amount) { AccountWithdrawalResponse response = new AccountWithdrawalResponse { Success = false }; if (account.Type != AccountType.Free) { response.Message = "Error: A non free account hit the Free Withdrawal Rule."; return(response); } if (amount > account.Balance) { response.Message = "Cannot withdraw more than you have"; return(response); } if (amount > 100) { response.Message = "Free account cannot withdraw more than $100 at a time"; return(response); } if (amount <= 0) { response.Message = "Withdrawal amount must be greater than 0"; return(response); } response.OldBalance = account.Balance; response.OverdraftFees = 0; account.Balance -= amount; response.Account = account; response.Amount = amount; response.Success = true; return(response); }
public async Task <IActionResult> Withdraw(CreateWithdrawalRequest request, CancellationToken token) { _logger.LogInformation($"{nameof(AccountController)}.{nameof(Withdraw)} - {request}"); if (!ModelState.IsValid) { return(BadRequest($"Problem with CreateWithdrawalRequest")); } CreateWithdrawal withdraw = new CreateWithdrawal(request.AccountId, Amount.FromInput(request.Amount)); WithdrawalResult withdrawalResult = await _accountService.WithDraw(withdraw); if (withdrawalResult.Status == AccountStatus.Error) { return(BadRequest($"{withdrawalResult.Message}")); } AccountWithdrawalResponse response = new AccountWithdrawalResponse(withdrawalResult.AccountId, withdrawalResult.Amount.Value, withdrawalResult.OldBalance.Value, withdrawalResult.NewBalance.Value); return(Ok(response)); }
public AccountWithdrawalResponse Withdraw(Account account, decimal amount) { AccountWithdrawalResponse response = new AccountWithdrawalResponse { Success = false }; if (account.Type != AccountType.Premium) { response.Message = "Error: A non Premium account hit the Premium Withdrawal Rule."; return(response); } if (amount <= 0) { response.Message = "Withdrawal amount must be greater than 0"; return(response); } response.OldBalance = account.Balance; account.Balance -= amount; if (account.Balance < -500M) { response.OverdraftFees = 10M; account.Balance -= response.OverdraftFees; } else { response.OverdraftFees = 0; } response.Account = account; response.Amount = amount; response.Success = true; return(response); }