/* Craig Persiko - CS 111A
   Retire.java

   A program to calculate retirement savings.
   Uses printf to format output.
*/

import java.util.Scanner;

class Retire
{
  public static void main(String args[])
  {
    Scanner keyIn = new Scanner (System.in);
    double balance = 0, contribution, monthlyRate;
    int age, numMonths, i;

    System.out.print("Welcome to Craig's retirement planner\nHow old are you? ");
    age = keyIn.nextInt();

    if (age >= 65)
      System.out.println("Since you're 65 or older already, it's too late to save for retirement!");
    else
    {
      numMonths = (65 - age) * 12;
      monthlyRate = 0.05 / 12;
      System.out.print("How much money do you want to invest each month? $");
      contribution = keyIn.nextDouble();

      i=0;
      while(i < numMonths)  // each month until age 65
      {
        balance += balance * monthlyRate;  // add one month's interest to the balance.
        balance += contribution;  // add another month's contribution
        i++;
      }

      // printf can be used to format output. Notice %% is needed to outupt a literal %
      System.out.printf("With 5%% interest, by the time you're 65" +
                         " you will have $%,12.2f\n", balance);
      // format balance to show comma separators for thousands, millions, etc.
      // in a field of 10 chars, with 2 digits after the decimal point
    }
  }
}

/* Sample Output:

-bash-3.2$ java Retire
Welcome to Craig's retirement planner
How old are you? 36
How much money do you want to invest each month? $100
With 5% interest, by the time you're 65 you will have $   78,006.99

-bash-3.2$ java Retire
Welcome to Craig's retirement planner
How old are you? 70
Since you're 65 or older already, it's too late to save for retirement!

-bash-3.2$ java Retire
Welcome to Craig's retirement planner
How old are you? 21
How much money do you want to invest each month? $125.50
With 5% interest, by the time you're 65 you will have $  240,473.79

-bash-3.2$ java Retire
Welcome to Craig's retirement planner
How old are you? 18
How much money do you want to invest each month? $500
With 5% interest, by the time you're 65 you will have $1,132,140.13

*/


syntax highlighted by Code2HTML, v. 0.9