In: Computer Science
2. Create two class, a student class, and a course class, take advantage of the codes provided in the class slides and sample codes. python
Student |
Course |
__Username:str __Courses:list |
__courseName:str __students: list |
addCourse():None dropCourse():None getCourse():list |
addStudent:None dropStudent:None getStudents():list getNumber of students:int |
3. Create a student object for every student in the UD.txt (you can use a loop for this)
4. Create 6 course objects:
1. CS131 2. CS132 3. EE210 4. EE310 5. Math320 6. Math220 |
5. After the student user login to their account as we have done in lab 2. Display the following:
A: Show all courses available B: Add a course C: Drop a course D: Show all my courses E: Exit |
When students choose option A, should display the following, where # is the actual number of students enrolled in the class.
1. CS131 students number:# 2. CS132 students number:# 3. EE210 students number:# 4. EE310 students number:# 5. Math 320 students number:# 6. Math 220 students number:# |
Let the students add or drop a classes as they wish. When they choose E, exit the program.
6. For option D, show the courses in the student’s course list.
7. The hard part. Since your program will end when user chose option E. You need to keep track the classes been added and dropped by each students, and who are actually in the classes. Therefore, based on your experiences in lab 3 and 4, create a SI.txt (student info) to store the courses in each student’s course list. Create CI.txt (course info) to store all the students enrolled in each course. This will be executed in the background every time when the user chose option E: Exit. When you run your program, and create your course objects, this information needs to be read into each student and course object. When a student log into his or her account, the student should be able to see what courses is in the course list by chose option D. The number of students in each course also need to be displayed in Option A.
Student info
UD.txt
USERNAME,PASSWORD sdd233,Pad231 dcf987, BHYW4fw dgr803,Sb83d2d mkr212,UNNHS322 lki065,dgw6234 ped332,9891ds ytr876,dsid21kk nmh223,3282jd3d2
thanks for the question, here is the fully implemented code in python. All functions are commented so that you can understand what each function does. Let me know in case you have any doubts or questions.
======================================================================================
# student class starts here
class Student():
def
__init__(self,user_name=''):
self.__username=user_name
self.__courses=[]
def
addCourse(self,course):
self.__courses.append(course)
def
dropCourse(self,course_name):
for
course in self.__courses:
if course.getCourseName()==course_name:
self.__courses.remove(course)
print('Course successfully deleted')
return
def
getCourse(self):return self.__courses
def
getUserID(self):return self.__username
# Course class starts here
class Course():
def
__init__(self,course_name=''):
self.__coursename=course_name
self.__students=[]
def
addStudent(self,student):
self.__students.append(student)
def dropStudent(self,id):
for
student in self.__students:
if student.getUserID()==id:
self.__students.remove(student)
print('Student successfully deleted')
return
def
getStudents(self):return self.__students
def
getNumberOfStudents(self):return
len(self.__students)
def
getCourseName(self):return self.__coursename
# read from the UD.txt file all the ids and password and
returns as a dictionary
def readStudentIDPwdFromFile(filename):
user_id_dict={}
with
open(filename,'r') as
infile:
for
line in infile.readlines():
line =
line.strip().split(',')
user_id_dict[line[0].strip()]=line[1].strip()
return user_id_dict
# function which ask user to enter id and password and returns
True if id password matches
# else it returns false if the id dont exist or the password dont
match
def login(student_id_password):
id=input('Enter user id to login:
')
password = input('Enter password:
')
if student_id_password.get(id)
is None:return False
return
student_id_password.get(id)==password
# display menu and ask user to enter a choice adn returns the
choice back to main function
def display_menu():
print('A: Show all courses
available')
print('B: Add a course')
print('C: Drop a course')
print('D: Show all my
courses')
print('E: Exit')
choice=input('Enter choice:
')
return choice
# displays all the courses the student is enrolled
to
def displayAllCourses(courses):
for i in
range(len(courses)):
print('{0}. {1}
students number:
{2}'.format(i+1,courses[i].getCourseName(),courses[i].getNumberOfStudents()))
# function to add a course to the student object courses
list
def addCourse(student_Id, students,
courses):
course_name= input('Enter course name:
')
for student in
students:
if
student.getUserID()==student_Id:
for course in courses:
if course.getCourseName()==course_name:
student.addCourse(course)
print('Course successfully added')
return
print('Course not available.')
return
# function to remove a course from the student courses
list
def dropCourse(student_Id, students,
courses):
course_name= input('Enter course name:
')
for student in
students:
if
student.getUserID()==student_Id:
for course in courses:
if course.getCourseName()==course_name:
student.dropCourse(course_name)
return
print('Course not available.')
return
# show all the courses for the students
def showAllCourses(student_Id, students):
for student in
students:
if
student.getUserID()==student_Id:
courses = student.getCourse()
for course in courses:
print('Course Enrolled to:
{}'.format(course.getCourseName()))
# driver function
def main():
# reads the UD.txt file
student_id_password=readStudentIDPwdFromFile('D:\\UD.txt')
# contains all the student objects in a
list
students=[]
for student_Id
in student_id_password.keys():
students.append(Student(student_Id))
# contain all the courses object as a
list
courses=[]
courses.append(Course('CS131'))
courses.append(Course('CS132'))
courses.append(Course('EE210'))
courses.append(Course('EE310'))
courses.append(Course('Math320'))
courses.append(Course('Math220'))
if login(student_id_password)
is not True:
print('Invalid
login id/password entered. Program will terminate
now.')
return
while True:
choice=display_menu().upper()
if
choice=='A': displayAllCourses(courses)
elif
choice=='B':addCourse(student_Id,students,courses)
elif
choice=='C':dropCourse(student_Id,students,courses)
elif
choice=='D':showAllCourses(student_Id,students)
elif
choice=='E':
print('Thanks for using the program')
break
main()