10-1 Create an object inheritance hierarchy

profileTopsolutions
 (Not rated)
 (Not rated)
Chat

Create an object inheritance hierarchy that a bank might use to represent customer’s bank accounts. Here are the specifications:

 

Class hierarchy:

 

  1. Create a Windows Form project.
  2. Add a Class “Account” (base class)anddefine the following members (Use the names given below):
    1. Instance variable balanceValue

Decimal type, stores the account balance

    1. Constructor Sub New

Need one Decimal parameter, initialize instance variable balanceValue using the parameter

    1. Property Balance

                                                              i.      Define Get method to return balanceValue

                                                            ii.      Define Set method

a)      If paramater >=0.0, assign it to  balanceValue

b)     If paramater < 0.0, assign 0.0 to balanceValue.

    1. Sub Credit

                                                              i.      Declared as overridable, so its derived classes could override it.

                                                            ii.      Need one Decimal parameter

                                                          iii.      Add this parameter to balanceValue

    1. Function Debit

                                                              i.      Declared as overridable, so its derived classes could override it.

                                                            ii.      Return a Boolean value

                                                          iii.      Need one Decimal parameter

                                                          iv.      If parameter > balanceValue, then just return False.

                                                            v.      If parameter <= balanceValue, deduct parameter value from balanceValue and return True.

    1. Function ToString

                                                              i.      Overrides Object’s ToString function

                                                            ii.      Display account’s balance

                                                          iii.      Please use the following definition in order to make testing easier.

 

'overrides ToString() to display account's information

Public Overrides Function ToString() As String

        Return (String.Format("Balance: {0:C2}", Balance) & vbCrLf)

End Function

 

  1. Add Class “CheckingAccount” in the project, which inherits from class Account and includes the following members (Use the names given below):
    1. Instance variable transactionFee

Decimal type, stores the fee chared per transaction

    1. Constructor Sub New

                                                              i.      Need two Decimal parameter

                                                            ii.      Call base class’s constructor to set up base class part

                                                          iii.      Initializes instance variable transactionFee using parameter’s value, if parameter  < 0.0, initialize transactionFee to 0

    1. Property Fee

                                                              i.      Define Get method to return transactionFee

                                                            ii.      Define Set method

a)      If paramater >=0.0, assign it to  transactionFee

b)     If paramater < 0.0, assign 0.0 to transactionFee.

    1. Sub Credit

                                                              i.      Overrides base class’s Credit method.

                                                            ii.      Call base class’s Credit method to perform credit transaction

                                                          iii.      Deduct transactionFee from balance

    1. Function Debit

                                                              i.      Overrides base class’s Debit method.

                                                            ii.      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

    1. Function ToString

                                                              i.      Overrides base class’s ToString function

                                                            ii.      Display transaction fee and balance (calling base class’s method)

                                                          iii.      Please use the following definition in order to make testing easier.

 

'overrides ToString() function to display checking's information

Public Overrides Function ToString() As String

        Dim message As String

        message = String.Format("Transaction fee: {0:C2}", Fee) & vbCrLf

        message &= MyBase.ToString()

        Return message

End Function

 

  1. Add another class “SavingsAccount” inproject,  which inherits from base class “Account” and includs the following members(Use the names given below):
    1. Instance variable interestRate

Doublel type, stores the inerest rate of savings account

    1. Constructor Sub New

                                                              i.      Need one Decimal parameter and one Double parameter

                                                            ii.      Call base class’s constructor to set up base class part

                                                          iii.      Initializes instance variable interestRate using the 2nd parameter’s value, if parameter  < 0.0, initialize inerestRate to 0

    1. Property Rate

                                                              i.      Define Get method to return interestRate

                                                            ii.      Define Set method

a)      If paramater >=0.0, assign it to  interestRate

b)     If paramater < 0.0, assign 0.0 to interestRate

    1. Function CalculateInterest

                                                              i.      No parameter

                                                            ii.      Return a decimal value

                                                          iii.      interest  = balance * interestRate / 12

                                                          iv.      add this interest to balance

                                                            v.      return the interest

    1. Function ToString

                                                              i.      Overrides base class’s ToString function

                                                            ii.      Display interest rate and interest amount, as well as balance (calling base class’s method)

                                                          iii.      Please use the following definition in order to make testing easier.

 

'overrides ToString() function to display savingsAccount's information

Public Overrides Function ToString() As String

        Dim message As String

        message = String.Format("Interest rate: {0:p2}", Rate) & vbCrLf

        message &= String.Format("Interest earned: {0:c2}", CalculateInterest()) & vbCrLf

        message &= MyBase.ToString()

        Return message

End Function

 

  1. 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.

 

BalanceLabel

 

BankAccountForm

 

 

 

        Dim account1 As New SavingsAccount(25, 0.03)

        Dim account2 As New CheckingAccount(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) = True Then

            balanceLabel.Text &= account1.ToString() & vbCrLf

        End If

 

        balanceLabel.Text &= "Attempting to debit account2 by $40.00." & vbCrLf

        If account2.Debit(40) Then

            balanceLabel.Text &= account2.ToString() & vbCrLf

        End If

 

        ' 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()

 

  1. Run your project in your computer, you should get the similar output, If not, please debug your code and run it again.

    • 12 years ago
    complete solution
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      accountapp.zip