0

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