Search code examples
javadoublejoptionpanebanking

Banking interest Q


Im trying to make a program that calculates the compound interest of an account with the principle,interest rate, and years. Im trying to do it with dialogs/ The program has the output the return on the invesment if the principle is left to accumulate.

Im stuck on the calculation part, please help

package firstAssignment;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class thinkingQuestion {

public static void main(String[] args) {

//Banking program that asks user for the amount of money they wish to invest in a 
//compound interest account (principle), the interest rate (percent value) and the time frame (years).

    Scanner in= new Scanner(System.in);

    String principle, interestVal, years;
    int newPrinciple,newYears;
    double total;

        principle=JOptionPane.showInputDialog("How much money would you like to invest?");

        interestVal=JOptionPane.showInputDialog("What's the interest rate?");   

        years=JOptionPane.showInputDialog("How many years?");

        //convert from String to integer

        newPrinciple=Integer.parseInt(principle);
        newYears=Integer.parseInt(years);

        double newInterestVal=Integer.parseInt(interestVal);

        total=JOptionPane.PLAIN_MESSAGE(newPrinciple*Math.pow(1+ newInterestVal, newYears), newYears);

Solution

  • I deleted somo variables you don't need, I think the main problem is on java syntax for show messages. Here you could see a tutorial:

    https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

    import javax.swing.JOptionPane;
    
    public class InterestBanking {
    
        public static void main(String[] args) {
    
            // Banking program that asks user for the amount of money they wish to
            // invest in a
            // compound interest account (principle), the interest rate (percent
            // value) and the time frame (years).
    
            String principle, interestVal, years;
            float newPrinciple, newYears;
    
            principle = JOptionPane.showInputDialog("How much money would you like to invest?");
    
            interestVal = JOptionPane.showInputDialog("What's the interest rate?");
    
            years = JOptionPane.showInputDialog("How many years?");
    
            // convert from String to integer
    
            newPrinciple = Float.parseFloat(principle);
            newYears = Float.parseFloat(years);
    
            double newInterestVal = Float.parseFloat(interestVal);
    
            //You could change your calculation here if this isn't the need formula
            double interest = newPrinciple * Math.pow(1 + newInterestVal, newYears);
    
            //you were assigning the result to a total variable. That's not neccesary
            JOptionPane.showMessageDialog(null, "Interest:" + NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(interest) + " In years: " + newYears);
        }
    }