//
// This program demonstrates procedural abstraction.
//
// The large main program has been divided up into smaller, easy to understand
// methods.   The main program itself is just a sequence of method calls that 
// are clear and easy to understand.
//
import java.util.*;
public class ProceduralAbstractionComplete
{ 
  public static Scanner input = new Scanner(System.in);
  
  // Precondition:  None
  // Postcondition: returns 1 if user wants to enter months
  //                returns 2 if user wants to enter days
  public static int getChoice()
  {
    int result;
    
    do 
    {
      System.out.println("Age in (1) months or (2) days: ");
      result = input.nextInt(); 
    } while ((result != 1) && (result != 2));
    
    return result;
  }
  
  // Precondition:  None
  // Postcondition: Prompts user for number of months then computes # of years
  //                based on # of months input.  Returns this computation.
  public static double computeYearsFromMonths()
  {
    int age;
    double result;
    
    System.out.println("Enter months: ");
    age = input.nextInt();
    
    result = (age / 12.0);
    
    return result;
  }
  
  // Precondition:  None
  // Postcondition: Prompts user for number of days then computes # of years
  //                based on # of days input.  Returns this computation.
  public static double computeYearsFromDays()
  {
    int age;
    double result;
    
    System.out.println("Enter days: ");
    age = input.nextInt();
    
    result = (age / 365.0);
    
    return result;
  }
  
  
  // Precondition:  years - the number of years
  // Postcondition: Outputs years rounded UP to the nearest whole number
  public static void outputResults(double years)
  {
    System.out.println("Age in years is: " + Math.ceil(years));
  }
  
  
  // Precondition:  None
  // Postcondition: Clears the screen
  public static void clearScreen()
  {
    for (int i=0; i<24; i++) 
    {
      System.out.println();
    }   
  }
  
  // Precondition:  none
  // Postcondition: returns true if user wants to run program again  
  public static boolean askUserToRunAgain()
  {
    boolean result;
    int yORn;
    
    // Should we continue?
    System.out.println("Continue? (1=y/0=n): ");
    yORn = input.nextInt();
    
    result = (yORn==1);
    
    return result;
  }
  
  public static void main(String[] args)
  {
    int yORn;
    
    do
    { 
      double years=0;
      
      clearScreen();
      
      if (getChoice() == 1) 
      { 
        years = computeYearsFromMonths();
      }
      else 
      {
        years = computeYearsFromDays();
      }
      
      outputResults(years);
      
    } while (askUserToRunAgain());
    
  }
}