-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab5.py
213 lines (193 loc) · 8.04 KB
/
Lab5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Christina Shafer
CIS 41A - Fall 2018
Dec 4, 2018
Lab 5
Summary:
A program that maintains a class list for a student. The student can take for-credit classes or non-credit classes
Program lets student enter data on their current classes, then the program prints out the class list, the supplies
needed for each class day and the grade record for each for-credit class
"""
from course import *
class ClassList:
FILE="lab5.txt" #create a class variable, use as self.FILE
"""
object that holds a list of courses
"""
def __init__(self):
self._courses=[]
self.addCourses("lab5.txt")
def getCourses(self):
"""
method to return the list of courses
:return: _courses
"""
return self._courses
def addCourses(self,filename=None):
"""
method to prompt user for courses for the ClassList
:return:
"""
if not filename:
goodCourseNum=False
while goodCourseNum==False:
try:
numCourses=int(input("How many courses do you want to enter?")) #can throw an exception
goodCourseNum=True
except:
print("Number of courses must be a digit")
for i in range(numCourses):
choice =input("Enter 'c' for credit or 'nc' for non-credit:")
while choice.upper() not in ("C","NC"):
print("Invalid Class type. ")
choice =input("Enter 'c' for credit or 'nc' for non-credit:")
if choice.upper() =="C" :
done=False
while done==False:
info = input("Enter comma-separated class name, time, days, units: ")
info=info.split(",")
while len(info)!=4:
print("Incorrect number of fields entered.")
info = input("Enter comma-separated class name, time, days, units: ")
info=info.split(",")
try:
newCourse=CreditCourse(*info)
newCourse.addSupplies()
newCourse.addGrades()
done=True
except (ValueError):
print("Incorrect data entered")
else:
done=False
while done==False:
info = input("Enter comma-separated class name, time, days: ")
info=info.split(",")
while len(info)!=3:
print("Incorrect number of fields entered.")
info = input("Enter comma-sarated class name, time, days: ")
info=info.split(",")
try:
newCourse= NonCreditCourse(*info)
newCourse.addSupplies()
newCourse.addActivities()
done=True
except (ValueError):
print("Incorrect data entered")
self._courses.append(newCourse)
else:
#this is where we do the work for parsing a file and creating classes
self.readClassList()
def readClassList(self) :
"""
method that reads in class data from a file and populates the classList
:return: None
"""
try:
filename="lab5.txt"
with open(filename) as infile : #possible IO Exception
done = False
while not done :
line=infile.readline()
if line=="": #end of file reached
done=True
continue
#CreditCourse processing
if line.strip().upper()=="C":
line=infile.readline() #read in course info line
info=line.split(",")
newCourse=CreditCourse(*info) #unpack
#read in supplies
supplies=[i.strip() for i in infile.readline().split(",")]
newCourse.addSupplies(supplies)
tasks=[i.strip() for i in infile.readline().split(",")]
scores=[float(i) for i in infile.readline().split(",")]
letterGrades=[i.strip() for i in infile.readline().split(",")]
newCourse.addGrades(tasks, scores, letterGrades)
self._courses.append(newCourse)
continue
#NonCreditCourse processing
if line.strip().upper()=="NC":
info=infile.readline().split(",") #read in course info line
newCourse=NonCreditCourse(*info) #unpack
supplies=[i.strip() for i in infile.readline().split(",")]
newCourse.addSupplies(supplies)
activities=[i.strip() for i in infile.readline().split(",")]
newCourse.addActivities(activities)
self._courses.append(newCourse)
continue
#error lines
if len(line.strip())>0 and line.strip().upper not in ("C","NC"):
print("Invalid line:",line, end="")
except IOError :
print("Can't open " + filename)
SystemExit(1)
def printSupplies(self,day):
"""
method to print the supplies needed in all courses for a particular day
:param day: could be M,T,W,R,F
:return: no return, just a print
"""
supplySet=set()
#create a list of courses on the day
daysClasses=set([course for course in self._courses if day in course._classDays])
for course in daysClasses:
courseSupplies=set(course.getSupplies())
supplySet=supplySet.union(courseSupplies)
items= sorted(supplySet)
if len(items)>0:
print(day + ": " , end="")
print(', '.join(items))
def report(self):
"""
method to print all info on classList. calls two methods: printSupplies, printClassList
:return:
"""
menu="\nC. Class List\nS. Supplies\nA. Activities\nQ. Quit"
print()
print(menu)
quit=False
while not quit:
choice= input("Choice:").upper().strip()
if choice not in ('A','S','C','Q'):
print("Invalid Choice.")
print()
continue
if (choice=='S'):
print()
print("Supplies for each day of class:")
allDays=['M','T','W','R','F']
for day in allDays:
self.printSupplies(day)
print()
continue
if (choice=='A'):
print()
self.printClassList()
print()
continue
if (choice=='C'):
print()
for c in sorted(self._courses):
print(c)
print()
continue
if (choice=='Q'):
print("\nGood-bye!")
quit=True
def printClassList(self):
"""
method to print out all class info for classes in ClassList in alphabetical order
:return:
"""
print()
print("Activities for each class")
alphaList=sorted(self._courses)
for i in range(len(alphaList)):
print()
print(str(alphaList[i]))
alphaList[i].printActivities()
def main():
"""the main"""
L=ClassList()
L.report()
main()