银行应用程序——用c#从一个类调用到一个窗体

本文关键字:一个 调用 窗体 应用程序 | 更新日期: 2023-09-27 18:13:19

我在c#中学习Windows窗体,发现很难理解窗体和类的工作。我正在创建一个银行应用程序,并希望用户能够:按下按钮就能看到他们的当前余额,输入一个金额借记(取钱)他们的帐户,得到一个成功通知,或警告通知,如果他们没有足够的资金和余额更新。

我不明白如何连接帐户类的形式和指定,标签和按钮。在过去的几个小时里,我尝试了不同的方法,但一无所获。有人能解释一下,当按下一个按钮时,我如何在标签框中显示余额为500。我也可以借借帐户通过输入一个金额在一个文本框,并按下一个按钮。还需要在标签上写一张确认借方的纸条。我的课堂上有"资金不足"的信息,但理想情况下,我希望在表单中有它,因为我听说这样做更好。

我希望我已经足够清楚,但将确认任何事情!我已经问了很多帮助在这里,因为我是新的windows窗体,但任何帮助或指导将不胜感激!

class AccountClass
 {
    private decimal balance = 500;

    public AccountClass(decimal myBalance)
    {
        Balance = myBalance;
    }
    public virtual bool Debit(decimal amount)
    {
        if (amount > Balance)
        {
            Console.WriteLine("There is not enough money in your account");
            return false;
        }
        else
        {
            Balance = Balance - amount;
            return true;
        }
    }

形式:

    //button to see balance
    private void btnAccountSeeBalance_Click(object sender, EventArgs e)
    {
        //label balance should print to
        lblAccountBalance.Text = myBalance.ToString();
    }
    //button to debit account
    private void btnAccountDebit_Click(object sender, EventArgs e)
    {
        //text box to enter amount to debit
        txtAccountDebitAmount
       //label to print confirm/insufficient funds to
       lblAccountDebitSuccess
    }

银行应用程序——用c#从一个类调用到一个窗体

您的Account类不应该在表单中直接调用任何内容。让Account类生成事件或使用函数的返回值。您的windows窗体应该在Account类中创建和调用函数,然后在视图中处理响应。Account类不应该有关于视图/表单的线索。

点击事件的简单实现:

private void btnAccountDebit_Click(object sender, EventArgs e)
{
    var ac = new AccountClass( balance);
    var rtn = ac.Debit( amount);
}

这应该有助于您开始。你要保持你所有的帐户逻辑内部的帐户类和任何消息或用户界面逻辑在你的表单和UI,而不是在你的帐户类。

public partial class Form1 : Form
{
    private AccountClass account;
    public Form1()
    {
        InitializeComponent();
        account = new AccountClass(500);
    }
    public class AccountClass
    {
        public Decimal Balance
        {
            get;
            private set;
        }

        public AccountClass(decimal initialBalance)
        {
            Balance = initialBalance;
        }
        public void Debit(decimal amount)
        {
            if (amount > Balance)
                throw new InsufficientFundsException();
            Balance -= amount;
        }
    }
    public class InsufficientFundsException : Exception { }
    private void btnGetBalance_Click(object sender, EventArgs e)
    {
        txtBalance.Text = account.Balance.ToString();
    }
    private void btnDebit_Click(object sender, EventArgs e)
    {
        Decimal debitAmount;
        try
        {
            debitAmount = Decimal.Parse(txtAmount.Text);
        }
        catch (Exception)
        {
            MessageBox.Show("Amount is not valid");
            return;
        }
        try
        {
            account.Debit(debitAmount);
            MessageBox.Show(debitAmount + " has been debited from your account");
        }
        catch (InsufficientFundsException)
        {
            MessageBox.Show("There were insufficient funds. Your current account balance is " + account.Balance);
        }
    }
}

我所做的就是创建一个表单并拖动两个文本框和按钮。如果您将它们命名为txtBalance和txtAmount,以及btnGetBalance和btnDebit,并将它们连接到单击事件,那么您可以使用此代码。

编辑:

为了简洁和简单,我把所有的代码放在Form类中。但显然,这些额外的类应该是单独的代码文件。考虑到我所说的分离UI和Account逻辑