# William M. Farmer
# 31 October 2010
#
# This is a solution for Lab 06: Unmarked Assignment 4.
#
# The grades file is assumed to have the following format:
#
# CHEM     1E03  3   9
# ENGINEER 1D04  4  12
# ENGINEER 1P03  3  10
# HISTORY  1B03  3   8
# MATH     1ZA3  3  11
# PHYSICS  1D03  3   9


import string
from graphics import *

def processFile(gradesFile):
   units = 0
   unitMarks = 0
   for line in gradesFile:
       quad = string.split(line)
       units = units + float(quad[2])
       unitMarks = unitMarks + (float(quad[2]) * float(quad[3]))
   average = unitMarks / units
   gradesFile.close()
   return [units, average]

def outputGUI(units,average):
   # Output Results to a GUI
   win = GraphWin("Grade Processor Results", 500, 200)
   win.setCoords(0,0,100,100)
   # Label for title 
   titleString = "Grade Processor Results"
   titleLabel = Text(Point(50,90), titleString)
   titleLabel.setSize(30)
   titleLabel.draw(win)
   # Label for number of units
   unitsString = "Total Number of Units = %i" % (units)
   unitsLabel = Text(Point(50,55), unitsString)
   unitsLabel.setSize(20)
   unitsLabel.setTextColor("blue")
   unitsLabel.draw(win)
   # Label for cumulative grade average
   averageString = "Cumulative Grade Average = %.2f" % (average)
   averageLabel = Text(Point(50,25), averageString)
   averageLabel.setSize(20)
   averageLabel.setTextColor("red")
   averageLabel.draw(win)
   win.getMouse()       # The program pauses until the window is clicked.
   win.close()          # Window is closed.
       
def main():

    print """\

This program is a solution to Lab 06: Unmarked Assignment 4.
"""

    # Input Grades File
    gradesFileName = raw_input("Enter the name of the grades file: ")
    gradesFile = open(gradesFileName, "r")

    # Process Grades File
    units, average = processFile(gradesFile)

    #Print Results to GUI
    outputGUI(units, average)



main()

# Design Question
# 
# Yes.  The processFile determines the format of the grades file and how
# the contents of the grades file are processed.  So any change to how
# the grades files is formatted or processed can be made by just
# modifying processFile.  This design is desirable because it separates
# the processing of the grades file from the input of the grades file
# and the output of the results.  Hence the input, processing, and
# output components can developed, tested, and maintained independently.
# 
# Testing Question
# 
# For testing processFile, the output GUI portion of the program could
# be replaced with a simple piece of code like:
# 
# 
#   print "Total Number of Units = ", units
#   print "Cumulative Grade Average = ", average
