Source of PensionInClass.java


  1: import java.util.Scanner;

  3: /**
  4:  * This is the version of Pension that I did in class on January 27th. It asks
  5:  * every question before checking if the user is eligible for the pension. In
  6:  * this program, the purpose of the boolean variables is just to simplify the
  7:  * condition in the if-else control at the end.
  8:  *
  9:  * The other program is more sophisticated, in that it doesn't bother even 
 10:  * asking questions where the answer doesn't matter. Why ask seniors (or 
 11:  * children) the yes/no questions when we already know that they ar (not)
 12:  * eligible?
 13:  *
 14:  * @author Mark Young (A00000000)
 15:  */
 16: public class PensionInClass {

 18:     /**
 19:      * @param args the command line arguments
 20:      */
 21:     public static void main(String[] args) {
 22:         Scanner kb = new Scanner(System.in);
 23:         int age;
 24:         String answerToDisabled, answerToAlone;
 25:         boolean isASenior, isPreSenior, disabled, alone;

 27:         System.out.println("This program determines "
 28:                 + "whether you're eligible for a pension.");
 29:         System.out.println("Please answer the following questions.");
 30:         System.out.print("-- How old are you? ");
 31:         age = kb.nextInt();
 32:         kb.nextLine();
 33:         System.out.print("-- Do you have a disability? ");
 34:         answerToDisabled = kb.nextLine().toLowerCase();
 35:         System.out.print("-- Do you live alone? ");
 36:         answerToAlone = kb.nextLine().toLowerCase();

 38:         // if you're 65 or older -- you are eligible
 39:         // if you're 55 to 64 AND you have a disability AND you live alone -- 
 40:         // yes, you are eligible
 41:         // otherwise NOT!
 42:         // DRY programming Don't Repeat Yourself
 43:         isASenior = age >= 65;
 44:         isPreSenior = age >= 55 && !isASenior;
 45:         disabled = answerToDisabled.startsWith("yes");
 46:         alone = answerToAlone.startsWith("yes");
 47:         
 48:         if (isASenior || (isPreSenior && disabled && alone)) {
 49:             System.out.println("You are eligible.");
 50:             System.out.println("Please go to https://gov.ca/NoSuchPension.html"
 51:                     + " for more information.");
 52:         } else {
 53:             System.out.println("NOT eligible.");
 54:         }
 55:     }

 57: }