public void Transfer(decimal amount, string fromAccountNumber, string toAccountNumber)
        {
            ITransactAccount account = Repository.GetAccount(fromAccountNumber);

            if (account == null)
            {
                throw new KeyNotFoundException($"Account number {account} does not exist.");
            }
            ITransactAccount toAccount = Repository.GetAccount(toAccountNumber);

            if (toAccount == null)
            {
                throw new KeyNotFoundException($"Account number {toAccount} does not exist.");
            }
            if (!(account is ITransactCashOutAccount))
            {
                throw new InvalidOperationException("Invalid from account type for Transfer.");
            }

            ITransactCashOutAccount fromAccount = (ITransactCashOutAccount)account;

            fromAccount.MoveCashOut(amount);
            toAccount.MoveCashIn(amount);
            Repository.SaveAccount(fromAccount);
            Repository.SaveAccount(toAccount);
            Console.WriteLine($"Successful transfer of {amount:C} from {fromAccount.Name} ({fromAccount.AccountNumber}) to {toAccount.Name} ({toAccount.AccountNumber})");
        }
        public void SaveAccount(ITransactAccount account)
        {
            ITransactAccount oldAccount = accounts.Find(a => a.AccountNumber == account.AccountNumber);

            if (oldAccount != null)
            {
                accounts.Remove(oldAccount);
            }

            accounts.Add(account.ShallowCopy());
        }
        public void Deposit(decimal amount, string toAccountNumber)
        {
            ITransactAccount toAccount = Repository.GetAccount(toAccountNumber);

            if (toAccount == null)
            {
                throw new KeyNotFoundException($"Account number {toAccountNumber} does not exist.");
            }

            toAccount.MoveCashIn(amount);
            Repository.SaveAccount(toAccount);
            Console.WriteLine($"Successful deposit of {amount:C} to {toAccount.Name} ({toAccount.AccountNumber})");
        }
        public virtual void PayBill(decimal amount, string fromAccountNumber, string payeeId, string billingAccountId)
        {
            //if (amount <= 0)
            //{
            //   throw new ArgumentException("Amount must be greater than 0.", nameof(amount));
            //}

            ITransactAccount account = Repository.GetAccount(fromAccountNumber);

            //if (fromAccount == null)
            //{
            //   throw new KeyNotFoundException($"Account number {fromAccountNumber} does not exist.");
            //}

            //if (fromAccount is SavingsAccount)
            //{
            //   // Asset account balances decrease on withdraw.
            //   fromAccount.Balance -= amount;
            //}
            //else if (fromAccount is CreditCardAccount)
            //{
            //   // Liability account balances increase on withdraw.
            //   fromAccount.Balance += amount;
            //}
            //else
            //{
            //   // Mortgage accounts do not support withdraw.
            //   throw new InvalidOperationException("Invalid from account type for bill payment.");
            //}

            //try
            //{
            //   BillPaymentService.ProcessBillPayment(amount, payeeId, billingAccountId);
            //}
            //catch (Exception ex)
            //{
            //   throw new ApplicationException($"Bill payment failed with error: {ex.Message}", ex);
            //}

            if (!(account is ITransactCashOutAccount))
            {
                throw new InvalidOperationException("Invalid from account type for bill payment.");
            }

            ITransactCashOutAccount fromAccount = (ITransactCashOutAccount)account;

            fromAccount.MoveCashOut(amount);
            Repository.SaveAccount(fromAccount);
            Console.WriteLine($"Successful bill payment of {amount:C} from {fromAccount.Name} ({fromAccount.AccountNumber})");
        }
        public virtual bool ValidateAmount(decimal amount)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("Amount must be greater than 0.", nameof(amount));
            }

            ITransactAccount toAccount = Repository.GetAccount(AccountNumber);

            if (toAccount == null)
            {
                throw new KeyNotFoundException($"Account number {AccountNumber} does not exist.");
            }

            return(true);
        }
Example #6
0
        static void Main(string[] args)
        {
            AccountRepository repository = new AccountRepository();
            BankingService    service    = new BankingService();

            List <ITransactAccount> accounts = repository.GetAccounts();

            string[,] openingBalances = new string[accounts.Count + 1, 3];
            openingBalances[0, 0]     = "Acct#";
            openingBalances[0, 1]     = "Acct Name";
            openingBalances[0, 2]     = "Balance";

            for (int index = 0; index < accounts.Count; index++)
            {
                ITransactAccount account = accounts[index];
                openingBalances[index + 1, 0] = account.AccountNumber;
                openingBalances[index + 1, 1] = account.Name;
                openingBalances[index + 1, 2] = $"{account.Balance:C}";
            }

            Console.WriteLine("OPENING BALANCES:");
            PrintTable(openingBalances, 3, new int[] { 2 });

            Console.WriteLine();
            Console.WriteLine("TRANSACTIONS:");

            try
            {
                // Deposit $500 to Vacation account
                service.Deposit(500, ("225893"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                // Withdraw $250 from Car account
                service.Withdraw(250, "285901");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                // Transfer $1000 from Visa account to Mortgage account
                service.Transfer(1000, "599051", "312590");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                // Pay bill of $375.12 from Visa account
                service.PayBill(375.12m, "599051", "05213", "0029591839");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                // Attempt to deposit a negative amount to the Vacation account
                service.Deposit(-100, "225893");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                // Attempt to withdraw from the Mortgage account
                service.Withdraw(100, "312590");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            accounts = repository.GetAccounts();

            string[,] closingBalances = new string[accounts.Count + 1, 3];
            closingBalances[0, 0]     = "Acct#";
            closingBalances[0, 1]     = "Acct Name";
            closingBalances[0, 2]     = "Balance";

            for (int index = 0; index < accounts.Count; index++)
            {
                ITransactAccount account = accounts[index];
                closingBalances[index + 1, 0] = account.AccountNumber;
                closingBalances[index + 1, 1] = account.Name;
                closingBalances[index + 1, 2] = $"{account.Balance:C}";
            }

            Console.WriteLine();
            Console.WriteLine("CLOSING BALANCES:");
            PrintTable(closingBalances, 3, new int[] { 2 });

            Console.ReadKey();
        }