commented, calculating interests
git-svn-id: svn://anubis/gvsu@74 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
a28700667a
commit
8818779daf
@ -1,12 +1,31 @@
|
||||
|
||||
/**
|
||||
* AmortizedLoan provides a concrete implementation of a Loan object.
|
||||
*/
|
||||
public class AmortizedLoan extends Loan
|
||||
{
|
||||
/**
|
||||
* This constructor simply calls the contructor from the Loan class.
|
||||
*/
|
||||
public AmortizedLoan(String name, double rate, int years, double amount)
|
||||
{
|
||||
super(name, rate, years, amount);
|
||||
}
|
||||
|
||||
/* Return the loan type as a string */
|
||||
public String toString()
|
||||
{
|
||||
return "Full Amortized Loan";
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the monthly payment for a AmortizedLoan
|
||||
*/
|
||||
public void calcMonthlyPayment()
|
||||
{
|
||||
double adjMonthlyRate = m_interestRate / 100.0 / 12.0;
|
||||
double lengthInMonths = m_length * 12;
|
||||
double n = Math.pow(1 + adjMonthlyRate, lengthInMonths);
|
||||
m_monthlyPayment = (m_principal * adjMonthlyRate * n) / (n - 1);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
|
||||
/**
|
||||
* This class provides a base class for loan types.
|
||||
* It uses the "template" design pattern where extending concrete
|
||||
* classes can override their own specific functionality.
|
||||
*/
|
||||
public abstract class Loan
|
||||
{
|
||||
protected String m_name;
|
||||
@ -7,6 +12,13 @@ public abstract class Loan
|
||||
protected double m_principal;
|
||||
protected double m_monthlyPayment;
|
||||
|
||||
/**
|
||||
* Construct a Loan object
|
||||
* @param name the name of the loanee
|
||||
* @param rate the interest rate of the loan
|
||||
* @param years the length of the loan in years
|
||||
* @param amount the initial principal of the loan
|
||||
*/
|
||||
public Loan(String name, double rate, int years, double amount)
|
||||
{
|
||||
m_name = name;
|
||||
@ -15,16 +27,39 @@ public abstract class Loan
|
||||
m_principal = amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* process() will calculate the monthly payment of the loan
|
||||
* @return a summary of the loan
|
||||
*/
|
||||
public String process()
|
||||
{
|
||||
calcMonthlyPayment();
|
||||
return makeSummary();
|
||||
}
|
||||
|
||||
/**
|
||||
* calcMonthlyPayment will calculate the monthly payment for the loan.
|
||||
* It is abstract and so implementing classes will have to provide
|
||||
* a definition for this method.
|
||||
*/
|
||||
public abstract void calcMonthlyPayment();
|
||||
|
||||
/**
|
||||
* makeSummary will create a textual description of the loan
|
||||
* @return a summary of the loan in text form
|
||||
*/
|
||||
public String makeSummary()
|
||||
{
|
||||
return "Summary!";
|
||||
return String.format(
|
||||
"%s for %s%n" +
|
||||
"%-16s: $%,.2f%n" +
|
||||
"%-16s: %.2f%n" +
|
||||
"%-16s: %d years%n" +
|
||||
"%-16s: $%,.2f%n",
|
||||
toString(), m_name,
|
||||
"Principal", m_principal,
|
||||
"Interest Rate", m_interestRate,
|
||||
"Length of Loan", m_length,
|
||||
"Monthly Payment", m_monthlyPayment);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,19 @@
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* LoanApplication is the main class for creating a loan and calculating payments.
|
||||
*/
|
||||
public class LoanApplication
|
||||
{
|
||||
private Loan m_loan;
|
||||
private String m_summary;
|
||||
|
||||
/**
|
||||
* The run method provides a simple command-line interface loop.
|
||||
* It continually prompts for information for creating a new loan
|
||||
* and displays the summary of each loan entered.
|
||||
*/
|
||||
public void run()
|
||||
{
|
||||
PrintSpooler spooler = PrintSpooler.getSpooler();
|
||||
@ -43,6 +51,7 @@ public class LoanApplication
|
||||
? new SimpleLoan(name, rate, years, principal)
|
||||
: new AmortizedLoan(name, rate, years, principal);
|
||||
|
||||
System.out.println("");
|
||||
spooler.printDocument(loan.process());
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,16 @@
|
||||
|
||||
/**
|
||||
* A class implementing the "singleton" design pattern for printing.
|
||||
*/
|
||||
public class PrintSpooler
|
||||
{
|
||||
/* The reference to the single PrintSpooler object
|
||||
* that will be used to print */
|
||||
private static PrintSpooler spooler;
|
||||
|
||||
/**
|
||||
* This method returns the singleton to use for printing documents.
|
||||
*/
|
||||
public static PrintSpooler getSpooler()
|
||||
{
|
||||
if (spooler == null)
|
||||
@ -10,6 +18,10 @@ public class PrintSpooler
|
||||
return spooler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a document
|
||||
* @param s the document (string) to print
|
||||
*/
|
||||
public void printDocument(String s)
|
||||
{
|
||||
System.out.println(s);
|
||||
|
@ -1,6 +1,13 @@
|
||||
|
||||
/**
|
||||
* Project3 is a driver class for the LoanApplication system.
|
||||
*/
|
||||
public class Project3
|
||||
{
|
||||
/**
|
||||
* The main method is called when the user first starts the program.
|
||||
* @param args any command-line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
LoanApplication la = new LoanApplication();
|
||||
|
@ -1,12 +1,30 @@
|
||||
|
||||
/**
|
||||
* SimpleLoan provides a concrete implementation of a Loan object.
|
||||
*/
|
||||
public class SimpleLoan extends Loan
|
||||
{
|
||||
/**
|
||||
* This constructor simply calls the contructor from the Loan class.
|
||||
*/
|
||||
public SimpleLoan(String name, double rate, int years, double amount)
|
||||
{
|
||||
super(name, rate, years, amount);
|
||||
}
|
||||
|
||||
/* Return the loan type as a string */
|
||||
public String toString()
|
||||
{
|
||||
return "Simple Interest Loan";
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the monthly payment for a SimpleLoan
|
||||
*/
|
||||
public void calcMonthlyPayment()
|
||||
{
|
||||
double adjMonthlyRate = m_interestRate / 100.0 / 12.0;
|
||||
double lengthInMonths = m_length * 12;
|
||||
m_monthlyPayment = (m_principal * (adjMonthlyRate * lengthInMonths + 1)) / lengthInMonths;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user