
import java.util.Scanner;
import java.util.ArrayList;

public class Number
{
	public static void main( String args[] )
	{
		Scanner scanner = new Scanner(System.in);

		while(true) {
			System.out.println("\nMenu Options\n");
			System.out.println("(1) Number Reader");
			System.out.println("(2) Roman Numeral Converter");
			System.out.println("(3) Quit");
			System.out.print("\nYour Selection: ");

			int choice = scanner.nextInt();

			if( choice == 1 ) {
				System.out.print("Enter an integer value: ");
				int input = scanner.nextInt();
				System.out.println( numReader(input) );
			}
			else if( choice == 2 ) {
				System.out.print("Enter an integer less than 4000: ");
				int input = scanner.nextInt();

				if( input > 0 && input < 4000 )
					System.out.println( romanNumeral(input) );
				else
					System.out.println("Invalid input.");
			}
			else if( choice == 3 ) {	// quit
				System.out.println("Bye bye.");
				break;
			}
			else
				System.out.println("Invalid choice");
		}
	}

	public static String numReader( int n )
	{
		// initialize the arraylist
		ArrayList<String> read = new ArrayList<String>();
		read.add("zero");
		read.add("one");
		read.add("two");
		read.add("three");
		read.add("four");
		read.add("five");
		read.add("six");
		read.add("seven");
		read.add("eight");
		read.add("nine");

		String out = "";	// output String

		// Determine # of digits in 'n'
		// 'e' will equal to 10^(number of digits - 1) afterward
		int e = 1;
		for( int tmp = n; (tmp/=10) != 0; e *= 10 );

		for( ; e > 0; e /= 10 )
		{	out +=  read.get(n/e) + " ";
			n %= e;
		}

		return out;
	}

	public static String romanNumeral( int n )
	{
		// initialize the arraylist
		ArrayList<RomanNumeralSymbol> list = new ArrayList<RomanNumeralSymbol>();
		list.add( new RomanNumeralSymbol( 'M', 1000 ));
		list.add( new RomanNumeralSymbol( 'D', 500 ));
		list.add( new RomanNumeralSymbol( 'C', 100 ));
		list.add( new RomanNumeralSymbol( 'L', 50 ));
		list.add( new RomanNumeralSymbol( 'X', 10 ));
		list.add( new RomanNumeralSymbol( 'V', 5 ));
		list.add( new RomanNumeralSymbol( 'I', 1 ));

		String out = "";	// output String

		for( int i = 0; i < list.size(); i+=2 )
		{
			// Store the leftmost digit of 'n' into 'digit'
			// then remove that digit from 'n'
			//
			int digit = n/list.get(i).getValue();
			n %= list.get(i).getValue();

			// If it is 4 or 9, add the corresponding 2 symbols
			if( digit == 4 || digit == 9 )
			{	out += list.get(i).toString();
				out += list.get(i-(digit/4)).toString();
				continue;
			}

			// If not 4 or 9 but larger than 5,
			// add the corresponding 5's symbol
			if( digit >= 5 && i > 0 )
			{	out += list.get(i-1).toString();
				digit -= 5;
			}

			// Then add one or more 1's symbol
			for( int j = 0; j < digit; j++ )
				out += list.get(i).toString();
		}

		return out;
	}

}

class RomanNumeralSymbol 
{
	private char symbol;
	private int value;

	public RomanNumeralSymbol()
	{
		symbol = 'I';
		value = 1;
	}

	public RomanNumeralSymbol( char symbol, int value )
	{
		this.symbol = symbol;
		this.value = value;
	}

	public char getSymbol()
	{
		return symbol;
	}

	public int getValue()
	{
		return value;
	}

	public String toString()
	{
		return "" + symbol;
	}
}

