Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment you are to develop a class that models a bank account. As pa

ID: 3600534 • Letter: F

Question

For this assignment you are to develop a class that models a bank account. As part of the class definition, create instance variables and class (static) variables as required. For each individual bank account, the class should be able to keep track of the: account number e current balance interest rate account numbe r assigned to it when the Each bank account should have its own unique account is created. Account numbers should be integer values ranging from 1000 - 9999. The first account created should be assigned the number "1000", the second should be assigned the number" 1001”, the third should be assigned the number" 1002", etc. The class should ensure that no two accounts are assigned the same account number A constructor for the class should be provided that allows values to be current balance and interest rate when a new account is created. The constructor should specified for the also assign a unique account number to each new account as it is created A toString) method for the class should be provided that returns a reasonable string representation of a bank account including the account number, current balance, and nterest rate Additionally, methods should be provided for the class that allow: a deposit to be made to an account a withdrawal to be made from an account the balance for an account to be retrieved the interest rate for an account to be retrieved the account number of an account to be retrieved e e After the bank account class has been established, write a main) method that creates 5 (or more) new bank account objects giving each different values for the current balance and interest rate as they are created. Print each of the objects to display the balance and interest rate values, and also to verify that each account receives a unique account number as it is created.

Explanation / Answer

public class BankAccount {

// Define constant values as account numbers should have a range

private static final int ACCOUNT_NUMBER_START_VALUE = 1000;

private static final int ACCOUNT_NUMBER_END_VALUE = 9999;

// Define a static variable which is also called a class variable

// that denotes current account number

// We can keep incrementing this value and assign each new object that is created

// This way we can ensure that unique account numbers are created

private static int accountNumTracker = ACCOUNT_NUMBER_START_VALUE;

// The below are the member variables - can be called object variables

// These are specific to a bank account

// Current Balance of the bank account

private int balance;

// Interest Rate of the bank account

private int interest;

// Account Number of the bank account

private int accNumber;

// Constructor that takes in balance and interest rates as params

// and assign those values to the member variables

// Also create a unique account number by assigning the class variable

// and later incrementing the class variable to make sure that the next

// account will have the next integer - different from the prev one - as

// its account number

public BankAccount(int balance,int interest){

if(accountNumTracker > ACCOUNT_NUMBER_END_VALUE){

throw new IllegalStateException("Can not create more accounts");

}

this.accNumber = accountNumTracker++;

this.balance = balance;

this.interest = interest;

}

// Override toString method to print the account details in a neat manner

@Override

public String toString() {

return "account number: " + accNumber

+ " interest rate: " + interest

+ "% balance:$ " + balance;

}

// deposit method that takes in amount as param and increments the balance by amount

public void deposit(int amount){

this.balance += amount;

System.out.println(" Deposited amount $ "+amount

+" to account: "+accNumber

+" updated balance: $ "+balance);

}

// withdraw method that reduces the balance by amount

// there's a check to see if the current balance is less than amount to be withdrawn

// in that case, print error statement and return

public void withdraw(int amount){

if(amount > this.balance){

System.out.println(" Cannot withdraw amount $ "+amount

+" from account: "+accNumber+" as sufficient funds are not available. "

+" balance: $ "+balance);

}else{

this.balance -= amount;

System.out.println(" Withdrew amount $ "+amount

+" from account: "+accNumber

+" updated balance: $ "+balance);

}

}

// The following are the getter methods to return the values of

// the member variables balance, interest and account number

public int getBalance() {

return balance;

}

public int getInterest() {

return interest;

}

public int getAccNumber() {

return accNumber;

}

// main method to test the BankAccount class

public static void main(String[] args){

// Create 5 bank accounts with different values of balance and interest rates

BankAccount acc1 = new BankAccount(0,5);

BankAccount acc2 = new BankAccount(100,4);

BankAccount acc3 = new BankAccount(2000,7);

BankAccount acc4 = new BankAccount(50,10);

BankAccount acc5 = new BankAccount(2500,12);

// Print the bank accounts

System.out.println("The account objects as initially created are:");

System.out.println(" "+acc1);

System.out.println(" "+acc2);

System.out.println(" "+acc3);

System.out.println(" "+acc4);

System.out.println(" "+acc5);

// Perform some deposits and withdrawals to/from bank accounts

acc1.deposit(3000);

acc2.withdraw(20);

acc3.withdraw(3000);

acc4.deposit(2400);

acc5.withdraw(50);

// Print the updated bank accounts

System.out.println(" The updated account objects are:");

System.out.println(" "+acc1);

System.out.println(" "+acc2);

System.out.println(" "+acc3);

System.out.println(" "+acc4);

System.out.println(" "+acc5);

// To test account number upper limit, try changing

/// ACCOUNT_NUMBER_END_VALUE to 1004 and uncomment below code

// try{

// BankAccount acc6 = new BankAccount(1000,10);

// }catch(IllegalStateException ex){

// System.out.println(" Cannot create any more bank accounts");

// }

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote