5 Replies - 2706 Views - Last Post: 04 July 2010 - 09:07 PM Rate Topic: -----

#1 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Critique simple Interest calculator

Posted 04 July 2010 - 08:31 PM

Hello I am new to Java & I have made a simple mortgage interest calculator.

I am looking for advice on my program such as:
- Proper java structure
- Better more efficient ways of doing what I am doing below
- Proper java format(variable names, function names)
- Are global functions a big no no in Java, should everything be in a class
- Should global functions always be public static ??


Thanks for your time :)

/*
 
   Mortgage Interest Calculator:
   	
	Hello I am looking for advice on:
	  - Proper java structure
	  - Better more efficient ways of doing what I am doing below
	  - Proper java format(variable names)
	  - Are global functions a big no no in Java, should everything be in a class
	  - Should global functions always be public static ??
 
*/


import java.util.Scanner;


public class MortageInterest
{
	
	public static void main(String[] args)
	{
		
		/// Variables
		float mortgage = 0;
		float interest = 0;
		int period = 0;
		float totalInterest = 0;
		String nPeriod;
		boolean validInput = false;
		Scanner in = new Scanner(System.in);
		
		
		
		/// Menu
		System.out.print(" *** Mortgage Interest Calculator *** \n");
		
		// Take mortgage value
		System.out.print(" Please enter the total value of the loan: $");
		mortgage = in.nextFloat();
		
		// Take interest value
		System.out.print(" Please enter the interest rate value (5.75% = 5.75): ");
		interest = in.nextFloat();
		
		// Take period calculation
		while (validInput == false)
		{
			System.out.print(" Please enter the period in which interest is calculated (in the form NP / 3M = 3 months / 2Q = 2 quarters / 1Y = Yearly): ");
			nPeriod = in.next();
			period  = determinePeriod( nPeriod );
			
			if (period != -1)
				validInput = true;
			
		}
		
		
		
        /// Calculate Interest
		float weeklyInterest = interest / period;
		totalInterest = calculateInterest(mortgage, weeklyInterest, period);
		
		
		/// Output total interest over period
		System.out.println(" * Interest Information * ");
		// In python I can go "This is a %s" % "string" can I do this in Java?
		//System.out.println(" The total interest paid over the period of %(period)s weeks = $(totalInterest)s");
		//System.out.println(" Total cost of mortgage over this period = $(totalInterest + mortgage)s \n ");
		System.out.println(" The total interest paid over the period of " + Integer.toString(period) + " weeks = $" + Float.toString(totalInterest));
		System.out.println(" Total cost of mortgage over this period = $" + Float.toString( totalInterest + mortgage ) + " \n ");
		
	}
	
	
	// Global Methods:
	public static int determinePeriod( String nPeriod ) // should this be final?? instead of static
	{
		// Post: Identify period type (that interest is calculated in) & convert
		//       into 'Week Units'
		
		
		nPeriod = nPeriod.trim();
		nPeriod = nPeriod.toUpperCase();
		char period = nPeriod.charAt( nPeriod.length() - 1 );
		
		// check we have some sort of valid input - catch something like "E53"
		if ( nPeriod.length() > 4 || !Character.isLetter(period) )
			return -1;
		
		
		// How can I erase the last character of nPeriod???
		// nPeriod.erase(nPeriod.length() - 1);
		// nPeriod[ nPeriod.length() -1 ] = '';
		// Seriously? this below is the only way to delete a char from a string?
		// thats totally complicated!!
		nPeriod = nPeriod.substring(0,nPeriod.length() - 1) + nPeriod.substring(nPeriod.length() - 1 +1);
		
		// Calculate time
		int time = 0;
		int decimalValue = 1;
		
		while ( !nPeriod.isEmpty() )
		{
			int digit = (int)nPeriod.charAt(nPeriod.length() - 1) - '0';
		    time = digit * decimalValue;
		    decimalValue *= 10;
		    nPeriod = nPeriod.substring(0,nPeriod.length() - 1) + nPeriod.substring(nPeriod.length() - 1 +1);
	    }
		
		
		switch ( period )
		{
		   case 'D':
			   
			   return  (int) time / 7;

		   case 'M':
		   
			   return  (int) time * 4;
			   
		   case 'Q':

			   return  (int) time * 12;
			   
		   case 'W':

			   return  (int) time;
			   
		   case 'Y':

			   return  (int) time * 52;
			   
		   default:
			      System.out.println("Invalid period value.");
			      return -1;

		}
		
	}
	
	
	public static float calculateInterest( float mortgage, float interest, int period )
	{
		// Post: 
		
		float intRate = interest / 100;
		
		
		return  mortgage * intRate * period;
	}
	
}

This post has been edited by gretty: 04 July 2010 - 08:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Critique simple Interest calculator

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Critique simple Interest calculator

Posted 04 July 2010 - 08:58 PM

first of all, it isn't that hard to get rid of the last character in a String:

nPeriod = nPeriod.substring(0, a.length() - 1);

This post has been edited by Luckless: 04 July 2010 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: Critique simple Interest calculator

Posted 04 July 2010 - 09:02 PM

Given where you are, I'd say your program looks pretty good overall. Just a few things. First, you should stick with doubles unless you have some really good reason to use float, like a standard JDK tool forces you to use floats for interacting with it. The doubles are 64-bit floating point numbers, while floats are 32-bits, so doubles are way more accurate.

In regards to globals, for where you are they are currently fine. However, in OOP, global methods and variables generally aren't good practice, as globals are the same for all instances of a class (like the law), while instance variables can be unique for each instance of a class (like salaries) and the instance methods usually correspond with those instance variables. To answer your question about the static keyword, it is used to make an element global, though you can use any access modifier you want (public, private, protected, or no access modifier). I saw in your comments a question about the final keyword, and applying it to a method would mean it couldn't be overriden in a subclass. You'll get to that later, though.

Just a note on integer division, but here (int) time / 7; , if time = 8, your result will be 1 as an int. So you might want to check first if time%7 == 0, and if not, append a +1 to the period.

Lastly, I saw a question in your comments about removing characters from Strings. The String class follows a design pattern of immutability. This basically means that once you create a String object, its contents don't change. However, you can use the String methods, many of which return a new String with the specified changes.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Critique simple Interest calculator

Posted 04 July 2010 - 09:02 PM

A main() method should have only a few lines
It should be use to unit test a Java object (if you don't know Java is an Oject Oriented language)
so your main() should be

public static void main(String[] args) {
   MortgageInterest mi = new MortgageInterest();
   .. may be input some data
   mi.run(with data as parameter);
}



thats it
Was This Post Helpful? 0
  • +
  • -

#5 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Critique simple Interest calculator

Posted 04 July 2010 - 09:03 PM

Also, why does your method need to be static? It seems to me that this is unnecessary. You see, if a method is static, that means that the data it is dealing with (static variables) are shared and can be edited by multiple instances of a class. Since none of the variables in the method are static

Edit: Mac addresses this better than I do. Ninja'd

This post has been edited by Luckless: 04 July 2010 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: Critique simple Interest calculator

Posted 04 July 2010 - 09:07 PM

All things considered, I do advocate for a more Object-Oriented approach. However, I'm guessing by the structure of the code, they maybe just started methods, so OOP may not be too far off.

@gretty: Also, I forgot to address your question about the formatters in my last post. In Java, you can use the System.out.printf() method with formatters. The PrintWriter class covers the exact specifications for this.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1