예제 #1
0
        /// <summary>
        /// Actually perform the transaction
        /// </summary>
        /// <param name="amount">amount to transfer</param>
        private void performTransaction(double amount)
        {
            // create service client
            TransactionService.TransactionClient transactionService =
                new TransactionService.TransactionClient();

            // get transaction type
            ETransactionType type = getTransactionType();

            // new balance, if successful
            double? newBalance = null;
            int transactionTypeId;

            // for billPayment, Transfer, Withdrawal, is greater than
            // the transaction amount
            if (type != ETransactionType.Deposit && account.Balance > amount)
            {

                switch (type)
                {
                    case ETransactionType.Withdrawal:
                        newBalance = transactionService.Withdrawal(account.BankAccountId, amount);
                        transactionTypeId = db.TransactionTypes
                            .Where(t => t.Description.ToLower().Contains("with"))
                            .Select(t => t.TransactionTypeId).First();
                        transactionService.CreateTransaction(false, account.BankAccountId, amount, transactionTypeId, "bank withdrawal");
                        break;
                    case ETransactionType.BillPayment:
                        transactionTypeId = db.TransactionTypes
                            .Where(t => t.Description.ToLower().Contains("bill"))
                            .Select(t => t.TransactionTypeId).First();
                        newBalance = transactionService.BillPayment(account.BankAccountId, amount);
                        transactionService.CreateTransaction(false, account.BankAccountId, amount, transactionTypeId, "Bill Payment");
                        break;
                    case ETransactionType.Transfer:
                        // get the to account
                        BankAccount toAccount =
                            ((BankAccount)bankAccountBindingSource.Current);

                        // transfer funds
                        newBalance = transactionService.Transfer(
                            account.BankAccountId,
                            toAccount.BankAccountId,
                            amount);

                        transactionTypeId = db.TransactionTypes
                            .Where(t => t.Description.ToLower().Contains("trans"))
                            .Select(t => t.TransactionTypeId).First();
                        transactionService.CreateTransaction(false, account.BankAccountId, amount, transactionTypeId, "transfer to " + toAccount.AccountNumber);
                        transactionService.CreateTransaction(true, toAccount.BankAccountId, amount, transactionTypeId, "transfer from " + account.AccountNumber);
                        break;

                    default:
                        break;
                }
            }
            // depositing doesn't need balance to be greater than amount
            else if (type == ETransactionType.Deposit)
            {
                transactionTypeId = db.TransactionTypes
                            .Where(t => t.Description.ToLower().Contains("depos"))
                            .Select(t => t.TransactionTypeId).First();
                newBalance = transactionService.Deposit(account.BankAccountId, amount);
                transactionService.CreateTransaction(true, account.BankAccountId, amount, transactionTypeId, "bank deposit");
            }
            // balance is less than amount
            else
            {
                Utility.Helpers.DisplayError("Insufficient funds");
            }

            // check if transaction was successful
            if (newBalance != null)
            {
                returnToClient();
            }
            else
            {
                Utility.Helpers.DisplayError("Transaction Failed");
            }
        }
예제 #2
0
        /// <summary>
        /// process all valid transactions
        /// </summary>
        /// <param name="transactions">Collection of valid transaction elements</param>
        private void processTransactions(IEnumerable<XElement> transactions)
        {
            // transaction Service
            TransactionService.TransactionClient service = new TransactionService.TransactionClient();
            foreach (XElement t in transactions)
            {
                // extract values from XML
                int type = int.Parse(t.Element("type").Value);
                int accountNo = int.Parse(t.Element("account_no").Value);
                double inAmount = double.Parse(t.Element("in").Value);
                double outAmount = double.Parse(t.Element("out").Value);
                string notes = t.Element("notes").Value;
                int accountId =
                    (db.BankAccounts.Where(a => a.AccountNumber == accountNo)
                        ).First().BankAccountId;

                // transction number returned by service.createTransaction
                long? transactionNo = null;

                // perform transaction
                switch (type)
                {
                    case 1:
                        // deposit
                        service.Deposit(
                            accountId,
                            inAmount);
                        transactionNo = service.CreateTransaction(true, accountId, inAmount, type, notes);
                        break;
                    case 2:
                        // withdrawal
                        service.Withdrawal(accountId, outAmount);
                        transactionNo = service.CreateTransaction(false, accountId, outAmount, type, notes);
                        break;
                    default:
                        break;
                }

                // transaction was successful
                if (transactionNo != null)
                {
                    putLine("Transaction " + transactionNo + " completed successfully");
                }
            }
        }