Programmer's Corner - Object-Oriented Programming (OOP) Tutorial - Example - Class & Interface Executable

Backup and Security Solutions 10% off all products with promo code: VISI-P1YR
Get the Programmer's Corner FireFox Search Plug-In

Class & Interface Example

Menu Driven Executable

This is a continuation from the Class & Interface Example so make sure you have completed that before continuing on with this page.

On this page we will develop a simple menu driven console application. It will use the SavingsAccount and CheckingAccount classes and just be able to do debits and credits. To start the two classes should already be in a Console Application project from when we first started. This program will consist of two menus. The first is the type of account they user wants and the second is the debit and credit options. This will show you one of the uses of interfaces. We can have one variable be either a SavingsAccount or CheckingAccount. We will also need two variables for the choices of the two menus and the amount they wish to debit or credit.

The menus will consist of:

Account Type
Create new Savings Account
Create new Checking Account
Exit

Transaction
Display Balance
Credit Account
Debit Account
Create new Account
Exit

Since we will be taking in menu options and amounts frequently lets make two functions: GetChoice and GetAmount. GetChoice will take in a Short that is the maximum choice they can choose. GetAmount will take in a Boolean when True will allow them to enter in a negative amount, when False won't let them enter a negative number. Both will loop until a valid value is entered. For simplicity reasons I won't do any validation of what the user enters so just enter numbers.

After declaring the variables and functions you should have a Module similar to:

Module Module1
    Sub Main()
        Dim pintAccType As Integer
        Dim pintTrans As Integer
        Dim pdblAmount As Double
        Dim pAccount As IBankAccount
    End Sub

    Private Function GetChoice(ByVal MaxChoice As Short) As Integer
        Dim pintChoice As Integer
        Do Until pintChoice > 0 And pintChoice <= MaxChoice
            Console.Write("Please enter a choice from 1 to " & MaxChoice.ToString() & ": ")
            pintChoice = CInt(Console.ReadLine())
        Loop
        Return pintChoice
    End Function

    Private Function GetAmount(ByVal CanBeNegative As Boolean) As Double
        Dim pdblAmount As Double
        If CanBeNegative Then
            Console.Write("Please enter an amount: ")
            pdblAmount = CDbl(Console.ReadLine())
        Else
            pdblAmount = -1
            Do Until pdblAmount >= 0
                Console.Write("Please enter an amount (not negative): ")
                pdblAmount = CDbl(Console.ReadLine())
            Loop
        End If
        Return pdblAmount
    End Function
End Module

Now we need to build the first menu which will loop until they exit.

Do Until pintAccType = 3 ' choice 3 is exit
    Console.WriteLine ("1. Create new Savings Account" & vbCrLf & _
        "2. Create new Checking Account" & vbCrLf & _
        "3. Exit")
    pintAccType = GetChoice(3)
    Select Case pintAccType
        Case 1 ' create new savings account
            ' get starting balance
            pdblAmount = GetAmount(False)
            pAccount = New SavingsAccount(pdblAmount)
        Case 2 ' create new checking account
            pdblAmount = GetAmount(True)
            pAccount = New CheckingAccount(pdblAmount)
        Case 3 ' exit
            Exit Do
    End Select
Loop

After the select case we will need another loop that gets the transactions the user wishes to do. It will be structured similar to the first loop.

pintTrans = 0
Do While pintTrans < 4 ' choice 4 and 5 would need to exit the loop
    Console.WriteLine ("1. Display Balance" & vbCrLf & _
        "2. Do Credit" & vbCrLf & _
        "3. Do Debit" & vbCrLf & _
        "4. Create new Account" & vbCrLf & _
        "5. Exit")
    pintTrans = GetChoice(5)
    Select Case pintTrans
        Case 1 ' display balance
            Console.WriteLine ("Your balance is " & pAccount.Balance.ToString())
        Case 2 ' do credit
            pdblAmount = GetAmount(False)
            pAccount.Credit (pdblAmount)
        Case 3 ' do debit
            pdblAmount = GetAmount(False)
            If pAccount.Debit(pdblAmount) = False Then
                Console.WriteLine ("Your balance is " & pAccount.Balance.ToString() & _
                    " you cannot take out " & pdblAmount.ToString())
            Else
                Console.WriteLine ("Debit executed successfully")
            End If
        Case 4 ' create new account
            Exit Do
        Case 5 ' exit
            pintAccType = 3 ' this is to exit the outer loop
            Exit Do
    End Select
Loop

Your final code should look similar to:

Module Module1
    Sub Main()
        Dim pintAccType As Integer
        Dim pintTrans As Integer
        Dim pdblAmount As Double
        Dim pAccount As IBankAccount

        Do Until pintAccType = 3 ' choice 3 is exit
            Console.WriteLine("1. Create new Savings Account" & vbCrLf & _
                "2. Create new Checking Account" & vbCrLf & _
                "3. Exit")
            pintAccType = GetChoice(3)
            Select Case pintAccType
                Case 1 ' create new savings account
                    ' get starting balance
                    pdblAmount = GetAmount(False)
                    pAccount = New SavingsAccount(pdblAmount)
                Case 2 ' create new checking account
                    pdblAmount = GetAmount(True)
                    pAccount = New CheckingAccount(pdblAmount)
                Case 3 ' exit
                    Exit Do
            End Select
            Do While pintTrans < 4 ' choice 4 and 5 would need to exit the loop
                Console.WriteLine("1. Display Balance" & vbCrLf & _
                    "2. Do Credit" & vbCrLf & _
                    "3. Do Debit" & vbCrLf & _
                    "4. Create new Account" & vbCrLf & _
                    "5. Exit")
                pintTrans = GetChoice(5)
                Select Case pintTrans
                    Case 1 ' display balance
                        Console.WriteLine("Your balance is " & pAccount.Balance.ToString())
                    Case 2 ' do credit
                        pdblAmount = GetAmount(False)
                        pAccount.Credit(pdblAmount)
                    Case 3 ' do debit
                        pdblAmount = GetAmount(False)
                        If pAccount.Debit(pdblAmount) = False Then
                            Console.WriteLine("Your balance is " & pAccount.Balance.ToString() & _
                                " you cannot take out " & pdblAmount.ToString())
                        Else
                            Console.WriteLine("Debit executed successfully")
                        End If
                    Case 4 ' create new account
                        Exit Do
                    Case 5 ' exit
                        pintAccType = 3 ' this is to exit the outer loop
                        Exit Do
                End Select
            Loop
        Loop
    End Sub

    Private Function GetChoice(ByVal MaxChoice As Short) As Integer
        Dim pintChoice As Integer
        Do Until pintChoice > 0 And pintChoice <= MaxChoice
            Console.Write("Please enter a choice from 1 to " & MaxChoice.ToString() & ": ")
            pintChoice = CInt(Console.ReadLine())
        Loop
        Return pintChoice
    End Function

    Private Function GetAmount(ByVal CanBeNegative As Boolean) As Double
        Dim pdblAmount As Double
        If CanBeNegative Then
            Console.Write("Please enter an amount: ")
            pdblAmount = CDbl(Console.ReadLine())
        Else
            pdblAmount = -1
            Do Until pdblAmount >= 0
                Console.Write("Please enter an amount (not negative): ")
                pdblAmount = CDbl(Console.ReadLine())
            Loop
        End If
        Return pdblAmount
    End Function
End Module

Notice that the code in the second loop in Main doesn't change depending on the the type of account. Since both accounts implement the IBankAccount interface they have the necessary properties and methods to act as an account but can do different things in those properties and methods.

Now test the program by starting with a new Savings Account. Give it a balance and try to Debit more than the starting balance. Then Debit and amount less than the starting balance and credit the account with any amount (positive). Create a new Checking Account and give it a balance. Now when you try to Debit more than the starting balance it will allow you and when you display the balance you will see a negative number.

Congratulation you have completed the Class & Interface Example. You may now proceed to the Introduction to Inheritance tutorial.

Author Information:

Adam Schentag

http://www.programmers-corner.com

adam@programmers-corner.com

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

There are currently no comments available.