# William M. Farmer
# 31 October 2010
#
# This is a solution for Lab 07: Minor Assignment 4.
#
# The grades file is assumed to have the following format (called "courses.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


import string
from graphics import *

def processFile(gradesFileName):
   gradesFile = open(gradesFileName, "r")
   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: ")

    # Process Grades File
    units, average = processFile(gradesFileName)
    
    #Prints Results to the shell
    print "The number of units are =" , units
    print "The average is =" , average
    
    #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.
# 
# Test Plan
# Using courses.txt (content listed above)
# 
# Test 1
# Input: [courses.txt]
# Expected Output: [19, 9.95]
#
# Test 2
# Input: [courses2.txt]
# Expected Output: [IO Error - invalid file name]
#
# Test 3 *contents of courses.txt changed: units column contains zeros
# Input: [courses.txt]
# Expected Output: [ZeroDivisionError: float division by zero]

