Purpose: Demonstrate the ability to create a graphical user interface using the
ID: 3535213 • Letter: P
Question
Purpose: Demonstrate the ability to create a graphical user interface using the Microsoft Foundation Classes (MFC).
For this assignment you will need Microsoft Visual Studio 2010 or 2012, professional edition.
In the OnInitDialog function, create three accounts with varying balances. Use account numbers 1111, 2222, and 3333. This will make your program easy to test.
Add a graphical interface to the banking system from http://pastebin.com/b8fCDAiV :
Step 1:Using Visual Studio, create a dialog box that looks like this:
Step 2:If the Deposit button is pressed, deposit the amount in the Amount control to the account and show, in the status area, the new balance.
Step 3:If the Withdraw button is pressed, withdraw the amount in the Amount control and show, in the status area, the new balance. If there isn't enough money to complete the withdrawal, show the account balance and a message.
Step 2:In general, do not let the user do invalid things. For example, if the Amount box does not contain a valid number, do not attempt to do a deposit or withdrawal and let the program crash. If there is nothing in the Account field, again, do not let your program crash if the user presses one of the buttons. Use the status area (a static text field, shown as label4 in the above screen) to display messages. You may have difficulty changing the background of this label; it defaults to grey. Don't worry about that.
Explanation / Answer
hOPE THIS HELPS, VERY CRUDE THOUGH:
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
BANKACC 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
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 = what???
If amount <= 0 Then
MessageBox.Show("You can not deposit a negative number", "Try Again")
Exit Sub
elseIf amount > 0 Then
oldBankAccount.balance += amount
End If
End Sub
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.