import java.util.*;

public class D3 extends DetailRecord
{
	private String description;
	private Grade grade;
	private static final int TOKENS = 6;
	private static final int HOURS = 4;
	private static final int GRADE = 5;
	private static final int YEAR = 0;
	private static final int TERM = 1;
	
	
	public D3(int y, int t, String c,  String d, int h, String gr)
	{
		super (y, t, c, h);
		description = d;
		
		for(Grade g : Grade.values())
		{
			if (g.getGrade().equals(gr))
				grade = g;
		}
		
		if (grade == null)
			grade = Grade.NC;
	}
	
	public int getGradedHours ()
	{
		int hours = 0;
		
		if (grade.isGraded())
			hours = getCreditHours();	
		return hours;
	}
	
	public Grade getGrade()
	{
		return grade ;
	}
	
	public int getEarnedHours()
	{
		int hours = 0;
		if (grade.isEarned())
			hours = getCreditHours();
			
		return hours;
	}
	
	public String getDescription()
	{
		return getCourse() + " - " + description;
	}
	
	public double getQualityPoints()
	{
		return grade.getQP() * getCreditHours();
	}
	
	public static int checkD3(String line)
	{
		int result, hours;
		String fields [];
		StringTokenizer st;
		fields = new String [TOKENS];
		
		st = new StringTokenizer(line, ",");		
		hours = 0;
		result = 0;
		
		if (st.countTokens() != TOKENS)
			result = 7;
		// pull off tokens to prep for next section
		else
		{
			for (int ii = 0; ii < fields.length; ii++)
				fields[ii] = st.nextToken();
			
			try
			{
				Integer.parseInt(fields[YEAR]);
				Integer.parseInt(fields[TERM]);
				hours = Integer.parseInt(fields[HOURS]);
			}
			catch(NumberFormatException nfe)
			{
				result = 8;
			}		
			if(hours < 0 || hours > 12)
				result = 9;
			else 
				if (Grade.getGrade(fields[GRADE]) == null)
					result = 10;
		}
		
		return result;
	}
	
	public static D3 makeD3(String line)
	{
		D3 detail = null;
		StringTokenizer st;
		int year, term, hours;
		String course, description, inGrade;
		Grade grade;
				
		// if no errors found
		if (checkD3(line) == 0)
		{
			// at this point we know that everything
			// is okay since we have checked.
			st = new StringTokenizer(line,",");
			year = Integer.parseInt(st.nextToken());
			term = Integer.parseInt(st.nextToken());
			course = st.nextToken();
			description = st.nextToken();
			hours = Integer.parseInt(st.nextToken());
			inGrade = st.nextToken();
			
			detail = new D3(year, term, course, description, hours, inGrade);
		}	
		return detail;
	}
}