1 Attachment(s)
BankAccount assignment problem
Hi everyone,
I have been given an assignment to create a file called FixedStartingBalanceBankAccount. I have also been given a BankAccount class file(attached).
My assignment is to create a BankAccount object in a variable called myFirstAccount with an initial balance of 5000.
After this I have to withdraw 200 from the account and display the balance after withdrawal using JOptionPane messageDialog.
This is the code I have for setting up account with initial balance and making withdrawal. I am not sure if this is the way to do it.
import javax.swing.JOptionPane;
public class FixedStartingBalanceBankAccount
{
public class BankAccount
{
//Constructors
public BankAccount()
{
BankAccount myFirstAccount = new BankAccount();
}
public BankAccount(double initialBalance)
{
initialBalance = 5000;
}
//Methods
public void withdraw(double amount)
{
amount = 200;
}
}
}
If this is correct how do i get the balance and display it using JOptionPane. Any help would be greatly appreciated as i have spent hours upon hours pulling my hair out trying to get this to work.
Re: BankAccount assignment problem
I have overcome some of my confusion and come up with this;
import javax.swing.JOptionPane;
public class FixedStartingBankBalanceAccount
{
public static void main(String[] args)
{
double balance;
BankAccount myFirstAccount = new BankAccount(5000);
myFirstAccount.withdraw(200);
System.out.println(myFirstAccount.getBalance());
JOptionPane.showMessageDialog(null, "myFirstAccount contains:" + balance);
}
}
I am still a little confused on whether I need to declare the variables such as balance or is this done in the BankAccount file??
Re: BankAccount assignment problem
I have overcome some of my confusion and come up with this;
import javax.swing.JOptionPane;
public class FixedStartingBankBalanceAccount
{
public static void main(String[] args)
{
double balance;
BankAccount myFirstAccount = new BankAccount(5000);
myFirstAccount.withdraw(200);
System.out.println(myFirstAccount.getBalance());
JOptionPane.showMessageDialog(null, "myFirstAccount contains:" + balance);
}
}
I am still a little confused on whether I need to declare the variables such as balance or is this done in the BankAccount file??