import java.util.*;
import java.text.*;
 

public class Interest {
	static DecimalFormat fmt=new DecimalFormat("0.00"); //2 decimal place.
    static Scanner input=new Scanner(System.in).useDelimiter("\r\n");
    static double[] principle = new double [10];
    static double[] rate      = new double [10];
    static double[] interest  = new double [10];
    static double[] days      = new double [10];
    static int i;
    static int choice;
    
    public Interest() {
    }
    
    
    public static void main(String[] args) {
    	
    	
     do
{
        System.out.println(" Interest Calculation ");
        System.out.println(" ---------------------");
        System.out.println("1. Calculate interest payable");
        System.out.println("2. Calculate principle required to earn $x of interest");
        System.out.println("3. Calculate time required to earn $x of interest");
        System.out.println("4. Exit program");
        System.out.println(" ");
        System.out.print("Please select your choice (1-4):"); 
        choice = input.nextInt();
        System.out.println("---------------------------------");
   	switch(choice)		
	
{
	case 1 ://Caluclate interest
				 
	System.out.print("Enter principle amount in $ :");
	principle[i]=input.nextInt();
	System.out.print("Enter interest rate in % :");
	rate[i]=input.nextDouble();
	System.out.print("Enter period(time) in days :");
	days[i]=input.nextInt();
	interest[i] = principle[i] * ((rate[i]/100)*(days[i]/365));
            System.out.println("Interest earned for principle of $" + principle[i]+" at interest rate of " +rate[i]+ "% is $" + interest[i]);	
	System.out.println(" ");	   
	break;	  										
								
	case 2 :  // Calculate principle		    
	System.out.print("Enter interest amount to be earned in $ :");		interest[i]=input.nextDouble();
  	System.out.print("Enter interest rate in % :");
	rate[i]=input.nextInt();		    	
	System.out.print("Enter period(time) in days :");
	days[i]=input.nextInt();
	principle[i] = interest[i] / (days[i]/365/rate[i]/100) ;	     
	System.out.println("To earn an interest of " +interest[i]+ ", over a period of " +days[i]+ "days, you need a principle of " +principle[i]);
	System.out.println(" ");		 
	  break;
	      		 	    
				
	case 3 :  // Calculate days	  
	System.out.print("Enter interest amount to be earned in $:");
            interest[i]=input.nextDouble();
	System.out.print("Enter interest rate in % :");
	rate[i]=input.nextDouble();
	System.out.print("Enter principle amount in $:");
	principle[i]=input.nextInt();
	days[i] = interest[i] / ( principle[i] * rate[i]/100 ); 
	System.out.println("To earn an interest of $" +interest[i]+ ", with a principle of $"+principle[i]+ " time required is " +days[i]+ " days");
	System.out.println(" ");
	break;
		       
		       
	  case 4 : //show entries displayed
	  System.out.println("Thank you for using Interest Calculator"); 
             System.out.println(" ");
             break;

			     	       
	default : System.out.println("Please enter 1, 2 or 3");
	System.out.println(" ");
		    
            }//end switch
             }while (choice!=4);
              }   
    	       }

The question is required to use arrays to display what the user has entered when they select choice 4. I'm also pulling my hair out figuring how to use method call in my code. Any help would be greatly appreciated.
EDIT: the current code is working, just don't know how to use array. Also, im using jcreator

Recommended Answers

All 4 Replies

"The question is required to use arrays to display what the user has entered when they select choice 4 "
@above
Do you want to display all the previously entered choices by a user when he finally presses 4.?

sorry for the late reply, my computer went bust few days ago.
@adarshcu, yes, it is required to displayed what the user has previously entered (principle, days, rate, interest and total), after the user choose option 4 before exiting the program

I don't think it'l be wise to display all the previously selected values. What can be done is to display the last chosen option and the values. Since you are implementing a do-while loop, the number of times the loop is run is left to the user , he may run the loop ten times or twenty times, which is known only at runtime. One way to do this is to encapsulate all the values into an object and create an array of objects . My real point is why would you want to display all the values that the user has entered till now .? Is it really required ?

right, i just asked my teacher about this and he says at least 10 of the previous entries must be shown..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.