jasonpogi1669

Calculate Compound Interest (Java)

Jan 19th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.   public static void main(String[] args) {
  6.     while (true) {
  7.       Scanner in = new Scanner(System.in);
  8.       // user input: account balance
  9.       boolean ok = false;
  10.       double initial_amount = 0;
  11.       do {
  12.         System.out.print("Enter account balance: ");
  13.         String init = in.nextLine();
  14.         try {
  15.           Double d = Double.parseDouble(init);
  16.           initial_amount = d;
  17.           ok = true;
  18.         } catch (NumberFormatException e) {
  19.           ok = false;
  20.         }
  21.         if (ok) {
  22.           break;
  23.         } else {
  24.           System.out.println("Invalid input. Please try again.");
  25.         }
  26.       } while(!ok);
  27.       // user input: interest rate
  28.       ok = false;
  29.       double interest_rate = 0;
  30.       do {
  31.         System.out.print("Enter interest rate(%): ");
  32.         String init = in.nextLine();
  33.         try {
  34.           Double d = Double.parseDouble(init);
  35.           interest_rate = d;
  36.           ok = true;
  37.         } catch (NumberFormatException e) {
  38.           ok = false;
  39.         }
  40.         if (ok) {
  41.           break;
  42.         } else {
  43.           System.out.println("Invalid input. Please try again.");
  44.         }
  45.       } while(!ok);
  46.       double interest_amount = 1;
  47.       // hold values (to be used later for resetting the initial amount)
  48.       double tmp1 = initial_amount;
  49.       double tmp2 = interest_rate;
  50.       for (int qq = 1; qq <= 3; qq++) {
  51.         int n;
  52.         if (qq == 1) {
  53.           // annually
  54.           n = 1;
  55.         } else if (qq == 2) {
  56.           // monthly
  57.           n = 12;
  58.         } else {
  59.           // daily
  60.           n = 365;
  61.         }
  62.         // percentage to decimal
  63.         interest_rate /= 100;
  64.         // time period of the interest
  65.         interest_rate /= n;
  66.         // calculate interest amount for the next 10 years
  67.         interest_rate++;
  68.         for (int i = 0; i < n * 10; i++) {
  69.           interest_amount *= interest_rate;
  70.         }
  71.         initial_amount *= interest_amount;
  72.         // create labels for printing
  73.         String s = "";
  74.         if (qq == 1) {
  75.           s = "Annually";
  76.         } else if (qq == 2) {
  77.           s = "Monthly";
  78.         } else {
  79.           s = "Daily";
  80.         }
  81.         System.out.print("Compounded " + s + ": ");
  82.         System.out.printf("%.2f\n", initial_amount);
  83.         // reset values
  84.         initial_amount = tmp1;
  85.         interest_rate = tmp2;
  86.         interest_amount = 1;
  87.       }
  88.       // prompt the user to continue or exit the program
  89.       char c = ' ';
  90.       boolean checker = false;
  91.       boolean exit = false;
  92.       do {
  93.         System.out.print("\nDo you want to calculate again? (Y/N): ");
  94.         c = in.next().charAt(0);
  95.         if (c == 'Y' || c == 'y') {
  96.           System.out.println();
  97.           checker = true;
  98.         } else if (c == 'N' || c == 'n') {
  99.           System.out.println("Exiting program...");
  100.           exit = true;
  101.           break;
  102.         } else {
  103.           System.out.println("Invalid input. Please try again.");
  104.         }
  105.       } while (!checker);
  106.       if (exit) {
  107.         break;
  108.       }
  109.     }
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment