FDSc in ICT Building a Program in C#
Objectives To build a complete application in C# from scratch Make a banking app Make use of: Methods/Functions Classes Inheritance
Scenario We have a bank that has accounts. Cheque accounts Savings accounts We need a teller to make withdrawals and retrieve balances The teller must update the balance when a withdrawal is made
Lets Look at This... How many classes do we need? What is the user in this scenario Who does what?
Create a New Project Create a new project in VS called Banking Rename Form1.cs to frmteller.cs Change the text of the form to Teller
The Interface Form: frmteller Text: Teller Label1: Account Number: Label2: Balance: Label3: Amount: textbox1: txtaccountnumber textbox2: txtbalance textbox3: txtamount radiobutton1: radcheque radiobutton2: radsavings button1: btngetbalance button2: btnwithdraw
Lets Look at This... Can you work out from the form who does what? What of your previous decisions will be acted out by each event?
Add Classes Right click on the bold letters of Banking Click on Add then on class Rename it to Account.cs Similarly, create 2 more classes called ChequeAccount.cs and SavingsAccount.cs
Account.cs The Account class needs to be able to give the balance [GetBalance()] It also needs to determine which account is being used (savings or cheque) It also needs to determine the account number It needs to have a withdrawal function/method [Withdraw()]
After Account.cs is Created using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Banking class Account
public int intaccountnumber; public double dblbalance; //We will have a function/method here to check the account number and get the balance //We need a withdraw method //We need a new balance method
The GetBalance() Method protected double GetBalance(int AccountNumber) intaccountnumber = AccountNumber; //Data normally retrieved from database. Hardcoded for demo only if (intaccountnumber == 1) return 1000; else if (intaccountnumber == 2) return 2000; else throw new Exception("Account number incorrect.");
The Withdraw() Method public double Withdraw(int AccountNumber, double Amount) dblbalance = GetBalance(AccountNumber); if (dblbalance >= Amount) dblbalance -= Amount; return dblbalance; else throw new Exception("Not enough funds.");
The NewBalance() Method public double newbalance(int AccountNumber) dblbalance = GetBalance(AccountNumber); return dblbalance;
Make Savings Class Inherit class SavingsAccount : Account
Make Cheque Class Inherit class ChequeAccount : Account
The Form We will need an If statement to differentiate between which radio button has been clicked
Lets Populate the GetBalance Button Double click on the GetBalance button private void btngetbalance_click(object sender, EventArgs e)
Right after the InitializeComponent(); brace, add //Create new accounts ChequeAccount ochequeaccount = new ChequeAccount(); SavingsAccount osavingsaccount = new SavingsAccount();
Add the following in the button event: private void btngetbalance_click(object sender, EventArgs e) //Check which radio button has been selected (or not) try if (radcheque.checked) txtbalance.text = ochequeaccount.newbalance(convert.toint32(txtaccountnumber.text)).tostring(); else if (radsavings.checked) txtbalance.text = osavingsaccount.newbalance(convert.toint32(txtaccountnumber.text)).tostring(); catch (Exception eexception) MessageBox.Show(eException.Message);
Lets Populate the Withdraw Button Double Click the Withdraw button private void btnwithdraw_click(object sender, EventArgs e)
try txtbalance.text = osavingsaccount.withdraw(convert.toint32(txtaccou ntnumber.text), Convert.ToDouble(txtAmount.Text)).ToString(); catch (Exception eexception) MessageBox.Show(eException.Message);
Add a Program Entry Point Create a new class called program.cs After the [STATREAD] add: static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmteller());
Run your App