Commit de9a21c7 authored by monireh sanaei's avatar monireh sanaei

Added Monireh's examples

parent a3ea8d22
public class DBParserOpenFoodFacts implements DBParser {
public static final String NO_PRODUCTS = "No products";
public static final String CODE = "code";
public static final String TITLE = "title";
public static final String PARAGRAPH = "<p>";
@Override
public Map<String, Product> parseResults(String entry, String productName) {
Map<String, Product> productResults = new HashMap<String, Product>();
if(!entry.contains(NO_PRODUCTS)) {
int nbProductsStart = entry.indexOf(PARAGRAPH) + PARAGRAPH.length();
int nbProductsEnd = entry.indexOf(" ");
String nbProductsString = entry.substring(nbProductsStart, nbProductsEnd);
int nbProducts = 0;
try {
nbProducts = Integer.parseInt(nbProductsString);
} catch(NumberFormatException e){
System.out.println(e.getStackTrace());
}
System.out.println("Number of products : " + nbProducts);
int codeSearchIndex = 0;
int codeStartIndex = 0;
int codeEndIndex = 0;
int titleSearchIndex = 0;
int titleStartIndex = 0;
int titleEndIndex = 0;
int indexOfCode = -1;
int indexOfTitle = -1;
while (indexOfCode != entry.lastIndexOf(CODE) && indexOfTitle != entry.lastIndexOf(TITLE)) {
indexOfCode = entry.indexOf(CODE, codeSearchIndex);
codeStartIndex = indexOfCode + CODE.length() + 1;
codeEndIndex = entry.indexOf("\\", codeStartIndex);
String barcode = entry.substring(codeStartIndex, codeEndIndex);
codeSearchIndex = codeEndIndex + 1;
indexOfTitle = entry.indexOf(TITLE, titleSearchIndex);
titleStartIndex = indexOfTitle + TITLE.length() + 3;
titleEndIndex = entry.indexOf("\\", titleStartIndex);
String title = entry.substring(titleStartIndex, titleEndIndex);
title = title.replace("&nbsp;", " ");
productResults.put(barcode, new Product(barcode, productName, title));
titleSearchIndex = titleEndIndex + 1;
}
}
return productResults;
}
}
\ No newline at end of file
public class CDBParserOpenFoodFacts implements DBParser {
public static final String Const_NO_PRODUCTS = "No products";
public static final String Const_CODE = "code";
public static final String Const_TITLE = "title";
public static final String Const_PARAGRAPH = "<p>";
@Override
public Map<String, Product> parseResults(String inString, String sProductName) {
Map<String, Product> productHashMap = new HashMap<String, Product>();
if(!inString.contains(Const_NO_PRODUCTS)) {
int iNbProductsStart = inString.indexOf(Const_PARAGRAPH) + Const_PARAGRAPH.length();
int iNbProductsEnd = inString.indexOf(" ");
String sNbProductsString = inString.substring(iNbProductsStart, iNbProductsEnd);
int iNbProducts = 0;
try {
iNbProducts = Integer.parseInt(sNbProductsString);
} catch(NumberFormatException e){
System.out.println(e.getStackTrace());
}
System.out.println("Number of products : " + iNbProducts);
int iCodeSearchIndex = 0;
int iCodeStartIndex = 0;
int iCodeEndIndex = 0;
int iTitleSearchIndex = 0;
int iTitleStartIndex = 0;
int iTitleEndIndex = 0;
int indexOfCode = -1;
int indexOfTitle = -1;
while (indexOfCode != inString.lastIndexOf(Const_CODE) && indexOfTitle != inString.lastIndexOf(Const_TITLE)) {
indexOfCode = inString.indexOf(Const_CODE, iCodeSearchIndex);
iCodeStartIndex = indexOfCode + Const_CODE.length() + 1;
iCodeEndIndex = inString.indexOf("\\", iCodeStartIndex);
String sBarcode = inString.substring(iCodeStartIndex, iCodeEndIndex);
iCodeSearchIndex = iCodeEndIndex + 1;
indexOfTitle = inString.indexOf(Const_TITLE, iTitleSearchIndex);
iTitleStartIndex = indexOfTitle + Const_TITLE.length() + 3;
iTitleEndIndex = inString.indexOf("\\", iTitleStartIndex);
String title = inString.substring(iTitleStartIndex, iTitleEndIndex);
title = title.replace("&nbsp;", " ");
productHashMap.put(sBarcode, new Product(sBarcode, sProductName, title));
iTitleSearchIndex = iTitleEndIndex + 1;
}
}
return productHashMap;
}
}
\ No newline at end of file
public class LkpCli {
public static final int PL_SZ = 100;
private final ExecutorService plServ;
private LinkedList<String> prdts;
private Map<String, Product> prdtsBN;
private String grcyPt = "resources//grocery.txt";
private String lkpPt = "resources//lookupData.json";
public LkpCli(int plSz) {
prdts = FileUtility.rdTxtF(grcyPt);
prdtsBN = Collections.synchronizedMap(new HashMap<String, Product>());
plServ = Executors.newFixedThreadPool(plSz);
}
public static void main(String[] args) {
LkpCli concrLkp = new LkpCli(PL_SZ);
while(concrLkp.prdts.size() > 0){
String prdNm = concrLkp.prdts.removeFirst();
CHttpConnection httpConn = new CHttpConnection(prdNm, concrLkp.prdtsBN);
concrLkp.plServ.submit(httpConn);
}
concrLkp.plServ.shutdown();
while (!concrLkp.plServ.isTerminated()) {
}
FileUtility.mp2Json(concrLkp.prdtsBN, concrLkp.lkpPt);
}
}
public class LookupClient {
public static final int POOL_SIZE = 100;
private final ExecutorService poolService;
private LinkedList<String> products;
private Map<String, Product> productsByName;
private String groceryPath = "resources//grocery.txt";
private String lookupPath = "resources//lookupData.json";
public LookupClient(int poolSize) {
products = FileUtility.readTextFile(groceryPath);
productsByName = Collections.synchronizedMap(new HashMap<String, Product>());
poolService = Executors.newFixedThreadPool(poolSize);
}
public static void main(String[] args) {
LookupClient concurrentLookup = new LookupClient(POOL_SIZE);
while(concurrentLookup.products.size() > 0){
String productName = concurrentLookup.products.removeFirst();
HttpConnection httpConnection = new HttpConnection(productName, concurrentLookup.productsByName);
concurrentLookup.poolService.submit(httpConnection);
}
concurrentLookup.poolService.shutdown();
while (!concurrentLookup.poolService.isTerminated()) {
}
FileUtility.mapToJson(concurrentLookup.productsByName, concurrentLookup.lookupPath);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment