Develop a python class named Enroll to simulate the course registration process. It should include (1) class attribute: enrollmentcount which counts the total number of enrollments, and initiates to 0 (2) static method named buildcourselist(filepath) that creates a course list from a text file that has such text Id Title Credit CSCI120 Elements of Computing 4 INFO230 Intro to Web Design 3 CSCI230 System Architecture 3 (the column separator has 2+ spaces, 列分隔用2+空格, file attached) The method should return a python list of Course objects (hint: A. open file for read, new a list, read line by line, for each line, create a Course object and insert into the list. Ignore the first line. B. a static method is also called class method, does not take the self argument ) (3) static method named buildstudentlist(filepath) that creates a Student list from a text file that has such text Id Name D10000000 Zhang Hong D10000001 Chen Huayue The method should return a python list of Student objects
时间: 2024-01-28 08:03:25 浏览: 91
Sure, here's an example implementation of the Enroll class in Python:
```python
class Course:
def __init__(self, course_id, title, credit):
self.course_id = course_id
self.title = title
self.credit = credit
class Student:
def __init__(self, student_id, name):
self.student_id = student_id
self.name = name
class Enroll:
enrollmentcount = 0
@staticmethod
def buildcourselist(filepath):
course_list = []
with open(filepath, 'r') as f:
lines = f.readlines()[1:] # Ignore header line
for line in lines:
data = line.strip().split(' ')
course = Course(data[0], data[1], int(data[2]))
course_list.append(course)
return course_list
@staticmethod
def buildstudentlist(filepath):
student_list = []
with open(filepath, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(' ')
student = Student(data[0], data[1])
student_list.append(student)
return student_list
```
This implementation defines the `Enroll` class with a class attribute `enrollmentcount` that counts the total number of enrollments. It also includes two static methods: `buildcourselist` and `buildstudentlist`, which read data from text files and create lists of `Course` and `Student` objects respectively.
To use this implementation, you can call the static methods on the `Enroll` class, for example:
```python
courses = Enroll.buildcourselist('courses.txt')
students = Enroll.buildstudentlist('students.txt')
```
Assuming that `courses.txt` and `students.txt` are text files in the format described in the question, this code will create lists of `Course` and `Student` objects that you can use for simulating the course registration process.
阅读全文