如果问题没有意义,我道歉。我制作了一个管理银行账户的银行账户计划,我得到了一项任务,要求我制定一个方法,为所有银行账户增加3%的利息。
以下是Account类的第一部分:
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;
}
以下是为单个帐户添加兴趣的方法:
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
以下是我正在努力让多个银行帐户获得兴趣的方法:
public double addInterestAll ()
for (int i = 0; i <= 29; i++){
account[i].addInterestAll();
}
return balance;
}
上面的帐户变量是我在main方法中创建的数组但是因为我在main方法中声明了数组,所以编译器找不到它。我不确定如何使这项工作成为另一种方式。
这是主要方法:
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);
}
}
总之,我不确定如何为数组中的每个帐户添加兴趣。提前谢谢
答案 0 :(得分:1)
你必须遵循与
相同的模式for (int i = 0; i <=29; i++){
account[i].deposit (100.01);
account[i].withdraw (50.05, 0.0);
}
像
for (int i = 0; i <=29; i++){
account[i].addInterest ();
}
如果您想要一种方法,请尝试
static public double addInterestAll (Accounts[] accounts)
double balance = 0.0;
for (int i = 0; i < accounts.length; i++){
balance += accounts[i].addInterest();
}
return balance;
}
然后调用此代码
double newBalance = addInterestAll (account);