Reputation: 95
I apologize if the question doesn't make sense. I made a bank account program that manages bank accounts and I was given an assignment that asks me to make a method that adds 3 percent interest to all bank accounts.
Here is the first part of the Account class:
public class Accounts
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final double RATE = 0.03; // interest rate of 3.5%
private int acctNumber;
private double balance;
private String name;
//public Accounts[] account;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Accounts (String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
Here is the method on adding interest to a single account:
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
Here is the method that I am working on to make multiple bank accounts gain interest:
public double addInterestAll ()
for (int i = 0; i <= 29; i++){
account[i].addInterestAll();
}
return balance;
}
The account variable above is an array that I made in the main method but because I declared the array in the main method, the compiler can't find it. I am not sure of another way for how I can make this work.
Here is the main method:
public class SixSix
{
public static void main (String []args)
{
Accounts[] account = new Accounts[30];
account[0] = new Accounts ("Gerry Agam", 1, 1111.00);
account[1] = new Accounts ("Jules Innes", 2, 2222.00);
account[2] = new Accounts ("Shani Cahya", 3, 3333.00);
account[3] = new Accounts ("Bao Shelly", 4, 4444.00);
account[4] = new Accounts ("Sasha Ashley", 5, 5555.00);
account[5] = new Accounts ("Özgür Mor", 6, 6666.00);
account[6] = new Accounts ("Katlego Lashawn", 7, 7777.00);
account[7] = new Accounts ("An Narcisse", 8, 8888.00);
account[8] = new Accounts ("Amal Lalita", 9, 9999.00);
account[9] = new Accounts ("Sabah Dalitso", 10, 1010.00);
account[10] = new Accounts ("Charley Robin", 11, 1111.00);
account[11] = new Accounts ("Jo Oluwasegun", 12, 1212.00);
account[12] = new Accounts ("Vic Nithya", 13, 1313.00);
account[13] = new Accounts ("Nuka Kirabo", 14, 1414.00);
account[14] = new Accounts ("Madhu Munashe", 15, 1515.00);
account[15] = new Accounts ("Saša Guanting", 16, 1717.00);
account[16] = new Accounts ("Esmé Emem", 17, 1717.00);
account[17] = new Accounts ("Ren Thoko", 18, 1818.00);
account[18] = new Accounts ("Lei Yaroslava", 19, 1919.00);
account[19] = new Accounts ("Xuân Seong-Hy", 20, 2020.00);
account[20] = new Accounts ("Tu Maria", 21, 2121.00);
account[21] = new Accounts ("Nasim Noor", 22, 2222.00);
account[22] = new Accounts ("Chandra Morgan", 23, 2323.00);
account[23] = new Accounts ("Camille Jie", 24, 2525.00);
account[24] = new Accounts ("Adetokunbo Sam", 25, 2626.00);
account[25] = new Accounts ("Makena Sothy", 26, 2726.00);
account[26] = new Accounts ("Hanne Kit", 27, 2727.00);
account[27] = new Accounts ("Almas Florence", 28, 2828.00);
account[28] = new Accounts ("Yoshi Saša", 29, 2929.00);
account[29] = new Accounts ("Cahyo Ime", 30, 3030.00);
for (int i = 0; i <=29; i++)
System.out.println(account[i]);
System.out.println("Depositing $100.01 from every account and withdrawing $50.05 out of every account with fee");
for (int i = 0; i <=29; i++){
account[i].deposit (100.01);
account[i].withdraw (50.05, 0.0);
}
System.out.println("After deposit and withdrawl");
for (int i = 0; i <=29; i++)
System.out.println(account[i]);
System.out.println("Account number 30 will make invalid transactions...");
account[29].deposit (-1000.00);
account[29].withdraw (9999999999.00, 0.1);
account[29].toString();
System.out.println("Adding 3 percent interest to all accounts.");
//account.addInterestAll(account);
}
}
In conclusion, I am not sure of how to make a method on adding interest to every account in the array. Thank you in advance
Upvotes: 0
Views: 101
Reputation: 44854
You just have to follow the same pattern as
for (int i = 0; i <=29; i++){
account[i].deposit (100.01);
account[i].withdraw (50.05, 0.0);
}
like
for (int i = 0; i <=29; i++){
account[i].addInterest ();
}
If you want one method to do all try
static public double addInterestAll (Accounts[] accounts)
double balance = 0.0;
for (int i = 0; i < accounts.length; i++){
balance += accounts[i].addInterest();
}
return balance;
}
Then call this code
double newBalance = addInterestAll (account);
Upvotes: 1