/**
 * 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
 *
 * Idea: find most common color across the row. Constrain allowed
 * inputs to small set. Keep an array of the votes for each possible
 * color, then find the maximum.
 *
 * We could use a construct like associative arrays in perl for this:
 *   favColors{"green"} = 0; 
 *
 * How to do something like this in Java? Need association between
 * name of color and integer. We saw something like this in the Color
 * class before.
 *
 * Updates today: finish debugging
 *
 * @author CPSC 111 class Jan 2010
 * @date Wed Mar 24 2010 
 **/

public class FavoriteColor {
	// private or public? public, for now
	public static final int BLUE = 0;
	public static final int GREEN = 1;
	public static final int ORANGE = 2;
	public static final int BLACK = 3;
	public static final int PINK = 4;
	public static final int PURPLE = 5;
	public static final int YELLOW = 6;
	private final int NUMCOLORS = 7; // 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]; // also legal, since we want all the subarrays to be the same length
	}

	public void addFaveForRow(int row, int color) {
		//error checking
		if ( !( color == BLUE || color == GREEN || color == ORANGE || 
				color == BLACK || color == PINK || color == PURPLE || 
				color == YELLOW))
			System.out.println("sorry, I don't know that color");
		else
			faves[row][color]++;
	}
	
	public void printFaveForRow(int row) {
		int max = -1;
		int maxColor = -1;
		for (int i = 0; i < faves[row].length; i++) {
			if (faves[row][i] > max) {
				max = faves[row][i];
				maxColor = i;
				// winner of ties will just be the first one, not perfect!
			}
		}
		String colorname;
		switch (maxColor) {
		case BLUE: colorname = "blue"; break;
		case GREEN: colorname = "green"; break;
		case ORANGE: colorname = "orange"; break;
		case BLACK: colorname = "black"; break;
		case PINK: colorname = "pink"; break;
		case PURPLE: colorname = "purple"; break;
		case YELLOW: colorname = "yellow"; break;
		default: colorname = "oops, error"; break;
		}
		System.out.println("fave color in row "+row+" is "+colorname);
	}
}

