Introduction: Java - Compounding Interest

Picture of Java  -  Compounding Interest

// This program determines the amount of money that is compounded at 8%
// over time (subtracting 30% per month for taxes. 
// Run this program from the command line.
// User is prompted to enter the number of years that they want to invest,
// amount that they want to deposit each month and the initial amount.
// Results sent to screen and text file.
// Distributed under the GNU GPL: www.gnu.org

import javax.swing.JOptionPane;
import java.io.*;
import java.text.DecimalFormat;

class Compound9
  {   // Start class Compound9
      // Declare variables
  float mike, mike2, mike3, mike4;
  float interest;  // Interest amount 
  float amount, amount2;    // Initial amount invested
         double i=1;
  float months, months2;   //  Number of months money is going to be
                  //  deposited/invested; calculated from years
  float MonthlyAmount;  //  Amount deposited each month
  float initial, initial2; // Initial deposit
  float years, hotel2, hotel3;
  int n;
  String line;
 
  String bravo;
  String charlie;
  int delta, redhat;
  int echo;
  int foxtrot;

    // Method to ask user to enter in number of years
    // Method returns float
    float NumberYears() throws java.io.IOException
        {  // Start NumberYears method
         int delta = 0;       
         float hotel;  // Number of years money is going to be deposited/invested
         hotel = 0;
         String alpha;
           do {
                try {
                    // Print input question to screen
                    System.out.println("\nHow many years do you want to invest?");
                    //  Use the standard input stream in the System class, feed keyboard input
                    //  into variable "isr" of InputStreamReader class
              InputStreamReader isr = new InputStreamReader(System.in);
                    //  Feed information from variable "isr" into variable "br" of BufferedReader class
              BufferedReader br = new BufferedReader(isr);
                    //  Reading the Buffered Reader "br" into the String variable "alpha"
                    alpha = br.readLine();
                    // Cast String "alpha" into an float and return result
                    // to main part of program
                    hotel = (Float.parseFloat(alpha));
                    delta++;  // Increment do-while flag variable if a number is entered
                     } // end try loop
                    
           catch (IOException golf) {  // Start catch for IOException
                    System.out.println("IO Error - error caught: " + golf.getMessage());
                    golf.printStackTrace();
                    } // End catch IO Exception
                   
            // Catch exception if letter is entered
           catch (NumberFormatException echo) {
                System.out.println("You did not enter a number.");
                System.out.println("Error caught; you entered: " + echo.getMessage());
                echo.printStackTrace();
                // When program reaches this point, delta is still 0
                // therefore it will do the loop again
                }  // End catch
           } // End do
           while (delta == 0);
           // returns value in hotel to main program
           return hotel;
        } // End NumberYears method

     // Method to get amount deposited each month from user
    float MonthlyDeposit() throws java.io.IOException
        {  // Start MonthlyDeposit method
        do
            {  // Start do loop
                try
                    { // Start try section
                    // Print input question to screen
                    System.out.println("\nHow much do you want to deposit each month?");
                    //  Use the standard input stream in the System class, feed keyboard input
                    //  into variable "isr" of InputStreamReader class
              InputStreamReader isr = new InputStreamReader(System.in);
                    //  Feed information from variable "isr" into variable "br" of BufferedReader class
              BufferedReader br = new BufferedReader(isr);
                    //  Reading the Buffered Reader "br" into the String variable "bravo"
                    bravo = br.readLine();
                    // Cast String "bravo" into an float
                    hotel2 = Float.parseFloat(bravo);
                    // If exception is thrown on the line before, foxtrot++ is skipped
                    foxtrot++;
                    } //  End try section
                  
                   catch (IOException xray) {
                            System.out.println("IO Error - error caught: " + xray.getMessage());
                            xray.printStackTrace();
                            } // End IOException
                           
                   // Catch exception if letter is entered
                   catch (NumberFormatException hp) { 
                        System.out.println("You did not enter a number.");
                        System.out.println("Error caught; you entered: " + hp.getMessage());
                        hp.printStackTrace();
                        // Foxtrot is still zero at this point
                        }  // End catch for NumberFormatException
            } // End do loop      
               //  Flag that determines when do-while loop ends
               while (foxtrot == 0);    
               return hotel2;
        }  // End MonthlyDeposit method
       
    // InitialAmount method gets the initial amount invested
    float InitialAmount() throws java.io.IOException {
    do {  // Start do loop
        try {
            System.out.println("\nHow much is the initial investment?");
            // Feed keyboard input into InputStreamReader "isr"
            InputStreamReader isr = new InputStreamReader(System.in);
            // Feed InputStreamReader into BufferedReader
            BufferedReader br = new BufferedReader(isr);
            // Read the BufferedReader into variable "charlie"
            charlie = br.readLine();
            //  Cast String "charlie" into float and put into variable "hotel3"
            hotel3 = (Float.parseFloat(charlie));
            // Increment do-while flag variable if a number is entered
            redhat++;
            }  // End try section
       
        catch (IOException redhat2) {
            System.out.println("IOError - error caught: " + redhat2.getMessage());
            redhat2.printStackTrace();
            }  // End IO catch statement
           
        catch (NumberFormatException suse) {
            System.out.println("You did not enter a number");
            suse.getMessage();
            suse.printStackTrace();
            }  // End of NumberFormat catch
        } // End do loop
        while (redhat == 0);
            return hotel3;
    } // End InitialAmount method
   
    // NegativeCheck method checks for a negative number
    float NegativeCheck(float kilo) {
            if (kilo < 0) {
                System.out.println("Please enter a positive number.");
                mike = 0;
                          }  // End if statement
            else {
                mike = 1;
                 }
            return mike;
            }  // End NegativeCheck

float months()   // Method to calculate number of months
  {
  months = years * 12;
  System.out.println("The number of months is: " + months);
  return months;
  }
 
    // Method to calculate interest, print it out to the
    // the screen and write it to a file
float calculate(float months3, float initial2) 
  {
      try  // Loop to try to write to a file
   {
                 File temp = new File("Compound9.txt"); // Create new file
             FileWriter fw = new FileWriter(temp); // Create new FileWriter
             BufferedWriter out = new BufferedWriter(fw);// Create new BufferedWriter
     // Pass initial2 into variable "amount"
             amount = initial2;
        
     // Calculate interest for the first month
            // Calculate monthly interest (.08/12)
            // 8% annual growth (historical avg for stocks)
           interest = (float)(amount * .00667);
    // 20% taxes taken out
       interest = (float)(interest * .8);
            //  Interest added to running total
              amount = (amount + interest);
            //  Format the output to 2 decimal places and put a comma in (US style)
                            DecimalFormat roundTwo = new DecimalFormat("###,###,###,###.##");
            // Information placed in the String object "line" to be sent to file
                line = ("The amount after " + i + " months is $ " + roundTwo.format(amount) + "\n");
            // Information sent to screen
                System.out.println("The amount after " + i + " months is $ " + roundTwo.format(amount));
            // Write String "line" to file
       out.write(line);
   // NOTE: The reason that the first month interest is separate from the main loop is
   // because I wanted to make sure that the first month's interest was only calculated
   // for the initial deposit, not the initial deposit plus the first month's deposit.
            // Loop calculates from month 2 onward
            // Start for loop to determine compounding interest; limited by the number of months
   for (i = 2; i <=months3; i++)
    {
            //  Add monthly deposit to total amount
    amount = (amount + MonthlyAmount);
            // Calculate monthly interest (.08/12)
            // 8% annual growth (historical avg for stocks)
      interest = (float)(amount * .00667);
    // 20% taxes taken out
           interest = (float)(interest * .8);
            //  Interest added to running total
    amount = (amount + interest);
            //  Format the output to 2 decimal places and put a comma in (US style)
            //  DecimalFormat roundTwo = new DecimalFormat("###,###,###,###.##");
            // Information placed in the String object "line" to be sent to file
                line = ("The amount after " + i + " months is $ " + roundTwo.format(amount) + "\n");
            // Information sent to screen
                System.out.println("The amount after " + i + " months is $ " + roundTwo.format(amount));
            // Write String "line" to file
       out.write(line);
    }  // End for loop
    out.close(); // Close text file
   }  // End try loop

       catch (Exception e) { // Catch exception if fail to write file
    S

Comments

About This Instructable

2,966views

3favorites

License:

Bio: Occupation: Tech Support
More by matt392:Box for Raspberry Pi Model B and Power Supply Made From Wood ShimsQ-Tip Paint PenReplace Letters on Computer Keyboard (or Make Bigger Letters) With Nail Polish
Add instructable to: