Question: Create a new Java Project called BankAccount. 1.BankAccount class Start by creating a BankAccount class. The BankAccount class will have just one instance variable -

Create a new Java Project called BankAccount.

1.BankAccount class

Start by creating a BankAccount class. The BankAccount class will have just one instance variable - balance. Create a constructor for the BankAccount class that takes the initial balance. Create getters and setters for balance. Finally, create a method called deposit that takes a double parameter. The amount passed in through the parameter should be added to the balance amount. Finally the balance should be set to the new number. Lastly, create a withdraw method that also accepts a double. Use the amount passed in to subtract from the balance. Assign balance to the new amount.

2. BankAccountFrame

Create a class called BankAccountFrame and have it extend JFrame.

Inside the BankAccountFrame class, we need to create the items for our interface. This includes a label called Amount, a textbox to hold the amount, two buttons (withdraw and deposit) and a label for the balance. Remember that these are instance variables so they are private.

Additionally, we need to create a master BankAccount object to use throughout the program. Above your labels and buttons, add an empty BankAccount object - BankAccount account;

For the time being, create an empty constructor for the BankAccountFrame class public BankAccountFrame(BankAccount b){

We need a click listener for withdraw and deposit - and we need two separate click listeners because they will perform different tasks. Outside of the constructor, create two inner classes - one for the depositClickListener and one for the withdrawClickListener. Make sure they both implement ActionListener. Add in the required method (actionPerformed). We will come back to these methods.

class depositListener implements ActionListener{

@Override

public void actionPerformed(ActionEvent e) {

Lets turn back to that constructor after youve created the two listeners. The constructor for BankAccountFrame should accept a BankAccount. First, we need to set the BankAccount parameter to be the BankAccount that we created in the instance variables. Inside the constructor, take the parameter and set it equal to account. It might look like this:

public BankAccountFrame(BankAccount b){

account = b;

Back in your constructor, create new objects for the depositClickListener and the withdrawClickListener and add an action listener to both the withdraw and deposit button. Your code might look like this:

depositListener d = new depositListener();

deposit.addActionListener(d);

After you have those items created, create a JPanel and add all five items to it. For example:

JPanel controlPanel = new JPanel();

controlPanel.add(amt);

Then add the new JPanel to the JFrame (add(nameOfJPanelYouSelected);).

For example:

add(controlPanel);

setSize(400,100);

Next, lets create those actionPerformed methods for the deposit and withdraw clickListeners. When the deposit button is pressed, we want to take whatever is in the textfield and add it to the account balance. This will be a little tricky because what is in the textfield is treated as a String. We will need to parse it to a double before we can add it together. Using the names for your textbox, get the text from it. If my textbook is called amount, I would use amount.getText() to get the string from it. Parse the text to a double.

String a = amount.getText();

double withdrawAmt = Double.parseDouble(a);

Call the withdraw method on the account passing in the withdrawAmt to the method. Finally, set the balance label to show the new balance amount.

balance.setText("balance=" + b.getBalance());

Do the same thing with your actionPerformed method for your deposit clickListener, except use the deposit method.

3. BankAccountViewer class

Create a BankAccountViewer with the main method. Create a new BankAccount with an initial balance of 1000. Now youre ready to construct the frame. Create a new JFrame based on the BankAccountFrame passing in the account you just created as a parameter. Set the default close operations and make it visible. Finally, run the program and give it a try!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!