// Arup Guha
// 7/14/03
// Solution to BHCSI 2003 Intro to Java Program #5: Car Payments
// Brief Description: Based on the user input for the price of a car
//                    loan, yearly loan interest, and monthly payment,
//                    a monthly payment schedule is printed out.
// Known Bugs: Does not error check for negative input values and
//             the dollar sign did not print out properly when run.

import java.io.*;
import java.text.NumberFormat;

public class Payment {

  public static void main(String[] args) throws IOException{


    BufferedReader stdin = new BufferedReader
				(new InputStreamReader(System.in));

    NumberFormat money = NumberFormat.getCurrencyInstance();

    double price, rate, payment;

    // Read in user input.
    System.out.println("What is the amount of your loan?");
    price = Double.valueOf(stdin.readLine()).doubleValue();

    System.out.println("What is the interest rate of your loan?");
    rate = Double.valueOf(stdin.readLine()).doubleValue();

    System.out.println("What is the amount of your monthly payment?");
    payment = Double.valueOf(stdin.readLine()).doubleValue();

    // Changes rate based on percentage and monthly vs. yearly charge.
    rate = rate/1200;

    // Calculate interest for first month.
    double first_interest = price*rate;

    // Check to see if car can be paid off.
    if (payment > first_interest) {

      System.out.println("Month\tPayment\t\tAmt. Left");

      // Print out schedule of payments.
      int month = 1;
      while (price > 0) {

        // Print out beginning of char and calculate new principle.
        System.out.print(month+"\t");
        price = price*(1+rate);

        // Take care of non-final payment.
        if (price > payment) {
          price -=payment;
          System.out.print(" "+money.format(payment)+"\t");
        }

        // Final payment.
        else {
          System.out.print(" "+money.format(price)+"\t");
          price = 0;
        }

        // Go to next month.
        System.out.println(" "+money.format(price));
        month++;
      }
    }

    // Print out error message.
    else
      System.out.println("Your monthly payment is not high enough.");
  }
}
