-1

大家好,我有这个银行账户项目,它在用户选择索引时显示账户信息。此信息包括帐户的当前余额。然后我还有存款模拟,我存款的金额应该加到当前余额中。我不明白为什么它不做这项工作。

我的 selectedindex 有这个代码,它可以提取帐户信息。

private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (accountNumComboBox.SelectedIndex == 0)
        {


            ownerIdLabel.Text = "0001";
            balanceLabel.Text = savings1.Balance.ToString("c");
            interestLabel.Text = (string.Format("{0}%", savings1.Interest));
            interestRLabel.Text = "Interest Rate:";
        }

我有存款按钮的代码

    private void depositButton_Click(object sender, EventArgs e)
    {
        decimal amount;
        if (decimal.TryParse(depositTextBox.Text, out amount))
        {
            account.Deposit(amount);
            account.Balance += amount;
            depositTextBox.Clear();
        }
        else
        {
            MessageBox.Show("Pls enter valid amount.");
        }
    }

我输入的金额不会加到 balancelabel.Text 中的当前余额。非常感谢你的帮助。

编辑:我也在我的 BankAccount 课程中得到了这个

public decimal Balance
    {
        get { return _balance; }
        set { _balance = value; }
    }
    public BankAccount(decimal intialBalance, string ownerId, string accountNumber)
    {

        _balance = intialBalance;
        _customerId = ownerId;
        _accountNumber = accountNumber;
    }


    public void Deposit(decimal amount)
    {
        if ( amount>0)
        _balance += amount;
        else
            throw new Exception("Credit must be > zero");
    }
    public void Withdraw(decimal amount)
    {
        _balance -= amount;
    }
4

2 回答 2

0

您的存款按钮事件处理程序没有更改余额标签的代码。

private void depositButton_Click(object sender, EventArgs e)
{
    decimal amount;
    if (decimal.TryParse(depositTextBox.Text, out amount))
    {
        account.Deposit(amount);
        account.Balance += amount;
        depositTextBox.Clear();
        balanceLabel.Text = account.Balance.ToString("c"); // This line added
    }
    else
    {
        MessageBox.Show("Pls enter valid amount.");
    }
}

编辑:

使用您的代码

private BankAccount account;
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (accountNumComboBox.SelectedIndex == 0)
    {
           account = (BankAccount) savings1;
    }
    updateUi();
}

private void updateUi() {
    ownerIdLabel.Text = "0001";
    balanceLabel.Text = account.Balance.ToString("c");
    interestLabel.Text = (string.Format("{0}%", account.Interest));
    interestRLabel.Text = "Interest Rate:";
}

private void depositButton_Click(object sender, EventArgs e)
{
    decimal amount;
    if (decimal.TryParse(depositTextBox.Text, out amount))
    {
        account.Deposit(amount);
        account.Balance += amount;
        depositTextBox.Clear();
        balanceLabel.Text = account.Balance.ToString("c"); // This line added
    }
    else
    {
        MessageBox.Show("Pls enter valid amount.");
    }
}

注意:以上编辑基于您提供的代码。您可能需要进行一些修改以适应您的目标。另外,请注意您实际上对 ownerId 文本进行了硬编码。

于 2015-12-15T05:53:28.653 回答
-1

在您的存款程序中,您正在更改余额:

_balance += amount;

但是,在您的 depositButton_Click 例程中,您还更改了余额:

account.Deposit(amount);
account.Balance += amount;

您无需在此处修改余额,因为存款已经完成。

于 2015-12-15T05:55:33.210 回答