C#:使用Windows窗体的ATM系统如何修复撤销按钮?这笔钱被扣除两次

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#:使用Windows窗体的ATM系统如何修复撤销按钮?这笔钱被扣除两次相关的知识,希望对你有一定的参考价值。

例如:当我的余额为200时,我第一次提取12美元,余额为188美元(这个有效)但是当我再次提取2美元(它必须是186)但我得到的答案是174美元。我在项目中使用了多态和抽象类。我做了调试,发现我创建的foreach语句是问题,但我无法修复它。我忘了把数据库类

public class BankDatabase
{
    private Account[] account;
    public BankDatabase()
    {
        account = new Account[3];
        account[0] = new Account(1000, 1111, 200, 100);
        account[1] = new Account(1001, 2222, 400, 500);
        account[2] = new Account(1002, 3333, 100, 700);
    }
    private Account GetAccount(int account_number)
    {
        foreach (Account currentAccount in account)
        {
            if (currentAccount.AccountNum == account_number)
                return currentAccount;
        }
        return null;
    }
    public bool Validation(int user_accnum, int user_pin)
    {
        Account user = GetAccount(user_accnum);
        if (user != null)
            return user.verification(user_pin);
        else
            return false;
    }
    public decimal GetAvailableBalance(int user_accum)
    {
        Account user = GetAccount(user_accum);
        return user.AvailableBalance;
    }
    public decimal GetTotalBalance(int user_accum)
    {
        Account user = GetAccount(user_accum);
        return user.TotalBalance;
    }
    public decimal Credit(int user_accum, decimal amount)
    {
        Account user = GetAccount(user_accum);
        return user.Credit(amount);
    }
    public decimal Debit(int user_accum, decimal amount)
    {
        Account user = GetAccount(user_accum);
        return user.debit(amount);
    }
}  


class Account
{
    private  int accountnum;
    private int pin;
    private decimal availablebalance;
    private  decimal totalbalance;

    public Account()
    {

    }
    public Account(int accountnum, int pin, decimal availablebalance, decimal totalbalance)
    {

        this.accountnum = accountnum;
        this.pin = pin;
        this.availablebalance = availablebalance;
        this.totalbalance = totalbalance;
    }
    public int  AccountNum
    {
        get
        {
            return accountnum;
        }
    }
    public int PIN
    {
        get
        {
            return pin;
        }
    }
    public decimal AvailableBalance
    {
        get
        {
            return availablebalance;
        }
    }
    public decimal TotalBalance
    {
        get
        {
            return totalbalance;
        }
    }

    public decimal Credit(decimal amount)
    {
        return availablebalance = availablebalance + amount;
    }

    public decimal debit(decimal amount)
    {
        return availablebalance = availablebalance - amount;
    }

    public bool verification (int userpin)
    {
        return (userpin == PIN);
    }
}

public class Withdraw : Transaction
{
    private decimal amount;
    decimal t2;
    public Withdraw()
    { }
    public Withdraw(int accountnum, BankDatabase database, decimal amount) : base(accountnum, database)
    {
        this.amount = amount;
    }
    public decimal Amount
    {
        get
        {
            return amount;
        }
    }
    public override string ToString()
    {
        return "Withdrawn amount is= " + t2;
    }
    public override decimal balance()
    {
        decimal availablebalance = DataBase.GetAvailableBalance(accountnum);
        if(amount<=availablebalance)
        {
            t2 = DataBase.Debit(accountnum, amount);

        }
        return t2;
    }       
}


public abstract class Transaction
{
    protected int accountnum;
    private BankDatabase database;
    public Transaction ()
    {

    }
    public Transaction(int accountnum,BankDatabase database)
    {
        this.accountnum = accountnum;
        this.database = database;
    }
    public int AccountNum
    {
        get
        {
            return accountnum;
        }
    }
    public BankDatabase DataBase
    {
        get
        {
            return database;
        }
    }
    public abstract decimal balance();
    public override string ToString()
    {
        return "Account Number" + accountnum;
    }
}

private void withdraw_button_Click(object sender, EventArgs e)
    {
        transaction.Add(new Withdraw(Form1.accountnumber, database, with_amount));
        foreach (Transaction trans in transaction)       
        {     
            label2.Text = "Withdraw" + trans.balance().ToString();
        }
    }
答案

在foreach循环中,您正在迭代事务而不清除它以进行下一个循环。实际上,您正在堆叠事务并每次重复它们。以某种方式清除transaction应解决您的问题。

以上是关于C#:使用Windows窗体的ATM系统如何修复撤销按钮?这笔钱被扣除两次的主要内容,如果未能解决你的问题,请参考以下文章

C# Windows 窗体使用 OracleDataAdapter 从系统数据表向 Oracle 表插入行

如何在 C# windows 窗体中使用事件作为方法?

如何设置 Windows 窗体标题栏和 UI 使用 Windows 8/10 的平面操作系统主题,而不是 XP/7 纹理外观?

C#如何在 Windows 窗体应用程序中使用事件

如何在 Windows 窗体中运行 MS Access 2013 中保存的查询。 C#

如何自定义Windows窗体的系统菜单?