import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * Asks the user for their name and year of birth,
 * and then prints their name and age to the screen.
 * 
 * Is safe in that it does not crash if the user enters
 * a String instead of a number.  Also demonstrates defining
 * and calling static methods.
 *
 * @author Zach Tomaszewski
 */
public class SafeUserAge {

  public static void main(String[] args) {
    try {
      Scanner keybd = new Scanner(System.in);

      //get user's name
      System.out.print("Enter your name: ");
      String name = keybd.nextLine();

      //get user's year of birth
      System.out.print("Enter your year of birth: ");
      int yob = keybd.nextInt();

      //print user's details
      System.out.println();
      SafeUserAge.printNameMesg(name);
      System.out.println("You will turn " + SafeUserAge.getAge(yob) + 
                         " sometime during this year.");
      
    }catch (InputMismatchException ime) {
      System.out.println("You must enter an integer for your year of birth.");
    }
  }
  
  
  /**
   * Prints "Your name is [...]", where [...] is the values of the passed
   * parameter.
   */
  public static void printNameMesg(String n) {
    System.out.print("Your name is ");
    System.out.print(n);
    System.out.println(".");
  }
  
  /**
   * Returns an age given a person's year of birth.
   * Simply subtracts year from the current year, so not exact.
   */
  public static int getAge(int yearOfBirth) {
    java.util.Calendar cal = java.util.Calendar.getInstance();
    int currentYear = cal.get(java.util.Calendar.YEAR);
    return currentYear - yearOfBirth;
  }  
}
