Advertisement
Shaun_B

Simple interest calculator

Oct 31st, 2012
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. /**
  2.  * This answers Exercise 3 of Worksheet one, please read and understand the code
  3.  * and check it against resources available online, or ask your tutor as I think
  4.  * that there might be some refinement needed here.
  5.  *
  6.  * @author Shaun B
  7.  * @version 2012-10-30
  8.  **/
  9. import java.util.Scanner;
  10. public class exerciseThree
  11. {
  12.     // Here is our fixed interest rate expressed as a final (ie, cannot be modified):
  13.     private static final double fixedInterestRate = (double)2.3;
  14.     private static int numberOfYears = 0;
  15.     private static Scanner keyboardInput = new Scanner(System.in);
  16.     private static boolean run = true;
  17.     public static void main(String [] elephants)
  18.     {
  19.         // Local variables available in this scope only:
  20.         int i = 0;
  21.         double total = 0.00;
  22.         double capital = 0.00;
  23.         int step = 0;
  24.         double currentBalance = 0.00;
  25.         String keyboardError="Illegal keyboard entry in main class";
  26.         // User instructions:
  27.         System.out.format("The current fixed interest rate is at %f\n",fixedInterestRate);
  28.         System.out.println("This example will ask for an investment capital and add");
  29.         System.out.println("the interest after each year up to 500 years. You will be");
  30.         System.out.println("asked to enter your initial capital amount, how many years");
  31.         System.out.println("of interest and the step in each year(ie, 5 will show you");
  32.         System.out.println("how much interest is added after each five years up to the");
  33.         System.out.println("maximum that you enter).");
  34.         while (run)
  35.         {
  36.             System.out.print("How much capital do you wish to invest? £");
  37.             try
  38.             {
  39.                 capital = keyboardInput.nextDouble();
  40.             }
  41.             catch(Exception e)
  42.             {
  43.                 System.err.println(keyboardError);
  44.                 run = false;
  45.                 System.exit(0);
  46.             }
  47.             System.out.print("How many years do you wish to calculate the interest? ");
  48.             try
  49.             {
  50.                 numberOfYears = keyboardInput.nextInt();
  51.             }
  52.             catch(Exception e)
  53.             {
  54.                 System.err.println(keyboardError);
  55.                 run = false;
  56.                 System.exit(0);
  57.             }
  58.             System.out.print("How often do you wish to see the balance (ie, every 5 years)? ");
  59.             try
  60.             {
  61.                 step = keyboardInput.nextInt();
  62.             }
  63.             catch(Exception e)
  64.             {
  65.                 System.err.println(keyboardError);
  66.                 run = false;
  67.                 System.exit(0);
  68.             }
  69.             if (capital == 0 || numberOfYears == 0 || step == 0)
  70.             {
  71.                 System.out.println("Goodbye!");
  72.                 run = false;
  73.                 System.exit(0);
  74.             }
  75.             for( i=1; i<=numberOfYears; ++i )
  76.             {
  77.                 // The exercise seems to be asking for a simple interest calculation,
  78.                 // rather than a compound interest calculation, and this is the formula
  79.                 // provided on the sheet, so it seems to be okay. I'd check this - search
  80.                 // online for a simple interest calculator in Java or C/C++ and you
  81.                 // should have a more perfect solution I think.
  82.                 currentBalance = (double)capital*(1.00f + 0.01f * fixedInterestRate);
  83.                 if(i%step==0 && i!=0)
  84.                 {
  85.                     // Prints out the current balance to two decimal places:
  86.                     System.out.printf("After %d years, the balance is %.2f", i, currentBalance);
  87.                     System.out.println("");
  88.                 }
  89.                 capital = (double)currentBalance;
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement