import java.util.Scanner; /********************************************************************* * * @author Duncan Buell **/ public class Loan { private double monthlyPayment,principal,rate; /********************************************************************* * This is the standard constructor, which ought for good form's sake * to explicitly initialize the instance variables for the class. **/ public Loan() { this.monthlyPayment = 0.0; this.principal = 0.0; this.rate = 0.0; } // public Loan() /********************************************************************* * accessor methods for the instance variables **/ /********************************************************************* * This method simply returns the double precision value of the * monthly payment. * * @return The double value that is the monthly loan payment. **/ public double getMonthlyPayment() { return(this.monthlyPayment); } /********************************************************************* * This method simply returns the double precision value of the current * loan principal. * * @return The double value that is the current loan principal. **/ public double getPrincipal() { return(this.principal); } /********************************************************************* * This method simply returns the double precision value of the loan * interest rate. * * @return The double value that is the loan interest rate. **/ public double getRate() { return(this.rate); } /********************************************************************* * mutator methods for the instance variables **/ /********************************************************************* * This method takes an input string, converts to a double, and sets * that double value as the monthly payment amount of the loan. * It SHOULD be the case that extensive error testing is done in this * method to determine that in fact the input string represents a legal * value for a double. However, we don't do that here. * * @param The (String) value of what is supposed to be a double. * @return A boolean false if the input was illegal, else true. **/ public boolean setMonthlyPayment(String inString) { boolean returnValue; double convertedValue; convertedValue = Double.valueOf(inString); returnValue = false; if(convertedValue > 0.0) { this.monthlyPayment = convertedValue; returnValue = true; } return(returnValue); } // public boolean setMonthlyPayment(String inString) /********************************************************************* * This method takes an input string, converts to a double, and sets * that double value as the prinicpal amount of the loan. * Same comments about error checking as for the monthly payment. * * @param The (String) value of what is supposed to be a double. * @return A boolean false if the input was illegal, else true. **/ public boolean setPrincipal(String inString) { boolean returnValue; double convertedValue; convertedValue = Double.valueOf(inString); returnValue = false; if(convertedValue > 0.0) { this.principal = convertedValue; returnValue = true; } return(returnValue); } // public boolean setPrincipal(String inString) /********************************************************************* * This method takes an input string, converts to a double, and sets * that double value as the interest rate of the loan. * Same comments about error checking as for the monthly payment. * * @param The (String) value of what is supposed to be a double. * @return A boolean false if the input was illegal, else true. **/ public boolean setRate(String inString) { boolean returnValue; double convertedValue; convertedValue = Double.valueOf(inString); returnValue = false; if(convertedValue > 0.0) { this.rate = convertedValue; returnValue = true; } return(returnValue); } // public boolean setRate(String inString) /********************************************************************* * general methods **/ /********************************************************************* * This method amortizes the loan until the principal balance is zero. * NOTE: This assumes monthly calculation of interest, without regard * to the number of days in a month, etc. **/ public void amortize() { int month; double interestThisMonth,newPrincipal; System.out.printf("Mon OldPrinc Int NewPrinc%n"); month = 0; while(this.principal > 0.0) { ++month; interestThisMonth = this.principal * (this.rate/100.0) / 12.0; newPrincipal = this.principal + interestThisMonth - this.monthlyPayment; System.out.printf("%3d %10.2f %10.2f %10.2f%n", month,principal,interestThisMonth,newPrincipal); this.principal = newPrincipal; if(month > 360) { System.out.printf("ERROR: Number of months is > 360. "); System.out.printf("This program only works for loans of 30 years or less%3n"); System.exit(0); } } } // public void amortize() /********************************************************************* * This method gets the monthly payment amount from the input console. * Note that the input value is checked for validity. **/ public void inputMonthlyPayment(Scanner console) { boolean goodData; String inputString = null; System.out.printf("Input loan payoff in dollars: "); inputString = console.next(); goodData = this.setMonthlyPayment(inputString); if(false == goodData) { System.out.println("ERROR: input value " + inputString + " is not valid for the payoff"); System.exit(0); } else { System.out.println("Loan payoff is: " + this.getMonthlyPayment()); } } // public void inputMonthlyPayment(Scanner console) /********************************************************************* * This method gets the principal amount from the input console. * Note that the input value is checked for validity. **/ public void inputPrincipal(Scanner console) { boolean goodData; String inputString = null; System.out.printf("Input loan principal in dollars: "); inputString = console.next(); goodData = this.setPrincipal(inputString); if(false == goodData) { System.out.println("ERROR: input value " + inputString + " is not valid for the principal"); System.exit(0); } else { System.out.println("Loan principal is: " + this.getPrincipal()); } } // public void inputPrincipal(Scanner console) /********************************************************************* * This method gets the loan rate from the input console. * Note that the input value is checked for validity. **/ public void inputRate(Scanner console) { boolean goodData; String inputString = null; System.out.printf("Input loan rate (input '5' for '5.0%%': "); inputString = console.next(); goodData = this.setRate(inputString); if(false == goodData) { System.out.println("ERROR: input value " + inputString + " is not valid for the rate"); System.exit(0); } else { System.out.println("Loan rate is: " + this.getRate()); } } // public void inputRate(Scanner console) } // public class Loan