java program Define a class Bank Account, with instance variables for the follow
ID: 3701460 • Letter: J
Question
java program
Define a class Bank Account, with instance variables for the following information: Account Owner Name Account Balance Also define two static instance variables for the following information: Bank Total Balance Bank Total Number of Accounts In the constructor method for the class, increment the Bank Total Number of Accounts by 1. Create methods for depositing and withdrawing funds. These methods should adjust the balance for the individual account, but also for the total bank balance. You should also create methods for viewing the current account balance, as well as the entire bank balance. Test your program by creating 3 accounts (instances of the Bank class) in a test program, depositing and withdrawing funds, and then showing the local and global balances.Explanation / Answer
BanAccountTester.java
public class BanAccountTester {
public static void main(String[] args) {
BanAccount b1 = new BanAccount();
BanAccount b2 = new BanAccount();
BanAccount b3 = new BanAccount();
b1.setAccountBalance(1000);
b2.setAccountBalance(2000);
b3.setAccountBalance(3000);
b1.deposit(100);
b2.deposit(200);
b3.deposit(300);
b1.withdrawal(50);
b2.withdrawal(150);
b3.withdrawal(250);
b1.setAccountOwnerName("Sures");
b2.setAccountOwnerName("Sekhar");
b3.setAccountOwnerName("Anshu");
b1.displayBalance();
b2.displayBalance();
b3.displayBalance();
}
}
BanAccount.java
public class BanAccount {
private String accountOwnerName;
private double accountBalance;
public static double totalBalance = 0;
public static int numberOfAccounts = 0;
public BanAccount() {
numberOfAccounts++;
}
public String getAccountOwnerName() {
return accountOwnerName;
}
public void setAccountOwnerName(String accountOwnerName) {
this.accountOwnerName = accountOwnerName;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
totalBalance+=accountBalance;
}
public void deposit(double b) {
accountBalance+=b;
totalBalance+=b;
}
public void withdrawal(double b) {
accountBalance-=b;
totalBalance-=b;
}
public void displayBalance() {
System.out.println("Account Balance: "+accountBalance+" Total Balance: "+totalBalance);
}
}
Output:
Account Balance: 1050.0 Total Balance: 6150.0
Account Balance: 2050.0 Total Balance: 6150.0
Account Balance: 3050.0 Total Balance: 6150.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.