Beispiel #1
0
    /// <summary>
    /// when click the submit button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //check validation
        if (IsValid)
        {
            //get the douBalance and douAmount from the labels
            double douBalance = double.Parse(lblBalance.Text.Replace("$", ""));
            double douAmount  = double.Parse(txtAmount.Text.Replace("$", ""));
            //when balance is insufficient
            if (douAmount > douBalance)
            {
                //set txtSummary
                txtSummary.Text = INSUFFICIENT_FUNDS_ERROR;
            }
            else
            {
                //create new instance
                TransactionManagerClient transactionManagerClient = new TransactionManagerClient();

                switch (ddlTransactionType.SelectedIndex)
                {
                //when select Bill Payment
                case 0:
                    //when have error throw execption
                    if (transactionManagerClient.BillPayment((int)Session["BankAccountID"], douAmount) == null)
                    {
                        throw new Exception(string.Format(ERROR_OCCUR_TEMPLATE, "pay the bill!"));
                    }
                    else
                    {
                        //invoke CreateTransaction to create a transaction
                        long?transactionNumber = transactionManagerClient.CreateTransaction(false, (int)Session["BankAccountID"], douAmount,
                                                                                            (int)TransactionTypes.BillPayment, "Payee: " + ddlAccountPayee.SelectedItem);
                        //when have error
                        if (transactionNumber == null)
                        {
                            // reverse the transaction and if there is an error
                            if (transactionManagerClient.Deposit((int)Session["BankAccountID"], douAmount) == null)
                            {
                                throw new Exception(string.Format(ERROR_OCCUR_TEMPLATE, "reverse the transaction!"));
                            }
                            else
                            {
                                //output error message to txtSummary
                                txtSummary.Text = "Payment is not successful!";
                            }
                        }
                        else
                        {
                            //output success message to txtSummary
                            txtSummary.Text = string.Format(SUCCESS_MESSAGE_TEMPLATE, TransactionTypes.BillPayment, douAmount, transactionNumber);
                        }
                    }
                    break;

                //when select Transfer Funds
                case 1:
                    //set toAccount id according to the Selected item
                    int toAccount = int.Parse(ddlAccountPayee.SelectedValue);
                    //transfer the money and when there is error
                    if (transactionManagerClient.Transfer((int)Session["BankAccountID"], toAccount, douAmount) == null)
                    {
                        throw new Exception(string.Format(ERROR_OCCUR_TEMPLATE, "transfer money!"));
                    }
                    else
                    {
                        //create Transaction for fromAccount
                        long?fromTransactionNumber = transactionManagerClient.CreateTransaction(false, (int)Session["BankAccountID"], douAmount,
                                                                                                (int)TransactionTypes.Transfer, "Transferred to: " + ddlAccountPayee.SelectedItem);
                        //when error
                        if (fromTransactionNumber == null)
                        {
                            // reverse the transaction
                            if (transactionManagerClient.Deposit((int)Session["BankAccountID"], douAmount) == null)
                            {
                                throw new Exception(string.Format(ERROR_OCCUR_TEMPLATE, "reverse the transaction!"));
                            }
                            else
                            {
                                //output error message to txtSummary
                                txtSummary.Text = "Transaction is not successful!";
                            }
                        }
                        else
                        {
                            //create Transaction for toAccount
                            long?toTransactionNumber = transactionManagerClient.CreateTransaction(true, toAccount, douAmount,
                                                                                                  (int)TransactionTypes.Transfer, "Transferred from: " + lblAccountNumber.Text);
                            //when error
                            if (toTransactionNumber == null)
                            {
                                // reverse the transaction and when reverse error
                                if (transactionManagerClient.Deposit((int)Session["BankAccountID"], douAmount) == null)
                                {
                                    throw new Exception(string.Format(ERROR_OCCUR_TEMPLATE, "reverse the transaction!"));
                                }
                                else
                                {
                                    try
                                    {
                                        //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);
                                        //output the error message to txtSummary
                                        txtSummary.Text = "Transaction is not successful!";
                                    }
                                    catch (Exception)
                                    {
                                        throw new Exception(string.Format(ERROR_OCCUR_TEMPLATE, "delete the transaction!"));
                                    }
                                }
                            }
                            else
                            {
                                //when succed set success message
                                txtSummary.Text  = string.Format(SUCCESS_MESSAGE_TEMPLATE, TransactionTypes.Transfer, douAmount, fromTransactionNumber);
                                txtSummary.Text += string.Format(SUCCESS_MESSAGE_TEMPLATE, TransactionTypes.Transfer, douAmount, toTransactionNumber);
                            }
                        }
                    }
                    break;
                }
                //get clientId and bankAccountId from Session
                int clientId      = (int)Session["ClientID"];
                int bankAccountId = (int)Session["BankAccountID"];

                //add the account balance to the account balance label from the balance in database
                lblBalance.Text = (from record in db.BankAccounts
                                   where record.ClientId == clientId &&
                                   record.BankAccountId == bankAccountId
                                   select record.Balance).Single().ToString("C");
            }
        }
    }
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);
            }
        }
    /// <summary>
    /// Handles the event of clicking complete transaction button
    /// </summary>
    protected void btnCompleteTransaction_Click(object sender, EventArgs e)
    {
        try
        {
            //Setting current balance to the balance available
            double currentBalance = double.Parse(lblBalance.Text.Replace("$", ""));

            //Setting the requested amount the amount entered
            double requestedAmount = double.Parse(txtAmount.Text);

            //Comparing the requested amount to the available current balance and displaying an error messages if it is less
            if (requestedAmount > currentBalance)
            {
                //Displaying error message if the current balance is less than the requested amount
                lblError.Text = ERROR_INSUFFICIENT_FUNDS;
            }
            else
            {
                //Creating an instance of TransactionManagerClient
                TransactionManagerClient transactionManagerClient = new TransactionManagerClient();

                //Checking if index selected is bill payment or a transfer
                if (ddlTransactionType.SelectedIndex == 0)
                {
                    //Quering and storing the account id logged in
                    int accountId = (from result in db.BankAccounts
                                     where result.AccountNumber == accountNumber
                                     select result.BankAccountId).SingleOrDefault();

                    //Calling the bill payment method
                    double?newBalance = transactionManagerClient.BillPayment(accountId, requestedAmount, "Bill Payment");

                    //Checking if bill payment method returns a value that indicates a failed transaction
                    if (newBalance == null)
                    {
                        //Throwing an exception with the right error message
                        throw new Exception(string.Format(ERROR_OCCURED, "paying bill!."));
                    }
                    //Happens if bill payment succeeds
                    else
                    {
                        //Showing the right new balance
                        lblBalance.Text = string.Format("{0:c}", newBalance);

                        //Updating the balance session variable
                        Session["Balance"] = newBalance;

                        //Clearing the amount text field
                        txtAmount.Text = string.Empty;
                    }
                }
                //Happens if a transfer is selected
                else if (ddlTransactionType.SelectedIndex == 1)
                {
                    //Quering and storing the account id logged in
                    int fromAccountId = (from result in db.BankAccounts
                                         where result.AccountNumber == accountNumber
                                         select result.BankAccountId).SingleOrDefault();

                    //Getting the receiver's account id
                    int toAccountId = int.Parse(ddlTo.Text);

                    //Calling the transfer method
                    double?newBalanceTransfer = transactionManagerClient.Transfer(fromAccountId, toAccountId, requestedAmount, "Transfer");

                    //Checking if transfer method returns a value that indicates a failed transaction
                    if (newBalanceTransfer == null)
                    {
                        //Throwing an exception with the right error message
                        throw new Exception(string.Format(ERROR_OCCURED, "transferring money!."));
                    }
                    //Happens if the transfer succeeds
                    else
                    {
                        //Showing the right new balance
                        lblBalance.Text = string.Format("{0:c}", newBalanceTransfer);

                        //Updating the balance session variable
                        Session["Balance"] = newBalanceTransfer;

                        //Clearing the amount text field
                        txtAmount.Text = string.Empty;
                    }
                }
            }
        }
        //Hanldes all thrown exceptions
        catch (Exception ex)
        {
            //Displays the error message
            lblError.Text = ex.Message;
        }
    }
Beispiel #4
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 #5
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;
            }
        }