2 Replies - 1129 Views - Last Post: 23 November 2012 - 07:26 PM Rate Topic: -----

#1 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Withdraw and deposit

Posted 23 November 2012 - 05:02 PM

Option Explicit On
Option Strict On
Option Infer Off

Imports System.Data.OleDb

Public Class frmAccountMaintenance
    Private account As BankAccount

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim accountNumber As String = Trim(txtSearchAccountNumber.Text)
        Try
            account = BankAccountDB.GetBankAccount(CStr(accountNumber))
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, ex.GetType.ToString)
        End Try
        If account Is Nothing Then
            MessageBox.Show("No bank account found with this ID. Please try again.", "Account Not Found")
            ' 
            Me.ClearControls()
        Else
            Me.DisplayBankAccount()
        End If
    End Sub

    Private Sub DisplayBankAccount()
        txtAccountNumber.Text = account.AccountNumber
        txtName.Text = account.Name
        txtBalance.Text = account.Balance.ToString("C2")
    End Sub

    Private Sub btnAddAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAccount.Click
        Dim frmAdd As New frmNewAccount
        frmAdd.ShowDialog()
        account = frmAdd.newAccount
        If frmAdd.newAccount Is Nothing Then
            MessageBox.Show("No new BankAccount was created")
        Else
            BankAccountDB.AddBankAccount(account)
            Me.DisplayBankAccount()
        End If
    End Sub

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub

    Private Sub ClearControls()
        txtAccountNumber.Text = ""
        txtName.Text = ""
        txtBalance.Text = ""
        txtSearchAccountNumber.Focus()
        txtSearchAccountNumber.SelectAll()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim result As DialogResult _
             = MessageBox.Show("Delete " & account.Name & "?",
               "Confirm Delete", MessageBoxButtons.YesNo, _
               MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            Try
                If Not BankAccountDB.DeleteBankAccount(account) Then
                    MessageBox.Show("Another user has updated or deleted " &
                        "that account.", "Database Error")
                    BankAccountDB.GetBankAccount(account.AccountNumber)
                    If account IsNot Nothing Then
                        Me.DisplayBankAccount()
                    Else
                        Me.ClearControls()
                    End If
                Else
                    Me.ClearControls()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, ex.GetType.ToString)
            End Try
        End If
    End Sub

    Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click
        Dim oldBankAccount As New BankAccount(account.AccountNumber, account.Name, account.Balance)
        Dim amount As Integer



        If amount < 0 Then
            MessageBox.Show("You can not deposit a negative number", "Try Again")
            Exit Sub
            If amount >= 0 Then
               

            End If
        End If
    End Sub
End Class



This is my code so far and I need help with the deposit and withdraw button. Any suggestions?

(FOR DEPOSIT BUTTON)

• Use an input box to get the deposit amount and convert that value into a Decimal value.
• Save the current account information in a second BankAccount instance. As an example of a statement that does this
Dim oldBankAccount As New BankAccount(account.AccountNumber, account.Name, account.Balance)
• If the amount to be deposited is negative, inform the user of this and exit the procedure.
• If the amount to be deposited is >=0, invoke the Deposit method of the account instance variable.
• Update the BankAccount table by invoking bankAccountDB.UpdateBankAccount. Use the form’s account instance variable as the newBankAccount argument, and the oldBankAccount local variable as the oldBankAccount argument.

(FOR WITHDRAW BUTTON)

This is very similar to the btnDeposit click event, except that it
• invokes the account.Withdraw method instead of account.Deposit, and
• it only attempts to withdraw if the withdrawal amount not only is positive, it is less than or equal to the current account balance.

(BANKACCOUNTDB CLASS)
 Private _accountNumber As String
    Private _name As String
    Private _balance As Decimal

    Public Shared Function AddBankAccount(ByVal account As BankAccount) As Integer
        Dim connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Bank.mdb"
        Dim connection As New OleDbConnection(connectionstring)
        Dim insertstatement As String =
            "INSERT Into BankAccount " &
            "(AccountNumber, AccountHolderName, Balance) " &
            "VALUES (?,?,?)"
        Dim insertCommand As New OleDbCommand(insertstatement, connection)
        insertCommand.Parameters.AddWithValue("?", account.AccountNumber)
        insertCommand.Parameters.AddWithValue("?", account.Name)
        insertCommand.Parameters.AddWithValue("?", account.Balance)

        Try
            connection.Open()
            Dim insertcount As Integer = insertCommand.ExecuteNonQuery()
            Return insertcount
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
    End Function

    Public Shared Function UpdateBankAccount(ByVal oldAccount As BankAccount, ByVal newaccount As BankAccount) As Boolean
        Dim connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Bank.mdb"
        Dim connection As New OleDbConnection(connectionstring)
        Dim updatestatement As String =
            "UPDATE BankAccount SET " &
            "AccountHolderName = ?, " &
            "Balance = ? " &
            "WHERE AccountNumber = ? " &
            "AND AccountHolderName = ? " &
            "AND Balance = ? "

        Dim updatecommand As New OleDbCommand(updatestatement, connection)
        updatecommand.Parameters.AddWithValue("?", newaccount.Name)
        updatecommand.Parameters.AddWithValue("?", newaccount.Balance)
        updatecommand.Parameters.AddWithValue("?", oldAccount.AccountNumber)
        updatecommand.Parameters.AddWithValue("?", oldAccount.Name)
        updatecommand.Parameters.AddWithValue("?", oldAccount.Balance)

        Try
            connection.Open()
            Dim count As Integer = updatecommand.ExecuteNonQuery
            If count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
    End Function

    Public Shared Function DeleteBankAccount(ByVal account As BankAccount) As Boolean
        Dim connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Bank.mdb"
        Dim connection As New OleDbConnection(connectionstring)
        Dim deletestatement As String =
            "DELETE FROM BankAccount " &
            "WHERE AccountNumber = ? " &
            "AND AccountHolderName = ? " &
            "AND Balance = ? "

        Dim deletecommand As New OleDbCommand(deletestatement, connection)
        deletecommand.Parameters.AddWithValue("?", account.AccountNumber)
        deletecommand.Parameters.AddWithValue("?", account.Name)
        deletecommand.Parameters.AddWithValue("?", account.Balance)

        Try
            connection.Open()
            Dim count As Integer = deletecommand.ExecuteNonQuery
            If count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
    End Function

    Public Shared Function GetBankAccount(ByVal accountNumber As String) As BankAccount

        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Bank.mdb"
        Dim connection As New OleDbConnection(connectionString)
        Dim account As BankAccount

        Dim selectStatement As String _
              = "SELECT AccountNumber, AccountHolderName, Balance FROM BankAccount where AccountNumber = ?"
        Dim commandObject As New OleDbCommand(selectStatement, connection)
        commandObject.Parameters.AddWithValue("?", accountNumber)

        Try
            connection.Open()
            Dim reader As OleDbDataReader = commandObject.ExecuteReader()
            If reader.Read Then
                Dim name As String
                Dim balance As Decimal
                accountNumber = reader("AccountNumber")
                name = reader("AccountHolderName")
                balance = reader("Balance")
                account = New BankAccount(accountNumber, name, balance)
                reader.Close()
            Else
                account = Nothing
            End If
            Return account
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try

    End Function
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Withdraw and deposit

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5526
  • View blog
  • Posts: 11,835
  • Joined: 02-June 10

Re: Withdraw and deposit

Posted 23 November 2012 - 06:49 PM

Quote

This is my code so far and I need help with the deposit and withdraw button. Any suggestions?


Suggestions about what? Your teacher already gave good instructions for what this should do. You haven't told us about any errors or exceptions.

You started writing code for the deposit and withdraw buttons... then just stopped. Keep going, you're doing fine. Giving you working code for your homework not only is against site policy but it doesn't help you learn your craft.
Was This Post Helpful? 0
  • +
  • -

#3 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Re: Withdraw and deposit

Posted 23 November 2012 - 07:26 PM

I get a squiggly line underneath this statement account.deposit and I dont know why? It says reference to a non-shared memeber requires an object reference

Sorry it says Deposit is not a member of decimal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1