Polymorphism I - VB.NET

Exercise on Polymorphism
This exercise uses a variety of access types.

The base class BankAccount is abstract. The job of the base class is to generate a new account number each time a derived class is instantiated. The maximum single debit is $50. NextNumber is a shared propery. The Balance property in the Student class returns the balance + the student's overdraftamount.

The PrintStatement prints the account number and the balance.
The PrintSpecialComments identifies the Student's overdraft limit.




Class Diagram

In a console app, write a procedure to process the student account by receiving a base class type polymorphically. The procedure should load up all the properties and use all the methods.



Public MustInherit Class BankAccount

    Private mBalance As Double
    Private mNextNumber As Integer
    Private mNumber As Integer

    Protected ReadOnly Property Balance As Double
        Get
            Return mBalance
        End Get
    End Property

    Public MustOverride Property MaxSingleDebit As Double
 
    Public Shared Property NextNumber As Integer

    Public Sub Credit(Amount As Double)
        mBalance += Amount
    End Sub

    Public Function Debit(Amount As Double) As Boolean
        If Amount > MaxSingleDebit Then
            Throw New ArgumentException("The amount exceeds the Maximum single debit amount")
        Else
            mBalance -= Amount
            Return True
        End If
    End Function

    Protected Sub New(Balance As Double)
        NextNumber += 1
        mNumber = NextNumber
        mBalance = Balance
    End Sub

    Public MustOverride Sub PrintSpecialComments()

    Public Overridable Sub PrintStatement()
        Console.WriteLine("Account number: " & mNumber)
        Console.WriteLine("Balance: " & mBalance.ToString("C"))
    End Sub

End Class


Public Class StudentAccount
    Inherits BankAccount

    Private mOverdraftLimit As Double

    Protected Shadows ReadOnly Property Balance As Double
        Get
            Return MyBase.Balance
        End Get
    End Property

    Public Overrides Property MaxSingleDebit As Double

    Public Sub IncreaseLimit(Amount As Double)
        _MaxSingleDebit += Amount
    End Sub

    Public Sub New(Balance As Double, OverdraftLimit As Double)
        MyBase.New(Balance)
        mOverdraftLimit = OverdraftLimit
        MaxSingleDebit = 50

    End Sub

    Public Overrides Sub PrintSpecialComments()
        Console.WriteLine("Overdraft limit: " & mOverdraftLimit.ToString("c"))
        Console.WriteLine("Student overdraft limit: " & mOverdraftLimit.ToString("c"))
        Console.WriteLine("Maximum single withdrawal: " & MaxSingleDebit.ToString("c"))
    End Sub

    Public Overrides Sub PrintStatement()
        MyBase.PrintStatement()
    End Sub
End Class

Public Class MyApp

    Public Sub Main()
        Try
            Dim x As BankAccount = New StudentAccount(3050, 310)
            Dim y As BankAccount = New StudentAccount(5050, 510)
            x.Credit(100)
            x.Debit(50)
            y.Credit(100)
            y.Debit(50)
            UseAnyKindOfAccount(x)
            Console.WriteLine()
            UseAnyKindOfAccount(y)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try


    End Sub

    Public Sub UseAnyKindOfAccount(anAccount As BankAccount)
        Dim x As StudentAccount
        x = TryCast(anAccount, StudentAccount)
        x.PrintStatement()
        x.PrintSpecialComments()
    End Sub
End Class


Imports ClassLib

Module Module1

    Sub Main()
        Dim x As New MyApp
        x.Main()
        Console.ReadLine()
    End Sub

End Module

Read more


The First Post

So this is my first post of this blog.   What my plans are with this blog is to post examples of programming code that I have done along the way.  I am currently going through college learning all kinds of nifty thing to do with coding and I thought I could share it.

Currently my coding skills are somewhat limited to Visual Basic, T-SQL,  JavaScript, and HTML.  Over time I would like to be able to place here all the useful code compiled into a giant compilation of everything I covered over the 2 year course.

As well if there are any requests for tutorials or anything let me know and maybe I can see what I can come up with.

Read more

About This Blog

My Blogs

Followers

Web hosting for webmasters