【问题标题】:Passing an object from an Array list of objects to a method in java将对象从对象数组列表传递给java中的方法
【发布时间】: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


【解决方案1】:

查看此解决方案并请重新考虑我已应用于您的代码的代码重构,最好将您的代码分解为子方法,而不是在一个长方法中列出整个逻辑。

如果您下次分享所有课程会很好,这有助于我们快速弄清楚事情而不是假设事情。

我将整个帐户列表传递给该方法,而不是传递 1 个帐户。

package stackoverflow.q1;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {

Integer accountNumber;
String customerName;
Double accountBalance;
Double startingBalance;
Double annualInterestRate;
Double monthlyDeposit;
Double monthlyWithdraw;
Double balanceAfterTransfer;
BankAccount account;
Scanner input;
private char[] passwordAccount;
List<BankAccount> accountDetails = new ArrayList<>();


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");
        startingBalance = enterAccountBalance();

        annualInterestRate = enterAnnualIntrestRate();

        monthlyDeposit = enterMonthlyDeposit();

        monthlyWithdraw = enterMonthlyWithdraw();


        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(accountDetails);
}

private Double enterMonthlyWithdraw() {
    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);
    return monthlyWithdraw;
}

private Double enterMonthlyDeposit() {
    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);
    return monthlyDeposit;
}

private Double enterAnnualIntrestRate() {
    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));
    return annualInterestRate;
}

private Double enterAccountBalance() {
    do {
        accountBalance = input.nextDouble();
        if (accountBalance <= 0) {
            System.out.println("Please enter a valid amount");
        }
    } while ((accountBalance <= 0));
    return accountBalance;
}



//you can pass the whole list to this method 
public void computeInterest( List<BankAccount> accounts){

    int interestEarningYears;
    double yearlyBalance = 0;

    for (BankAccount account: accounts) {
        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);


            //set the account field in here 
            //I am not sure which field in the back account that has to store the yearlyBalance because you 
            //did not share all the calsses 
            //account.setXXXX(yearlyBalance);
        }
    }
}

}

【讨论】:

    猜你喜欢
    • 2015-02-16
    • 2016-01-14
    • 2019-03-22
    • 2012-01-20
    • 2019-01-02
    • 2021-03-16
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多