Beispiel #1
0
        /// <summary>
        /// This method is used to process all valid transaction records
        /// </summary>
        /// <param name="transactionRecords"> the result set passed to this method </param>
        private void processTransactions(IEnumerable <XElement> transactionRecords)
        {
            //Evaluate the type node
            IEnumerable <XElement> depositTransactionElements = transactionRecords.Where(d => d.Element("type").Value.Equals("1"));

            int iDepositCount = depositTransactionElements.Count();

            IEnumerable <XElement> withdrawalTransactionElements = transactionRecords.Where(d => d.Element("type").Value.Equals("2"));

            int iWithdrawalCount = withdrawalTransactionElements.Count();

            //create new instance
            TransactionManagerClient transactionManagerClient = new TransactionManagerClient();

            long?transactionNumber;

            //If the type is a 1 (indicating deposit)
            if (iDepositCount > 0)
            {
                //retrive the account number
                List <string> sBankAccountNumbers = depositTransactionElements.Select(d => d.Element("account_no").Value).ToList();
                List <string> sAmounts            = depositTransactionElements.Select(d => d.Element("in").Value).ToList();
                List <string> sNotes = depositTransactionElements.Select(d => d.Element("notes").Value).ToList();
                for (int i = 0; i < iDepositCount; i++)
                {
                    int iBankAccountNumber = int.Parse(sBankAccountNumbers[i]);

                    //retrive the accountID
                    int iBankAccountID = db.BankAccounts.Where(d => d.AccountNumber == iBankAccountNumber).Select(d => d.BankAccountId).Single();
                    //make the deposit
                    transactionManagerClient.Deposit(iBankAccountID, double.Parse(sAmounts[i]));

                    transactionNumber = transactionManagerClient.CreateTransaction(true, iBankAccountID, double.Parse(sAmounts[i]),
                                                                                   1, sNotes[i]);

                    logData += "\nTransaction " + transactionNumber + " completed successfully";
                }
            }

            //If the type is a 2 (indicating withdrawal)
            if (iWithdrawalCount > 0)
            {
                //retrive the account number
                //IEnumerable<XElement> xBankAccountNumbers = withdrawalTransactionElements.Where(d => d.Element("account_no")).ToList();
                List <string> sBankAccountNumbers = withdrawalTransactionElements.Select(d => d.Element("account_no").Value).ToList();
                List <string> sAmounts            = withdrawalTransactionElements.Select(d => d.Element("out").Value).ToList();
                List <string> sNotes = withdrawalTransactionElements.Select(d => d.Element("notes").Value).ToList();
                for (int i = 0; i < iWithdrawalCount; i++)
                {
                    int iBankAccountNumber = int.Parse(sBankAccountNumbers[i]);
                    //retrive the accountID
                    int iBankAccountID = db.BankAccounts.Where(d => d.AccountNumber == iBankAccountNumber).Select(d => d.BankAccountId).Single();
                    //make the deposit
                    transactionManagerClient.Withdrawal(iBankAccountID, double.Parse(sAmounts[i]));

                    transactionNumber = transactionManagerClient.CreateTransaction(false, iBankAccountID, double.Parse(sAmounts[i]),
                                                                                   2, sNotes[i]);
                    logData += "\nTransaction " + transactionNumber + " completed successfully";
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Perform the transaction chosen by the user
        /// </summary>
        /// <param name="transactionType">The type of the transaction chosen by the user</param>
        public void PerformTransaction(string transactionType)
        {
            //Creating an instance of transacion manager
            TransactionManagerClient transactionManager = new TransactionManagerClient();

            //Creating a variable to store the new balance in it
            double newBalance;

            try
            {
                //Evaluating the type chosen by the user
                switch (cboTransactionType.Text)
                {
                //In case the user choice is a bill payment
                case "Bill Payment":

                    //Performing the payment and storing the new balance
                    newBalance = (double)transactionManager.BillPayment(constructorData.bankAccount.BankAccountId, double.Parse(txtAmount.Text), "Bill Payment");

                    //Update the current balance to be equal to the new one
                    lblBalance.Text = newBalance.ToString("c");

                    //Checking if the transaction was successfull by calling the check transaction success method
                    CheckTransactionSuccess(newBalance);
                    break;

                //In case the user choice is a transfer
                case "Transfer":

                    //Getting the recepient's account number
                    int toAccountNumber = int.Parse(cboAccountPayee.Text);

                    //Quering the reciepient's account number
                    int toAccountId = db.BankAccounts.Where(x => x.AccountNumber == toAccountNumber).Select(x => x.BankAccountId).SingleOrDefault();

                    //Performing the transfer and storing the new balance
                    newBalance = (double)transactionManager.Transfer(constructorData.bankAccount.BankAccountId, toAccountId, double.Parse(txtAmount.Text), "Transfer");

                    //Update the current balance to be equal to the new one
                    lblBalance.Text = newBalance.ToString("c");

                    //Checking if the transaction was successfull by calling the check transaction success method
                    CheckTransactionSuccess(newBalance);
                    break;

                //In case the user choice is a withdrawal
                case "Withdrawal":

                    //Performing the withdrawal and storing the new balance
                    newBalance = (double)transactionManager.Withdrawal(constructorData.bankAccount.BankAccountId, double.Parse(txtAmount.Text), "Withdrawal");

                    //Update the current balance to be equal to the new one
                    lblBalance.Text = newBalance.ToString("c");

                    //Checking if the transaction was successfull by calling the check transaction success method
                    CheckTransactionSuccess(newBalance);
                    break;

                //In case the user choice is a deposit
                case "Deposit":

                    //Performing the deposit and storing the new balance
                    newBalance = (double)transactionManager.Deposit(constructorData.bankAccount.BankAccountId, double.Parse(txtAmount.Text), "Deposit");

                    //Update the current balance to be equal to the new one
                    lblBalance.Text = newBalance.ToString("c");

                    //Checking if the transaction was successfull by calling the check transaction success method
                    CheckTransactionSuccess(newBalance);
                    break;
                }
            }
            catch (Exception ex)
            {
                //Displaying a message in case an exception is caught
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #3
0
        /// <summary>
        /// according to the TransactionType, create the Transaction, insert and update the database
        /// </summary>
        /// <param name="sTransactionType"></param>
        /// <returns>true when there is no error, false when there have error</returns>
        private bool makeTransaction(string sTransactionType)
        {
            //create new instance
            TransactionManagerClient transactionManagerClient = new TransactionManagerClient();

            try
            {
                //declare a bool variable and set default value
                bool bErrorFlag = true;
                //check the TransactionType
                switch (sTransactionType)
                {
                //when TransactionType is "Bill Payment"
                case "Bill Payment":
                    //execute BillPayment method and when have error
                    if (transactionManagerClient.BillPayment(iBankAccountID, dAmount) == null)
                    {
                        //set bErrorFlag to false
                        bErrorFlag = false;
                    }
                    //when no error
                    else
                    {
                        //invoke CreateTransaction to create a transaction
                        long?transactionNumber = transactionManagerClient.CreateTransaction(false, iBankAccountID, dAmount,
                                                                                            (int)TransactionTypes.BillPayment, "Payee: " + cboAccountPayee.Text);
                        //when have error
                        if (transactionNumber == null)
                        {
                            //set bErrorFlag to false
                            bErrorFlag = false;
                            // reverse the transaction and if there is an error
                            transactionManagerClient.Deposit(iBankAccountID, dAmount);
                        }
                    }
                    //jump out of the switch
                    break;

                //when TransactionType is "Transfer"
                case "Transfer":
                    //set toAccount id according to the Selected item
                    int toAccountNumber = int.Parse(cboAccountPayee.Text);
                    int toAccountId     = db.BankAccounts.Where(record => record.AccountNumber == toAccountNumber).Select(record => record.BankAccountId).Single();
                    //transfer the money and when there is error
                    if (transactionManagerClient.Transfer(iBankAccountID, toAccountId, dAmount) == null)
                    {
                        //set bErrorFlag to false
                        bErrorFlag = false;
                    }
                    else
                    {
                        //create Transaction for fromAccount
                        long?fromTransactionNumber = transactionManagerClient.CreateTransaction(false, iBankAccountID, dAmount,
                                                                                                (int)TransactionTypes.Transfer, "Transferred to: " + cboAccountPayee.Text);
                        //when error
                        if (fromTransactionNumber == null)
                        {
                            //set bErrorFlag to false
                            bErrorFlag = false;
                            // reverse the transaction
                            transactionManagerClient.Deposit(iBankAccountID, dAmount);
                        }
                        else
                        {
                            //create Transaction for toAccount
                            long?toTransactionNumber = transactionManagerClient.CreateTransaction(true, toAccountId, dAmount,
                                                                                                  (int)TransactionTypes.Transfer, "Transferred from: " + cboAccountPayee.Text);
                            //when error
                            if (toTransactionNumber == null)
                            {
                                //set bErrorFlag to false
                                bErrorFlag = false;
                                // reverse the transaction and when reverse error
                                transactionManagerClient.Deposit(iBankAccountID, dAmount);

                                //find out the transaction inserted
                                Transaction transactionResult = (from t in db.Transactions
                                                                 where t.TransactionNumber == fromTransactionNumber
                                                                 select t).Single();
                                //remove the transaction
                                db.Transactions.Remove(transactionResult);
                            }
                        }
                    }
                    //jump out of the switch
                    break;

                //when TransactionType is "Deposit"
                case "Deposit":
                    //execute Deposit method and when occor error
                    if (transactionManagerClient.Deposit(iBankAccountID, dAmount) == null)
                    {
                        //set bErrorFlag to false
                        bErrorFlag = false;
                    }
                    else
                    {
                        //invoke CreateTransaction to create a transaction
                        long?transactionNumber = transactionManagerClient.CreateTransaction(true, iBankAccountID, dAmount,
                                                                                            (int)TransactionTypes.Deposit, "Deposit amount: " + dAmount.ToString());
                        //when have error
                        if (transactionNumber == null)
                        {
                            //set bErrorFlag to false
                            bErrorFlag = false;
                            // reverse the transaction
                            transactionManagerClient.Withdrawal(iBankAccountID, dAmount);
                        }
                    }
                    //jump out of the switch
                    break;

                //when TransactionType is "Withdrawal"
                case "Withdrawal":
                    //execute Withdrawal method and when occor error
                    if (transactionManagerClient.Withdrawal(iBankAccountID, dAmount) == null)
                    {
                        //set bErrorFlag to false
                        bErrorFlag = false;
                    }
                    else
                    {
                        //invoke CreateTransaction to create a transaction
                        long?transactionNumber = transactionManagerClient.CreateTransaction(false, iBankAccountID, dAmount,
                                                                                            (int)TransactionTypes.Deposit, "Withdrawal amount: " + dAmount.ToString());
                        //when have error
                        if (transactionNumber == null)
                        {
                            //set bErrorFlag to false
                            bErrorFlag = false;
                            // reverse the transaction
                            transactionManagerClient.Deposit(iBankAccountID, dAmount);
                        }
                    }
                    //jump out of the switch
                    break;
                }
                //return the flag
                return(bErrorFlag);
            }
            //when occor Exception throw it
            catch (Exception)
            {
                throw;
            }
        }