I am struggling trying to finish an assignment in my beginner programming class. We have to create a bank account gui. The Account.java file was supplied to us for use within the BankAccount.java file. I have been able to get the BankAccount program to compile, but I don't fully understand what I need to have in the actionPerformed to make sure that both the withdraw and deposit work correctly. As of now, it does have fields to input withdraw and deposit amount but the buttons do nothing. Could someone assist me in being able to get this to work properly?
The description of the assignment is:
Design, implement, and test a GUI for the Account class which represents a bank account. Your GUI should display the account information (account number, name, and balance) of an Account object (instance) with suitable labels. In addition, it should provide inputs and buttons to make deposits and withdrawals, add interest, and quit from the program, by calling the appropriate methods of the Account object. Note that the withdraw method of the Account class requires two arguments: the amount withdrawn, and a withdrawal fee. You get to make a policy decision here, by setting the fee. You could decide the fee will be zero, or a small positive amount (say, 10 cents). Your account GUI class should have a reference to an Account object, which provides the data to be displayed and on which it operates when making deposits, withdrawals, etc. You will need to modify the Account class, adding accessor methods for some private fields.
You have two classes one called Account, which represents an Account, and one called BankAccount which doesn't represent a bank account. Always make sure your class names sound like what the class means. Your Account class has an incorrectly‑built constructor, which doesn't do what the comment says it does. You need to correct that class before even thinking of a GUI. You also have a FEE field (which should be marked final), but you don't appear to use it anywhere. Make sure to test that code from the command line without a GUI first.
Next, explain what you want to happen when you push that button. Write it down; the process of writing will probably help you understand what the listener is supposed to do; this is sometimes called rubber‑duck programming. Once you have done that, tell us what gaps remain in your understanding.
Please format your code correctly; your indentation is incorrect. Divide up that long method into sections with empty lines. Change the // comments, which are intended for short things at the end of the line, into documentation comments /**...*/.
Despite what you see in the Java™ Tutorials and many books, don't make a display class implement ActionListener.
Your problem is with variable declaration. You are declaring all component type variables e.g. private JButton withdrawButton; and again inside makePanel() method. That's why the event listener is never registered to the instance level variable.
I'd also think about other this method's approach. I have in mind fee. I wouldn't expect this method to have parameter fee. I'd expect this to be somewhere else, so you set the fee for withdrawal and if there is a need to apply fee it get's another method probably called.
The Account class code was given to us by our instructor to be used for the BankAccount class. I understand that when the amount is input into the withdraw that it should return the account balance minus the amount of the withdraw and with the deposit amount is input it should add to the current balance and return the new balance. I am just having trouble understanding java at all. Part one of this class dealt with Python and I was able to understand it, but Java is stumping me. I cannot seem to comprehend it. I need the withdraw and deposit buttons to accept user input and return the correct balance.
Stacey Axe wrote:. . . I understand that when the amount is input into the withdraw that it should return the account balance minus the amount of the withdraw and . . .
I am afraid that isn't quite correct. The account object should behave like a real‑life account. You go to the bank and pay in £100 and they stamp the paying‑in book. Or nowadays, more likely, the ATM gives you a receipt. Nobody tells you how much you have in the account. You can ask for a balance separately. Similarly your account object should have deposit and withdraw methods, which don't return the balance. You have a separate getBalance method for that. The three actions are separate, and should therefore be handled by separate methods.
Part one of this class dealt with Python and I was able to understand it, but Java is stumping me. I cannot seem to comprehend it. . . . .
But the logic is exactly the same. There are two ways to look at it:-
You go to the bank and pay in $100 to your account, and your balance becomes that amount greater.
Man gehe zur Bank: man zahle €100 aufs Konto und der Saldo wird um diese Menge größer.
Those are the same thing in different languages (and different currencies ). You have to work out the logic behind the actions before you try to turn it into a computing language. Also, divide and rule. Work out how you can return the balance. Work out how you can add the amount to the balance. Work out how you subtract a withdrawal from the balance. Work out how you can subtract the withdrawal fee from the balance. Write those things down with pencil and paper and eraser. Work out how to put those things together as methods. I have before now done that sort of thing, writing lots of different lines on paper and cut the paper into small pieces and stuck it together with sticky tape. Whatever helps you envisage the logic of the operations.
I would normally have three methods to deposit withdraw and get the balance. You should be able to work out the names of the methods from that preceding sentence. I would not have the deposit and withdraw methods return a balance because a method does one thing and a method to deposit and return a new balance is doing two things. If however you have been told to do something different you are stuck with it, with methods doing two things. It is unfortunate if an assignment doesn't encourage what many of us regard as best practice.
As for paper: lots of it. As for pencils: get the softest you can lay your hands on, at an artists' shop or proper stationer's. 4B for example. As for erasers: get the biggest you can carry. You will have lots of evidence to destroy
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop