简体   繁体   English

帮助使用Java,HW中的方法逻辑

[英]Help with method logic in Java, hw

I have a Loan class that in its printPayment method, it prints the amortization table of a loan for a hw assignment. 我有一个Loan类,在其printPayment方法中,它为硬件分配打印贷款的摊销表。 We are also to implement a print first payment method, and a print last payment method. 我们还将实现先打印后付款的方式和先打印后付款的方式。 Since my calculation is done in the printPayment method, I didn't know how I could get the value in the first or last iteration of the loop and print that amount out. 由于我的计算是通过printPayment方法完成的,所以我不知道如何在循环的第一次或最后一次迭代中获取值并将其打印出来。

One way I can think of is to write a new method that might return that value, but I wasn't sure if there was a better way. 我可以想到的一种方法是编写一个可能返回该值的新方法,但是我不确定是否有更好的方法。 Here is my code: 这是我的代码:

public abstract class Loan
{   
    public void setClient(Person client)
    {
        this.client = client;
    }

    public Person getClient()
    {
        return client;
    }

    public void setLoanId()
    {
        loanId = nextId;
        nextId++;
    }

    public int getLoanId()
    {
        return loanId;
    }

    public void setInterestRate(double interestRate)
    {
        this.interestRate = interestRate;
    }

    public double getInterestRate()
    {
        return interestRate;
    }

    public void setLoanLength(int loanLength)
    {
        this.loanLength = loanLength;
    }

    public int getLoanLength()
    {
        return loanLength;
    }

    public void setLoanAmount(double loanAmount)
    {
        this.loanAmount = loanAmount;
    }

    public double getLoanAmount()
    {
        return loanAmount;
    }

    public void printPayments()
    {
        double monthlyInterest;
        double monthlyPrincipalPaid;
        double newPrincipal;
        int paymentNumber = 1;
        double monthlyInterestRate = interestRate / 1200;
        double monthlyPayment = loanAmount * (monthlyInterestRate) / 
                                (1 - Math.pow((1 + monthlyInterestRate),( -1 * loanLength)));

        System.out.println("Payment Number | Interest | Principal | Loan Balance");     

        // amortization table
        while (loanAmount >= 0) {
            monthlyInterest = loanAmount * monthlyInterestRate;
            monthlyPrincipalPaid = monthlyPayment - monthlyInterest;
            newPrincipal = loanAmount - monthlyPrincipalPaid;
            loanAmount = newPrincipal;


            System.out.printf("%d, %.2f, %.2f, %.2f", paymentNumber++, monthlyInterest, monthlyPrincipalPaid, loanAmount);
        }
    }
    /*
    //method to print first payment
    public double getFirstPayment()
    {
    }

    method to print last payment
    public double getLastPayment()
    {
    }*/

    private Person client;
    private int loanId;
    private double interestRate;
    private int loanLength;
    private double loanAmount;
    private static int nextId = 1;

}

Thanks! 谢谢!

You've already identified that the printPayments() , printFirstPayment() and printLastPayment() methods have common logic. 您已经确定了printPayments()printFirstPayment()printLastPayment()方法具有共同的逻辑。 You generally want to minimize duplication of such code and the two common ways to do this are: 通常,您希望最大程度地减少此类代码的重复,并且这样做的两种常见方法是:

  1. Implement all but one of the methods in terms of one of them; 根据其中一种方法实施除一种方法外的所有方法; or 要么

  2. Implement all the methods in terms of a private method. 根据私有方法实现所有方法。

So, for example: 因此,例如:

public void printPayments() {
  for (Payment : getPayments()) {
    printPayment(payment);
  }
}

public void printFirstPayment() {
  printPayment(getPayments().get(0));
}

public void printLastPayment() {
  List<Payment> payments = getPayments();
  printPayment(payments.get(payments.size()-1));
}

private void printPayment(Payment payment) {
  ...
}

private List<Payment> getPayments() {
  ...
}

Now this is homework so you may not have come across the syntax List<Payment> yet. 现在这是家庭作业,因此您可能还没有遇到语法List<Payment> If not, it's generics. 如果不是,则为泛型。 There are other ways to do this: using a non-generic Collection or using arrays for example. 还有其他方法可以这样做:例如,使用非通用Collection或使用数组。

The points I wanted to illustrate here is that: 我想在这里说明的要点是:

  1. The logic for creating the payments and displaying them has been separated; 创建付款和显示付款的逻辑已分开;

  2. A single method getPayments() does the calculations and returns a List of Payment objects. 单个方法getPayments()进行计算并返回“ Payment List ”对象。 Payment is a new object in this mock up; Payment是此模型中的新对象;

  3. All three methods are implemented in terms of getPayments() and printPayment() . 这三种方法都是根据getPayments()printPayment()

So I hope this leads you in the right direction. 因此,我希望这会引导您朝正确的方向前进。 The concept here I guess is functional composition , composing your functions in terms of other functions and making your internal functions granular enough to be grouped together usefully. 我想这里的概念是功能组合 ,即根据其他功能组合您的功能,并使内部功能细化到可以有效组合在一起的程度。

Your printPayments function is awfully big. 您的printPayments函数非常大。 It is generally better to make each function "do one thing and one thing well", and to make functions relatively short. 通常最好使每个功能“做好一件事情,一件事情做好”,并使功能相对较短。 I would recommend that you separate your computation logic from your printing logic; 我建议您将计算逻辑与打印逻辑分开; provide functions for computing these various payments, and have your print function merely print the result of invoking those computation functions. 提供用于计算这些各种付款的功能,并让您的打印功能仅打印调用这些计算功能的结果。

If you are worried about redundancy (that is some of the later computations depend on earlier computations which you might have previously performed), then you can use dynamic programming, which basically means that you accumulate previous results in an array or matrix so that they can be reused in subsequent computations. 如果您担心冗余(某些后续计算取决于您先前可能执行的早期计算),则可以使用动态编程,这基本上意味着您将先前的结果累加到数组或矩阵中,以便它们可以可在后续计算中重用。 You could compute the entire amortization table as a 2-dimensional array, in which case you could lookup the earlier payments that you computed simply by looking them up in that array. 您可以将整个摊销表计算为二维数组,在这种情况下,您可以简单地通过在该数组中进行查找来查找以前计算的付款。

也许您应该有一个返回数组/集合/列表/结果集/数据容器的方法( 添加更多的流行词使您感到困惑 -毕竟是您的功课;),该方法可以在其他方法中使用。

if when you describe what a method does, you use the word 'and', chances are the method is doing too much. 如果在描述方法的作用时使用“和”一词,则可能是该方法做得太多。 each method should do one thing, so printing is one thing, and calculating is another .. so two methods. 每种方法应该做一件事,所以打印是一件事,而计算是另一件事..所以是两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM