//Victor Tong
//Ms. Sivarjah
//May 8th, 2007
import java.awt.*;
import java.text.*;
import hsa.Console;
import java.lang.Math; // Debug

public class Chart_edited
{
    static Console c;

    public static void main (String[] args)
    {
	c = new Console ();
	double[] [] marks = new double [11] [6];
	double bestmark = 0, beststudent = 0;
	boolean[] samemark = new boolean [10];
	int count1 = 0, count2 = 0;
	Color Green = new Color (60, 219, 36); // Sets the colour green
	c.setCursor (2, 30);
	c.print ("Test Number");
	for (int i = 0 ; i < 10 ; i++)
	{
	    c.setCursor (5 + i * 2, 9); // Outputs the rows
	    c.print (i + 1, 2);
	}
	for (int j = 0 ; j < 5 ; j++)
	{
	    c.setCursor (3, 20 + j * 8); // Outputs the columns
	    c.print (j + 1);
	}
	c.setCursor (3, 59);
	c.print ("Average");
	c.setCursor (25, 4);
	c.print ("Average");
	makecells ();
	c.setCursor (13, 1);
	c.print ("Student");


	// DecimalFormat fmt = new DecimalFormat ("##0.00");

	for (int j = 0 ; j < 4 ; j++)
	{
	    for (int i = 0 ; i < 10 ; i++)
	    {
		c.setCursor (1, 1);
		c.print ("Input: ");
		c.setCursor (1, 8);
		// marks [i] [j] = c.readDouble (); // Receives the marks
		// marks [i] [j] = 100; // Debug
		marks [i] [j] = randebug (); // Debug
		marks [10] [j] = marks [10] [j] + marks [i] [j];
		c.setCursor (5 + i * 2, 15 + j * 8);
		colour (marks [i] [j]); // Changes colours of the text
		c.print (marks [i] [j], 6, 2);
		c.setTextColor (Color.black);
		c.setCursor (1, 1);
		c.println ("");
	    }
	    c.setCursor (24, 15 + j * 8);
	    marks [10] [j] = marks [10] [j] / 10;
	    c.setCursor (25, 15 + j * 8);
	    colour (marks [10] [j]);
	    c.print (marks [10] [j], 6, 2); // Outputs the average of each test
	    c.setTextColor (Color.black);
	}
	for (int i = 0 ; i < 10 ; i++)
	{
	    c.setTextColor (Color.black);
	    c.setCursor (1, 1);
	    c.print ("Input: ");
	    c.setCursor (1, 8);
	    // marks [i] [4] = c.readDouble (); // Receives the input
	    // marks [i] [4] = 100; // Debug
	    marks [i] [4] = randebug (); // Debug
	    c.setCursor (5 + i * 2, 47); // Sets the cursor
	    colour (marks [i] [4]);
	    c.print (marks [i] [4], 6, 2);
	    for (int x = 0 ; x < 5 ; x++)
	    {
		marks [i] [5] = marks [i] [5] + marks [i] [x];
	    }
	    marks [i] [5] = marks [i] [5] / 5;
	    c.setCursor (5 + i * 2, 60);
	    colour (marks [i] [5]);

	    c.print (marks [i] [5], 6, 2);
	    c.setCursor (1, 1);
	    c.println ("");
	}
	for (int i = 0 ; i < 10 ; i++)
	{
	    marks [10] [4] = marks [10] [4] + marks [i] [4];
	}
	marks [10] [4] = marks [10] [4] / 10;
	colour (marks [10] [4]); // Sets the colour according to the mark
	c.setCursor (25, 47);
	c.print (marks [10] [4], 6, 2);
	c.setTextColor (Color.black);
	for (int i = 0 ; i < 5 ; i++)
	{
	    marks [10] [5] = marks [10] [i] + marks [10] [5];
	}
	marks [10] [5] = marks [10] [5] / 5; // Calculates each student's average
	c.setCursor (25, 60);
	colour (marks [10] [5]); // Sets colour according to mark
	c.print (marks [10] [5], 6, 2);
	c.setTextColor (Color.black);
	for (int rows = 0 ; rows < marks.length - 1 ; rows++)
	{
	    if (marks [rows] [5] > bestmark)
	    {
		beststudent = rows + 1;
		bestmark = marks [rows] [5];
		samemark [rows] = true;
		count1 = 1;
		for (int i = 0 ; i < rows ; i++)
		{
		    samemark [i] = false;
		}
	    }
	    else if (marks [rows] [5] == bestmark)
	    {
		samemark [rows] = true;
		count1++;
	    }
	}

	c.setCursor (1, 1);
	c.print ("Student");
	if (count1 > 1)
	{
	    c.print ("s");
	}
	c.print (" ");
	for (int i = 0 ; i < samemark.length ; i++)
	{
	    if (samemark [i])
	    {
		c.print ((i + 1));
		count2++;
		if (count1 == count2)
		{
		    break;
		}
		else
		{
		    c.print (", ");
		    if (count1 - count2 == 1)
		    {
			c.print ("and ");
		    }
		}
	    }
	}
	c.print (" achieved the class-high of ");
	c.println (bestmark, 0, 2);
    }


    public static void colour (double marks)
    {
	Color colour;
	if (marks < 0 || marks > 100)
	    colour = new Color (0, 0, 0);
	else if (marks < 50)
	    colour = new Color (224, 0, 0);
	else if (marks < 75)
	    colour = new Color (224, (int) Math.round (112 + (25 - (75 - marks)) * (112 / 25)), 0);
	else
	    colour = new Color ((int) Math.round ((100 - marks) * (224 / 25)), 224, 0);
	c.setTextColor (colour);
    }


    static void makecells ()
    {
	int place;
	
	c.setColor (new Color (192, 192, 192));
	for (int i = 0 ; i < 10 ; i++)
	{
	    place = 110 + 40 * i;
	    c.drawLine (60, place, 520, place); // Horizontal lines
	}
	for (int i = 0 ; i < 5 ; i++)
	{
	    place = 168 + 64 * i;
	    c.drawLine (place, 40, place, 520);
	}
	
	c.setColor (Color.black);
	c.drawLine (60, 70, 520, 70);
	c.drawLine (104, 40, 104, 520);
    }
    
    static double randebug ()
    {
	return (double) Math.round (Math.random () * 10000) / 100;
    }
}
