rendered paste bodypackage components.pricesheet;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
public class PriceSheetManager {
private CSVReader reader;
private List<String[]> rows;
public PriceSheetManager (String file) {
try {
reader = new CSVReader(new FileReader(file));
rows = reader.readAll();
this.fixEmptyRows();
} catch (Exception e) {
e.printStackTrace();
}
}
private void fixEmptyRows() {
Boolean isEmpty = true;
for(int i=0; i < rows.size();i++) {
for(String col : rows.get(i)) {
if(col.length() > 2) {
isEmpty = false;
}
}
if(isEmpty == true) {
System.out.println("Removing "+ rows.get(i).toString());
rows.set(i, null);
rows.remove(i);
}
isEmpty = true;
}
}
public List getRow(int index) {
List toReturn = new ArrayList();
for(String str : rows.get(index)) {
toReturn.add(str);
}
return toReturn;
}
public List getRandomRow () {
int random = (int) (Math.random() * rows.size());
return this.getRow(random);
}
public List getAll() {
int cols = rows.size();
List toReturn = new ArrayList();
for(int i =0; i < cols; i++ ) {
toReturn.add(getRow(i));
}
return toReturn;
}
public List getAll(int limit) {
return getAll().subList(1, limit);
}
// public static void main(String[] args) {
// PriceSheetManager mgr = new PriceSheetManager("test.csv");
// System.out.println(mgr.getRandomRow());
// }
}