Create an object inheritance hierarchy that a bank might use to represent customer’s bank accounts. Here are the specifications:
Class hierarchy:
- Create a Windows Form project.
- Add a Class “Account”(base class)anddefine the following members (Use the names given below):
- Instance variable balanceValue
Decimal type, stores the account balance
- Constructor Sub New
Need one Decimal parameter, initialize instance variable balanceValue using the parameter
- Property Balance
- Define Get method to return balanceValue
- Define Set method
a)If paramater >=0.0, assign it to balanceValue
b)If paramater < 0.0, assign 0.0 to balanceValue.
- Sub Credit
- Declared as overridable, so its derived classes could override it.
- Need one Decimal parameter
- Add this parameter to balanceValue
- Function Debit
- Declared as overridable, so its derived classes could override it.
- Return a Boolean value
- Need one Decimal parameter
- If parameter > balanceValue, then just return False.
- If parameter <= balanceValue, deduct parameter value from balanceValue and return True.
- Function ToString
- Overrides Object’s ToString function
- Display account’s balance
- Please use the following definition in order to make testing easier.
'overrides ToString() to display account's information
PublicOverridesFunction ToString() AsString
Return (String.Format("Balance: {0:C2}", Balance) & vbCrLf)
EndFunction
- Add Class “CheckingAccount”in the project, which inherits from class Accountand includes the following members (Use the names given below):
- Instance variable transactionFee
Decimal type, stores the fee chared per transaction
- Constructor Sub New
- Need two Decimal parameter
- Call base class’s constructor to set up base class part
- Initializes instance variable transactionFee using parameter’s value, if parameter < 0.0, initialize transactionFee to 0
- Property Fee
- Define Get method to return transactionFee
- Define Set method
a)If paramater >=0.0, assign it to transactionFee
b)If paramater < 0.0, assign 0.0 to transactionFee.
- Sub Credit
- Overrides base class’s Credit method.
- Call base class’s Credit method to perform credit transaction
- Deduct transactionFee from balance
- Function Debit
- Overrides base class’s Debit method.
- Call base class’s Debit method to perform debit transaction
a)If the call returns True (successful), deduct transactionFee from balance and return True
b)If the call returns False (unsuccessful), do NOT deduct transactionFee from balance, just return False
- Function ToString
- Overrides base class’s ToString function
- Display transaction fee and balance (calling base class’s method)
- Please use the following definition in order to make testing easier.
'overrides ToString() function to display checking's information
PublicOverridesFunction ToString() AsString
Dim message AsString
message = String.Format("Transaction fee: {0:C2}", Fee) & vbCrLf
message &= MyBase.ToString()
Return message
EndFunction
- Add another class “SavingsAccount”inproject, which inherits from base class “Account”and includs the following members(Use the names given below):
- Instance variable interestRate
Doublel type, stores the inerest rate of savings account
- Constructor Sub New
- Need one Decimal parameter and one Double parameter
- Call base class’s constructor to set up base class part
- Initializes instance variable interestRate usingthe 2ndparameter’s value, if parameter < 0.0, initialize inerestRate to 0
- Property Rate
- Define Get method to return interestRate
- Define Set method
a)If paramater >=0.0, assign it to interestRate
b)If paramater < 0.0, assign 0.0 to interestRate
- Function CalculateInterest
- No parameter
- Return a decimal value
- interest = balance * interestRate / 12
- add this interest to balance
- return the interest
- Function ToString
- Overrides base class’s ToString function
- Display interest rate and interest amount, as well as balance (calling base class’s method)
- Please use the following definition in order to make testing easier.
'overrides ToString() function to display savingsAccount's information
PublicOverridesFunction ToString() AsString
Dim message AsString
message = String.Format("Interest rate: {0:p2}", Rate) & vbCrLf
message &= String.Format("Interest earned: {0:c2}", CalculateInterest()) & vbCrLf
message &= MyBase.ToString()
Return message
EndFunction
- Now, let’s test the classes, rename the file “Form1.vb” to “AccountTesterForm.vb”, create “Form_Load” event handler method, copy the following code and paste into the “Form_Load” method. Please do not modify the code in this tester in order to make testing easier.
Dim account1 AsNewSavingsAccount(25, 0.03)
Dim account2 AsNewCheckingAccount(80, 1)
' display initial balance of each object
balanceLabel.Text = "account1" & vbCrLf & Convert.ToString(account1.GetType()) & vbCrLf & account1.ToString() & vbCrLf
balanceLabel.Text &= "account2" & vbCrLf & Convert.ToString(account2.GetType()) & vbCrLf & account2.ToString() & vbCrLf
' debit each account and show new balance
balanceLabel.Text &= "Attempting to debit account1 by $20.00." & vbCrLf
If account1.Debit(20) = TrueThen
balanceLabel.Text &= account1.ToString() & vbCrLf
EndIf
balanceLabel.Text &= "Attempting to debit account2 by $40.00." & vbCrLf
If account2.Debit(40) Then
balanceLabel.Text &= account2.ToString() & vbCrLf
EndIf
' credit each account and show new balance
balanceLabel.Text &= "Crediting $40.00 to account1." & vbCrLf
account1.Credit(40)
balanceLabel.Text &= account1.ToString() & vbCrLf
balanceLabel.Text &= "Crediting $65.00 to account2." & vbCrLf
account2.Credit(65)
balanceLabel.Text &= account2.ToString()
- Run your project in your computer, you should get the similaroutput, If not, please debug your code and run it again.