}//end of back // Method to to create new account // If a student exists it will create a new acount that will be linked to existing student // If there is no student it will create a student and a new account private void btnAdd_Click(object sender, EventArgs e) { //variable to control display of succesfull record insetion bool success = false; BLLAccountManager manager = new BLLAccountManager(); List <Account> accList = new List <Account>(); //following two lines are here to chech if we have account type allready created List <Account> currentExist = new List <Account>(1); List <Account> savingtExist = new List <Account>(1); accList = manager.GetAccountDetailsAsList(); //Creating empty account Account acc = new Account(); int accNo; int studIdFromDb = 0; if (!ExistingStud) { if (ValidateChildren(ValidationConstraints.Enabled)) { PopulateStudent(); manager.AddStudent(Student, out studIdFromDb); success = true; } else { success = false; } //PopulateStudent(); //manager.AddStudent(Student, out studIdFromDb); //manager.AddStudent(Student, out studIdFromDb); acc.StudentID = studIdFromDb; } else if (ExistingStud) { acc.StudentID = int.Parse(txtStudentId.Text); } string accType = ""; //getting all accs for a given student that are current or saving currentExist = accList.Where(Account => Account.AccountType == "current" && Account.StudentID == acc.StudentID).ToList(); savingtExist = accList.Where(Account => Account.AccountType == "saving" && Account.StudentID == acc.StudentID).ToList(); acc.SortCode = int.Parse(txtSortCode.Text); string initBal = txtInitialBalance.Text; int initBal2 = MoneyTransformer.TransformMoney(initBal); acc.InitialBalance = initBal2; // setting the current Balance to the same value as the inital balance acc.CurrentBalance = initBal2; string ovaDraft = txtOverdraft.Text; int ovaDraft2 = MoneyTransformer.TransformMoney(ovaDraft); acc.OverDraftLimit = ovaDraft2; if (rdoCurrentAcc.Checked) { //checking if student has a current account allready if (currentExist.Any()) { MessageBox.Show("Student allready has a current account"); } else { accType = "current"; acc.AccountType = accType; if (ValidateChildren(ValidationConstraints.Enabled)) { manager.AddAccount(acc, out accNo); success = true; } else { success = false; } } } else if (rdoSavingsAcc.Checked) { //checking if student has saving account allready. if (savingtExist.Any()) { MessageBox.Show("Student allready has saving account"); } else { accType = "saving"; acc.AccountType = accType; if (ValidateChildren(ValidationConstraints.Enabled)) { manager.AddAccount(acc, out accNo); success = true; } else { success = false; } } } if (success) { MessageBox.Show("New Account Created"); } else { MessageBox.Show("No account created try again with correct details"); } }//end of add acc button click
}//end of NoEdit //click to initiate a withdraw private void btnWithdraw_Click(object sender, EventArgs e) { // get account type string accType = ""; if (rdoCurrentAcc.Checked) { accType = "current"; } else if (rdoSavingsAcc.Checked) { accType = "saving"; } // for old balance string ob = txtCurrentBal.Text; ob = ob.Replace("€", ""); ob = ob.Replace(".", ""); ob = ob.Replace(",", "");//this is needed if the sum is larger string newBbb = ob; int oldBalance; oldBalance = int.Parse(ob);//Convert.ToInt32(ob); // get overdraft amount string od = txtOverdraft.Text; od = od.Replace("€", ""); od = od.Replace(".", ""); od = od.Replace(",", ""); int ovaDraft; ovaDraft = Convert.ToInt32(od); // negative OverDraft to compare new balance with int negOvaDraft = -ovaDraft; // for withdraw amount string wd = txtWithdraw.Text; int withdrawAmount = MoneyTransformer.TransformMoney(wd); // setting the new balance int newBalance; newBalance = oldBalance - withdrawAmount; // bool set to true for transaction to complete bool completeTrans = false; if (newBalance >= 0) { completeTrans = true; } else if (newBalance >= negOvaDraft) { completeTrans = true; } if (completeTrans) { int accNum = int.Parse(txtAccountNum.Text); BLLTransactionManager bllTransMngr = new BLLTransactionManager(); bllTransMngr.WithdrawMonies(accNum, newBalance, accType); //Start of code to deal with transaction int transID = 0; TransactionRecord trans = new TransactionRecord(); trans.TransactionType = TransactionType; trans.TransactionAmmount = withdrawAmount; trans.AccountNumber = accNum; trans.Date = DateTime.Now; trans.Balance = newBalance; bllTransMngr.CreateTransaction(trans, out transID); // end of transaction // reseting the Withdraw field to blank txtWithdraw.Text = ""; // to display the new balance out to the user, ensuring "0.00" string displayBal = newBalance.ToString(); if (displayBal.Count() >= 2) { int c = displayBal.Count(); displayBal = displayBal.Insert((c - 2), "."); } else if (displayBal.Count() == 1) { displayBal = "0.0" + displayBal; } // displaying the new balance to the user, before exiting the screen MessageBox.Show("Withdraw Complete, New Balance = €" + displayBal); // reseting the Current Balance txtCurrentBal.Text = "€" + displayBal; } else { MessageBox.Show("Insufficent funds!", "Please try again"); } }//end of Withdraw money
}//end of NoEdit //button click to deposit money private void btnDeposit_Click(object sender, EventArgs e) { // getting the account type string accType = ""; if (rdoCurrentAcc.Checked) { accType = "current"; } else if (rdoSavingsAcc.Checked) { accType = "saving"; } // to get the old balance string ob = txtCurrentBal.Text; // removing the Euro Sign ob = ob.Replace("€", ""); // by removing the period, the value equates to the integer value ob = ob.Replace(".", ""); ob = ob.Replace(",", ""); int oldBalance; oldBalance = Convert.ToInt32(ob); // converting the deposit amount from a string to int string da = txtDeposit.Text; int depositAmount = MoneyTransformer.TransformMoney(da); int newBalance; newBalance = oldBalance + depositAmount; int accNum = int.Parse(txtAccountNum.Text); BLLTransactionManager bllTransMngr = new BLLTransactionManager(); // sending the Desposit Money through the layers bllTransMngr.DepositMonies(accNum, newBalance, accType); // Start of code to deal with transaction int transID = 0; TransactionRecord trans = new TransactionRecord(); trans.TransactionType = TransactionType; trans.TransactionAmmount = depositAmount; trans.AccountNumber = accNum; trans.Date = DateTime.Now; trans.Balance = newBalance; bllTransMngr.CreateTransaction(trans, out transID); // end of transaction // reseting the Deposit field to blank txtDeposit.Text = String.Empty; // to display the new balance out to the user, ensuring "0.00" string displayBal = newBalance.ToString(); int c = displayBal.Count(); displayBal = displayBal.Insert((c - 2), "."); // displaying the new balance to the user, before exiting the screen MessageBox.Show("Deposit Complete, New Balance = €" + displayBal); // reseting the Current Balance txtCurrentBal.Text = "€" + displayBal; }//end of deposit button click
}//end of event handler //transfer button click event to transfer money from one account to aother private void btnTransfer_Click(object sender, EventArgs e) { //Note that we are creatinf a transaction here in order to make sure that if something goes //wrong in a middle of transfering money our database stays in consistent state. using (TransactionScope transaction = new TransactionScope()) { try { BLLTransactionManager manager = new BLLTransactionManager(); //Dealing with getting monney from one acc first int transferAmmount = MoneyTransformer.TransformMoney(txtAmmount.Text); int debitedAccNewBal = SourceAccount.CurrentBalance - transferAmmount; // new balance for credited account int creditedAccNewBal = DestinationAccount.CurrentBalance + transferAmmount; bool transferPossible = false; // setting the possibility of transaction to true if (SourceAccount.CurrentBalance >= transferAmmount) { transferPossible = true; } else { MessageBox.Show("Not Enough funds"); } if (transferPossible) { //testing to make sure saving account doesnt transfer to external if (SourceAccount.AccountType.Equals("saving")) { if (!ExternalTransfer) { manager.WithdrawMonies(SourceAccount.AccountNo, debitedAccNewBal, SourceAccount.AccountType); //This code can be uncommented in order to show that database doesnt change if there was an error or exceprion somewhere //Exception ex = new Exception(); //if (ex != null) //{ // MessageBox.Show("Opps SOMETHING WENT WRONG HERE !!!"); //} //throw ex; manager.DepositMonies(DestinationAccount.AccountNo, creditedAccNewBal, DestinationAccount.AccountType); int debitTransNo = 0; int creditTransNo = 0; //setting and creating transaction for debit TransactionRecord debitTrans = new TransactionRecord(); debitTrans.AccountNumber = SourceAccount.AccountNo; debitTrans.TransactionType = TransactionType.debit; debitTrans.Date = DateTime.Now; debitTrans.TransactionAmmount = transferAmmount; debitTrans.Balance = debitedAccNewBal; //creating a transaction for debited account manager.CreateTransaction(debitTrans, out debitTransNo); //setting and creating transaction for credit TransactionRecord creditTrans = new TransactionRecord(); creditTrans.AccountNumber = DestinationAccount.AccountNo; creditTrans.TransactionType = TransactionType.credit; creditTrans.Date = DateTime.Now; creditTrans.TransactionAmmount = transferAmmount; creditTrans.Balance = creditedAccNewBal; //creating a transaction for credit account manager.CreateTransaction(creditTrans, out creditTransNo); //Create Transfer Record object first TransferRecord record = new TransferRecord(); record.RecepientName = txtRecepientName.Text; record.DebitedAccount = int.Parse(txtAccountNumber.Text); //NOTE here I am setting the record transaction number to the number created for debited account record.TransactionNumber = debitTransNo; record.DestinationSortCode = txtDestinationSortCode.Text; record.DestinationAccNo = int.Parse(txtAccOutNumber.Text); record.TransferAmmount = transferAmmount; record.TransferDate = DateTime.Now; record.TransferDescription = rtfDescription.Text; bool success = false; //creating new transfer record success = manager.CreateTransfer(record); transaction.Complete(); transaction.Dispose(); if (success) { MessageBox.Show("Transfer Successfull"); } } else { MessageBox.Show("Cant transfer to an external account from saving account\nOnly internal transfers are allowed"); } } else { manager.WithdrawMonies(SourceAccount.AccountNo, debitedAccNewBal, SourceAccount.AccountType); //This code can be uncommented in order to show that database doesnt change if there was an error or exceprion somewhere //Exception ex = new Exception(); //if (ex != null) //{ // MessageBox.Show("Opps SOMETHING WENT WRONG HERE !!!"); //} //throw ex; manager.DepositMonies(DestinationAccount.AccountNo, creditedAccNewBal, DestinationAccount.AccountType); int debitTransNo = 0; int creditTransNo = 0; //setting and creating transaction for debit TransactionRecord debitTrans = new TransactionRecord(); debitTrans.AccountNumber = SourceAccount.AccountNo; debitTrans.TransactionType = TransactionType.debit; debitTrans.Date = DateTime.Now; debitTrans.TransactionAmmount = transferAmmount; debitTrans.Balance = debitedAccNewBal; //creating a transaction for debited account manager.CreateTransaction(debitTrans, out debitTransNo); //setting and creating transaction for credit TransactionRecord creditTrans = new TransactionRecord(); creditTrans.AccountNumber = DestinationAccount.AccountNo; creditTrans.TransactionType = TransactionType.credit; creditTrans.Date = DateTime.Now; creditTrans.TransactionAmmount = transferAmmount; creditTrans.Balance = creditedAccNewBal; //creating a transaction for credit account manager.CreateTransaction(creditTrans, out creditTransNo); //Create Transfer Record object first TransferRecord record = new TransferRecord(); record.RecepientName = txtRecepientName.Text; record.DebitedAccount = int.Parse(txtAccountNumber.Text); //NOTE here I am setting the record transaction number to the number created for debited account record.TransactionNumber = debitTransNo; record.DestinationSortCode = txtDestinationSortCode.Text; record.DestinationAccNo = int.Parse(txtAccOutNumber.Text); record.TransferAmmount = transferAmmount; record.TransferDate = DateTime.Now; record.TransferDescription = rtfDescription.Text; bool success = false; //creating new transfer record success = manager.CreateTransfer(record); transaction.Complete(); transaction.Dispose(); if (success) { MessageBox.Show("Transfer Successfull"); } } } } catch (TransactionException ex) { transaction.Dispose(); MessageBox.Show("OOpppps somethinge went WRONG there"); throw ex; } catch (Exception ex) { MessageBox.Show("No money were transfered"); } } }//End of TransferClick