##Student Name: Wurong Weng
##MacID:wengwr
##Student Number:1224989
##Date: Feb. 27, 2013

from graphics import *

def main():
    n = raw_input("Enter the name of grade file: ") ## get input
    gradeFile = open(n, "r")
    U,A = processFile(gradeFile) ## process the grade file
    outputGUI(U,A) ## get output

def processFile(gradeFile):
    units = 0
    gradeMarks = 0
    for line in gradeFile:
        lineList = list(line.split())
        ## get summation of units and grade marks
        units += float(lineList[2]) 
        gradeMarks += float(lineList[2]) * float(lineList[3])
    gradeFile.close()
    average = gradeMarks/units
    return[units,average]

def outputGUI(U,A):
    W = GraphWin("Grade Processor Results", 600, 200)
    ## label the title
    titleLable = Text(Point(300,20),"Grade Processor Results")
    titleLable.setSize(30)
    titleLable.draw(W)
    ## lable the units
    unitsLable = Text(Point(300,100),"Total Number Of Units =" + str(U))
    unitsLable.setSize(20)
    unitsLable.setTextColor("blue")
    unitsLable.draw(W)
    ## lable the average
    averageLable = Text(Point(300,150),"Cumulative Grade Average =" + str(A))
    averageLable.setSize(20)
    averageLable.setTextColor("green")
    averageLable.draw(W)
    ## close the window
    W.getMouse()
    W.close()

main()

##Design Question:
##    Yes, it is because the file is only processed inside the processFile function,
##    so you can make changes to the process without modify the other part. This is
##    desirable because sometime the program contain big content, so it will be easier
##    to locate where the process part is and can modify and test the process part
##    seperatly.
##
##Test Plan:
##    Test: 1
##    Input: [abc.txt]
##        abc.txt :
##            CHEM 1E03      3   9
##            ENGINEER 1D04  4  12
##            ENGINEER 1P03  3  10
##            HISTORY 1B03   3   8
##            MATH 1ZA3      3  11
##            PHYSICS 1D03   3   9
##    Expected Output: [19.0, 9.947]
##    Actual Output: [19.0, 9.94736842105]
##    Result: PASS
##
##    Test: 2
##    Input: [abc.txt]
##        abc.txt :
##            CHEM 1E03      a   9
##            ENGINEER 1D04  4  12
##            ENGINEER 1P03  3  10
##            HISTORY 1B03   3   8
##            MATH 1ZA3      3  11
##            PHYSICS 1D03   3   9
##    Expected Output: [error]
##    Actual Output: [ValueError]
##    Result: PASS
##
##    Test: 3
##    Input: [abc.txt]
##        abc.txt :
##            CHEM 1E03      0   9
##            ENGINEER 1D04  0  12
##            ENGINEER 1P03  0  10
##            HISTORY 1B03   0   8
##            MATH 1ZA3      0  11
##            PHYSICS 1D03   0   9
##    Expected Output: [error]
##    Actual Output: [ZeroDivisionError]
##    Result: PASS
