EDN Admin
Well-known member
I have written code that shows a simple bank account. Checking & Savings - deposit and withdrawal. I am stuck on how I would code a transfer from a checking acc into a savings account in one transaction. I have added my code below thus far.Option Strict On
Public Class frmAccountTester
Dim account As New Account()
Private Sub frmAccountTester_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
displayBalance()
lblBalance.Text = String.Format("{0:C}")
End Sub
Private Sub btnDepositChecking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepositChecking.Click
Try
Account.depositChecking(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Deposit amount must be positive.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnDepositSavings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepositSavings.Click
Try
account.depositSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Deposit amount must be positive.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnWithdrawChecking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdrawChecking.Click
Try
account.withdrawChecking(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Withdrawal amount must be greater than 0 " & "and less than or equal to the account balance.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnWithdrawSavings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdrawSavings.Click
Try
account.withdrawSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Withdrawal amount must be greater than 0 " & "and less than or equal to the account balance.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnMoveCtoS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveCtoS.Click
End Sub
Private Sub btnMoveStoC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveStoC.Click
Try
account.depositSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Deposit amount must be positive.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
Try
account.withdrawSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Withdrawal amount must be greater than 0 " & "and less than or equal to the account balance.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub displayBalance()
lblBalance.Text = "Checking balance: " & String.Format("{0:C}",
account.Checking) & vbCrLf & "Savings balance: " & String.Format("{0:C}",
account.Savings)
End Sub
End Class
Public Class Account
Private savingsBalance As Decimal
Private checkingBalance As Decimal
Public Sub New()
savingsBalance = 1000.0
checkingBalance = 1000.0
End Sub
Public Property Savings() As Double
Get
Return savingsBalance
End Get
Set(ByVal value As Double)
If value < 0 Then
savingsBalance = 0
Else
savingsBalance = value
End If
End Set
End Property
Public Property Checking() As Double
Get
Return checkingBalance
End Get
Set(ByVal value As Double)
If value < 0 Then
checkingBalance = 0
Else
checkingBalance = value
End If
End Set
End Property
Public Sub depositChecking(ByVal amount As Decimal)
if depositAmount is less than or equal to 0, throw an exception
If amount <= 0D Then
Throw New ArgumentOutOfRangeException(
"Deposit amount must be positive.")
End If
checkingBalance += amount
End Sub
Public Sub depositSavings(ByVal amount As Decimal)
if depositAmount is less than or equal to 0, throw an exception
If amount <= 0D Then
Throw New ArgumentOutOfRangeException(
"Deposit amount must be positive.")
End If
savingsBalance += amount
End Sub
Public Sub withdrawChecking(ByVal amount As Decimal)
If amount > checking Then
Throw New ArgumentOutOfRangeException("Withdrawal amount must be less than or eqaul to balance.")
ElseIf amount <= 0D Then
Throw New ArgumentOutOfRangeException("Withrawal amount must be positive.")
End If
Checking -= amount
End Sub
Public Sub withdrawSavings(ByVal amount As Decimal)
If amount > Savings Then
Throw New ArgumentOutOfRangeException("Withdrawal amount must be less than or eqaul to balance.")
ElseIf amount <= 0D Then
Throw New ArgumentOutOfRangeException("Withrawal amount must be positive.")
End If
Savings -= amount
End Sub
Public Sub moveMoneyCheckingToSavings(ByVal amount As Double)
End Sub
Public Sub moveMoneySavingsToChecking(ByVal amount As Double)
if depositAmount is less than or equal to 0, throw an exception
If amount <= 0D Then
Throw New ArgumentOutOfRangeException(
"Deposit amount must be positive.")
If amount > Savings Then
Throw New ArgumentOutOfRangeException("Withdrawal amount must be less than or eqaul to balance.")
ElseIf amount <= 0D Then
Throw New ArgumentOutOfRangeException("Withrawal amount must be positive.")
End If
End If
checkingBalance += amount
Savings -= amount
End Sub
Public ReadOnly Property savBalance As Decimal
Get
Return savingsBalance
End Get
End Property
Public ReadOnly Property checkBalance As Decimal
Get
Return checkingBalance
End Get
End Property
End Class
View the full article
Public Class frmAccountTester
Dim account As New Account()
Private Sub frmAccountTester_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
displayBalance()
lblBalance.Text = String.Format("{0:C}")
End Sub
Private Sub btnDepositChecking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepositChecking.Click
Try
Account.depositChecking(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Deposit amount must be positive.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnDepositSavings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepositSavings.Click
Try
account.depositSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Deposit amount must be positive.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnWithdrawChecking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdrawChecking.Click
Try
account.withdrawChecking(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Withdrawal amount must be greater than 0 " & "and less than or equal to the account balance.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnWithdrawSavings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdrawSavings.Click
Try
account.withdrawSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Withdrawal amount must be greater than 0 " & "and less than or equal to the account balance.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub btnMoveCtoS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveCtoS.Click
End Sub
Private Sub btnMoveStoC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveStoC.Click
Try
account.depositSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Deposit amount must be positive.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
Try
account.withdrawSavings(Convert.ToDecimal(txtAmount.Text))
displayBalance()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Withdrawal amount must be greater than 0 " & "and less than or equal to the account balance.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtAmount.Clear()
txtAmount.Focus()
End Sub
Private Sub displayBalance()
lblBalance.Text = "Checking balance: " & String.Format("{0:C}",
account.Checking) & vbCrLf & "Savings balance: " & String.Format("{0:C}",
account.Savings)
End Sub
End Class
Public Class Account
Private savingsBalance As Decimal
Private checkingBalance As Decimal
Public Sub New()
savingsBalance = 1000.0
checkingBalance = 1000.0
End Sub
Public Property Savings() As Double
Get
Return savingsBalance
End Get
Set(ByVal value As Double)
If value < 0 Then
savingsBalance = 0
Else
savingsBalance = value
End If
End Set
End Property
Public Property Checking() As Double
Get
Return checkingBalance
End Get
Set(ByVal value As Double)
If value < 0 Then
checkingBalance = 0
Else
checkingBalance = value
End If
End Set
End Property
Public Sub depositChecking(ByVal amount As Decimal)
if depositAmount is less than or equal to 0, throw an exception
If amount <= 0D Then
Throw New ArgumentOutOfRangeException(
"Deposit amount must be positive.")
End If
checkingBalance += amount
End Sub
Public Sub depositSavings(ByVal amount As Decimal)
if depositAmount is less than or equal to 0, throw an exception
If amount <= 0D Then
Throw New ArgumentOutOfRangeException(
"Deposit amount must be positive.")
End If
savingsBalance += amount
End Sub
Public Sub withdrawChecking(ByVal amount As Decimal)
If amount > checking Then
Throw New ArgumentOutOfRangeException("Withdrawal amount must be less than or eqaul to balance.")
ElseIf amount <= 0D Then
Throw New ArgumentOutOfRangeException("Withrawal amount must be positive.")
End If
Checking -= amount
End Sub
Public Sub withdrawSavings(ByVal amount As Decimal)
If amount > Savings Then
Throw New ArgumentOutOfRangeException("Withdrawal amount must be less than or eqaul to balance.")
ElseIf amount <= 0D Then
Throw New ArgumentOutOfRangeException("Withrawal amount must be positive.")
End If
Savings -= amount
End Sub
Public Sub moveMoneyCheckingToSavings(ByVal amount As Double)
End Sub
Public Sub moveMoneySavingsToChecking(ByVal amount As Double)
if depositAmount is less than or equal to 0, throw an exception
If amount <= 0D Then
Throw New ArgumentOutOfRangeException(
"Deposit amount must be positive.")
If amount > Savings Then
Throw New ArgumentOutOfRangeException("Withdrawal amount must be less than or eqaul to balance.")
ElseIf amount <= 0D Then
Throw New ArgumentOutOfRangeException("Withrawal amount must be positive.")
End If
End If
checkingBalance += amount
Savings -= amount
End Sub
Public ReadOnly Property savBalance As Decimal
Get
Return savingsBalance
End Get
End Property
Public ReadOnly Property checkBalance As Decimal
Get
Return checkingBalance
End Get
End Property
End Class
View the full article