/**StudentLoanProgram.java
 *
 * Caitlin Ross
 * 100735219 
 * 03/10/07
 */

import java.util.*; 
import java.text.DecimalFormat;

public class StudentLoanProgram{
 
public static void main(String[] args){
	
	//Variable Declaration
	
	double principle=0;
	int term=0;
	double annualInterestRate=0;
	double amountStillOwing=0;
	boolean validInput; // checks to see if the input matches the variable type
	String userResponse;
	
	Date today;
	today = new Date();
	
	DecimalFormat decimal = new DecimalFormat("0.00");
		
	do{ //Large do-while that re-iterates the entire program
	
	   System.out.println("Welcome to the Student Loan Calculator");
	   System.out.println("Today is: " + today);
	   System.out.println("=======================================");
	   System.out.println("");
	   
	   //Entering the loan, with type-checking
	   
	   validInput = false;
	   while (!validInput || principle<0){
	   	try{
	   		System.out.print("Enter the amount of the loan (ex. 6000.00): ");
	   		principle = new Scanner(System.in).nextDouble();
	   		validInput = true;
	   		if (principle<0)
	   			System.out.println("\tInvalid number entered. Please try again.\n");
	   	}
	   	catch (java.util.InputMismatchException e) {
            System.out.println("\tInvalid number entered. Please try again.\n");
        }
	   }
	   
	   System.out.print('\n');
	   
	   //Entering the interest rate, with type-checking
	   
	   validInput = false;
	   while (!validInput || annualInterestRate<0 || annualInterestRate>100){
	   	try{
	   		System.out.print("Enter the annual interest rate: (ex. 12.0, must be between 0 and 100): ");
	   		annualInterestRate = new Scanner(System.in).nextDouble();
	   		validInput = true;
	   		if (annualInterestRate<0 || annualInterestRate>100)
	   			System.out.println("\tInvalid number entered. Please try again.\n");
	   	}
	   	catch (java.util.InputMismatchException e) {
            System.out.println("\tInvalid number entered. Please try again.\n");
        }
	   }
	   
	   System.out.print('\n');
	   
	   //Entering the number of months, with type-checking
	   
	   validInput = false;
	   while (!validInput || term<0){
	   	try{
	   		System.out.print("Enter the term in months: (ex. 18): ");
	   		term = new Scanner(System.in).nextInt();
	   		validInput = true;
	   		if (term<0)
	   			System.out.println("\tInvalid number entered. Please try again.\n");
	   	}
	   	catch (java.util.InputMismatchException e) {
            System.out.println("\tInvalid number entered. Please try again.\n");
        }
	   }
	   
	   //interest rate is divided by 100 for percent, then by 12 for months in a year
	   annualInterestRate/=1200;
	   
	   double monthlyPayment = principle * annualInterestRate * Math.pow( annualInterestRate + 1 , term )
	   	 /( Math.pow( annualInterestRate + 1 , term ) - 1 );
	      
	   System.out.println("\nMonthly payment will be: $" + decimal.format(monthlyPayment));
	      
	   System.out.println("\nThe loan is $" + decimal.format(principle) + " at an interest rate of " + 
	      decimal.format(annualInterestRate*1200) + "%");
	      
	   amountStillOwing = principle;
	      
	   System.out.println("\nMonth	Owing  	   		Payment   		Amount Remaining");
	   System.out.println("========================================================");
	   
	   //Loop that creates the payment chart
	   
	   for (int x=1; x<=term; x++)
	   {
	   	  amountStillOwing*=(1 + annualInterestRate);
	      	
	      if(x<10)
	      	System.out.print(x + "   ");
	      else if (x<100)
	      	System.out.print(x + "  ");
	      else
	      	System.out.print(x + " ");
	      		
	      System.out.print("\t$" + decimal.format(amountStillOwing));
	      
	      //If-else statements that line up columns in the chart
	      	
	      if (amountStillOwing<10)
	      	System.out.print("     \t\t");
	      else if (amountStillOwing<100)
	      	System.out.print("    \t\t");
	      else if (amountStillOwing<1000)
	      	System.out.print("   \t\t");
	      else if (amountStillOwing<10000)
	      	System.out.print("  \t\t");
	      else if (amountStillOwing<100000)
	      	System.out.print(" \t\t");
	      else if (amountStillOwing < 1000000)
	      	System.out.print("\t\t");
	      else if (amountStillOwing < 10000000)
	      	System.out.print("  \t");
	      else if (amountStillOwing < 100000000)
	      	System.out.print(" \t");
	      else
	      	System.out.print("\t");
	      	
	      System.out.print("$" + decimal.format(monthlyPayment));
	      amountStillOwing-=monthlyPayment;
	      
	      //Gets rid of minor rounding errors, if they exist
	      
	      if (x==term && amountStillOwing<0)
	      	amountStillOwing=0;
	      	
	      //If-else statements that line up columns in the chart
	      	
	      if (monthlyPayment<1000)
	      	System.out.print("\t\t\t");
	      else if (monthlyPayment<10000000)
	      	System.out.print("\t\t");
	      else
	      	System.out.print("\t");
	      	
	      System.out.println("$" + decimal.format(amountStillOwing));
	   }
	   
	   //Conclusion of current calculation
	   
	   System.out.println("\nTotal Amount Paid: $" + decimal.format(monthlyPayment*term));
	   System.out.print("\nDo You Want to Perform Another Calculation? [Y or N] ");
	   userResponse = new Scanner(System.in).next();
	   System.out.println('\n');
	
	}while(userResponse.startsWith("Y") || userResponse.startsWith("y"));
	
	//End of program
	
	System.out.println("GOODBYE");
  }
}
