I have the following classes for a bank account. I am hoping someone can point me to where the problems in my code are. When I run the program my output is:
account1
Inheritance_Part1.SavingsAccount
Interest rate: 0.00%
Interest earned: $25.00
Balance: $25.00
account2
Inheritance_Part1.CheckingAccount
Transaction fee: $1.00
Balance: $80.00
Attempting to debit account1 by $20.00
Interest rate: 0.00%
Interest earned: $20.00
Balance: $20.00
Attempting to debit account2 by $40.00
Transaction fee: $1.00
Balance: $40.00
Crediting $40.00 to account1
Interest rate: 0.00%
Interest earned: $20.00
Balance: $20.00
Crediting $65.00 to account2
Transaction fee: $1.00
Balance: $40.00
When it should say...
account1
Inheritance_Part1.SavingsAccount
Interest rate: 3.00%
Interest earned: $0.06
Balance: $25.06
account2
Inheritance_Part1.CheckingAccount
Transaction fee: $1.00
Balance: $80.00
Attempting to debit account1 by $20.00
Interest rate: 3.00%
Interest earned: $0.01
Balance: $5.08
Attempting to debit account2 by $40.00
Transaction fee: $1.00
Balance: $39.00
Crediting $40.00 to account1
Interest rate: 3.00%
Interest earned: $0.11
Balance: $45.19
Crediting $65.00 to account2
Transaction fee: $1.00
Balance: $103.00
Here are the codes:
Public Class Account
Private balanceValue As Decimal
Public Sub New(ByVal balance As Decimal)
balanceValue = balance
End Sub
Public Property Balance() As Decimal
Get
Return balanceValue
End Get
Set(ByVal value As Decimal)
If value >= 0.0 Then
balanceValue = value
Else
balanceValue = CDec(0.0)
End If
End Set
End Property
Overridable Sub Credit(ByVal value As Decimal)
value = balanceValue + value
End Sub
Public Overridable Function Debit(ByVal value As Decimal) As Boolean
If value > balanceValue Then
value = CDec(False)
ElseIf value <= balanceValue Then
balanceValue = value
value = CDec(True)
End If
Return CBool(value)
End Function
Public Overrides Function ToString() As String
Return (String.Format("Balance: {0:C2}", Balance) & vbCrLf)
End Function
End Class
Public Class SavingsAccount
Inherits Account
Dim interestRate As Double
Public Sub New(ByVal balance As Decimal, ByVal rate As Double)
MyBase.New(balance)
rate = interestRate
End Sub
Public Property Rate() As Double
Get
Return interestRate
End Get
Set(ByVal value As Double)
If value >= 0.0 Then
interestRate = Rate
Else
interestRate = 0.0
End If
End Set
End Property
Public Function CalculateInterest() As Decimal
Dim interest As Decimal
interest = CDec(Balance * interestRate / 12)
interest = interest + Balance
Return interest
End Function
Public Overrides Function ToString() As String
Dim message As String
message = String.Format("Interest rate: {0:p2}", Rate) & vbCrLf
message &= String.Format("Interest earned: {0:c2}", CalculateInterest()) & vbCrLf
message &= MyBase.ToString()
Return message
End Function
End Class
Public Class CheckingAccount
Inherits Account
Private transactionFee As Decimal
Public Sub New(ByVal balance As Decimal, ByVal transaction As Decimal)
MyBase.New(balance)
Fee = transaction
End Sub
Public Property Fee() As Decimal
Get
Return transactionFee
End Get
Set(ByVal value As Decimal)
If value >= 0.0 Then
transactionFee = value
Else
transactionFee = CDec(0.0)
End If
End Set
End Property
Public Overrides Sub Credit(ByVal value As Decimal)
MyBase.Credit(value)
value = Balance - transactionFee
End Sub
Public Overrides Function Debit(ByVal value As Decimal) As Boolean
Return MyBase.Debit(value)
If True Then
value = Balance - transactionFee
Return True
Else
Return False
End If
End Function
Public Overrides Function ToString() As String
Dim message As String
message = String.Format("Transaction fee: {0:C2}", Fee) & vbCrLf
message &= MyBase.ToString()
Return message
End Function
End Class
Public Class AccountTesterForm
Private Sub AccountTesterForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim account1 As New SavingsAccount(25, 0.03)
Dim account2 As New CheckingAccount(80, 1)
' display initial balance of each object
balanceLabel.Text = "account1" & vbCrLf & Convert.ToString(account1.GetType()) & vbCrLf & account1.ToString() & vbCrLf
balanceLabel.Text &= "account2" & vbCrLf & Convert.ToString(account2.GetType()) & vbCrLf & account2.ToString() & vbCrLf
' debit each account and show new balance
balanceLabel.Text &= "Attempting to debit account1 by $20.00." & vbCrLf
If account1.Debit(20) = True Then
balanceLabel.Text &= account1.ToString() & vbCrLf
End If
balanceLabel.Text &= "Attempting to debit account2 by $40.00." & vbCrLf
If account2.Debit(40) Then
balanceLabel.Text &= account2.ToString() & vbCrLf
End If
' credit each account and show new balance
balanceLabel.Text &= "Crediting $40.00 to account1." & vbCrLf
account1.Credit(40)
balanceLabel.Text &= account1.ToString() & vbCrLf
balanceLabel.Text &= "Crediting $65.00 to account2." & vbCrLf
account2.Credit(65)
balanceLabel.Text &= account2.ToString()
End Sub
End Class