-2

I have two classes named Bank and BankAccount. There is a createBankAccount method in Bank in class. I want to create an object of Bank and then add it in the record array which is a instance variable of Bank class.

class BankAccount{
double balance;
int id; 
// Type of the account. Can be of two types: current or savings
String accountType;

// i. Write a Constructor
public BankAccount(double balance, int id, String accountType) {
    this.balance = balance;
    this.id = id;
    this.accountType = accountType;
}

// ii. This function increses the balance by amount
public void deposit(double amount){
    balance += amount;
}



// iii. This function decreases the balance by amount
public void withdraw(double amount){
    balance -= amount;
}

}

class Bank{
BankAccount records[];
double savingInterestRate = 3.5; 
double currentInterestRate = 0.4;

// iv. Write a constructor
public Bank(BankAccount[] records, double savingInterestRate, double currentInterestRate) {
    this.records = records;
    this.savingInterestRate = savingInterestRate;
    this.currentInterestRate = currentInterestRate;
}

// v. Complete the function
public void createBankAccount(double initialBalance, int accountId, String accountType){
    //Create a object of BankAccount class
    // Add the object to records array
    BankAccount ac = new BankAccount(initialBalance, accountId, accountType);
    
}

}

How do I add the BankAccount object which is created in createBankAccount method to the records array??

Deepu
  • 31
  • 1
  • 5

1 Answers1

0

Is there any specific requirement to use array for records? Can't you just use a list for the same and then you can add to and remove from the list as you wish.

Hisham
  • 411
  • 3
  • 9
  • It must be done using an array only. That's the requirement. – Deepu Jul 16 '21 at 18:46
  • Have a look at https://stackoverflow.com/questions/1647260/java-dynamic-array-sizes it will give you the idea on how to dynamically set array sizes. – Hisham Jul 16 '21 at 18:52