/* * @topic T10017 Date to words demo * @brief Date2TextConverter utility class */ package a1_demo; public class Date2TextConverter { //--------------------------------------- // data //--------------------------------------- int month = 0; int day = 0; int year = 0; //--------------------------------------- // constructors //--------------------------------------- public Date2TextConverter(int month, int day, int year ) { this.month = month; this.day = day; this.year = year; }//Date2TextConverter //--------------------------------------- // operations //--------------------------------------- public String month2text() { switch( month ) { case 1: return "Jan"; case 2: return "Feb"; //... default: return "Invalid Month"; } }//month2text public String day2text() { return Integer.toString( day ); }//day2text public String year2text() { int century = year / 100; int yearInCentury = year % 100; String centuryText = number2words( century ); String yearText = number2words( yearInCentury ); if ( yearInCentury < 10 ) { return centuryText + " zero " + yearText; } else { return centuryText + " " + yearText; } }//year2text private String number2words( int number ) { // convert to text a number 0 through 99 if ( number < 20 ) { // 0, 1, 2, 3, ..., 19 return zero2nineteen[ number ]; } else if ( number % 10 == 0 ) { // 20, 30, 40, ... 90 return decades[ number / 10 - 2 ]; } else { // 21, 22, 23, 24, ... 29, 31, 32, ..., 99 return decades[ number / 10 - 2 ] + " " + zero2nineteen[ number % 10 ]; } } //number2words //--------------------------------------- // static vocabulary //--------------------------------------- private static final String[] zero2nineteen = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fouteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; private static final String[] decades = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; }//class Date2TextConverter