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;
	}
	
}

Recommended Answers

All 3 Replies

I'm new myself, but there are many situations where you don't use static. For instance, adding static to a method means you can call that method without instantiating the class, but it also means that you can't input something and have the method interact with it. Static methods take a static variable, so it's always the same.

I seee nothing wrong in a static method provided that it gets all its data from the parameters (or embedded constants). There are many examples of this in the Java API - which I would use as the definitive guide to "normal" practice.
Re line 76: use the printf method rather than println.

"while (validInput == false)", it would be more esthetically pleasing to write "while(!validInput)".

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

This can be made more efficient by writing:

public static float calculateInterest( float mortgage, float interest, int period )
	{

		return  mortgage * (interest / 100) * period;
	}

You do some strange stuff to get the time. If I understand it correctly you got a string with the numbers in them. Then what you want to do is this:

//This turns the string into an integer if it contains only numbers (as I undestand it).
int time = Integer.parseInt(nPeriod);

The part where you try to remove a char:

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

Here you do not do anything. That is a hard way of saying: nPeriod = nPeriod;

to erase the last char only you can delete the last part so you are left with:

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

I hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.