/* Import the Java I/O package */
import java.io.*;
import AppletConsoleApp;

/**
 * Solution to the 1997 British Informatics Olympiad exam
 * question 1: translate time on the 12-hour clock to words.
 *
 * This application that can be run either from the console
 * as a stand-alone application or from an in-applet
 * console created with the AppletConsole applet.
 *
 * Solution copyright (c) 1997 The British Informatics Olympiad (BIO).
 *
 * This program may be freely copied by persons or organisations
 * involved in the British Informatics Olympiad or the International
 * Olympiad in Informatics, on condition that no changes are made and
 * this notice is not altered. Distribution for profit is forbidden
 * unless permission is first obtained in writing from the BIO.
 *
 * This program is for educational purposes only and comes with no
 * warranty, implied or otherwise, as to its fitness for any purpose.
 *
 * Author:   Antony Rix
 * Internet: http://www.christs.cam.ac.uk/bio/
 * E-mail:   a.rix@lineone.net
 * S-mail:   The British Informatics Olympiad
 *           Christ's College
 *           Cambridge CB2 3BU
 *           United Kingdom
 *
 * @author  Antony Rix
 * @version 0.1
 * @see     AppletConsoleApp
 * @see     AppletConsole
 */
class BIO97R1Q1App extends AppletConsoleApp {
  /**
   * Start the application from the command line.
   */
  public static void main(String[] args) {
	  BIO97R1Q1App thisApp = new BIO97R1Q1App();
	  thisApp.redirectStreams(System.in, System.out);
	  thisApp.run();
	}

  /**
   * The numbers from 0 to 29.
   */
  static String[] numtoword = {
    "zero", "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten", "eleven", "twelve",
    "thirteen", "fourteen", "fifteen", "sixteen",
    "seventeen", "eighteen", "nineteen", "twenty",
    "twenty-one", "twenty-two", "twenty-three",
    "twenty-four", "twenty-five", "twenty-six",
    "twenty-seven", "twenty-eight", "twenty-nine" };

  /**
   * The implementation of this application.
   * run() gets the time in numbers, translates it using
   * toTime(), and prints it.
   */
	public void run() {
    int hours, minutes, status;
    String theTime;

    out.println(
      "Enter the time in numbers (hours then " );
    out.println(
      "minutes) on the 12-hour clock" );
    out.println(
      "(e.g. 12 20) then press enter." );
    out.print( ">" );
    
    try {
  	  /* Create a StreamTokenizer to allow us to read in the two numbers
	    */
  	  StreamTokenizer sin = new StreamTokenizer( in );
	    sin.nextToken();
	    hours = (int)sin.nval;
  	  sin.nextToken();
	    minutes = (int)sin.nval;
	    out.println(
	      "The time is " + convertTimeToWord( hours, minutes ) + "." );
  	} catch (IOException e) { out.println("I/O failure"); };
	  out.println( "Program finished." );
	}

  /**
   * Finds the times with the longest representations in words.
   */
	public void run2() {
    int hours, minutes, lgth, longest;
    String theTime;

    /* Find the longest length */
    longest = 0;
    for( hours = 1; hours <= 12; hours++ )
      for( minutes = 0; minutes < 60; minutes++ ) {
        lgth = convertTimeToWord( hours, minutes ).length();
        if( lgth > longest )
          longest = lgth;
      }

    out.println( "Times with the longest representations in words:" );
    /* Display times with the longest length */
    for( hours = 1; hours <= 12; hours++ )
      for( minutes = 0; minutes < 60; minutes++ ) {
        theTime = convertTimeToWord( hours, minutes );
        lgth = theTime.length();
        if( lgth == longest ) {
          out.println( Integer.toString( hours ) + ":" +
            Integer.toString( minutes ) + " - '" + theTime + "'." );
        }
      }
  }
  
  /**
   * Translates time specified in numbers to words.
   */
	String convertTimeToWord( int hours, int minutes ) {
	  String S;
    /* check for special cases */
    if( minutes > 30 ) {
     hours = hours + 1;
     if( hours == 13 ) hours = 1;
    }
    switch ( minutes ) {
     case 0: S = numtoword[hours] + " o'clock"; break;
     case 1: S = "one minute past " + numtoword[hours]; break;
     case 15: S = "quarter past " + numtoword[hours]; break;
     case 30: S = "half past " + numtoword[hours]; break;
     case 45: S = "quarter to " + numtoword[hours]; break;
     case 59: S = "one minute to " + numtoword[hours]; break;
     default:
      if( minutes < 30 )
       S = numtoword[minutes]
         + " minutes past " + numtoword[hours];
      else
       S = numtoword[60 - minutes]
         + " minutes to " + numtoword[hours];
    }
    return S;
	}
}
