import java.io.*;

/**
 * Tranditional Hebrew number formatting 
 *
 *
 * @author Judah Wischnitzer
 */

public class HebrewNumberFormat{

    public static String format(int num) throws NumberFormatException{
        StringBuffer formatted = new StringBuffer();
        int thousands = 0;

        //thousands
        while (num > 1000){
            thousands++;
            num -= 1000;
        }
        if(thousands > 0){
            formatted.append(formatDigit(thousands)).append("'");
        }

        //four hundreds
        while (num > 400) {
            num -= 400;
            formatted.append('\u05ea');
        }

        //hundreds
        for(int comp=300; comp >= 100; comp-=100){
            if(num == 270 || num == 272 || num == 275){
              formatted.append(formatExceptions(num));
              num = 0;
              break;
            }
            if(num >= comp){
              formatted.append(formatDigit(comp));
              num -= comp;
            }
        }

        //tens
        for(int comp=90; comp >= 10; comp-=10){
            //if 15 or 16
            if (num >= 15 && num <= 16){
              formatted.append(formatDigit(num));
              num = 0;
              break;
            }
            if(num >= comp){
              formatted.append(formatDigit(comp));
              num -= comp;
            }
/*            if(num == comp){
              formatted.append(formatFinal(comp));
              num -= comp;
              break;
            }*/
        }

        //below 20
        for(int comp=9; comp > 0; comp-=1){
            if(num >= comp){
              formatted.append(formatDigit(comp));
              num -= comp;
            }
        }

        return formatted.toString();
    }


    private static String formatExceptions(int digit){
        switch (digit){
            case 270:
                return "\u05e2\u05e8"; //reish ayin
            case 272:
                return "\u05e2\u05e8\u05d1"; //reish ayin bais
            case 275:
                return "\u05e2\u05e8\u05d4"; //reish ayin hay
	    case 344:
		return "\u05e9\u05de\u05d3"; //shin mem daled
            default:
                return formatDigit(digit);
        }
    }

    private static String formatDigit(int digit){
        switch (digit){
            case 1:
                return "\u05d0"; //alef
            case 2:
                return "\u05d1"; //bais
            case 3:
                return "\u05d2"; //gimel
            case 4:
                return "\u05d3"; //daled
            case 5:
                return "\u05d4"; //hay
            case 6:
                return "\u05d5"; //vav
            case 7:
                return "\u05d6"; //zayin
            case 8:
                return "\u05d7"; //ches
            case 9:
                return "\u05d8"; //tes
            case 10:
                return "\u05d9"; //yud
            case 15:
                return "\u05d8\u05d5"; //tes-vav
            case 16:
                return "\u05d8\u05d6"; //tes-zayin
            case 20:
                return "\u05db"; //chaf
            case 30:
                return "\u05dc"; //lamed
            case 40:
                return "\u05de"; //mem
            case 50:
                return "\u05e0"; //nun
            case 60:
                return "\u05e1"; //samech
            case 70:
                return "\u05e2"; //ayin
            case 80:
                return "\u05e4"; //pay
            case 90:
                return "\u05e6"; //tzadi
            case 100:
                return "\u05e7"; //koof
            case 200:
                return "\u05e8"; //reish
            case 300:
                return "\u05e9"; //shin
            case 400:
                return "\u05ea"; //tav
            default:
                throw new NumberFormatException ("Invalid number in sequence.");
        }
    }

    
/*    public static void main(String [] args){
        try{
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("NumFormatTest.txt"), "UTF-8"));
            for(int i=1; i<2005; i+=1){
                bw.write(HebrewNumberFormat.format(i));
                bw.write("\n");
            }
            bw.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
*/
}