从支票帐户转帐?

时间:2016-12-07 01:52:58

标签: c# transfer

我从昨天起开始研究这个程序,但我不知道问题的原因。当我运行程序并输入从支票账户转账到储蓄账户的金额时,程序从支票账户中减去输入的金额,但不会添加到储蓄账户。

如何解决此问题?任何帮助表示赞赏。

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);
        }
    }


}

}

2 个答案:

答案 0 :(得分:0)

使用此语句,您实际上是复制引用而不是值。

chequingAccount = AccountsList[i];

所以这两个都有相同的参考。

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

你正在减去x金额然后再添加x金额。你需要仔细检查你的逻辑。

这是一篇关于值类型与参考类型的好文章 http://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type

答案 1 :(得分:0)

我很惊讶你说它正在减去检查。我对代码的阅读使其无法在检查中添加或不执行任何操作。

原因,当您到达+/-代码时,您可能会根据GUI中的布尔值设置chequingAccount = AccountsList[i] ...否则它是一个新创建的帐户,不在您的AccountsList中。

因此,您的+/-代码会导致:

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

或者

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

您需要找到储蓄帐户。

我还注意到您不检查后退帐号,只检查PIN。如果两个帐户具有相同的PIN,则可能无法从正确的帐户中扣除。