I am new to Java and wrote one small program. It works fine, but it looks like it doesn't meet OOP concepts. Could someone look and give me advice so that I can fine tune?
public class App {
public static void main(String[] arg) {
String str = new String(
"]d010036195815011121123456789]d17YYMMDD1012345678901");
String matchItemAI = new String("01"); //GTIN-14
String matchSNoAI = new String("21"); //Serial Number
String matchExpDtAI = new String("]d17");// Expiry Date
String matchLotNoAI = new String("10"); //Lot Number
//Company Prefix
String matchCompPrefixUS = new String("03"); //US Company Prefix
String matchCompPrefixCork = new String("03"); //US Company Cork
String matchCompPrefixSKorea = new String("03"); //US Company South Korea
String value = str;
String value1 = new String();
String value2 = new String();
String value3 = new String();
char ch;
int pos;
// 1. Need to print ]d0100361958150111 like that 61958-1501-1
// 2. Need to print 21123456789 like that 123456789
// 3. Need to print ]d17YYMMDD like that YYMMDD
// 4. Need to print 1012345678901 like that 12345678901
// GS1 Start with this String....It's a GS1 2d Bar Code, Confirmed Then
// Process the record
if (str.startsWith("]d")) {
System.out.println("GS1 2D Input String :" + str);
ch = str.charAt(2);
switch (ch) {
case '0':
System.out
.println("Calculating Unit of Sale (0) Packaging Indicator digits for GTIN 14's : "
+ str.charAt(2));
pos = str.indexOf(matchItemAI);
System.out.println("GS1 pos:" + value.substring(pos)+" POS Value " +pos);
value = value.substring(pos + 5, value.length());
value1 = value.substring(0, 5);
value2 = value.substring(value1.length(), value1.length() + 4);
value3 = value.substring(value1.length() + value2.length(),
value1.length() + value2.length() + 1);
value3 = value1 + "-" + value2 + "-" + value3;
value = value.trim();
System.out.println("Found string Item : " + value3);
// /GET Serial Number
pos = str.indexOf(matchSNoAI); // AI 21
// System.out.println("Found string SNo : " + pos);
value = str.substring(pos + 2, str.lastIndexOf(matchExpDtAI)); //pos + 2 + 9);
value = value.trim();
System.out.println("Found string SNo : " + value);
pos = str.lastIndexOf(matchExpDtAI);
// System.out.println("Found string Expiry Date " + pos);
value = str.substring(pos + 4, pos + 4 + 6);
value = value.trim();
System.out.println("Found string Expiry Date : " + value);
pos = str.lastIndexOf(matchLotNoAI);
// System.out.println("Found string Lot Number " + pos);
value = str.substring(pos + 2, pos + 2 + 11);
value = value.trim();
System.out.println("Found string Lot Number : " + value);
break;
case '3':
System.out
.println("Calculating Bundle/Multipack 3 Packaging Indicator digits for GTIN 14's : "
+ str.charAt(2));
break;
case '5':
System.out
.println("Calculating Shipper 5 Packaging Indicator digits for GTIN 14's : "
+ str.charAt(2));
break;
default:
System.out
.println("Error - invalid selection entered! for Multipacking ");
break;
}
}
}
}
Here I made change as per your suggestion, can you please review and let me know any other suggestion ?
public class App {
public static void main(String[] arg) {
String str = new String(
"]d010036195815011121123456789]d17YYMMDD1012345678901");
String matchItemAI = "01"; //GTIN-14
String matchSNoAI = "21"; //Serial Number
String matchExpDtAI = "]d17";// ExpiryDate
String matchLotNoAI = "10"; //Lot Number
String WitoutFNC1 = str;
String NDCCode = "";
String itemRef = "";
String itemNo = "";
int pos;
// GS1 Start with this String....It's a GS1 2d Bar Code, Confirmed
if (str.startsWith("]d")) {
System.out.println("GS1 2D Input String :" + str);
switch (str.charAt(2)) {
case '0':
pos = str.indexOf(matchItemAI);
System.out.println("GS1 pos:" + WitoutFNC1.substring(pos)+" POS Value " +pos);
WitoutFNC1 = WitoutFNC1.substring(pos+5, WitoutFNC1.length());
NDCCode = WitoutFNC1.substring(0, 5);
itemRef = WitoutFNC1.substring(NDCCode.length(), NDCCode.length() + 4);
itemNo = WitoutFNC1.substring(NDCCode.length() + itemRef.length(),NDCCode.length() + itemRef.length() + 1);
itemNo = String.format("%s-%s-%s",NDCCode, itemRef ,itemNo);
WitoutFNC1 = WitoutFNC1.trim();
System.out.println("Found string Item : " + itemNo);
// /GET Serial Number
pos = str.indexOf(matchSNoAI); // AI 21
// System.out.println("Found string SNo : " + pos);
WitoutFNC1 = str.substring(pos + 2, str.lastIndexOf(matchExpDtAI)); //pos + 2 + 9);
WitoutFNC1 = WitoutFNC1.trim();
System.out.println("Found string SNo : " + WitoutFNC1);
pos = str.lastIndexOf(matchExpDtAI);
// System.out.println("Found string Expiry Date " + pos);
WitoutFNC1 = str.substring(pos + 4, pos + 4 + 6);
WitoutFNC1 = WitoutFNC1.trim();
System.out.println("Found string Expiry Date : " + WitoutFNC1);
pos = str.lastIndexOf(matchLotNoAI);
// System.out.println("Found string Lot Number " + pos);
WitoutFNC1 = str.substring(pos + 2, pos + 2 + 11);
WitoutFNC1 = WitoutFNC1.trim();
System.out.println("Found string Lot Number : " + WitoutFNC1);
break;
case '3':
System.out
.println("Calculating Bundle/Multipack 3 Packaging Indicator digits for GTIN 14's : "
+ str.charAt(2));
break;
case '5':
System.out
.println("Calculating Shipper 5 Packaging Indicator digits for GTIN 14's : "
+ str.charAt(2));
break;
default:
System.out
.println("Error - invalid selection entered! for Multipacking ");
break;
}
}
}
}