Visual Basic ATM程序

时间:2015-11-09 18:06:56

标签: vb.net

我正在尝试使用预览框在Visual Basic中编写ATM程序,但每当我在继续按钮之前计时预览按钮时,我的值“加倍”。例如,如果我在存款和点击预览的交易价值框中输入10,我得到160,但是当我点击继续时,我得到170.

任何想法都可能出错?我知道我必须在预览值后重置天平,但我不知道如何。

Public Class transactionsGBox
    Const SERVICE_CHARGE_DECIMAL As Decimal = 6.5
    Const PIN As Integer = 9343
    Dim Balance As Decimal = 150

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
    End Sub
    Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles topUpButton.CheckedChanged
    End Sub
    Private Sub transactionsGBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Function withdraw(ByVal amount As Decimal)
        Balance -= amount
        Return Balance
    End Function
    Private Function deposit(ByRef amount As Decimal)
        Balance += amount
        Return Balance
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton1.Click
    End Sub
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles confirmButton.Click
        If pinBox.Text = "9343" Then
            transactionGroupBox.Enabled = True
            previewButton.Enabled = True
            proceedButton.Enabled = True
            pinBox.Enabled = False
        Else
            MessageBox.Show("Incorrect pin, try again", "Pin Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub
    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton2.Click
    End Sub
    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub
    Private Sub previewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previewButton.Click
        If depositButton.Checked = True Then
            previewBalance.Text = deposit(transactionValueBox.Text)
        Else
            previewBalance.Text = withdraw(transactionValueBox.Text)
        End If
    End Sub
    Private Sub proceedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles proceedButton.Click
        If depositButton.Checked = True Then
            finalBalance.Text = deposit(transactionValueBox.Text)
        Else
            finalBalance.Text = withdraw(transactionValueBox.Text)
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

你可以通过做三件事来解决这个问题:

1。)在预览按钮Click事件中创建局部变量作为“余额预览”变量,该变量使用余额变量的值进行定义。

2。)修改Withdraw和Deposit函数以接受两个传递的变量(一个用于金额,一个用于余额)。

3.)修改预览并继续单击事件模块以传递适当的余额变量(进行余额或预览的“余额预览”)。

所以存款/取款功能的新代码如下:

Private Function deposit(amount As Decimal, ByRef result As Decimal)
   result += amount
   Return result
End Function

确保在结果变量中包含ByRef,因为您希望通过引用传递它(意味着您希望实际更改传递的第二个变量中的值)。

previewButton的新代码如下:

Private Sub previewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previewButton.Click

    Dim balancePreview As Decimal = Balance

    If depositButton.Checked = True Then
        previewBalance.Text = deposit(transactionValueBox.Text, balancePreview)
    Else
        previewBalance.Text = withdraw(transactionValueBox.Text, balancePreview)

    End If


End Sub

Proceed按钮的新代码如下所示:

If depositButton.Checked = True Then

    finalBalance.Text = deposit(transactionValueBox.Text, Balance)

Else
    finalBalance.Text = withdraw(transactionValueBox.Text, Balance)

End If

如果单击“预览”按钮,它将会将BalancePreview变量传递给函数,或者如果单击“继续”按钮,它将传递实际的“平衡”变量。无论哪种方式,它都会修改传递的实际变量。这可确保Balance变量仅在适当时保持更新。