Java Amortization Program

Getting from input to If loop and question on Class Amortization

Page 1 of 1

6 Replies - 14134 Views - Last Post: 13 January 2008 - 05:36 PM Rate Topic: -----

#1 Reason4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 20-December 07

Java Amortization Program

Posted 11 January 2008 - 10:12 AM

In working on an assignment to revise an amortization program I ran across the following description of an Amortization Class (It's not really code but may be more readable in that format):


com.cedarspring.tvm 
Class Amortization
java.lang.Object
  |
  +--com.cedarspring.tvm.Amortization
All Implemented Interfaces: 
java.io.Serializable 
________________________________________
public class Amortization 
extends java.lang.Object 
implements java.io.Serializable
This class represents the amortization for one payment period or a range of periods. It contains the total principal and interest paid, and the final balance at the end of the period. 
It is normally constructed and returned as a read-only object by the getAmortization(period1 [,period2]) method of the AmortizationSchedule class. 
Version: 
1.2.2 15-May-2003 
Author: 
Cedar Spring Software, Inc. 
See Also: 
Serialized Form
________________________________________
Field Summary
private  java.math.BigDecimal	balance 
		   
private  java.math.BigDecimal	interest 
		   
private  java.math.BigDecimal	principal 
		   
  Constructor Summary
(package private)	Amortization() 
		  Create a new default Amortization object.
(package private)	Amortization(java.math.BigDecimal principal, java.math.BigDecimal interest, java.math.BigDecimal balance) 
		  Create a new Amortization object containing the principal, interest, and balance for one period or a range of periods.
  
Method Summary
 java.math.BigDecimal	getBalance() 
		  Get the remaining balance.
 java.math.BigDecimal	getInterest() 
		  Get the interest portion of the payment(s).
 java.math.BigDecimal	getPrincipal() 
		  Get the principal portion of the payment(s).
  Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  
Field Detail
principal
private java.math.BigDecimal principal
________________________________________
interest
private java.math.BigDecimal interest
________________________________________
balance
private java.math.BigDecimal balance


Constructor Detail
Amortization
Amortization()
Create a new default Amortization object. Principal, interest, and balance are 0. 
________________________________________
Amortization
Amortization(java.math.BigDecimal principal,
			 java.math.BigDecimal interest,
			 java.math.BigDecimal balance)
Create a new Amortization object containing the principal, interest, and balance for one period or a range of periods. No checks are made for null values. 
Parameters: 
principal - Amount of the payment applied to principal. 
interest - Amount of the payment applied to interest. 
balance - Balance at the end of the period.

Method Detail
getPrincipal
public java.math.BigDecimal getPrincipal()
Get the principal portion of the payment(s). 
Returns: 
principal Principal portion of the payment.
________________________________________
getInterest
public java.math.BigDecimal getInterest()
Get the interest portion of the payment(s). 
Returns: 
interest Interest portion of the payment.
________________________________________
getBalance
public java.math.BigDecimal getBalance()
Get the remaining balance. 
Returns: 
balance Balance at end of period(s).
________________________________________




From the above it looks as if we can import an Amortization class at the beginning of a program, then call a subset of that class to perform calculations.

For instance if I had the following:


int [] loanNo = {1, 2, 3, 4};
int [] term = {84, 810, 360};
int [] rate = {5.35, 5.50, 5.75};
int [] payment = {2859.79, 1634.17, 1167.15};




and I am asking for and receiving user input like this:


System.out.println("Enter your choice of Loan 1, 2 or 3. Choose 4 to exit the program.");
	  loanNo = System.in.read();
	  System.in.read(); //To take care of the carriage return
	  System.in.read(); //To take care of the line feed
		  System.out.println("You have chosen Loan No. " + loanNo);




My first question is how do I get from "You have chosen X" to using that value in my IF loop.

I propose those loops be as follows:

-------- (pseudocode):

If LoanNo == 1
Do calculations for Loan 1
Display loan amortization for Loan 1

If LoanNo == 2
Do calculations for Loan 2
Display loan amortization for Loan 2

If LoanNo == 3
Do calculations for Loan 3
Display loan amortization for Loan 3

If LoanNo == 4
Break

Else
"You have entered an invalid selection. Select a number from 1 to 3 or type 4 to exit program."

---------- (/pseudocode)

Question on Class Amortization: Does the Class Amortization have components that will handle calculating the payment, interest and balance due for each month of the chosen loan? For instance, it looks like I could put something like:


getBalance
public java.math.BigDecimal getBalance()




and it would calculate a balance due. Would it, and if so, would it do so for each month in the loan period?

Thanks for any assistance you can provide.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Amortization Program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Java Amortization Program

Posted 11 January 2008 - 03:55 PM

First of all your pseudocode is along the right lines. You want to collect the chosen number and do a bunch of if STATEMENTS (they are not loops) and evaluate the chosen value and execute the proper calculations based on their choice.

Second I want to point out that the amortization class you are showing is actually part of a larger scheme where another class, called "AmortizationSchedule" generates the amortization class upon using the method "getAmortization". This class is designed to hold only three pieces of data... the principle, the interest, and the balance.

So think of an amortization schedule for a moment... each line is pulled out and put into this amortization class. It does no calculations itself, it is just holding data for a line of the amortizationschedule. For example, consider the variable "schedule" to be an instance of the amortization schedule. To generate this class instance you would do something like...

// Create an instance of the amortization class that reflects the period of Jan 10, 08
Amortization amortInstance = schedule.getAmortization("1/10/08");

// Print out the principle, the interest paid up till jan 10, 2008 and the current balance as of the period specified

System.out.println("Principle: " + amortInstance.getPrinciple() + " Interest Paid to Date: " + amortInstance.getInterest() + " Balance Owing: " + amortInstance.getBalanace());




So if you are wanting to input the content of those arrays, you have to look at your AmortizationSchedule class which will provide a constructor that will take in data and creates the schedule. Then use this amortization class to pull out data from that generated schedule.

I hope this makes sense to you. Just think of this amortization class as a read only line of the amortization schedule generated over in AmortizationSchedule class.

Good luck!

"At DIC we be amortization schedule generating code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Reason4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 20-December 07

Re: Java Amortization Program

Posted 12 January 2008 - 04:11 PM

Well, this is my second effort at a reply - I touched a button somehow that ditched my first effort. I really am getting frustrated; this program is not that hard!

First, Martyr2, thank you for diving into this mess with me -- if it were not for folks like you, we neophytes would be truly sunk!

I guess my first question is whether the AmortizationSchedule class is a "standard" Java import. I was looking at a class called TerminalIO for user input but my facilitator says it is not standard java and "doesn't recommend it." If AmortizationSchedule is in that same group then I am barking up a dead-end tree. (How's that for mixing analogies!)

Second question, assuming I can use it, do I import it by typing:

import AmortizationSchedule.*;



or do I need to do something like:

import com.cedarspring.tvm.*;

?

Thanks again for any advice you can give.

View PostMartyr2, on 11 Jan, 2008 - 03:55 PM, said:

First of all your pseudocode is along the right lines. You want to collect the chosen number and do a bunch of if STATEMENTS (they are not loops) and evaluate the chosen value and execute the proper calculations based on their choice.

Second I want to point out that the amortization class you are showing is actually part of a larger scheme where another class, called "AmortizationSchedule" generates the amortization class upon using the method "getAmortization". This class is designed to hold only three pieces of data... the principle, the interest, and the balance.

So think of an amortization schedule for a moment... each line is pulled out and put into this amortization class. It does no calculations itself, it is just holding data for a line of the amortizationschedule. For example, consider the variable "schedule" to be an instance of the amortization schedule. To generate this class instance you would do something like...

// Create an instance of the amortization class that reflects the period of Jan 10, 08
Amortization amortInstance = schedule.getAmortization("1/10/08");

// Print out the principle, the interest paid up till jan 10, 2008 and the current balance as of the period specified

System.out.println("Principle: " + amortInstance.getPrinciple() + " Interest Paid to Date: " + amortInstance.getInterest() + " Balance Owing: " + amortInstance.getBalanace());




So if you are wanting to input the content of those arrays, you have to look at your AmortizationSchedule class which will provide a constructor that will take in data and creates the schedule. Then use this amortization class to pull out data from that generated schedule.

I hope this makes sense to you. Just think of this amortization class as a read only line of the amortization schedule generated over in AmortizationSchedule class.

Good luck!

"At DIC we be amortization schedule generating code ninjas!" :snap:

Was This Post Helpful? 0
  • +
  • -

#4 Reason4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 20-December 07

Re: Java Amortization Program

Posted 12 January 2008 - 11:14 PM

I have made some progress since my last post. I decided to stick with the textbook for ideas rather than getting creative and risking not having a program to turn in by due date (Monday).

Here's what I have so far:


/***********************************************
Week 5 Individual Assignment: Mortgage Payment Calculation - Change Request #3
Due Date: 01/14/2008

The purpose of this program is to implement Change Request #3 to SR-mf-003, which asks that
the mortgage payment program display interest and principal payments required to fully
amortize three loans with different loan periods and different interest rates. The program
must use an array for the different loans and should display the.mortgage payment amount for
each loan and then list the loan balance and interest paid for each payment over the term of
the loan. Loops should be used to keep lists from scrolling off the screen.This change request
asks that a graphical user interface (GUI) not be used.
**********************************************/


/***********************************************
Import Utilities
***********************************************/

import java.io.*; //This calls error exception code
import javax.swing.JOptionPane; //This module allows users to input values in response to requests.
import java.text.*; //This is for decimal formatting.
import java.util.*; //This offers several methods, including input aids.

/**********************************************
Identify class Week5Mortgage and set access as public
*********************************************/

public class Week5Mortgage
{//begin Class Week5Mortgage
	public static void main (String[] args) throws IOException
	{//begin main

/**********************************************
Initialize and declare variables
**********************************************/

		DecimalFormat two = new DecimalFormat("$###,###.00");
		//DecmalFormat renders output in currency;
		double principal = 200000;
		int code = 0;
		String userChoice;
		int choice;
		BufferedReader myIn= new BufferedReader(new InputStreamReader(System.in));

/***********************************************
Initialize and declare arrays
***********************************************/

		double[] term = {7, 15, 30};
		double[] rate = {.0535, .0550, .0575};
		double[] payment = {2859.72, 1634.17, 1167.15};

/***********************************************
Calculations for principal and interest payments
**********************************************/


/**********************************************
System Output
**********************************************/

System.out.println();
	   System.out.println("+==================================================+");
	   System.out.println("|  ***										***  |");
	   System.out.println("|  ***		Mortgage Payment Program		***  |");
	   System.out.println("|  ***										***  |");
	   System.out.println("+==================================================+");
	   System.out.println();

	   System.out.println("\t\t Loans Available from \n \t McBride Financial Services \n");
	   System.out.println("Loan No. \t Term \t Interest \t Payment");

	   System.out.println("\n  1	\t	   7 Yrs \t	5.35% \t" + (two.format(payment[0])));
	   System.out.println("\n  2	\t	  15 Yrs \t	5.50% \t" + (two.format(payment[1])));
	   System.out.println("\n  3	\t	  30 Yrs \t	5.75% \t" + (two.format(payment[2])));
	   System.out.println("");

/**********************************************
Request for User Input
***********************************************/

System.out.print("Enter a number between 1 and 4, then press ENTER.\n");
userChoice = myIn.readLine();
choice = Integer.parseInt(userChoice);

/***********************************************
IF statements to display loan information and exit the program
***********************************************/

if (choice == 1)
{
  System.out.println("Your number is 1.");} //placeholder for calculations and results display

//need to do calculations for Loan 1
//need to display loan amortization for Loan 1


if (choice == 2)
{
  System.out.println("Your number is 2.");} //placeholder for calculations and results display


//need to do calculations for Loan 1
//need to display loan amortization for Loan 1
if (choice == 3)
{
  System.out.println("Your number is 3.");} //placeholder for calculations and results display


//need to do calculations for Loan 1
//need to display loan amortization for Loan 1


/*
if (choice == 4);

  break;
*/

 if (choice < 1 || choice > 4)
 {
   System.out.println("You have entered an invalid selection.\nSelect a number from 1 to 3 or type 4 to exit program.");
  }

 } //end main

} //end Class Week5Mortgage




I apologize for the poor formatting. My laptop is without internet access at the moment and my desktop (which is where I am now) does not have Textpad on it.

Anyway, I am still at a loss for a calculation to figure the monthly Balance, Interest Payment, and Principal Payment.

I also seem to have a problem with the "Break" statement.

Any help with these two puzzles is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Java Amortization Program

Posted 12 January 2008 - 11:54 PM

Well you appear to have a lot of questions on how to setup this amortization program, so let me ease some of the problems by telling you that I have already done an amortization program example on my blog some time ago. Guess what? It was written in Java as well as other languages and explains it step by step. It may help you. Check it out.

Martyr2's Programming Underground - Amortization Definitive

This can show you how to setup an amortization schedule one way and will show you some of the syntax behind it. May help you with your project.

Enjoy!

:)
Was This Post Helpful? 0
  • +
  • -

#6 Reason4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 20-December 07

Re: Java Amortization Program

Posted 13 January 2008 - 02:26 PM

Thanks Martyr2,

I have printed out your example and will be playing with it shortly. On first glance it looks like it may answer most of my questions. However, I have never run short on questions -- I will probably be back for more! :D

View PostMartyr2, on 12 Jan, 2008 - 11:54 PM, said:

Well you appear to have a lot of questions on how to setup this amortization program, so let me ease some of the problems by telling you that I have already done an amortization program example on my blog some time ago. Guess what? It was written in Java as well as other languages and explains it step by step. It may help you. Check it out.

Martyr2's Programming Underground - Amortization Definitive

This can show you how to setup an amortization schedule one way and will show you some of the syntax behind it. May help you with your project.

Enjoy!

:)

Was This Post Helpful? 0
  • +
  • -

#7 Reason4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 20-December 07

Re: Java Amortization Program

Posted 13 January 2008 - 05:36 PM

Hi Martyr2,

I copied your sample program into Textpad to see what it would do. It compiled just fine but did not run as expected.

I entered 100 and 10 at the user prompts. The result was just a continuous staring of zeros filling the command line window. I could stop it both by closing the command line and clicking control-C.

It could just be that something didn't copy correctly but I thought you might want to look at it at your end and see if it works okay for you.

Your program does give me something to work on as far as calculations, so I will continue to see what I can do with that. I particularly appreciate the method you have for catching the final payment. I am hopeful that I can incorporate that code into my program.

Again, thanks for the time you are taking to advise me on this program. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1