Bank Account Program Deposit Button is not working

Go To StackoverFlow.com

2

The program is a GUI that has two tabs. On the first tab there are four textboxes for name, id, age, and account balance. There is also a button on this tab that will add the account to a combobox on the second tab. On the second tab, there is the combobox and four textboxes for name, id, age, and balance. when i select a name from the combobox the four textboxes automatically fill in with their information. The problem i'm running into is that i have to have a withdraw and a deposit button that the user can enter an amount and have it either subtracted or added to the balance in the textbox. I have some example code for the withdraw button that someone already helped me out with. Can anyone tell me why it isn't changing the balance when i hit the button?

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


}

}

and here's the form

 public partial class Form1 : Form
 {

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


        }


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


    }

    private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
    {
        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)
    {

        double amount = 0;

        if (double.TryParse(txtWithdraw.Text, out amount))
        {
            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("c");
                }
                else
                {
                    MessageBox.Show("You don't have enough money!");
                }



            }

        }
    }


}
}
2012-04-05 00:32
by james clemens
There's quite a bit of code here. When you debug through it, at what point does the observed behavior deviate from the expected behavior? Identify that line, indicate what the values are, and explain what you expect it to do - David 2012-04-05 00:39
this has more than a whiff of homework, too; is it - Andras Zoltan 2012-04-05 00:42
This doesn't address your problem, but why are you using separate Get and Set methods in your BankAccount class. Properties might be more appropriat - psubsee2003 2012-04-05 00:43
it runs just fine. it has no problem whatsoever. when i click the withdraw button nothing happens - james clemens 2012-04-05 00:44
@jamesclemens are you stepping through the code in your btnWithdraw_Click event handler method? Does the event fire - psubsee2003 2012-04-05 00:46
@psubsee2003: Looks like a Java developer moving to .NET - David 2012-04-05 00:48
Do you all have any suggestions as far as the actual code goes - james clemens 2012-04-05 00:49
@jamesclemens Here's MSDN's overview of properties. That's probably a good place to start http://msdn.microsoft.com/en-us/library/x9fsa0sw.asp - psubsee2003 2012-04-05 00:51
@David Even in Java it doesn't make much sense to have both public fields and getters/setters - Brian Rasmussen 2012-04-05 01:22
@BrianRasmussen: Ah, you're right. I noticed the getter/setter methods, but I didn't notice that the members themselves were public. Definitely sloppy - David 2012-04-05 12:19


2

"Can anyone tell me why it isn't changing the balance when i hit the button?"

The withdraw button works because there is a event coded in to handle the click:

private void btnWithdraw_Click(object sender, EventArgs e)

You dont have any such event for the balance button.

I'd recommend a good book as a reference, it can be hard googling to get your head around some programming things and ask your friend to help tutor you. Coding is a fusion of Art, Science, Math and Technology and it is complex.

2012-04-05 00:45
by Jeremy Thompson
The books I currently own do not seem to help muc - james clemens 2012-04-05 00:51
@jamesclemens yeah that's why mentioned to ask your friend to help as a tutor. It made absolutely no sense to me when I started. Things like Classes are objects, delegates, etc meant nothing. I had a older friend explain it to me in plain English with examples on what I was coding up - Jeremy Thompson 2012-04-05 00:56
http://stackoverflow.com/questions/477748/what-are-the-best-c-sharp-net-book - phadaphunk 2012-04-05 01:59


0

You have two if statements inside of your button. One or both of them could be false. Put breakpoints near the if statements and see what value is being passed on debug. Or put else statements with message boxes similar to the one you've used for if Amountleft is not greater then 0. You should get something.

If it helps put breakpoints for all your values to see what values are being passed.

2012-04-05 01:17
by user1270384
Ads