【发布时间】:2018-08-19 16:47:41
【问题描述】:
我正在创建许多对象并存储在数组列表中。然后我通过“computeInterest”方法计算每个“账户”对象的年度余额。 我需要从“bankAccountDetails”返回对象并将其传递给“computeInterest”方法。
这里我创建了一个对象“Account”的数组列表
public void bankAccount1Details() {
for (int x=0; x<(x+1); x++){
System.out.println("");
System.out.println("Please Enter The Following Details To create The Bank Account");
System.out.println("");
System.out.println("The Customer Account Number");
accountNumber = input.nextInt();
for (int a = 0; a < accountDetails.size(); a++) {
if (accountNumber == account.accountNumber) {
System.out.println("The account number already exist");
bankAccount1Details();
}
}
if (accountNumber == 0){
System.out.println("you have entered 0 for account number");
break;
}
if ((accountNumber < 1000) || (accountNumber > 9999)) {
System.out.println("Please Enter a valid Account number");
bankAccount1Details();
}
System.out.println("The Customer Name");
customerName = input.next();
System.out.println("The Customer Account Balance");
do {
accountBalance = input.nextDouble();
if (accountBalance <= 0) {
System.out.println("Please enter a valid amount");
}
} while ((accountBalance <= 0));
startingBalance = accountBalance ;
do {
System.out.println("Please Enter the Annual Interest Rate :");
annualInterestRate = input.nextDouble();
if ((annualInterestRate< 0.01) || (annualInterestRate > 15)){
System.out.println("Please enter a valid rate");
}
}while ((annualInterestRate< 0.01) || (annualInterestRate > 15));
do {
System.out.println("please enter the monthly Automatic Deposit Amount ");
monthlyDeposit = input.nextDouble();
if (monthlyDeposit< 0) {
System.out.println("ERROR: Withdraw amount, not valid");
monthlyDeposit = input.nextDouble();
}
}while (monthlyDeposit <0);
do {
System.out.println("please enter the monthly withdrawal amount");
monthlyWithdraw = input.nextDouble();
if ((monthlyWithdraw < 0)) {
System.out.println("ERROR: Withdraw amount, not valid");
monthlyDeposit = input.nextDouble();
}
}while (monthlyWithdraw <0);
System.out.println("The Account Password");
passwordAccount = input.next().toCharArray();
account = new BankAccount(accountNumber, customerName, accountBalance, balanceAfterTransfer, annualInterestRate,
startingBalance, monthlyDeposit, monthlyWithdraw);
accountDetails.add(account);
}
computeInterest(account);
}
这里我将对象“account”传递给方法“computeInterest”。
int interestEarningYears;
double yearlyBalance = 0;
public void computeInterest( BankAccount account){
System.out.println("please enter the number of years the interest to be earned");
interestEarningYears = input.nextInt();
for (int x=0; x<=interestEarningYears; x++) {
yearlyBalance = accountBalance;
double interest = ((account.annualInterestRate * yearlyBalance) / 100);
yearlyBalance = yearlyBalance + interest;
System.out.println("Ending balance for the year " +x + ": " + yearlyBalance);
}
}
那么,这里当我创建一个多于 1 个对象时,我如何计算年度余额?
我有一个解决方案:- 我也可以使用此代码
for (int a = 0; a < accountDetails.size(); a++) {
accountDetails.get(a).accountBalance....
}
但我想传递对象并为所需帐户计算结果。
我想通过传递对象作为参数来重写 computeInterest() 的代码。
记住,需要的对象可以有很多
【问题讨论】:
-
为什么不
computeInterest(accountDetails.get(a))? -
是的,这种方式很简单,但项目要求说'• 一个接受 BankAccount 参数的 computeInterest() 方法。在该方法中,提示用户该帐户将获得利息的年数。该方法根据银行帐户附加的利率显示该帐户在输入的年数中每年的期末余额"
-
您在 OP 中没有提到任何这些。您对此有何期待?你想要上面的代码吗?编辑帖子并清楚地说明您面临什么问题?
-
我想通过传递对象作为参数来重写 computeInterest() 的代码。
-
@HovercraftFullOfEels 对不起,如果我在以前的 cmets 中错了,但是看看我的排名和你的排名,我只是一个初学者,基本问题很少。每次我发布我搜索好的问题时,也许我错过了它们,因为我解释标题的方式可能不同。仍然,我尽力了。 ://
标签: java oop arraylist methods parameter-passing