// Challenge: how to average favorite colors across rows of students
//
// back row favorite colors:
// green
// black
// blue
// blue
//
// second-to-last row favorite colors:
// orange
// purple
// pink
// yellow
// yellow
// yellow
// blue
//
// ideas on how to average colors:
// - constrain the allowed inputs
// - mix colors
// - decompose into rgb primaries and mix those
// - order colors in 1D by wavelength into rainbow/spectrum and find avg
// - order colors in 2D grid
// - find unicode of name and order that
// - find most common: max vs average
//
// we'll do last one, find the max, seems easiest!
// also, let's constrain allowed inputs for now, in a first pass.
//
// basic idea: have array of counts for each possible color. then find max value.
//
// favColors{"green"} = 0; // could do this with associative arrays in perl
// how to do something like this in Java? need association between name of color and integer

public class FavoriteColor {
	// private or public? public, for now
	// static or not? leave it, let's see...
	public static final int BLUE = 0;
	public static final int GREEN = 1;
	public static final int ORANGE = 2;
	public static final int BLACK = 3;
	private final int NUMCOLORS = 4; // this is a bit of a hack...
	
	private int [][] faves;
	// we store color votes for each row
	
	public FavoriteColor(int rowsInRoom) {
//		faves = new int[rowsInRoom][];
//		for (int i = 0; i < rowsInRoom; i++) {
//			faves[i] = new int[NUMCOLORS];
//		}
		faves = new int[rowsInRoom][NUMCOLORS];
	}
	// public void enterFavesForRow(int row)
	public void addFaveForRow(int row, int color) {
		//error checking
		if ( !( color == BLUE || color==GREEN || color==ORANGE || color==BLACK))
			System.out.println("sorry, I don't know that color");
		else
			faves[row][color]++;
	}
	
	public void printFaveForRow(int row) {
		
		int max = -1;
		for (int i = 0; i < faves[row].length; i++) {
			if (faves[row][i] > max) {
				max = faves[row][i];
				// winner of ties will just be the first one, not perfect!
			}
		}
		String colorname;
		switch (max) {
		case BLUE: colorname = "blue"; break;
		case GREEN: colorname = "green"; break;
		case ORANGE: colorname = "orange"; break;
		case BLACK: colorname = "black"; break;
		default: colorname = "oops, error"; break;
		}
		System.out.println("fave color in row "+row+" is "+colorname);
	}
}
