Bank Account Simulation

Bank Account Simulation System

The program features of the system will generate the deposit, withdrawal, transfer and sign check of the whole system, this system is not using any third party application to use on its database.

Output:



Source Code:

Form1

Public Class Form1
    Dim WithEvents chkAccount As Account = New Account("Checking")
    Dim WithEvents scAccount As Account = New Account("Savings")
    Dim WithEvents _transaction As Transaction = New Transaction()
    Dim chkAccountMode As Boolean
    Dim transferFlag As Boolean = False
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        chkAccountMode = True
        cboAccount.SelectedIndex = 0
        txtDate.Text = DateTime.Today.Date
    End Sub
    Private Sub cboAccount_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboAccount.SelectedIndexChanged
        Dim accountToDisplay As String = cboAccount.SelectedItem.ToString()
        Dim acc As Account
        If accountToDisplay = "Checking" Then
            chkAccountMode = True
            acc = chkAccount
        Else
            chkAccountMode = False
            acc = chkAccount
        End If
        acc.AccountBalance = 0
        Transaction.ReadTransactionFiles(acc)
        Presentation.ChangeForm(acc)

    End Sub
    Private Function AccountUsed() As Account
        If chkAccountMode Then
            Return chkAccount
        End If
        Return scAccount
    End Function
    Private Sub btnDeposit_Click(sender As Object, e As EventArgs) Handles btnDeposit.Click, btnWithdraw.Click, btnSign.Click
        Dim transType As String = Nothing
        Dim transAmount As Double
        Dim transName As String = Nothing
        Dim whatTextbox As TextBox = Nothing
        Dim addOrTake As Integer = 1
        If sender Is btnDeposit Then
            transType = "Deposit"
            transName = "Deposit"
        ElseIf sender Is btnWithdraw Then
            transType = "Withdrawal"
            transName = "Withdrawal"
            whatTextbox = txtWithdraw
            addOrTake = -1
        Else
            transType = "Withdrawal"
            transName = "Check to the order of " & txtOrder.Text
            whatTextbox = txtAmountPay
            addOrTake = -1
        End If
        transAmount = _transaction.TryTransaction(whatTextbox, addOrTake, AccountUsed())
        _transaction.CreateTransaction(transName, transType, transAmount, AccountUsed)
    End Sub
    Sub CommittedTransaction() Handles _transaction.TransactionCommitted
        _transaction.isTransactionCommitted = True
        If Not transferFlag Then
            MessageBox.Show("Transaction has been added.")
            Transaction.ReadTransactionFiles(AccountUsed)
            Presentation.ChangeForm(AccountUsed)
        End If
    End Sub
    Sub InsufficientFunds() Handles chkAccount.InsufficientFunds, scAccount.InsufficientFunds
        _transaction.isTransactionCommitted = False
        MessageBox.Show("Insufficient Funds for this Transaction")
    End Sub
    Sub AmountLessThanZero() Handles chkAccount.AmountLessThanZero, scAccount.AmountLessThanZero
        _transaction.isTransactionCommitted = False
        MessageBox.Show("Amount must be greater than zero.")
    End Sub
    Sub AccountChanged(accName As String) Handles chkAccount.AccountChange, scAccount.AccountChange
        Label8.Text = accName
        Label10.Text = If(chkAccountMode = True, "Savings", "Checking")
    End Sub
    Private Sub btnTransfer_Click(sender As Object, e As EventArgs) Handles btnTransfer.Click
        Dim transAmount As Double = _transaction.TryTransaction(txtTransfer, -1, AccountUsed())
        _transaction.CreateTransaction("Transfer To " & If(AccountUsed().AccountName = "Checking", "Savings", "Checking"), "Withdrawal", transAmount, AccountUsed())
        transferFlag = True
        txtTransfer.Text = transAmount
        chkAccountMode = Not (chkAccountMode)
        _transaction.CreateTransaction("Transfer From " & If(AccountUsed().AccountName = "Checking", "Savings", "Checking"), "Deposit", transAmount, AccountUsed())
        chkAccountMode = Not (chkAccountMode)
        transferFlag = False
    End Sub
End Class
Class: Presentation.vb

Public Class Presentation
    Public Shared Sub ChangeForm(acc As Account)
        SetGridView()
        RefreshForm(acc.AccountName)
        DisplayGrid()
    End Sub
    Public Shared Sub SetGridView()
        Form1.DataGridView1.DataSource = Nothing
        Form1.DataGridView1.Rows.Clear()
        Form1.DataGridView1.ColumnCount = 5
        Form1.DataGridView1.Columns(0).HeaderText = "Date"
        Form1.DataGridView1.Columns(1).HeaderText = "Description"
        Form1.DataGridView1.Columns(2).HeaderText = "Amount"
        Form1.DataGridView1.Columns(3).HeaderText = "New Balance"
        Form1.DataGridView1.Columns(4).HeaderText = "Transaction type"
    End Sub
    Public Shared Sub RefreshForm(accName As String)
        If accName = "Savings" Then
            Form1.GroupBox5.Visible = False
            Form1.Label8.Text = "Savings"
            Form1.Label10.Text = "Checking"
        Else
            Form1.GroupBox5.Visible = True
            Form1.Label8.Text = "Checking"
            Form1.Label10.Text = "Savings"
        End If
        Form1.txtWithdraw.Clear()
        Form1.txtDeposit.Clear()
        Form1.txtAmountPay.Clear()
        Form1.txtOrder.Clear()
        Form1.txtTransfer.Clear()
    End Sub
    Public Shared Sub DisplayGrid()
        Dim trans As List(Of Transaction) = Transaction.TransactionList
        Dim acc As List(Of Double) = Transaction.NewBalanceList
        For i As Integer = 0 To trans.Count - 1
            Form1.DataGridView1.Rows.Add(trans(i).TransactionDate.ToString("MM/dd/yyyy"), trans(i).TransactionName, trans(i).TransactionAmount, acc(i), trans(i).TransactionDescription)
        Next
        DisplayBalance(acc(acc.Count - 1))
    End Sub
    Private Shared Sub DisplayBalance(balance As Double)
        Form1.txtBalance.Text = FormatCurrency(balance, 2)
    End Sub
End Class
Class: Transaction.vb

Imports System.IO
Imports System.Object
Imports System.Text
Public Class Transaction
    Public Property TransactionName() As String
    Public Property TransactionAmount() As Double
    Public Property TransactionDate() As Date
    Public Property TransactionType() As Boolean
    Public Property TransactionDescription() As String
    Public Property isTransactionCommitted() As Boolean
    Private Shared trans As List(Of Transaction) = New List(Of Transaction)
    Private Shared newBalance As List(Of Double) = New List(Of Double)()
    Sub New()
    End Sub
    Sub New(tDate As Date, tName As String, tAmount As Double, tType As Boolean)
        TransactionName = tName
        TransactionAmount = tAmount
        TransactionDate = tDate
        TransactionType = tType
        isTransactionCommitted = False
        TransactionDescription = If(TransactionType = True, "Deposit", "Withdrawal")
    End Sub
    Public Shared ReadOnly Property TransactionList() As List(Of Transaction)
        Get
            Return trans
        End Get
    End Property
    Public Shared ReadOnly Property NewBalanceList() As List(Of Double)
        Get
            Return newBalance
        End Get
    End Property
    Public Shared Sub ReadTransactionFiles(acc As Account)
        Dim tDate As Date
        Dim tName As String
        Dim tAmount As Double
        Dim tType As Boolean
        trans.Clear()
        newBalance.Clear()
        acc.AccountBalance() = 0
        Using myReader As New FileIO.TextFieldParser(
            acc.AccountName + ".txt")
            myReader.TextFieldType = FileIO.FieldType.Delimited
            myReader.SetDelimiters(",")
            Dim line As String()
            While Not (myReader.EndOfData)
                line = myReader.ReadFields()
                If Date.TryParse(line(0), tDate) Then
                    tDate = line(0)
                End If
                tName = line(1)
                tAmount = If((Double.TryParse(line(2), tAmount)), line(2), MsgBox("Amount " + line(2) + " is not a valid number"))
                tType = line(3)
                trans.Add(New Transaction(tDate, tName, tAmount, tType))
                newBalance.Add(acc.GetAccountBalance(tType, tAmount))
            End While
        End Using
    End Sub
    Public Function TryTransaction(whatTextbox As TextBox, addOrTake As Integer, acc As Account) As Double
        Dim transAmount As Double
        Dim deposit As Boolean = If(addOrTake > 0, True, False)
        If Double.TryParse(whatTextbox.Text, transAmount) Then
            If acc.CheckAccountBalance(transAmount, deposit) Then
                acc.AccountBalance += transAmount * addOrTake
                isTransactionCommitted = True
            End If
        Else
            MsgBox("Invalid Input")
            Return False
        End If
        Return transAmount
    End Function
    Public Sub CreateTransaction(transName As String, transType As String, transAmount As Double, acc As Account)
        Dim trans As Transaction
        If isTransactionCommitted Then
            TransactionDate = DateTime.Today.Date
            TransactionName = transName
            TransactionAmount = transAmount
            TransactionType = If(transType = "Deposit", True, False)
            CommitTransaction(acc)
        End If
    End Sub
    Private Sub CommitTransaction(acc As Account)
        Using w As StreamWriter = File.AppendText(acc.AccountName & ".txt")
            w.WriteLine(TransactionDate & "," & TransactionName & "," & TransactionAmount & "," & TransactionType.ToString())
            w.Close()
            RaiseEvent TransactionCommitted()
        End Using
    End Sub
    Public Event TransactionCommitted()
End Class
Class: Account.vb

Public Class Account
    Public Property AccountName() As String
    Public Property AccountBalance() As Double
    Sub New(aName As String)
        AccountName = aName
    End Sub
    Public Function GetAccountBalance(tType As Boolean, tAmount As Double) As Double
        If (tType) Then
            AccountBalance += tAmount
        Else
            AccountBalance -= tAmount
        End If
        Return AccountBalance
    End Function
    Public Function CheckAccountBalance(tAmount As Double, deposit As Boolean) As Boolean
        If tAmount <= 0 Then
            RaiseEvent AmountLessThanZero()
            Return False
        End If
        If Not deposit And AccountBalance - tAmount < 0 Then
            RaiseEvent InsufficientFunds()
            Return False
        End If
        Return True
    End Function
    Public Sub ChangeAccount()
        RaiseEvent AccountChange(AccountName)
    End Sub
    Public Event AccountChange(accName As String)
    Public Event AmountLessThanZero()
    Public Event InsufficientFunds()
End Class

Credits: Youtube

DOWNLOAD LINK: BankAccountVS2013

Bank Account Simulation Bank Account Simulation Reviewed by code-dev on 3:32 PM Rating: 5

No comments:

Powered by Blogger.