# -*- coding: utf-8 -*- __author__ = 'willems' class Course(object): """ Represents a course identified by a course name and a list of students. """ def __init__(self, coursename): """ Creates a new course with the given name. Keyword arguments: course_name : the course's name """ self.__coursename = coursename; self.__participants_list = []; def getName(self): """ Returns the course's name. """ return self.__coursename; def setName(self, coursename): """ Sets the course's name to the given name. """ self.__coursename = coursename; def getParticipantsList(self): """ Returns the participants list. """ return self.__participants_list; def addParticipant(self, participant): """ Adds a participant to the course's list. """ self.__participants_list.append(participant); def removeParticipant(self, participantName): """ Removes the given participant from the list. """ self.__participants_list.remove(participantName); class CoursesList(object): """ Represents a list to contain Courses with some methods to make its use more comfortable. """ def __init__(self): """ Creates a courses list. No need for a distinguishing id so far. """ self.__coursesList = []; def getCoursesList(self): return self.__coursesList; def addCourse(self, course): """ Adds the given course to the courses list. """ self.__coursesList.append(course); def getCourse(self, courseName): """ Searches the courses list for the given course name and returns the matching course. Otherwise returns None. """ for tmpCourse in self.__coursesList: if courseName == tmpCourse.getName(): return tmpCourse; def removeCourse(self, courseName): """ Removes the given course from the courses list. """ self.__coursesList.remove(courseName); class Student(object): """ Represents a student. """ """ The global number of students. """ __numberOfStudents = 0; """ A list containing all created courses. """ __courses_list = CoursesList.CoursesList(); def __init__(self, surname, last_name, date_of_birth): """ Creates a student object with the given initial values. """ self.__surname = surname; self.__last_name = last_name; self.__date_of_birth = date_of_birth; Student.__increment_number_of_students(); def getSurname(self): return self.__surname; def setSurname(self, surname): self.__surname = surname; def getLastname(self): return self.__last_name; def setLastname(self, lastname): self.__last_name = lastname; def getDateOfBirth(self): return self.__date_of_birth; def setDateOfBirth(self, date): self.__date_of_birth = date; def printDetails(self): result = self.getSurname(); result += " "; result += self.getLastname(); result += ", "; result += self.getDateOfBirth(); print(result); def enroll(self, course): """ Enrolls a student to the given course. """ if Student.__courseExists(course) is not True: Student.__createCourse(course); Student.__enlist(self, course); @staticmethod def __courseExists(coursename): """ Checks if a course exists and then returns True, otherwise False. """ coursesList = Student.__courses_list.getCoursesList(); for tmpCourse in coursesList: if tmpCourse.getName() == coursename: return True; #print("Course not existing") return False; @staticmethod def __createCourse(courseName): """ Creates a new course with the given name and adds it to the courses list. """ course = Course.Course(courseName); Student.__courses_list.addCourse(course); @staticmethod def __enlist(participant, course): """ Adds the given participant to the given course. """ Student.__courses_list.getCourse(course).addParticipant(participant); def printNoOfStudents(self): """ Prints the total number of students to stdout. """ print(Student.__numberOfStudents); def printCourseParticipants(self, coursename): """ Prints the participants of the given course to stdout. """ tmpCourse = Student.__courses_list.getCourse(coursename); tmpList = tmpCourse.getParticipantsList(); print("Participants of course: " + coursename); for participant in tmpList: participant.printDetails(); @staticmethod def __increment_number_of_students(): Student.__numberOfStudents += 1; @staticmethod def __decrement_number_of_students(): Student.__numberOfStudents -= 1;
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question