2016-12-07 133 views
-2

我正在從這個程序開始工作,但我不知道問題的原因。當我運行程序並輸入從檢查賬戶轉移到儲蓄賬戶的金額時,程序會從檢查賬戶中扣除輸入的金額,但不會添加到儲蓄賬戶。從支票賬戶轉賬?

我該如何解決這個問題?任何幫助表示讚賞。

public partial class Transfer : Window 
{ 
    private string PIN; 
    Accounts AccountsList = new Accounts(); 

    //constructor 
    public Transfer(string pin, Accounts myAcounts) 
    { 
     InitializeComponent(); 
     AccountsList = myAcounts; 
     PIN = pin; 
    } 

    //save to file method 
    public void saveToFile() 
    { 
     using (StreamWriter sw = new StreamWriter("Acounts.txt")) 
     { 
      for (int i = 0; i < AccountsList.Count; i++) 
      { 
       var data = new List<string> 
       { 
        AccountsList[i].ACCOUNTYPE.ToString() 
        ,AccountsList[i].PIN 
        ,AccountsList[i].ACCOUNTNUMBER 
        ,AccountsList[i].ACCOUNTBALANCE.ToString() 
       }; 

       var account = String.Join(";", data); 
       sw.WriteLine(account); 
      } 
     } 
    } 

    private void btnOK_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      string txtAmount = txtAmountInTransfer.Text; 
      double amount = 0; 

      bool AmountCorrect = double.TryParse(txtAmount, out amount); 

      Account chequingAccount = new Account(); 
      Account savingAccount = new Account(); 

      //deposit from CHEQUING ACCOUNT to SAVING ACCOUNT 
      { 
       //validate user entries 

       for (int i = 0; i < AccountsList.Count; i++) 
       { 
        //withdraw from CHEQUING ACCOUNG 
        if (AccountsList[i].ACCOUNTYPE == 'C' && AccountsList[i].PIN == PIN) 
        { 
         if (rbChequing_to_Saving.IsChecked == true) 
         { 
          chequingAccount = AccountsList[i]; 
         } 
         else 
         { 
          savingAccount = AccountsList[i]; 
         } 

         chequingAccount.ACCOUNTBALANCE -= amount; 
         AccountsList[i].ACCOUNTBALANCE += amount; 


         //saveToFile(); 
         //break; 

        } 
        //if (AccountsList[i].ACCOUNTYPE == 'S') 
        // savingAccount.ACCOUNTBALANCE += amount; 

        //saveToFile(); 
       } 


      } 


     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
    } 


} 

}

回答

0

有了這個說法,你實際上是複製不參考價值。

chequingAccount = AccountsList[i]; 

所以這兩個都有相同的參考。

chequingAccount.ACCOUNTBALANCE -= amount; 
AccountsList[i].ACCOUNTBALANCE += amount; 

您正在減去x的金額,然後將x金額加回到它。你需要仔細檢查你的邏輯。

這裏是關於值類型的好文章VS引用類型 http://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type

+0

你能解釋一下我一點請即時通訊新的C# – afgboy

+0

@afgboy:由於賬戶(類)是引用類型,當你做'chequingAccount = AccountsList [I]',您要複製AccountsList的'參考文獻[1 ]'到'chequingAccount'。所以如果你改變其中的任何一個,那會影響另一個的價值。閱讀**值類型與參考類型**。 – DarkKnight

0

我驚訝,你說,這是從檢查中減去。我對代碼的閱讀是爲了檢查而添加或不做任何事情。

原因,當您到達+/-代碼時,您可能會根據GUI中的布爾值設置chequingAccount = AccountsList[i] ...否則,它是不在您的AccountsList中的新創建的帳戶。

因此您+/-代碼的結果之一:

AccountsList[i].ACCOUNTBALANCE -= amount; 
AccountsList[i].ACCOUNTBALANCE += amount; 

或者

(new Account()).ACCOUNTBALANCE -= amount; 
AccountsList[i].ACCOUNTBALANCE += amount; 

某處你需要找到儲蓄賬戶。

我還注意到,你不檢查後面的帳號,只有PIN。如果兩個帳戶具有相同的PIN碼,則不能從正確的帳戶中扣除。