2015-12-15 148 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