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

问题描述 投票:-1回答:1

例如:当我的余额为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();
        }
    }
c# button polymorphism abstract-class windows-forms-designer
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.