CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Help with loops

  1. #1
    Join Date
    Feb 2009
    Posts
    4

    Help with loops

    I am really new to programming and I created this program for my Java Programming class for a life insurance calculator. It works perfectly, it's just not looped and were required to place the appropriate input lines inside a loop so the customer can try variations on certain parameters and get different quotes. Most likely, the customer name will remain static, so it can be outside the loop. We were also instructed to maybe try the JOptionPane with a YES_OPTION Sentinel. Just any help would be appreciated, like where to place this at cause i've been trying to figure it out for a few days. Thanks.

    Code:
    import java.lang.String;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class LifeInsCalc {
      public static void main(String[] args) {
    
    	  // Create a Scanner object
    
    	  Scanner input = new Scanner(System.in);
    
    	  // Prompt the user to enter their name
    
    
    	  System.out.print(
    		  "Welcome to UCC Life Insurance!\n" +
    		  "Note: only those under 80 years of age and non-smokers may apply.\n" +
    		  "Enter your name: ");
    
          String custName = input.next();
    
    	  // Prompt the user to enter their age and declare/initialize the variable 'age'
    
    	  System.out.print("Enter your age:");
    
    	  int age = input.nextInt(); // user the Scanner to get an integer input
    
    	  if(age >=80) {
    
    		  String message = custName +", you must be under the age of 80 to apply.\n" +
    
    		  "Thanks for visiting!";
    
    		  JOptionPane.showMessageDialog(null,
    
    		  message,"Sorry!",JOptionPane.INFORMATION_MESSAGE);
    
    		  System.exit(0);
    	  } // end if (age >=80)
    
    
    	  // Prompt the user to enter tobacco use and declare/initialize the variable 'smoker'
    
    	  System.out.print("Do you use tobacco products (1 = Yes, 0 = No)? ");
    
    	  int smoker = input.nextInt();
    
    	  if(smoker == 1) {
    
    		  String message = custName +", you cannot apply if you use tobacco products.\n" +
    
    		  		"Thanks for visiting!";
    
    		  JOptionPane.showMessageDialog(null,
    
    		  		message,"Sorry!",JOptionPane.INFORMATION_MESSAGE);
    
    		  System.exit(0);
          } // end if
    
          // Prompt the user to enter amount of insurance and declare/initialize the variable 'insAmount'
    
          System.out.print("Enter the amount of insurance you need, \n" +
    
          		"i.e. 20000, 100000, etc. (without commas):");
    
          int insAmount = input.nextInt();
    
          // Prompt the user to enter term of the policy and declare/initialize the variable 'term'
    
          System.out.print("enter the term in 5 or 10 year increments:");
    
          int term = input.nextInt();
    
          // if the term plus age equals or exceeds 80, reduce the term to 80 - age
    
          if((term + age)>=80) {
    
    		  term = 80 - age;
    
    		  String message = "The term plus your age cannot exceed age 80." +
    
    		  "the term will be reduced to " + term;
    
    		  JOptionPane.showMessageDialog(null, message,
    
    		  "Notice",JOptionPane.INFORMATION_MESSAGE);
    
    	  } // end if
    
    	  int rate = 1; // declare and initialize rate which is per 10k of coverage
    
    	  // Determine rate for age with multiple if...else if statements
    	  // i.e. if age is greater than or equal to 0 and age is less than 10 then rate = $1
    
    	  if((age>=0) && (age<10)) {
    
    		  rate = 1;
    
    	  }
    
    	  else if((age >=10) && (age <20)) {
    
    		  rate = 3;
    
    	  }
    
    	  else if((age >=20) && (age <30)) {
    
    		  rate = 5;
    
    	  }
    
    	  else if((age >=30) && (age <40)) {
    
    		  rate = 8;
    
    	  }
    
    	  else if((age >=40) && (age <50)) {
    
    		  rate = 12;
    
    	  }
    
    	  else if((age >=50) && (age <60)) {
    
    		  rate = 15;
    
    	  }
    
    	  else if((age >=60) && (age <70)) {
    
    		  rate = 18;
    
    	  }
    
    	  else if((age >=70) && (age <80)) {
    
    		  rate = 21;
    
    	  }
    
    	  // declare variables monthly payment and total payment for the term
    
    	  int monthlyPayment, totalPayment;
    
    	  // calculate payments
    
    	  monthlyPayment = rate * (insAmount / 10000);
    
    	  totalPayment = monthlyPayment * 12 * term;
    
    
    
    	    String message = custName +", the monthly payment is $" + monthlyPayment +
    
    	     "\nThe total payment over the term is $" + totalPayment;
    
    	    JOptionPane.showMessageDialog(null,message,"Result",
    
    	  		  JOptionPane.INFORMATION_MESSAGE);
    
    	  
    
    
    
      } // end main
    
    } // end class LifeInsCalc()

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with loops

    It works perfectly, it's just not looped and were required to place the appropriate input lines inside a loop so the customer can try variations on certain parameters and get different quotes. Most likely, the customer name will remain static, so it can be outside the loop.
    Presumably their age and whether or not they have smoked will not change during the quotation process either so they can also be outside the loop. So start the loop immediately after this code block.

    You need to display the results of the insurance calculation for each iteration of the loop, so end the loop after this code block. Remember you need a way of exiting the loop eg add a question asking the user if they want to change the quotation parameters.

    You also need to decide on what type of loop to use (for, while, do-while). A do-while loop is probably the most logical as you want the loop to execute at least once.

    To learn how to use a JOptionPane read a tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured