How can I extract this method and have it the same name?
I am creating a mortgage calculator, and I'm following a tutorial. I want to extract a method and name it getMonthlyInterest, because there were two instances of a method, and the tutorial originally is able to use getMonthlyInterest for both.
开发者_开发知识库annualInterest / PERCENT / MONTHS_IN_YEAR
This code was replaced by "getMonthlyInterest" I typed this a second time:
private float getMonthlyInterest() {
return annualInterest / PERCENT / MONTHS_IN_YEAR;
}
and the software did not allow me to name the float "getMonthlyInterest," saying that the name "getMonthlyInterest" was already used. I want to use "getMonthlyInterest" for both of them, how can I do that?
Whole Code of Class:
public class MortgageCalculator {
public final static byte MONTHS_IN_YEAR = 12;
public final static byte PERCENT = 100;
private int principal;
private float annualInterest;
private byte years;
public MortgageCalculator(int principal, float annualInterest, byte years) {
this.principal = principal;
this.annualInterest = annualInterest;
this.years = years;
}
public double calculateBalance(short numberOfPaymentsMade) {
float monthlyInterest = getMonthlyInterest();
float numberOfPayments = years * MONTHS_IN_YEAR;
double balance = principal
* (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPaymentsMade))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
return balance;
}
private float getMonthlyInterest() {
return annualInterest / PERCENT / MONTHS_IN_YEAR;
}
public double calculateMortgage() {
float monthlyInterest = getMonthlyInterest();
float numberOfPayments = years * MONTHS_IN_YEAR;
double mortgage = principal *
(monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
return mortgage;
}
private float getMonthlyInterest() {
return annualInterest / PERCENT / MONTHS_IN_YEAR;
}
public short getYears() {
return years;
}
}
精彩评论