February 4, 2014

التفقيط بلغة جافا Translating Numbers to Arabic Words



I have been assigned to a task about Translating Numbers to Arabic Words in Arabic called "التفقيط".

The assignment was to build a simple Java class that contains the business logic.

I have finished it and hope it will be useful for all.

The Code:

package tafqeet;

public class TafqeetMain {

    public TafqeetMain() {
    }

    final private static String[] units = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
            "Nineteen" };
    final private static String[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty",
            "Ninety" };

    private static String convert(Integer number) {
        if (number < 20) {
            return units[number];
        }
        if (number < 100) {
            return tens[number / 10] + ((number % 10 > 0) ? " " + convert(number % 10) : "");
        }
        if (number < 1000) {
            return units[number / 100] + " Hundred" + ((number % 100 > 0) ? " and " + convert(number % 100) : "");
        }
        if (number < 1000000) {
            return convert(number / 1000) + " Thousand " + ((number % 1000 > 0) ? " " + convert(number % 1000) : "");
        }
        return convert(number / 1000000) + " Million "
                + ((number % 1000000 > 0) ? " " + convert(number % 1000000) : "");
    }

    public static String convertNumberToEnglishWords(String number) throws NumberFormatException {
        Integer i = Integer.parseInt(number);
        return convert(i);

    }

    public static String convertNumberToArabicWords(String number) throws NumberFormatException {

        // check if the input string is number or not
        Double.parseDouble(number);

        // check if its floating point number or not
        if (number.contains(".")) { // yes
            // the number
            String theNumber = number.substring(0, number.indexOf('.'));
            // the floating point number
            String theFloat = number.substring(number.indexOf('.') + 1);
            // check how many digits in the number 1:x 2:xx 3:xxx 4:xxxx 5:xxxxx
            // 6:xxxxxx
            switch (theNumber.length()) {
            case 1:
                return convertOneDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 2:
                return convertTwoDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 3:
                return convertThreeDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 4:
                return convertFourDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 5:
                return convertFiveDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 6:
                return convertSixDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            default:
                return "";
            }
        }

        else {
            switch (number.length()) {
            case 1:
                return convertOneDigits(number);
            case 2:
                return convertTwoDigits(number);
            case 3:
                return convertThreeDigits(number);
            case 4:
                return convertFourDigits(number);
            case 5:
                return convertFiveDigits(number);
            case 6:
                return convertSixDigits(number);
            default:
                return "";
            }

        }
    }

    // -------------------------------------------

    private static String convertOneDigits(String oneDigit) {
        switch (Integer.parseInt(oneDigit)) {
        case 1:
            return "واحد";
        case 2:
            return "إثنان";
        case 3:
            return "ثلاثه";
        case 4:
            return "أربعه";
        case 5:
            return "خمسه";
        case 6:
            return "سته";
        case 7:
            return "سبعه";
        case 8:
            return "ثمانيه";
        case 9:
            return "تسعه";
        default:
            return "";
        }
    }

    private static String convertTwoDigits(String twoDigits) {
        String returnAlpha = "00";
        // check if the first digit is 0 like 0x
        if (twoDigits.charAt(0) == '0' && twoDigits.charAt(1) != '0') { // yes
            // convert two digits to one
            return convertOneDigits(String.valueOf(twoDigits.charAt(1)));
        } else { // no
            // check the first digit 1x 2x 3x 4x 5x 6x 7x 8x 9x
            switch (getIntVal(twoDigits.charAt(0))) {
            case 1: { // 1x
                if (getIntVal(twoDigits.charAt(1)) == 1) {
                    return "إحدى عشر";
                }
                if (getIntVal(twoDigits.charAt(1)) == 2) {
                    return "إثنى عشر";
                } else {
                    return convertOneDigits(String.valueOf(twoDigits.charAt(1))) + " " + "عشر";
                }
            }
            case 2: // 2x x:not 0
                returnAlpha = "عشرون";
                break;
            case 3: // 3x x:not 0
                returnAlpha = "ثلاثون";
                break;
            case 4: // 4x x:not 0
                returnAlpha = "أريعون";
                break;
            case 5: // 5x x:not 0
                returnAlpha = "خمسون";
                break;
            case 6: // 6x x:not 0
                returnAlpha = "ستون";
                break;
            case 7: // 7x x:not 0
                returnAlpha = "سبعون";
                break;
            case 8: // 8x x:not 0
                returnAlpha = "ثمانون";
                break;
            case 9: // 9x x:not 0
                returnAlpha = "تسعون";
                break;
            default:
                returnAlpha = "";
                break;
            }
        }

        // 20 - 99
        // x0 x:not 0,1
        if (convertOneDigits(String.valueOf(twoDigits.charAt(1))).length() == 0) {
            return returnAlpha;
        } else { // xx x:not 0
            return convertOneDigits(String.valueOf(twoDigits.charAt(1))) + " و " + returnAlpha;
        }
    }

    private static String convertThreeDigits(String threeDigits) {

        // check the first digit x00
        switch (getIntVal(threeDigits.charAt(0))) {

        case 1: { // 100 - 199
            if (getIntVal(threeDigits.charAt(1)) == 0) { // 10x
                if (getIntVal(threeDigits.charAt(2)) == 0) { // 100
                    return "مائه";
                } else { // 10x x: is not 0
                    return "مائه" + " و " + convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else {// 1xx x: is not 0
                return "مائه" + " و " + convertTwoDigits(threeDigits.substring(1, 3));
            }
        }
        case 2: { // 200 - 299
            if (getIntVal(threeDigits.charAt(1)) == 0) { // 20x
                if (getIntVal(threeDigits.charAt(2)) == 0) { // 200
                    return "مائتين";
                } else { // 20x x:not 0
                    return "مائتين" + " و " + convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else { // 2xx x:not 0
                return "مائتين" + " و " + convertTwoDigits(threeDigits.substring(1, 3));
            }
        }
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9: { // 300 - 999
            if (getIntVal(threeDigits.charAt(1)) == 0) { // x0x x:not 0
                if (getIntVal(threeDigits.charAt(2)) == 0) { // x00 x:not 0
                    return convertOneDigits(String.valueOf(threeDigits.charAt(1) + "مائه"));
                } else { // x0x x:not 0
                    return convertOneDigits(String.valueOf(threeDigits.charAt(0))) + "مائه" + " و "
                            + convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else { // xxx x:not 0
                return convertOneDigits(String.valueOf(threeDigits.charAt(0))) + "مائه" + " و "
                        + convertTwoDigits(threeDigits.substring(1, 3));
            }
        }

        case 0: { // 000 - 099
            if (threeDigits.charAt(1) == '0') { // 00x
                if (threeDigits.charAt(2) == '0') { // 000
                    return "";
                } else { // 00x x:not 0
                    return convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else { // 0xx x:not 0
                return convertTwoDigits(threeDigits.substring(1, 3));
            }
        }
        default:
            return "";
        }
    }

    private static String convertFourDigits(String fourDigits) {
        // xxxx
        switch (getIntVal(fourDigits.charAt(0))) {

        case 1: { // 1000 - 1999
            if (getIntVal(fourDigits.charAt(1)) == 0) { // 10xx x:not 0
                if (getIntVal(fourDigits.charAt(2)) == 0) { // 100x x:not 0
                    if (getIntVal(fourDigits.charAt(3)) == 0) { // 1000
                        return "ألف";
                    } else { // 100x x:not 0
                        return "ألف" + " و " + convertOneDigits(String.valueOf(fourDigits.charAt(3)));
                    }
                } else { // 10xx x:not 0
                    return "ألف" + " و " + convertTwoDigits(fourDigits.substring(2, 3));
                }
            } else { // 1xxx x:not 0
                return "ألف" + " و " + convertThreeDigits(fourDigits.substring(1, 4));
            }
        }
        case 2: { // 2000 - 2999
            if (getIntVal(fourDigits.charAt(1)) == 0) { // 20xx
                if (getIntVal(fourDigits.charAt(2)) == 0) { // 200x
                    if (getIntVal(fourDigits.charAt(3)) == 0) { // 2000
                        return "ألفين";
                    } else { // 200x x:not 0
                        return "ألفين" + " و " + convertOneDigits(String.valueOf(fourDigits.charAt(3)));
                    }
                } else { // 20xx x:not 0
                    return "ألفين" + " و " + convertTwoDigits(fourDigits.substring(2, 3));
                }
            } else { // 2xxx x:not 0
                return "ألفين" + " و " + convertThreeDigits(fourDigits.substring(1, 4));
            }
        }
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9: { // 3000 - 9999
            if (getIntVal(fourDigits.charAt(1)) == 0) { // x0xx x:not 0
                if (getIntVal(fourDigits.charAt(2)) == 0) { // x00x x:not 0
                    if (getIntVal(fourDigits.charAt(3)) == 0) { // x000 x:not 0
                        return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف";
                    } else { // x00x x:not 0
                        return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف" + " و "
                                + convertOneDigits(String.valueOf(fourDigits.charAt(3)));
                    }
                } else { // x0xx x:not 0
                    return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف" + " و "
                            + convertTwoDigits(fourDigits.substring(2, 3));
                }
            } else { // xxxx x:not 0
                return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف" + " و "
                        + convertThreeDigits(fourDigits.substring(1, 4));
            }
        }

        default:
            return "";
        }
    }

    private static String convertFiveDigits(String fiveDigits) {
        if (convertThreeDigits(fiveDigits.substring(2, 5)).length() == 0) { // xx000
                                                                            // x:not
                                                                            // 0
            return convertTwoDigits(fiveDigits.substring(0, 2)) + " ألف ";
        } else { // xxxxx x:not 0
            return convertTwoDigits(fiveDigits.substring(0, 2)) + " ألفا " + " و "
                    + convertThreeDigits(fiveDigits.substring(2, 5));
        }
    }

    private static String convertSixDigits(String sixDigits) {

        if (convertThreeDigits(sixDigits.substring(2, 5)).length() == 0) { // xxx000
                                                                           // x:not
                                                                           // 0
            return convertThreeDigits(sixDigits.substring(0, 3)) + " ألف ";
        } else { // xxxxxx x:not 0
            return convertThreeDigits(sixDigits.substring(0, 3)) + " ألفا " + " و "
                    + convertThreeDigits(sixDigits.substring(3, 6));
        }
    }

    private static int getIntVal(char c) {
        return Integer.parseInt(String.valueOf(c));
    }

    // ----------------------------------------------------------

    public static void main(String[] args) {
        System.out.println(TafqeetMain.convertNumberToEnglishWords("123456"));

        System.out.println(TafqeetMain.convertNumberToArabicWords("123412.11"));
    }
}

December 8, 2011

Best Eclipse Code Formatter


Download the following file: eclipse_formatter.xml

At eclipse go to Window > Preferences, press on import and select eclipse_formatter.xml
 
Select “Perform the selected actions on save” so that nothing is checked in without being formatted:

 
Use Configure to add the “Additional actions” listed above.

August 8, 2011

هل صحيح العقل زينة؟

هل صحيح العقل زينة؟

سئل  أحد الشعراء : هل يمكن أن يكتسب العقل بالتعقل كما يكتسب العلم بالتعلم ؟؟

ما بالتعقل يكتسب العقل ولا باكتساب المال يكتسب الغنى

سئل عمرو بن العاص عما يكون العقل قال : العقل هو الاصابة بالظن ومعرفة ما يكون بما قد كان.

وقال معاويه : العقل مكيال ثلثه فطنه وثلثاه تغافل.

سئل المغيرة بن شعبة عن عمر بن الخطاب فقال : كان أفضل من يخدع وأعقل من أن يخدعه أحد

وقال زياد : ليس العاقل الذى اذا وقع فى الامر احتال للخروج منه لكن العاقل هو من يحتال للامر حتى لا يقع فيه .

فماذا عن اوضح بيان وأرجح برهان يدل على عقل العاقل ؟
انه اللسان .لذا قالوا لسان المرء ترجمان عقله ودليل فضله

وقال الحسن البصرى : لسان العقل وراء قلبه فاذا أراد الكلام تفكر فان كان له قال وان كان عليه سكت وقلب الاحمق وراء لسانه فاذا اراد ان يقول قال فان كان له سكت وان كان عليه قال ، ويقول الرسول عليه الصلاة والسلام " المرء بأصغرية قلبه ولسانه " .

لما أهبط الله آدم الى الارض اتاه جبريل وقال له : يا آدم ان الله قد حباك ثلاث خصال لتختار منها واحدة وتترك اثنتين
قال آدم : وما هى يا جبريل ؟
قال : الحياء والدين والعقل .
قال آدم : اللهم انى اخترت العقل
فقال جبريل للحياء والدين : ارتفعا قالا : لا لن نرتفع
قال لهما جبريل :أعصيتما
قالا : لا ..لكننا أمرنا الا نفارق العقل حيث كان

January 23, 2011

"JDeveloper - 11.1.1.4" java.lang.OutOfMemoryError: PermGen space Error

When I start running an ADF pages from JDeveloper 11g (11.1.1.4), I faced an error in the  PermGen space.
I have search a solution for it, and I found a small argument that can be added to the Weblogic environment setting file "setDomainEnv.sh" in Linux and Mac.

In this file I've add the following tow lines:

USER_MEM_ARGS="-Xms512m -Xmx512m -XX:MaxPermSize=256m"
export USER_MEM_ARGS


And it works fine.

November 24, 2010

KISS: Keep It Simple, Stupid

The Kiss Principle: "Keep It Simple, Stupid"

What does KISS stand for?

The KISS is an abbreviation of Keep It Stupid Simple or Keep It Simple, Stupid

What does that mean?

This principle has been a key, and a huge success in my years of software engineering. A common problem among software engineers and developers today is that they tend to over complicate problems.
Typically when a developer is faced with a problem, they break it down into smaller pieces that they think they understand and then try to implement the solution in code. I would say 8 or 9 out of 10 developers make the mistake that they don't break down the problem into small enough or understandable enough pieces. This results in very complex implementations of even the most simple problems, another side effect is spagetthi code, something we tought only BASIC would do with its goto statements, but in Java this results in classes with 500-1000 lines of code, methods that each have several hundreds of lines.
This code clutter is a result of the developer realizing exception cases to his original solution while he is typing in code. These exception cases would have solved if the developer had broken down the problem further.

How will I benefit from KISS

  • You will be able to solve more problems, faster.
  • You will be able to produce code to solve complex problems in fewer lines of code
  • You will be able to produce higher quality code
  • You will be able to build larger systems, easier to maintain
  • You're code base will be more flexible, easier to extend, modify or refactor when new requirements arrive
  • You will be able to achieve more than you ever imagined
  • You will be able to work in large development groups and large projects since all the code is stupid simple

How can I apply the KISS principle to my work

There are several steps to take, very simple, but could be challenging for some. As easy as it sounds, keeping it simple, is a matter of patience, mostly with yourself.
  • Be Humble, don't think of yourself as a super genius, this is your first mistake
    By being humble, you will eventually achieve super genius status =), and even if you don't, who cares! your code is stupid simple, so you don't have to be a genius to work with it.
  • Break down your tasks into sub tasks that you think should take no longer than 4-12 hours to code
  • Break down your problems into many small problems. Each problem should be able to be solved within one or a very few classes
  • Keep your methods small, each method should never be more than 30-40 lines. Each method should only solve one little problem, not many uses cases
    If you have a lot of conditions in your method, break these out into smaller methods.
    Not only will this be easier to read and maintain, but you will find bugs a lot faster.
    You will learn to love Right Click+Refactor in your editor.
  • Keep your classes small, same methodology applies here as we described for methods.
  • Solve the problem, then code it. Not the other way around
    Many developers solve their problem while they are coding, and there is nothing wrong doing that. As a matter of fact, you can do that and still adhere to the above statement.
    If you have the ability to mentally break down things into very small pieces, then by all means, do that while you are coding. But don't be afraid of refactor your code over and over and over and over again. Its the end result that counts, and number of lines is not a measurement, unless you measure that fewer is better of course.
  • Don't be afraid to throw away code. Refactoring and recoding are two very important areas. As you come across requirements that didn't exist, or you weren't aware of when you wrote the code to begin with you might be able to solve the old and the new problems with an even better solution.
    If you had followed the advice above, the amount of code to rewrite would have been minimal, and if you hadn't followed the advice above, then the code should probably be rewritten anyway.
  • And for all other scenarios, try to keep it as simple as possible, this is the hardest behavior pattern to apply to, but once you have it, you'll look back and will say, I can't imagine how I was doing work before.

Are there any examples of the KISS principle

There are many, and I will look for some really great one to post here. But I will leave you with the following thought:
Some of the world's greatest algorithms are always the ones with the fewest lines of code. And when we go through the lines of code, we can easily understand them. The innovator of that algorithm, broke down the problem until it was so easy to understand that he/she could implement it.
Many great problem solvers were not great coders, but yet they produced great code!

Does KISS only apply to java coding

Absolutely not, it applies to many other programming languages and extends to many other areas in your life.
The areas that the principle doesn't apply to are: emotions, love and most importantly, your marriage :)

November 3, 2010

Dynamic runtime named bind variables

public void search(String param1, String param2) {
    String sql_1 = " AND SOMETHING = :P_PARAM_1)";
    String sql_2 = " AND SOMETHING_ELSE = :P_PARAM_2";

    setWhereClause(null);
    clearWhereState();
    setWhereClause("1=1");

    if (param1 != null && param1.length() > 0) {
        addWhereClause(sql_1);
        defineNamedWhereClauseParam("P_PARAM_1", null, null);
        setNamedWhereClauseParam("P_PARAM_1", param1);
    }

    if (param2 != null && param2.length() > 0) {
        addWhereClause(sql_2);
        defineNamedWhereClauseParam("P_PARAM_2", null, null);
        setNamedWhereClauseParam("P_PARAM_2", param2);
    }

    executeQuery();
}

protected void clearWhereState() {
    ViewDefImpl viewDef = getViewDef();
    Variable[] viewInstanceVars = null;
    VariableManager viewInstanceVarMgr = ensureVariableManager();
    if (viewInstanceVarMgr != null) {
        viewInstanceVars = viewInstanceVarMgr.getVariablesOfKind(Variable.VAR_KIND_WHERE_CLAUSE_PARAM);
        if (viewInstanceVars != null) {
            for (Variable v : viewInstanceVars) {
                // only remove the variable if its not on the view def.
                if (!hasViewDefVariableNamed(v.getName())) {
                    removeNamedWhereClauseParam(v.getName());
                }
            }
        }
    }
    getDefaultRowSet().setExecuteParameters(null, null, true);
    setWhereClause(null);
    getDefaultRowSet().setWhereClauseParams(null);
}

private boolean hasViewDefVariableNamed(String name) {
    boolean ret = false;
    VariableManager viewDefVarMgr = getViewDef().ensureVariableManager();
    if (viewDefVarMgr != null) {
        try {
            ret = viewDefVarMgr.findVariable(name) != null;
        } catch (NoDefException ex) {
            // ignore
        }
    }
    return ret;
}