I have this code to generate some tables in my PDF document for my android app. It works fine, I just think I need to refactor it, as basically is the same structure repeated 3 times.
//tables!
public static PdfPTable createTableA() {
//empty space
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 3);
int numberOfCells = 0;
int numberOfIdealCells = 0;
//int
numberOfCells = Checks.aResults.size();
numberOfIdealCells = numberOfCells;
Log.d("mensa", "numero de preguntas : "+numberOfCells);
//TODO REFACTOR THIS LOGIC TO KNOW IF NUMBER OF CELLS OK!
if (numberOfCells %3 != 0) {
Log.d("mensa", "no lo es");
numberOfIdealCells +=1;
if (numberOfIdealCells %3 != 0) {
numberOfIdealCells +=1;
}
}
Log.d("mensa", "sikas");
Log.d("mensa", "numero de preguntas afters: "+numberOfIdealCells);
// a table with three columns
PdfPTable table = new PdfPTable(3);
Checks.aResults.keySet().iterator();
Iterator<String> iteratorSectionC = Checks.aResults.keySet().iterator();
while(iteratorSectionC.hasNext()) {
String key=(String)iteratorSectionC.next();
int value=(int)Checks.aResults.get(key);
// Log.d("mensa", "mi Pregunta iteratorSectionC::\n"+key+" valor::"+value);
//Paragraph pN = new Paragraph(key+" val::"+value);
//document.add(pN);
table.addCell(key+" val::"+value);
}
//add empty cells
int difference = numberOfIdealCells - numberOfCells;
if (difference > 0) {
for(int i = 0; i<difference ; i++){
table.addCell("");
}
}
return table;
}
public static PdfPTable createTableB() {
//empty space
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 3);
int numberOfCells = 0;
int numberOfIdealCells = 0;
//int
numberOfCells = Checks.bResults.size();
numberOfIdealCells = numberOfCells;
Log.d("mensa", "numero de preguntas : "+numberOfCells);
//TODO REFACTOR THIS LOGIC TO KNOW IF NUMBER OF CELLS OK!
if (numberOfCells %3 != 0) {
Log.d("mensa", "no lo es");
numberOfIdealCells +=1;
if (numberOfIdealCells %3 != 0) {
numberOfIdealCells +=1;
}
}
Log.d("mensa", "sikas");
Log.d("mensa", "numero de preguntas afters: "+numberOfIdealCells);
// a table with three columns
PdfPTable table = new PdfPTable(3);
Checks.bResults.keySet().iterator();
Iterator<String> iteratorSectionC = Checks.bResults.keySet().iterator();
while(iteratorSectionC.hasNext()) {
String key=(String)iteratorSectionC.next();
int value=(int)Checks.bResults.get(key);
// Log.d("mensa", "mi Pregunta iteratorSectionC::\n"+key+" valor::"+value);
//Paragraph pN = new Paragraph(key+" val::"+value);
//document.add(pN);
table.addCell(key+" val::"+value);
}
//add empty cells
int difference = numberOfIdealCells - numberOfCells;
if (difference > 0) {
for(int i = 0; i<difference ; i++){
table.addCell("");
}
}
return table;
}
public static PdfPTable createTableC() {
//empty space
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 3);
int numberOfCells = 0;
int numberOfIdealCells = 0;
//int
numberOfCells = Checks.cResults.size();
numberOfIdealCells = numberOfCells;
Log.d("mensa", "numero de preguntas : "+numberOfCells);
//TODO REFACTOR THIS LOGIC TO KNOW IF NUMBER OF CELLS OK!
if (numberOfCells %3 != 0) {
Log.d("mensa", "no lo es");
numberOfIdealCells +=1;
if (numberOfIdealCells %3 != 0) {
numberOfIdealCells +=1;
}
}
Log.d("mensa", "sikas");
Log.d("mensa", "numero de preguntas afters: "+numberOfIdealCells);
// a table with three columns
PdfPTable table = new PdfPTable(3);
Checks.cResults.keySet().iterator();
Iterator<String> iteratorSectionC = Checks.cResults.keySet().iterator();
while(iteratorSectionC.hasNext()) {
String key=(String)iteratorSectionC.next();
int value=(int)Checks.cResults.get(key);
// Log.d("mensa", "mi Pregunta iteratorSectionC::\n"+key+" valor::"+value);
//Paragraph pN = new Paragraph(key+" val::"+value);
//document.add(pN);
table.addCell(key+" val::"+value);
}
//add empty cells
int difference = numberOfIdealCells - numberOfCells;
if (difference > 0) {
for(int i = 0; i<difference ; i++){
table.addCell("");
}
}
return table;
}
My problem is that i need to pass my iterator for each different block, but I can't seem to find a proper way to pass it to my function, Shall I make it a static variable and according to case A,B,C use the corresponding iterator?
Iterator<String> iteratorSectionC = Checks.cResults.keySet().iterator();
or how to pass it in a function parameter?