简体   繁体   中英

Banking Application - Calling from a class to a windows form in c#

Im learning Windows Forms in c# and finding it difficult to understand working with forms and classes. I am creating a banking application and want the user to be able to: press a button and see their current balance, enter an amount to debit(take money out) their account, get a success note, or a warning note if they dont have sufficient funds and the balance to update.

I dont understand how to connect the account class to the form and designated, labels and buttons. I have just been trying different things first the past few hours but getting nowhere. Could someone explain how I would show in a label box that there is a balance of 500 when a button is pressed. Also how I could debit the account by entering an amount in a text box and pressing a button. And for a note to be wrote to a label confirming the debit. I have the 'insufficient funds' message in my class but ideally I would like to have that in the form because I have heard that its better practice that way.

I hope I have been clear enough, but will confirm anything! I have asked for a lot of help here as I am new to windows forms, but any help or guidance at all would be appreciated!

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

Form:

    //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
    }

Your Account class shouldn't be calling anything directly in the form. Have your Account class generate events or use return values from the functions. Your windows form should be creating and calling functions in the Account class, then you handle the response in the view. The Account class should not have a clue about the view/form.

Simple implementation of your clicked event:

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

This should help you get started. You want to keep all your account logic internal to the Account class and any messages or user interface logic in your form and UI and not inside your Account class.

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

}

All I did was create a Form and drag 2 TextBoxes and Buttons on them. If you name them txtBalance and txtAmount, and btnGetBalance and btnDebit and connect them to the click events then you can use this code.

edit:

I put all the code in the Form class for brevity and simplicity. But obviously those extra classes should be separate code files. Considering what I said about separating UI and Account logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM