Java Help Calculating The Balance

Dr Octogonapus

Active member
Regular
Joined
Nov 10, 2009
Messages
1,522
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
Hey guys i am in need of some desperate help with a program i'm trying to create. Basically it's a program that has 3 options. Selecting "A" will prompt the user for 3 values. An Initial Deposit Amount, Rate of interest per year and Amount to be deposited each month.

Once the user has supplied the initial deposit amount and interest rate for the account as well as the monthly deposit amount, the program should then proceed to calculate the new balance for the account including interest earned over a six month period.

So far i have the basic outline out and i have the 3 input values but for the 2nd value "Enter the annual interest rate as a percentage" i don't know how to code it to it says for example 6.0 instead of 60.

And how would i do the calculation for the initial deposit amount, monthly deposit amount and new balance for the account including interest? I'm new to java programming so i'm still a bit unsure. Any help would be greatly appreciated. Here is my program code,

Code:
import java.util.Scanner;
 
public class MenuDrivenProgram
{
    private static Scanner s = new Scanner(System.in);
   
    public static void main(String[] args)
    {
        String inputLine;
        char option = ' ';
       
        do
        {
            //Prints the line allowing you to select one of the valid options
            printMenu();
            System.out.printf("Please select a valid option: ");
            inputLine = s.nextLine();
           
            //Prints out an error message if length is equal to 1
            if (inputLine.length() != 1)
            {
                System.out.printf("Error! You did not select a valid option!\n");
                System.out.printf("Please try again\n\n");
            }
            else
            {
                option = inputLine.charAt(0);
                option = Character.toUpperCase(option);
               
                switch (option)
                {
                        //Brings up the InvestmentReport menu
                    case 'A':
                        InvestmentReport();
                        break;
                       
                        //Brings up the RainfallReport menu
                    case 'B':
                        RainfallReport();
                        break;
                       
                        //Exits the program with a goodbye message
                    case 'X':
                        System.out.printf("Exiting the program - goodbye...\n\n");
                        break;
                       
                        //Errors out if keyed in value isn't a valid menu option
                    default:
                        System.out.printf("Error!" +
                                          "\"%c\" is not a valid menu option!\n\n",
                                          option);
                       
                }
            }
           
           
        } while (option != 'X');
    }
    //This prints the main menu and let's you choose between 3 different options. InvestmentReport, RainfallReport and Exiting the program
    public static void printMenu()
    {
        System.out.printf("*************** Assignment 2 Menu ***************\n\n");
        System.out.printf("\tA. InvestmentReport.\n");
        System.out.printf("\tB. RainfallReport.\n");
        System.out.printf("\tX. Exit the program\n\n");
        System.out.printf("*************************************************\n\n");
    }
   
    public static void InvestmentReport()
    {
        System.out.printf("*** Investment Report stub ***\n");
        // This is where your Part A solution goes
        System.out.printf("*************** InvestmentReport Menu ***************\n\n");
        Scanner console=new Scanner(System.in);
        int deposit, monthly;
        double interest;
        System.out.println ("Enter your initial deposit amount in dollars\n");
        deposit = console.nextInt();
         
        System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
        interest = console.nextInt();
       
        System.out.println ("Enter your monthly deposit amount in dollars\n");
        monthly = console.nextInt();
       
        System.out.println ("Savings growth over the next 6 months:\n");
       
        System.out.println ("Balance after first month:\n");
        System.out.println ("Interest earned for this month:\n");
       
        System.out.println ("Balance after second month:\n");
        System.out.println ("Interest earned for this month:\n");
       
        System.out.println ("Balance after third month:\n");
        System.out.println ("Interest earned for this month:\n");
       
        System.out.println ("Balance after fourth month:\n");
        System.out.println ("Interest earned for this month:\n");
       
        System.out.println ("Balance after fifth month:\n");
        System.out.println ("Interest earned for this month:\n");
       
        System.out.println ("Balance after sixth month:\n");
        System.out.println ("Interest earned for this month:\n");
    }
   
   
    public static void RainfallReport()
    {
        System.out.printf("*** Rainfall Report stub ***\n");
        // This is where your Part B solution goes
        System.out.printf("*************** RainfallReport Menu ***************\n\n");
            Scanner sc = new Scanner(System.in);
            double[] fall = new double[7];
            for (int i = 0 ; i < 7 ; i++)
            {
                System.out.print("Enter RainFall Measure In Millimetres: "  + (i+1) + ": ");
                fall[i] = sc.nextDouble();
            }
            System.out.println("R.Fall Reverse Order ");
            for ( int i = 0;  i>=7    ;  i--)
            {
                System.out.println(fall[i]);
           
        }
    }
   
   
}
 

Hypemaster

Active member
Elite
Joined
Jan 3, 2013
Messages
9,686
Kin
6💸
Kumi
0💴
Trait Points
0⚔️
Alright so are you familiar with the compound interest formula? It's A = P(1 + r) ^ n

P is the principle, r is the rate, and n is whatever time you set it for.

The rate you have to enter will have to be a decimal, maybe that's why you got such a large number. Or if you want to type in like 70% in the input prompt for rate, make sure you divide the rate by 100 afterwards to get a decimal

Here is a code I used in a similar situation


Code:
                double amount;
		//number of viewers
		double principal = 10000;
		//gain 5% of viewer/month
		double rate = .05;
		//leave in bank for 12 months


		for (int month= 1; month<=12; month++)
		{
                        //the formula
			amount = principal * Math.pow(1 + rate, month);
                        //Prints out how many views I have each month
			System.out.println(month+ "   " + amount);
		}

What this does is shows the month it is and the amount with the interest included every month as it increments up.

So to display the initial deposit amount it will just be the amount without interest, so you would print the amount before the for loop.

What I have shown in the code will display the monthly deposit amount for how many months you want it to.

And the new new balance amount will be printed after and outside of the for loop (outside of its brackets)

Hope this might help to some degree.
 
Last edited:
Top