/* Craig Persiko - CS 111A - Sample program
   InvestAgain.java

   A program to calculate the interest earned on an investment over time,
   and repeat the calculation as many times as user wants.
*/

import java.util.Scanner;
import java.text.DecimalFormat;


public class InvestAgain
{
  public static void main(String args[])
  {
    Scanner scan = new Scanner (System.in);
    double balance;
    char runAgain;
    // money object used to format numeric output (alternative to printf):
    DecimalFormat money = new DecimalFormat("#,##0.00");
    int yearsPassed;
    
    do{
	    yearsPassed= 0;
	
	    System.out.print("How much money do you want to invest? $");
	    balance = scan.nextDouble();
//	    scan.nextLine();  // This would be needed if we used scan.nextLine() below.
	
	    while(yearsPassed < 7)  // repeat each year for 7 years
	    {
	      balance += balance * 0.05;  // add 5% interest to the balance.
	      yearsPassed++;     // count the year
	      System.out.println("after " + yearsPassed + " years: " + balance);
	    }
	    System.out.println();
	    System.out.println("After 7 years at 5% interest, " +
	                       "you will have $" + money.format(balance));

	    System.out.println("Do you want to perform another calculation?");
	    runAgain = scan.next().toLowerCase().charAt(0);
    }while(runAgain == 'y');
  }
}

/* Sample output:
 
How much money do you want to invest? $100
after 1 years: 105.0
after 2 years: 110.25
after 3 years: 115.7625
after 4 years: 121.550625
after 5 years: 127.62815624999999
after 6 years: 134.00956406249998
after 7 years: 140.71004226562496

After 7 years at 5% interest, you will have $140.71
Do you want to perform another calculation?
Yes
How much money do you want to invest? $50
after 1 years: 52.5
after 2 years: 55.125
after 3 years: 57.88125
after 4 years: 60.7753125
after 5 years: 63.814078124999995
after 6 years: 67.00478203124999
after 7 years: 70.35502113281248

After 7 years at 5% interest, you will have $70.36
Do you want to perform another calculation?
no

*/



syntax highlighted by Code2HTML, v. 0.9