/*
 * Created on Dec 9, 2004
 *
 * Lecture class which contains the main method for examples done
 * in lectures for CS1020-001 Winter 2004-2005. 
 */
package cs1020;

import javax.swing.*;
import java.util.*;

/**
 * @author t a y l o r@msoe.edu
 *
 * Lecture class which contains the main method for examples done
 * in lectures for CS1020-001 Winter 2004-2005. 
 */
public class Lecture {

    /**
     * Exception handling
     * 
     * @author t a y l o r@msoe.edu
     * 
     * @param args Currently ignored
     */
    public static void main(String[] args) {
        GregorianCalendar today = new GregorianCalendar();
        int age;
        age = getAge("How old are you?");
        int thisYear = today.get(Calendar.YEAR);
        int yearBorn = thisYear - age;
        int hadBDay = JOptionPane.showConfirmDialog(null,
                "Already had your birthday this year?",
                "Y/N",
                JOptionPane.YES_NO_OPTION);
        if(hadBDay == JOptionPane.NO_OPTION) {
            --yearBorn;
        }
        JOptionPane.showMessageDialog(null, "You were born in " + yearBorn);
    }   
    
    public static int getAge(String prompt) {
        int age = 4;
        boolean done = false;
        while(!done) {
            String ans = JOptionPane.showInputDialog(null, prompt);
	        try {
	            age = Integer.parseInt(ans);
	            if(age<0) {
	                throw new Exception();
	            }
	            done = true;
	        } catch (NumberFormatException e) {
	            JOptionPane.showMessageDialog(null, "Enter digits!");
	        }
        }
        return age;
    }
}