CS105: Computer Programming: PYTHON Project 04 Assigned on Friday, October 19, 2007 Due on Friday, October 26 at 11:59PM ============= 1. Objectives ============= Through this project, we want to review various concepts that we have learned in the class. In particular, we will focus on the following concepts: (1) Handling commandline arguments; (2) Class definition and usage; (3) File input and output and some other details. There are TWO MAJOR THINGS to do: (1) We are asked to implement all the necessary classes and functions in one file. The file will take both input (i.e., sys.argv[1]) and output file (i.e., sys.argv[2]) names from the command line. After checking validity of the commandline arguments, it will read contents of the given input file (called "employee.txt"), compute earnings for each person, and print (on screen) as well as store (in output file) processed contents. (2) we'll be asked to divide each class and a driver functions in each separate file. Make sure the following line and other testing statements as added at the end of each file so as to do a unit test: if __name__ == '__main__': ============== 2. Description ============== We will do the following detailed requirements: (REQUIREMENT I) =============== (2.1) The script will be called "testEmployeeAll.py" (2.2) It will contain two class definitions, "Employee" and "Boss", whose details will be discussed later. (2.3) It will also contain several functions through which we want to double-check our understanding of OOP concepts. (2.4) Add "#!/usr/bin/env python" at the first line of the script so that the script can be run from the commandline as below: > ./testEmployeeAll.py emoloyee.txt out.txt (2.5) From this project, all script should be well documented. (REQUIREMENT II) ================ (2.6) Store each of the two classes in a separate file, called "employee.py" and "boss.py", respectively. (2.7) Put the other remaining functions in a file, called "testEmployee.py". (2.8) Then, make sure that each file contains "if __name__ == '__main__':" so as to do a unit test for each file. (NOTE ON INPUT DATA FILE, "employee.txt") ========================================= --------------------------------------------- LINE ACTUAL CONTENTS EXPLANATION --------------------------------------------- 01 0 employeeType 02 Sam firstName 03 Smith lastName 04 1200.56 monthlySalary 05 1 employeeType 06 Harry firstName 07 Hacker lastName 08 5800.21 monthlySalary 09 3000.50 annualBonus ... ... --------------------------------------------- Notice that only employee whose employeeType == 1 has annualBonus. So, if you know employeeType == 1, you need to read another line for it. ============================== 3. Grading criteria and policy ============================== Here are the details of each subroutine in the menu. NOTE: The script will be graded based on how well the required functionality is fulfilled. Therefore, please follow the description as much as possible to get full credits. (REQUIREMENT I) =============== class Employee: def __init__(self, firstName = None, lastName = None, monthlySalary = 0.0): """Initializes the corresponding field values""" def getFirstName(self): """Returns fistname""" def getLastName(self): """Returns lastname""" def getMonthlySalary(self): """Returns monthly salary""" def getEarnings(self): """Returns yearly earnings""" def __repr__(self): """Converts object to string NOTE that it should be implemented so as to show the same contents as the following example: >> myEmployee = Employee('Bob', 'Dylon', 7000) >> print myEmployee Bob Dylon earns $7000 a month. """ class Boss(Employee): def __init__(self, firstName = None, lastName = None, monthlySalary = 0.0, annualBonus = 0.0): """Initializes the corresponding field values NOTE that firstName, lastName, and monthlySalary should be initialized through Employee's constructor. """ def getAnnualBonus(self): """Returns annual bonus""" def getEarnings(self): """Returns yearly earnings. NOTE that it should be sum of yearly earnings and annual bonus. """ def __repr__(self): """Converts object to string NOTE that it should be implemented so as to show the same contents as the following example: >> myEmployee = Employee('Boss', 'Dylon', 7000, 1000) >> print myEmployee Boss Dylon earns $7000 a month. Also, he/she has $1000 of annual bonus. """ def checkCommandLine(argv): """Checks number of commandline arguments. If number of arguments is not the same as the required number, it shows the following messages and exit the program: 'Usage: nameOfProgram inputFile outputFile' """ def openFile(fileName, accessMode): """Returns a file handler""" def getInformation(inFile): """Reads and returns employType, firstName, lastName, and monthlySalary""" def putResult(outFile, myEmployee): """Prints out (on screen) as well as writes (to the file handler, outFile) the following contents for myEmployee: IF THE PERSON IS AN USUSAL EMPLOYEE, print/write out the following contents: Sam Smith earns $1200.56 a month. Therefore, annual earnings is $14406.72 IF THE PERSON IS A BOSS, print/write out the following contents: Harry Hacker earns $5800.21 a month. Also, he/she has $3000.5 of annual bonus. Therefore, annual earnings is $72603.02 """ def processFile(inFile, outFile): """Read contents from inFile and prints/writes out appropriate contents to screen/outFile """ while True: (1) Get employType, firstName, lastName, and monthlySalary using "getInformation()". (2) if employeeType is not valid, break the loop. NOTE 0(for usual employee) or 1(for boss) is a valid employeeType. (3) Get instance of either Employee() or Boss(). (4) Print/write contents of the instance using "putResult()" def testEmployee(argv): """Tests implementation""" (1) Check commandline arguments using "checkCommandLine()". (2) Open both input and output files. (3) Read input file, process, and print/write out results to output file. (REQUIREMENT II) ================ (1) employee.py --------------- class Employee: ... if __name__ == '__main__': myEmployee = Employee('Bob', 'Dylon', 7000) print myEmployee (2) boss.py ----------- class Boss: ... if __name__ == '__main__': myEmployee = Boss('Boss', 'Dylon', 7000) print myEmployee myEmployee = Boss('Boss', 'Dylon', 7000, 1000) print myEmployee (3) testEmployee.py ------------------- def checkCommandLine(argv): def openFile(fileName, accessMode): def getInformation(inFile): def putResult(outFile, myEmployee): def processFile(inFile, outFile): def testEmployee(argv): if __name__ == '__main__': testEmployee(sys.argv) ============================== 4. Grading criteria and policy ============================== Please make sure to include the followings at the header of the code. In summary, each script should contain AT LEAST the followings: -------------------------------------------------------------------- #!/usr/bin/env python # Project: 04 # Description: Employee-Boss class # Program: testEmployeeAll.py # Input: employee.txt # Output: out.txt # Usage: ./testEmployeeAll.py employee.txt out.txt # Name: Your name # UT EID: Your EID # Comments (or README): # Describe what you with to grader to know. # e.g., What did you like/dislike about this project? # What was the most challenging in this project? # etc. class Employee: ... class Boss: ... def ... ... if __name__ == '__main__': ------------------------------------------------------------ ================== 5. Running example ================== The followings are expected upon running the script: -------------------------------------------------------------- > ./testEmployeeAll.py Usage: ./testEmployeeAll.py inputFile outputFile > ./testEmployeeAll.py employee.txt Usage: ./testEmployeeAll.py inputFile outputFile > ./testEmployeeAll.py employee.txt out.txt Sam Smith earns $1200.56 a month. Therefore, annual earnings is $14406.72 Harry Hacker earns $5800.21 a month. Also, he/she has $3000.5 of annual bonus. Therefore, annual earnings is $72603.02 Herald John earns $5000.0 a month. Also, he/she has $5000.0 of annual bonus. Therefore, annual earnings is $65000.0 Paul Michael earns $5000.0 a month. Therefore, annual earnings is $60000.0 > more out.txt Sam Smith earns $1200.56 a month. Therefore, annual earnings is $14406.72 Harry Hacker earns $5800.21 a month. Also, he/she has $3000.5 of annual bonus. Therefore, annual earnings is $72603.02 Herald John earns $5000.0 a month. Also, he/she has $5000.0 of annual bonus. Therefore, annual earnings is $65000.0 Paul Michael earns $5000.0 a month. Therefore, annual earnings is $60000.0 > ----------------------------------------------------------- ======= 6. NOTE ======= This project is worth a total of 200 (10 points for each functions + 10 points for each file) points. We'll discuss details about the project in the class. Submit four script files, "testEmployeeAll.py", "employee.py", "boss.py", and "testEmployee.py" as follows: > turnin --submit hyukcho project04 testEmployeeAll.py employee.py boss.py testEmployee.py Good luck!