Windows应用程序中的C#存款/取款按钮?

时间:2012-04-04 22:23:45

标签: c#

好的,我有一份上学的任务。该程序是一个有两个选项卡的GUI。在第一个标签上有四个文本框,分别是名称,ID,年龄和帐户余额。此选项卡上还有一个按钮,用于将帐户添加到第二个选项卡上的组合框中。在第二个选项卡上,有组合框和四个文本框,用于名称,ID,年龄和余额。当我从组合框中选择一个名称时,四个文本框会自动填入他们的信息。我遇到的问题是我必须有一个撤销和存款按钮,用户可以输入金额并将其减去或添加到文本框中的余额。有谁知道如何做到这一点?如果您想看到代码让我知道。

 class BankAccount
    {
        //attributes
        public string accountID;
        public string customerName;
        public int customerAge;
        public double balance;
        public const double DEFAULT_BALANCE = 500.00;

    //construct
    public BankAccount()
    {
    }

    public BankAccount(string anID, string aName, int anAge, double aBalance)
    {
        accountID = anID;
        customerName = aName;
        customerAge = anAge;
        balance = aBalance;
        if (aBalance == 0)
        {
            balance = DEFAULT_BALANCE;
        }
        else
        {
            balance = aBalance;
        }
    }

    public BankAccount(string anID, string aName, int anAge)
    {
        accountID = anID;
        customerName = aName;
        customerAge = anAge;
        balance = DEFAULT_BALANCE;
    }






     //accessors
    public void SetID(string anID)
    {
        accountID = anID;
    }

    public void SetName(string aName)
    {
        customerName = aName;
    }

    public void SetAge(int anAge)
    {
        customerAge = anAge;
    }

    public void SetBalance(double aBalance)
    {
        balance = aBalance;
    }

    public string GetID()
    {
        return accountID;
    }

    public string GetName()
    {
        return customerName;
    }

    public int GetAge()
    {
        return customerAge;
    }

    public double GetBalance()
    {
        return balance;
    }

这是表格

public partial class Form1 : Form
    {

    //ArrayList account = new ArrayList();
    private List<BankAccount> account = new List<BankAccount>();

    public Form1()
    {
        InitializeComponent();
    }



    private void btnAddAccount_Click(object sender, EventArgs e)
    {
        BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
            int.Parse(txtAge.Text), double.Parse(txtBalance.Text));

        account.Add(aBankAccount);
        AddToComboBox();
        ClearText();


    }

    private void AddToComboBox()
    {
        cboAccount.Items.Clear();
        foreach (BankAccount person in account)
        {
            cboAccount.Items.Add(person.GetName());
            //cboAccount.Items.Add(person);               

        }


    }
    private void ClearText()
    {
        txtName.Clear();
        txtAccountID.Clear();
        txtBalance.Clear();
        txtAge.Clear();
        txtAccountID.Focus();


    }

    private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
    {

        //txtNameTab2.Text = cboAccount.SelectedItem.ToString();
        txtNameTab2.Text = account[cboAccount.SelectedIndex].customerName;
        txtAgeTab2.Text = account[cboAccount.SelectedIndex].customerAge.ToString();
        txtAccountIDTab2.Text = account[cboAccount.SelectedIndex].accountID.ToString();
        txtBalanceTab2.Text = account[cboAccount.SelectedIndex].balance.ToString();





    }

    private void btnWithdraw_Click(object sender, EventArgs e)
    {







    }




}

}

1 个答案:

答案 0 :(得分:1)

如果您使用以下对象代表您的帐户:

public class Account{

    public int Id { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }

    public Decimal Balance { get; set; }

}

然后考虑提款将涉及从余额中移除资金,存款将涉及增加余额。您可以通过以下方式表示:

public class Account{

    public int Id { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }

    public Decimal Balance { get; set; }

    public Decimal DepositMoney(Decimal amount)
    {
        Balance+=amount;
        return Balance;
    }

    public Decimal WithdrawMoney(Decimal amount)
    {
        Decimal moneyAfterWithdrawal = Balance-amount;

        if(moneyAfterWithdrawal >= 0)
        {
           Balance = moneyAfterWithdrawal;
           return Balance;
        }

        throw new Exception(String.Format("Withdrawing {0} would leave you overdrawn!", amount.ToString());

    }


}

然后,您可以根据需要调用DepositMoney和WithdrawMoney,具体取决于文本框中的值以及您单击的按钮。像Decimal.TryParse这样的函数在将输入字符串转换为所需类型时非常有用,您可能还希望在存款或取款后更新屏幕上的余额状态。

在您的示例中,您需要一个Double类型来表示余额 - 在我的示例中只需将Decimal替换为Double。

修改

好的,让我们来看看你的一个事件处理程序,btnWithdraw_Click:

private void btnWithdraw_Click(object sender, EventArgs e) 
{ 
    //First, it is necessary to get the amount to be withdrawn
    //Am assuming this is in a textbox, which I'll call txtAmount

    double amount = 0;

    //Check is a valid double
    if(Double.TryParse(txtAmount.Text, out amount))
    {
       //Lets ignore negative amounts as they are technically a deposit ;-)
       if(amount > 0)
       {
            BankAccount currentAccount = account[cboAccount.SelectedIndex];

            double currentBalance = currentAccount.GetBalance();

            double amountLeft = currentBalance - amount;

            if(amountLeft >= 0)
            {

                currentAccount.SetBalance(amountLeft);
                txtBalanceTab2.Text = amountLeft.ToString("D2");
            }
            else
            {
                //Warn user they would go overdrawn
            }

       }

    }


}

但请注意,我认为Withdraw和Deposit方法属于帐户本身,因为它们会更改类的状态,就像在我的帐户示例中一样。保持这种逻辑的最佳位置是类本身。

我将存款留作读者的练习; - )