2013-01-07 46 views
1

對於我的決賽,我必須用C#編寫一個程序。假設這是一個銀行賬戶,用戶可以從原始賬戶餘額中添加或提取總額。該程序本身有效,但由於某種原因,它會第二次使用更新後的餘額。相反,它仍然使用初始餘額。例如,你在開始時有100美元,你拿出20美元,並使新的餘額爲80美元。下次您添加甚至刪除資金時,它將從100美元而不是80美元。關於我可能做錯了的任何想法?我的程序不會使用新的更新總數,而是使用原始總數

這裏是我到目前爲止的代碼:

namespace BankingAccount 
{ 
    public partial class Entrance : Form 
    { 
     public string name; 
     public int number; 
     public double total; 
     public double with; 
     public double dep; 

     public Entrance() 
     { 
      InitializeComponent(); 
     } 

     private void btnEnter_Click(object sender, EventArgs e) 
     { 
      setVisPart1(true); 
     } 

     private void btnClear_Click(object sender, EventArgs e) 
     { 
      txtAcctName.Clear(); 
      txtAcctName.Enabled = true; 
      txtAcctNumber.Clear(); 
      txtAcctNumber.Enabled = true; 
      lblBalance.Text = "Beginning Balance:"; 
      txtBalance.Clear(); 
      txtBalance.Enabled = true; 
      setVisibility(false); 
     } 

     private void btnContinue_Click(object sender, EventArgs e) 
     { 
      string line; 
      int val; 
      double value; 

      name = txtAcctName.Text; 

      line = txtAcctNumber.Text; 

      if (int.TryParse(line, out val)) 
      { 
       if (val < 0) 
       { 
        MessageBox.Show("Invalid number"); 

        txtAcctNumber.Clear(); 
       } 
       else 
       { 
        number = int.Parse(txtAcctNumber.Text); 

        txtAcctNumber.Enabled = false; 
       } 
      } 
      else 
      { 
       MessageBox.Show("Invalid number"); 

       txtAcctNumber.Clear(); 
      } 

      line = txtBalance.Text; 

      if (double.TryParse(line, out value)) 
      { 
       total = double.Parse(txtBalance.Text); 

       txtBalance.Enabled = false; 
       lblBalance.Text = "Available Balance:"; 
      } 
      else 
      { 
       MessageBox.Show("Invalid number"); 

       txtBalance.Clear(); 
      } 

      txtAcctName.Enabled = false; 
      setVisPart2(true); 
      btnContinue.Visible = false; 
     } 

     private void btnCalculate_Click(object sender, EventArgs e) 
     { 
      string line; 
      double value; 

      total = double.Parse(txtBalance.Text); 

      line = txtWithdrawal.Text; 

      if (double.TryParse(line, out value)) 
      { 
       with = double.Parse(txtWithdrawal.Text); 
      } 
      else 
      { 
       MessageBox.Show("Invalid number"); 

       txtWithdrawal.Clear(); 
      } 

      line = txtDeposit.Text; 

      if (double.TryParse(line, out value)) 
      { 
       dep = double.Parse(txtDeposit.Text); 
      } 
      else 
      { 
       MessageBox.Show("Invalid number"); 

       txtDeposit.Clear(); 
      } 

      Banking anAccount = new Banking(name, number, total, with, dep); 

      txtBalance.Text = anAccount.Balance.ToString("C"); 

      txtWithdrawal.Clear(); 
      txtDeposit.Clear(); 
     } 

     private void setVisibility(bool visibilityValue) 
     { 
      lblName.Visible = visibilityValue; 
      lblNumber.Visible = visibilityValue; 
      lblBalance.Visible = visibilityValue; 
      lblWithdrawal.Visible = visibilityValue; 
      lblDeposit.Visible = visibilityValue; 
      txtAcctName.Visible = visibilityValue; 
      txtAcctNumber.Visible = visibilityValue; 
      txtBalance.Visible = visibilityValue; 
      txtWithdrawal.Visible = visibilityValue; 
      txtDeposit.Visible = visibilityValue; 
      btnCalculate.Visible = visibilityValue; 
      btnContinue.Visible = visibilityValue; 
     } 

     private void setVisPart1(bool visibilityValue) 
     { 
      lblName.Visible = visibilityValue; 
      lblNumber.Visible = visibilityValue; 
      lblBalance.Visible = visibilityValue; 
      txtAcctName.Visible = visibilityValue; 
      txtAcctNumber.Visible = visibilityValue; 
      txtBalance.Visible = visibilityValue; 
      btnContinue.Visible = visibilityValue; 
     } 

     private void setVisPart2(bool visibilityValue) 
     { 
      lblWithdrawal.Visible = visibilityValue; 
      lblDeposit.Visible = visibilityValue; 
      txtWithdrawal.Visible = visibilityValue; 
      txtDeposit.Visible = visibilityValue; 
      btnCalculate.Visible = visibilityValue; 
     } 
    } 
} 

這裏是我的類編碼:

namespace BankingAccount 
{ 
    class Banking 
    { 
     private string acctName; 
     private int acctNumber; 
     private double balance; 
     private double withdrawal; 
     private double deposit; 

     public Banking(string name, int number, double total, double with, double dep) 
     { 
      acctName = name; 
      acctNumber = number; 
      balance = total; 
      withdrawal = with; 
      deposit = dep; 
      DetermineBalance(); 
     } 

     public string AcctName 
     { 
      set 
      { 
       acctName = value; 
      } 
      get 
      { 
       return acctName; 
      } 
     } 

     public int AcctNumber 
     { 
      set 
      { 
       acctNumber = value; 
      } 
      get 
      { 
       return acctNumber; 
      } 
     } 

     public double Withdraw 
     { 
      set 
      { 
       withdrawal = value; 
      } 
      get 
      { 
       return withdrawal; 
      } 
     } 

     public double Deposit 
     { 
      set 
      { 
       deposit = value; 
      } 
      get 
      { 
       return deposit; 
      } 
     } 

     public double Balance 
     { 
      set 
      { 
       balance = value; 
      } 
      get 
      { 
       return balance; 
      } 
     } 

     public void DetermineBalance() 
     { 
      balance = balance + (deposit - withdrawal); 
     } 
    } 
} 
+1

你似乎在每次計算時都要實例化一個新的銀行對象。 –

+0

所以如果是這樣的話,我會如何阻止這種情況發生?我還是這個新手。 –

+0

沒有看到「銀行」類是什麼樣的,我只能提供這個:在**級別聲明**。然後,在整個方法中重新使用「銀行」對象。這都是範圍問題。 –

回答

0

由於這是全部內存中(即沒有使用數據庫),這是所有範圍的問題。

作用域定義哪些變量可見,其中..每當您在按鈕單擊內計算時,您正在定義對象Banking ..一旦按鈕單擊方法完成,此對象就不可用。

爲了補救,在類級別聲明它:

public partial class Entrance : Form 
{ 
    public string name; 
    public int number; 
    public double total; 
    public double with; 
    public double dep; 

    public Banking anAccount = new Banking(); // <---- declare it up here 

我會離開它,因爲你將如何得到這工作了「爲讀者任務」。就目前而言,我的回答不會幫助你,因爲你的構造函數需要參數。考慮刪除該構造函數並使用該對象的公共屬性。每次點擊計算,而不是重新創建Banking對象。設置屬性並致電CalculateBalance

(我很抱歉沒有給你一個直接的答案..但是因爲這顯然是功課,我在道德上有義務不只是給你答案..也許別人是?我給你的東西雖然很多!)

+0

謝謝,好先生!這是非常感謝。那個提示讓我變成了'A'。 –

0

我認爲這個問題躺在在此刻的ToString(「C」)解析字符串翻番它有一些唱片不解析正確。

相關問題