开发者

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. 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.

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. 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. If not, it's generics. There are other ways to do this: using a non-generic Collection or using arrays for example.

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. Payment is a new object in this mock up;

  3. All three methods are implemented in terms of getPayments() and 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. 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.


Maybe you should have a method that returns an array/set/list/resultset/datacontainer (add more buzzwords to confuse you - its your homework after all ;)) which you can use in the other methods.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜