All pastes #359958 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-02-17 04:29 UTC
rendered paste body
package 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();
		}
	}

	public void fixEmptyRows() {
		Boolean isEmpty = true;

		for(int i=0; i < rows.size();i++) {

			for(String col : rows.get(i)) {
				if(col.length() > 1) {
					isEmpty = false;
				}
			}
			if(isEmpty == true) {
				this.rows.remove(i);
				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 range(int from,int to) {
		return getAll().subList(from, to);
	}
}