ThomasII 0 Newbie Poster

Hello-
I am trying to create a program that will allow a user to enter in checking or savings data; date, type of transaction (deposit or withdrawl), display the previous balance and the new balance based on the new transaction entered. I cannot figure out how to locate the element of the prior transaction to make the appropriate calculation. My code is quite long, and I should mention that I am brand new to this forum, so I hope I have placed my code in here properly. Thanks ahead of time.

Imports System.IO
Public Class frmAccounts
    Dim calc As Account
    Dim activity As Transaction
    Private Sub frmAccounts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtDate.Text = CStr(Today)                          'Displays today's date 
        lblTo.Visible = False
    End Sub

    Private Sub cboAccounts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAccounts.SelectedIndexChanged
        lstTransactions.Items.Clear()
        activity = New Transaction
        Dim nameOfAccount As String = cboAccounts.Text
        Dim listDisplay As ListBox
        If cboAccounts.Text = "Savings" Then
            grpCheck.Visible = False
            activity.AcctName = "Savings"
        Else
            grpCheck.Visible = True
            activity.AcctName = "Checking"
        End If
        listDisplay = lstTransactions
        calc = New Account
        AddHandler calc.approvedTransaction, AddressOf ApprovedFunds
        AddHandler calc.negativeBalance, AddressOf insufficientFunds
        calc.ReadFile(nameOfAccount, listDisplay)
        txtBalance.Text = FormatCurrency(calc.Balance)
    End Sub
    Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click
        'Sends all deposit data to WriteTransaction, which writes the user data to either csvSAVINGS.TXT or csvCHECKING.TXT 
        activity = New Transaction                          'instance of Transaction 
        calc = New Account                                  'instance of Account 
        activity.TransactionDate = CStr(Today)              'Date of transaction 
        activity.AcctName = cboAccounts.Text                '=Savings or Checking Account chose on form 
        activity.TransactionAmount = CDbl(txtDeposit.Text)  'Amount depositing into Account selected above 
        activity.DepoOrWith = grpDeposit.Text               'Description for writing to file 
        calc.CalcBalance(activity)
        calc.WriteTransaction(activity)                     'WriteTransaction Method in Class Account, writes all user data to selected file.
        txtBalance.Text = FormatCurrency(calc.Balance)
    End Sub
    Private Sub insufficientFunds(ByVal m_balance As Double)

    End Sub
    Private Sub ApprovedFunds()
        MessageBox.Show("Transaction Complete", "Approved")
    End Sub
End Class 'frmAccounts 
Class Transaction
    Private m_prevBal As Double
    Private m_date As Date                                  'Today's date = CStr(Today) 
    Private m_creditDebit As String                         'Deposit or Withdrawl 
    Private m_amount As Double                              'Amount for the transaction 
    Private m_checkOrSave As String                         'Checking or Savings 
    Public Property TransactionDate() As Date               'Date of transaction being made 
        Get
            Return m_date
        End Get
        Set(ByVal value As Date)
            m_date = value
        End Set
    End Property
    Public Property DepoOrWith() As String                  'Deposit or Withdrawl (for both checking and savings)-->DESCRIPTION of transaction 
        Get
            Return m_creditDebit
        End Get
        Set(ByVal value As String)
            m_creditDebit = value
        End Set
    End Property
    Public Property TransactionAmount() As Double           'Amount on transaction attempting to complete 
        Get
            Return m_amount
        End Get
        Set(ByVal value As Double)
            m_amount = value
        End Set
    End Property
    Public Property AcctName() As String                    'Checking or Savings account 
        Get
            Return m_checkOrSave
        End Get
        Set(ByVal value As String)
            m_checkOrSave = value
        End Set
    End Property
    Public Property PreviousBalance() As Double
        Get
            Return m_prevBal
        End Get
        Set(ByVal value As Double)
            m_prevBal = value
        End Set
    End Property
    Public ReadOnly Property NewBalance() As Double
        Get
            If m_creditDebit = "Deposit" Then           'Need to somehow inclued the transfer from and check clearings.
                NewBalance = m_prevBal + m_amount
                Return NewBalance
            ElseIf m_creditDebit.ToLower = "Withdrawl" Then
                NewBalance = m_prevBal - m_amount
                Return NewBalance
            End If
        End Get
    End Property
End Class
Class Account
    Private m_Balance As Double                         'Balance for either checking or savings 
    Private m_Savings As Account
    Private m_transSet() As Transaction
    Private m_Checking As Account
    Dim fileInput() As String
    Public Event negativeBalance(ByVal m_balance As Double)
    Public Event approvedTransaction()
    Public Property SavAccount() As Account
        Get
            Return m_Savings
        End Get
        Set(ByVal value As Account)
            m_Savings = value
        End Set
    End Property
    Public Property CheckAccount() As Account
        Get
            Return m_Checking
        End Get
        Set(ByVal value As Account)
            m_Checking = value
        End Set
    End Property
    Public ReadOnly Property Balance() As Double
        Get
            Return m_transSet(m_transSet.Length - 1).NewBalance
        End Get
    End Property
    Public WriteOnly Property transDate() As Date
        Set(ByVal value As Date)
            m_transSet(0).TransactionDate = value
        End Set
    End Property
    Public WriteOnly Property Description() As String
        Set(ByVal value As String)
            m_transSet(1).DepoOrWith = value
        End Set
    End Property
    Public WriteOnly Property Amount() As Double
        Set(ByVal value As Double)
            m_transSet(2).TransactionAmount = value
        End Set
    End Property
    Public WriteOnly Property AccountName() As String
        Set(ByVal value As String)
            m_transSet(3).AcctName = value
        End Set
    End Property
    Sub ReadFile(ByVal NameofAccount As String, ByRef listDisplay As ListBox)
        Dim fmtStr As String = "{0,-10} {1,-30}{2,20:c} {3, 10}"
        Dim tran As Transaction
        Dim x As Integer = 0
        Dim sr As StreamReader = Nothing
        If NameofAccount = "Savings" Then
            sr = File.OpenText("csvSAVINGS.TXT")
        Else                                            'If NameofAccount = "Checking" Then 
            sr = File.OpenText("csvCHECKING.TXT")
        End If
        Do While (sr.Peek <> -1)
            tran = New Transaction
            fileInput = sr.ReadLine.Split(","c)
            tran.TransactionDate = fileInput(0)
            tran.DepoOrWith = fileInput(1)
            tran.TransactionAmount = fileInput(2)
            tran.PreviousBalance = fileInput(3)
            ReDim Preserve m_transSet(x)
            m_transSet(x) = tran
            listDisplay.Items.Add(String.Format(fmtStr, tran.TransactionDate.ToShortDateString, tran.DepoOrWith, _
            FormatCurrency(tran.TransactionAmount) & " PREV BAL", FormatCurrency(tran.PreviousBalance) & _
            " New BAL" & FormatCurrency(tran.NewBalance)))
            x += 1
            tran = Nothing
        Loop
        sr.Close()
    End Sub
    Function CalcBalance(ByVal activity As Transaction) As Double
        Dim tran As Transaction
        Dim x As Integer = -1
        Dim sr As StreamReader = Nothing
        If activity.AcctName = "Savings" Then
            sr = File.OpenText("csvSAVINGS.TXT")
        ElseIf activity.AcctName = "Checking" Then
            sr = File.OpenText("csvCHECKING.TXT")
        End If
        Do While (sr.Peek <> -1)
            tran = New Transaction
            fileInput = sr.ReadLine.Split(","c)
            tran.TransactionDate = fileInput(0)
            tran.DepoOrWith = fileInput(1)
            tran.TransactionAmount = fileInput(2)
            tran.PreviousBalance = fileInput(3)
            ReDim Preserve m_transSet(x)
            m_transSet(x) = tran
            x += 1
            tran = Nothing
        Loop
        sr.Close()
        If Balance <= 0 Then
            RaiseEvent negativeBalance(Balance)
        Else
            RaiseEvent approvedTransaction()
        End If
        Return Balance
    End Function
    Sub WriteTransaction(ByVal activity As Transaction)
        CalcBalance(activity)
        Dim temp() As String = {activity.TransactionDate & "," & activity.DepoOrWith.ToString & "," & _
        activity.TransactionAmount & "," & Balance}
        Select Case activity.AcctName
            Case Is = "Savings"
                Dim sw As StreamWriter = File.AppendText("csvSAVINGS.TXT")
                sw.WriteLine(Join(temp, ","))
                sw.Close()
            Case Is = "Checking"
                Dim sw As StreamWriter = File.AppendText("csvCHECKING.TXT")
                sw.WriteLine(Join(temp, ","))
                'RaiseEvent approvedTransaction(temp(0)) 
                sw.Close()
            Case Is = "Transfer to Checking"
                Dim sw1 As StreamWriter = File.AppendText("csvCHECKING.TXT")
                Dim sw2 As StreamWriter = File.AppendText("csvSAVINGS.TXT")
                Dim temp2() As String = {activity.TransactionDate & "," & activity.AcctName & "," & activity.TransactionAmount}
                sw1.WriteLine(Join(temp, ","))
                sw2.WriteLine(Join(temp2, ","))
                sw1.Close()
                sw2.Close()
            Case Is = "Transfer to Savings"
                Dim sw1 As StreamWriter = File.AppendText("csvSAVINGS.TXT")
                Dim sw2 As StreamWriter = File.AppendText("csvCHECKING.TXT")
                Dim temp2() As String = {activity.TransactionDate & "," & activity.AcctName & "," & activity.TransactionAmount}
                sw1.WriteLine(Join(temp, ","))
                sw2.WriteLine(Join(temp2, ","))
                sw1.Close()
                sw2.Close()
        End Select
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.