C#银行账户存款

时间:2015-12-15 05:44:10

标签: c# winforms account bank

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

我为我的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;
    }

2 个答案:

答案 0 :(得分: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文本进行了硬编码。

答案 1 :(得分:-1)

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

_balance += amount;

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

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

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