Exercise 1:: 1-Create 2 Classes: 2
Exercise 1:: 1-Create 2 Classes: 2
6- After the Account class, add the following code to create the SavingsAccount derived
classe:
class SavingsAccount : Account
{
}
7- Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 2:
Testing the Classes
To test the classes, follow these steps:
1. Open the Teller form in the code editor and locate the btnGetBalance click event code.
2. Inside the event procedure, prior to the Try block, declare and instantiate a variable of type
CheckingAccount called oCheckingAccount, a variable of type SavingsAccount called
oSavingsAccount, and a variable of type Account called oAccount:
3. Depending on which radio button is selected, call the GetBalance method of the
appropriate object and pass the account number value from the Account Number text box.
Show the return value in the Balance text box. Place the following code in the Try block prior
to the Catch statement:
if (rdbChecking.Checked)
{
txtBalance.Text =
oCheckingAccount.GetBalance(int.Parse(txtAccountNumber.Text)).ToString();
}
else if (rdbSavings.Checked)
{
txtBalance.Text =
oSavingsAccount.GetBalance(int.Parse(txtAccountNumber.Text)).ToString();
}
else if (rdbGeneral.Checked)
{
txtBalance.Text =
oAccount.GetBalance(int.Parse(txtAccountNumber.Text)).ToString();
}
4. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
5. Select Debug ➤ Start to run the project. Enter an account number of 1 and click the Get
Balance button for the Checking Account type. You should get a balance of 1,000. Test the
other account types. You should get the same result, since all classes are using the same
GetBalance function defined in the base class.
6. After testing, close the form, which will stop the debugger.
Exercise 3:
if (rdbChecking.Checked)
{
oCheckingAccount.AccountNumber = int.Parse(txtAccountNumber.Text);
txtBalance.Text =
oCheckingAccount.Withdraw(double.Parse(txtAmount.Text)).ToString();
}
else if (rdbSavings.Checked)
{
oSavingsAccount.AccountNumber = int.Parse(txtAccountNumber.Text);
txtBalance.Text =
oSavingsAccount.Withdraw(double.Parse(txtAmount.Text)).ToString();
}
3. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
4. Select Debug ➤ Start to run the project.
5. Enter an account number of 1, choose the Checking option button, and click the Get
Balance button.You should get a balance of 1000.
6. Enter a withdrawal amount of 200 and click the Withdraw button. You should get a
resulting balance of 800.
7. Enter a withdrawal amount of 700 and click the Withdraw button. You should get an
insufficient funds message because the resulting balance would be less than the minimum
balance of 200.
8. Enter an account number of 1, choose the Savings option button, and click the Get Balance
button. You should get a balance of 1000.
9. Enter a withdrawal amount of 600 and click the Withdraw button. You should get a
resulting balance of 400.
10. Enter a withdrawal amount of 400 and click the Withdraw button. You should get a
resulting balance of 0 because there is no minimum balance for the savings account that uses
the Account base class’s Withdraw method.
11. After testing, close the form, which will stop the debugger.
Exercise 7:
3. Change the Withdraw method of the CheckingAccount class so that after it checks for
sufficient funds, it calls the Withdraw method of the Account base class.
4. Add a Withdraw method to the SavingsAccount class that is similar to the Withdraw
method of the CheckingAccount class but does not check for a minimum balance.
5. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 8:
4. Add the oCheckingAccount and oSavingsAccount to the list using the Add method of the
list.
AccountList.Add(oCheckingAccount);
AccountList.Add(oSavingsAccount);
5. Loop through the list and call the GetAccountInfo method of each Account type in
the list and show the results in a console window.
6. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
7. Select Debug ➤ Start to run the project. You should see a console window with
the return string for the GetAccountInfo method of each object in the list.
8. After testing the polymorphism, hit the enter key to close the console window,
which will stop the debugger.
Exercise 10:
Implementing Polymorphism Using an Interface
To implement polymorphism using an interface, follow these steps:
1. View the code for the Account.cs file in the code editor.
2. Comment out the code for the Account, CheckingAccount, and SavingsAccount
classes.
3. Define an interface IAccount that contains the GetAccountInfo method.
public interface IAccount
{
string GetAccountInfo();
}
4. Add the following code to create two classes: CheckingAccount and SavingsAccount.
These classes will implement the IAccount interface.
5. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 11:
Testing the Polymorphic Interface Method
To test the polymorphic method, follow these steps:
1. Open the Program.cs file in the code editor and locate the Main method.
2. Change the code to instantiate an instance of a list of IAccount types.
List<IAccount> AccountList = new List<IAccount>();
3. Change the for each loop to loop through the list and call the GetAccountInfo()
method of each IAccount type in the list.
4. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
5. Select Debug ➤ Start to run the project. You should see a console window with
the return string for the GetAccountInfo method of each object in the list.
6. After testing the polymorphism, hit the enter key to close the console window,
which will stop the debugger.