3
\$\begingroup\$

I need a review on this. This code will read a 2D matrix bar code, which I hard-coded as a string str. It has "]d" starting string, which is a GS1 International body Bar Code spec. after that. It has information about Item Number, Serial Number, Locator Information. This code will read the entire string, split and validate fields as per US, SK or Ireland country specs.

public class App {
    public static int pos;
    public static String getLotNo;
    public static String getExpDt;
    public static String getItemNo;

    public static String getTrimItemNo(String itemNo) {
        return itemNo.trim();
    }

    public static String getItemNumber(String str) {
        String matchItemAI = "01";
        pos = str.indexOf(matchItemAI);

        // Company Prefix for US

        if (str.substring(5, 7).equals("03")) {
            System.out.println("US Company Prefix :");
            String itemStr = str.substring(pos + 5, str.length());
            System.out.println(itemStr);
            String itemNo = String.format("%s-%s-%s", itemStr.substring(0, 5),
                    itemStr.substring(5, 9), itemStr.substring(9, 10));
            return itemNo = itemNo.trim();

        }// For Ireland
        else if (str.substring(5, 7).equals("53")) {
            System.out.println("Cork Company Prefix :");// 539150714
            System.out.println("Cork Company Prefix ----POS:" + pos);
            System.out.println("Cork Company Prefix ----STR:" + str);
            String itemStr = str.substring(pos + 8, str.length());
            System.out.println("Cork Company Prefix ----:" + itemStr);
            String itemNo = String.format("%s-%s-%s", itemStr.substring(0, 5),
                    itemStr.substring(5, 9), itemStr.substring(9, 10));
            return itemNo = itemNo.trim();
        }// For South Korea
        else if (str.substring(5, 7).equals("88")) {
            System.out.println("South Korea Company Prefix :");
            String itemStr = str.substring(pos + 8, str.length());
            System.out.println(itemStr);
            String itemNo = String.format("%s-%s-%s", itemStr.substring(0, 5),
                    itemStr.substring(5, 9), itemStr.substring(9, 10));
            return itemNo = itemNo.trim();
        } else {
            System.out.println("Not Found Company Prefix");
            return null;
        }
    }

    public static String getSNo(String str) {
        String matchSNoAI = "21"; // AI Serial Number 21
        pos = str.indexOf(matchSNoAI);
        String matchExpDtAI = "]d17";// ExpiryDate Group Seprator
        String getSNo = str.substring(pos + 2, str.lastIndexOf(matchExpDtAI));
        return getSNo = getSNo.trim();
    }



    public static String getExpDt(String str, String matchExpDtAI) {
            pos = str.lastIndexOf(matchExpDtAI);
            getExpDt = str.substring(pos + 4, pos + 4 + 6);
            return getExpDt = getExpDt.trim();
        }

        public static String getLotNo(String str) {
            String matchLotNoAI = "10"; // AI Lot Number
            pos = str.lastIndexOf(matchLotNoAI);
            getLotNo = str.substring(pos + 2, pos + 2 + 11);
            return getLotNo = getLotNo.trim();
        }

        public static void main(String[] arg) {

            // US GS1 String
            // 01 AI
    // 0 Packing Indicator
    // 03 GS1 US Code
    // 61958 FDA Code
    // 07011 Assigned Product Code
    // 1 Asigned Packinging Code
    // 9 Check Digit Code
               String str = new String(
             "]d010036195815011121123456789]d17YYMMDD1012345678901");

            // South Korea String


            // String str = new String(
            // "]d010880625915011821123456789]d17YYMMDD1012345678901");

            // Ireland String


            // String str = new String(
            //      "]d010539150714011821123456789]d17YYMMDD1012345678901");

            String withoutFunctionKey = str;

            if (str.startsWith("]d")) {
                System.out.println("GS1 2D Input String :" + str);

                switch (str.charAt(2)) { // Scan UNIT 0

                case '0':

                    // Get Item Number
                    withoutFunctionKey = getItemNumber(str);
                    System.out.println("Found Item Number :    "
                            + withoutFunctionKey);

                    // Get Serial Number
                    String matchExpDtAI = "]d17";// ExpiryDate
                    withoutFunctionKey = getSNo(str);
                    System.out.println("Found string Serial Number :    "
                            + withoutFunctionKey);

                    // Get Expiry Date
                    withoutFunctionKey = getExpDt(str, matchExpDtAI);
                    System.out.println("Found string Expiry Date :   "
                            + withoutFunctionKey);

                    // Get Lot Number
                    withoutFunctionKey = getLotNo(str);
                    System.out.println("Found string Lot Number  : "
                            + withoutFunctionKey);
                    break;
                case '3':
                    // BUNDLE 3
                    System.out
                            .println("Bundle/Multipack 3 Packaging Indicator digits for GTIN 14's  : "
                                    + str.charAt(2));
                    break;
                // SHIPPER 5
                case '5':
                    System.out
                            .println("Shipper 5 Packaging Indicator digits for GTIN 14's  : "
                                    + str.charAt(2));
                    break;
                default:
                    System.out
                            .println("Error - invalid selection entered! for Multipacking ");
                    break;
                }
            }

        }
    }
\$\endgroup\$
1
\$\begingroup\$

Either your interpretation of the specification is incorrect, or I'm looking at the wrong specs. GS1 country codes are three digits, not two. Indeed, you disregard the character after your "two digit" country code, but it's not obvious that you skipped that character since you take some substrings with fixed offsets and some substrings relative to the position of the "01" string. "03" is just one of the many possible strings for the US. "53" could be "539" for Ireland, but also possibly "530" for Albania or "535" for Malta. "88" could be "880" for South Korea, but also possibly "884" for Cambodia, "885" for Thailand, or "888" for Singapore.

In any case, this is a kind of problem where a programmer who is maintaining the code (which could be you yourself in three months!) cannot rely on intuition at all. Therefore, it pays to embed generous amounts of comments, justifying the interpretation of every field by citing the relevant section of the specification document. GS1 publishes its specifications freely online, so there's no excuse for lack of citations, especially if you're asking other people to review your code.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I removed all the comments from my source code before posting it so that i should source code only for review but i am keeping comments in the original code but thanks for good suggestion, GS1 has three digit range but as per GTIN-14,"03" goes for US in GTIN-14 starts with Application Identifier // 01 AI // 0 Packing Indicator // 03 GS1 US Code // XXXXX FDA Code // 15011 Product Code // 1 Asigned Packinging Code Unit,Packet etc // 9 Check Digit Code \$\endgroup\$ – user3246453 Jan 30 '14 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.