2015-12-15 149 views
-1

嗨,大家好我有这个银行帐户项目,显示用户选择索引时的帐户信息。这些信息包括账户的当前余额。然后我也有存款模拟,我存入的数额应该加起来到目前的余额。我不知道为什么它不做这项工作。C#银行帐户存款

我有我的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; 
    } 
+0

看起来'savings1'和'account'是您正在操作的'BankAccount'类的两个单独实例。我没有看到有关'savings1'实例的更新。详细说明如何填充/刷新'savings1'实例。 – niksofteng

+1

你也在做双重存款。一旦进入'Deposite'方法,然后在'depositButton_Click'中再次设置'account.Balance'。我祈祷不要在最终的代码中完成。 :) – niksofteng

+0

哦,我在这里获得了savings1实例, public partial class Form1:Form { List savings = new List (); SavingsAccount savings1 = new SavingsAccount(“0001”,“31-1000”,100m,0.01); – tapsilog

回答

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文本进行了硬编码。

+0

Oooh它工作了!非常感谢! – tapsilog

-1

在你的日常存款要更改的平衡:

_balance += amount; 

然而,在你的日常depositButton_Click你也改变了平衡:

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

你并不需要修改平衡这里因为存款已经做到了。

+1

问题是为什么存款不反映新的价值,而不是为什么它是加倍。 – niksofteng