/* Lab4.java  -  Craig Persiko - CS 111A
   Solution to Homework 8: Consumer Loan program
*/

import java.util.Scanner;


class Lab4
{
  public static void main(String args[])
  {
    double principle, payment, interestRate, firstMonthsInterest;

    System.out.println("** Welcome to the Consumer Loan Calculator **");

    principle = getPosNumber("How much do you want to borrow? $");
    interestRate = getPosNumber("What is the annual interest rate expressed as a percent? ");
    payment = getPosNumber("What is the monthly payment amount? $");

    interestRate /= 100.0;  // Now it's a decimal

    interestRate /= 12.0;   // Now it's a monthly rate


    firstMonthsInterest = principle * interestRate;
    if (firstMonthsInterest < payment)
    {
      payOffLoan(principle, interestRate, payment);
    }
    else
      System.out.printf("You must make payments of at least $%,.2f"
            + "\nBecause your monthly interest is $%,.2f\n",
            firstMonthsInterest + 1, firstMonthsInterest);

    System.out.println("** Don't get overwhelmed with debt! **");
  }

  /* This function outputs the prompt to the console, reads input
     from the console, and converts it to a number,
     which the function returns.  If the number is not positive,
     the user is told to enter a new number until a positive number is entered. */
  static double getPosNumber(String prompt)
  {
    Scanner keyIn = new Scanner(System.in);
    double val;
    do
    {
      System.out.print(prompt);
      val = keyIn.nextDouble();
      if (val <= 0) System.out.println("You must enter a positive number");
    } while(val <= 0.0);
    return val;
  }

  /* This function assumes the payment is enough
     to cover first month's interest.  It has a loop to
     pay off the loan one month at a time.
     It outputs to the console:
       - how many months it will take to pay off the loan
       - total amount of interest paid during that time
       - amount of final payment
  */
  static void payOffLoan(double principle, double rate, double payment)
  {
    double monthsInterest, finalPayment, totalInterest = 0;
    int months = 0;

    while (principle > 0)
    {
      monthsInterest = principle * rate;
      totalInterest += monthsInterest;
      principle -= (payment - monthsInterest);
      months++;
    }
    finalPayment = payment + principle;
    // Above works because principle is either negative or zero.

    // It stores how much we overpaid by paying the full payment.


    System.out.printf("Your debt will be paid off after %d months, with a "
         + "final payment of just $%,.2f\n", months, finalPayment);

    System.out.printf("The total amount of interest you will "
         + "pay during that time is $%,.2f\n", totalInterest);
  }
}

/*     SAMPLE OUTPUT:

** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $1000
What is the annual interest rate expressed as a percent? 18
What is the monthly payment amount? $50
Your debt will be paid off after 24 months, with a final payment of just $47.83
The total amount of interest you will pay during that time is $197.83
** Don't get overwhelmed with debt! **


** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $15000
What is the annual interest rate expressed as a percent? 10
What is the monthly payment amount? $100
You must make payments of at least $126.00
Because your monthly interest is $125.00
** Don't get overwhelmed with debt! **


** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $-50
You must enter a positive number
How much do you want to borrow? $-200
You must enter a positive number
How much do you want to borrow? $20000
What is the annual interest rate expressed as a percent? -2.5
You must enter a positive number
What is the annual interest rate expressed as a percent? 5
What is the monthly payment amount? $0
You must enter a positive number
What is the monthly payment amount? $200
Your debt will be paid off after 130 months, with a final payment of just $125.79
The total amount of interest you will pay during that time is $5,925.79
** Don't get overwhelmed with debt! **

*/


syntax highlighted by Code2HTML, v. 0.9