Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Föreläsning 7 Klasser och annat: ett exempel. 2 Klassen Course class Course(object): def __init__(self, code, name, points): self.code = code self.name.

Similar presentations


Presentation on theme: "1 Föreläsning 7 Klasser och annat: ett exempel. 2 Klassen Course class Course(object): def __init__(self, code, name, points): self.code = code self.name."— Presentation transcript:

1 1 Föreläsning 7 Klasser och annat: ett exempel

2 2 Klassen Course class Course(object): def __init__(self, code, name, points): self.code = code self.name = name self.points = points def shortString(self): return "%s %s %s hp" % \ (self.code, self.name, self.points)

3 3 __str__-metoden class Course(object): …… def __str__(self): return self.shortString() # Create a course prgi = Course("DD1312", "Programmeringsteknik och Matlab", 8)

4 4 Klassen Program class Program(object): def __init__(self, name, code, points): self.name = name self.code = code self.points = points self.compulsoryCourses = [] def shortString(self): return "%s (%s) %d hp" % \ (self.name, self.code, self.points)

5 5 En hjälpfunktion def makeInfoString(myList): infoString = "" for element in myList: infoString += \ element.shortString() + "\n" return infoString

6 6 Klassen Program def __str__(self): infoString = self.shortString() infoString += \ "\nCompulsory courses:\n" + \ makeInfoString(self.compulsoryCourses) return infoString def addCompulsoryCourse(self, course): self.compulsoryCourses.append(course)

7 7 Klassen CourseClass class CourseClass(object): def __init__(self, course, year, period, teacher): self.course = course self.year = year self.period = period self.teacher = teacher self.students = [] def shortString(self): return self.course.shortString() + \ " %s %s given by:\n%s" % \ (self.period, self.year, self.teacher)

8 8 Klassen CourseClass def __str__(self): infoString = self.shortString() infoString += \ "\nThe class has the “ + \ ”following students:\n“ infoString += \ makeInfoString(self.students) return infoString def addStudent(self, student): self.students.append(student)

9 9 Klassen Person class Person(object): def __init__(self, name, surname, persnr, address, postalcode, city, country = "Sweden", = None): self.name = name self.surname = surname self.persnr = persnr errorString = "Not a valid personnummer! " + \ "(checksum should be %d)" % self.persnrChecksum() if not self.verifyPersnr(): raise ValueError(errorString) self.address = address self.postalcode = postalcode self.city = city self.country = country self. =

10 10 Klassen Person def __str__(self): infoString = "Name:\t%s %s\nPersnr:\t%s\n" % \ (self.name, self.surname, self.persnr) infoString += "Address:\n\t%s\n\t%s %s\n\t%s" % \ (self.address, self.postalcode, self.city, self.country) if self. != None: infoString += "\n %s\n" % self. return infoString def getBirthYear(self): birthDigits = self.persnr[0:2] birthYear = int(birthDigits) return birthYear

11 11 Klassen Person def getAge(self): dateToday = datetime.date.today() thisYear = dateToday.year return thisYear - self.getBirthYear() def getGender(self): genderNumber = int(self.persnr[9]) % 2 if genderNumber == 1: return "male“ else: return "female"

12 12 Klassen Person def sumDigitsInStr(str): sum = 0 for d in str: sum += int(d) return sum sumDigitsInStr = staticmethod(sumDigitsInStr) def persnrChecksum(self): noDashContrPersnr = self.persnr[0:6] + self.persnr[7:10] expandedString = "" for i in range(9): if i % 2 == 0: expandedString += str(2 * int(noDashContrPersnr[i])) else: expandedString += noDashContrPersnr[i] return Person.sumDigitsInStr(expandedString) % 10

13 13 Klassen Person def verifyPersnr(self): if len(self.persnr) != 11 \ or self.persnr[6] != "-": return false try: int(self.persnr[0:6]) int(self.persnr[7:11]) except: return false return self.persnrChecksum()\ == int(self.persnr[10])

14 14 Klassen Teacher class Teacher(Person): def __init__(self, name, surname, persnr, room, address, postalcode, city, country = "Sweden"): super(Teacher, self).__init__(name, surname, persnr, address, postalcode, city, country) self.room = room def __str__(self): infoString = super(Teacher, self).__str__() infoString += "\nRoom:\t%s" % self.room return infoString

15 15 Klassen Student class Student(Person): def __init__(self, name, surname, persnr, address, postalcode, city, country = "Sweden", = None): super(Student, self).__init__(name, surname, persnr, address, postalcode, city, country, ) self.currentCourseClasses = [] self.finishedCourseClasses = []

16 16 Klassen Student def __str__(self): infoString = \ super(Student, self).__str__() infoString += "\n\nFinished courses:\n“ infoString += \ makeInfoString(self.finishedCourseClasses) infoString += "\nCurrent courses:\n“ infoString +=\ makeInfoString(self.currentCourseClasses) return infoString

17 17 Klassen Student def addCourseClass(self, courseClass): self.currentCourseClasses.append(courseClass) courseClass.addStudent(self) def finishCourseClass(self, code): for courseClass in self.currentCourseClasses: if courseClass.course.code == code: self.finishedCourseClasses.\ append(courseClass) self.currentCourseClasses.\ remove(courseClass) return

18 18 Klassen Student def getPoints(self): points = 0 for courseClass in self.finishedCourseClasses: points += courseClass.course.points return points

19 19 Att använda klasserna from student import Program, Course, CourseClass, Person, Student, Teacher # Create program indEk = Program("Industriell ekonomi", "I", 180) # Create some courses prgi = Course("DD1312", "Programmeringsteknik och Matlab", 8) envarre = Course("MM0001", "Analys i en variabel", 10) makro = Course("EE0034", "Makroekonomi", 10) print prgi print "“ indEk.addCompulsoryCourse(prgi) indEk.addCompulsoryCourse(envarre) indEk.addCompulsoryCourse(makro) print indEk

20 20 Att använda klasserna # Create a person sven = Person("Sven", "Svensson", " ","Teknikhuset 4", "00000", "Stockholm") print sven print "Age: %d" % sven.getAge() print "Gender: %s" % sven.getGender() print "Birth year: %d" %\ sven.getBirthYear() print ""

21 21 Att använda klasserna # Create some teachers douglas = Teacher("Douglas", "Wikström", " ", "1443","Teknikgatan 23", "12345", "Stockholm") micke = Teacher("Mikael", "Goldmann", " ", "1444", "Teknikvägen 5", "54321", "Stockholm") stina = Teacher("Stina", "Bengtsson", " ", "1447", "Teknikvägen 7", "21123", "Stockholm") print douglas print "" print micke print "" print stina print ""

22 22 Att använda klasserna # Create some course classes prgi07 = CourseClass(prgi, 2007, "ht", douglas) makro06 = CourseClass(makro, 2006, "vt", micke) makro05 = CourseClass(makro, 2005, "vt", douglas) envarre07 = CourseClass(envarre, 2007, "ht", stina)

23 23 Att använda klasserna # Create some students olle = Student("Olle", "Olsson", " ", "Ollevägen 7", "67432", "Nacka") lisa = Student("Lisa", "Ek", " ", "Klasvägen 5", "62789", "Örby") olle.addCourseClass(prgi07) olle.addCourseClass(makro05) lisa.addCourseClass(prgi07) lisa.addCourseClass(makro06) lisa.addCourseClass(envarre07) print prgi07 print "" lisa.finishCourseClass("DD1312") lisa.finishCourseClass("MM0001") print lisa print ""


Download ppt "1 Föreläsning 7 Klasser och annat: ett exempel. 2 Klassen Course class Course(object): def __init__(self, code, name, points): self.code = code self.name."

Similar presentations


Ads by Google