#PLEASE DO NOT EDIT THIS CODE #This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language! # line 9 "../ManyToNTest.ump" class Student(): #------------------------ # MEMBER VARIABLES #------------------------ #Student Attributes #Student Associations #------------------------ # CONSTRUCTOR #------------------------ def __init__(self, aNumber): self._mentors = None self._number = None self._number = aNumber self._mentors = [] #------------------------ # INTERFACE #------------------------ def setNumber(self, aNumber): wasSet = False self._number = aNumber wasSet = True return wasSet def getNumber(self): return self._number # Code from template association_GetMany def getMentor(self, index): aMentor = self._mentors[index] return aMentor def getMentors(self): newMentors = tuple(self._mentors) return newMentors def numberOfMentors(self): self._number = len(self._mentors) return self._number def hasMentors(self): has = len(self._mentors) > 0 return has def indexOfMentor(self, aMentor): index = (-1 if not aMentor in self._mentors else self._mentors.index(aMentor)) return index # Code from template association_MinimumNumberOfMethod @staticmethod def minimumNumberOfMentors(): return 0 # Code from template association_AddManyToManyMethod def addMentor(self, aMentor): wasAdded = False if (aMentor) in self._mentors : return False self._mentors.append(aMentor) if aMentor.indexOfStudent(self) != -1 : wasAdded = True else : wasAdded = aMentor.addStudent(self) if not wasAdded : self._mentors.remove(aMentor) return wasAdded # Code from template association_RemoveMany def removeMentor(self, aMentor): wasRemoved = False if not (aMentor) in self._mentors : return wasRemoved oldIndex = (-1 if not aMentor in self._mentors else self._mentors.index(aMentor)) self._mentors.remove(oldIndex) if aMentor.indexOfStudent(self) == -1 : wasRemoved = True else : wasRemoved = aMentor.removeStudent(self) if not wasRemoved : self._mentors.insert(oldIndex, aMentor) return wasRemoved # Code from template association_AddIndexControlFunctions def addMentorAt(self, aMentor, index): wasAdded = False if self.addMentor(aMentor) : if index < 0 : index = 0 if index > self.numberOfMentors() : index = self.numberOfMentors() - 1 self._mentors.remove(aMentor) self._mentors.insert(index, aMentor) wasAdded = True return wasAdded def addOrMoveMentorAt(self, aMentor, index): wasAdded = False if (aMentor) in self._mentors : if index < 0 : index = 0 if index > self.numberOfMentors() : index = self.numberOfMentors() - 1 self._mentors.remove(aMentor) self._mentors.insert(index, aMentor) wasAdded = True else : wasAdded = self.addMentorAt(aMentor, index) return wasAdded def delete(self): from Mentor import Mentor copyOfMentors = self._mentors.copy() self._mentors.clear() for aMentor in copyOfMentors: if aMentor.numberOfStudents() <= Mentor.minimumNumberOfStudents() : aMentor.delete() else : aMentor.removeStudent(self) def __str__(self): return str(super().__str__()) + "[" + "number" + ":" + str(self.getNumber()) + "]" #PLEASE DO NOT EDIT THIS CODE #This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language! # line 3 "../ManyToNTest.ump" class Mentor(): #------------------------ # MEMBER VARIABLES #------------------------ #Mentor Attributes #Mentor Associations #------------------------ # CONSTRUCTOR #------------------------ def __init__(self, aName, *allStudents): allStudents = list(allStudents) self._students = None self._name = None self._name = aName self._students = [] didAddStudents = self.setStudents(*allStudents) if not didAddStudents : raise RuntimeError ("Unable to create Mentor, must have 2 students. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html") #------------------------ # INTERFACE #------------------------ def setName(self, aName): wasSet = False self._name = aName wasSet = True return wasSet def getName(self): return self._name # Code from template association_GetMany def getStudent(self, index): aStudent = self._students[index] return aStudent def getStudents(self): newStudents = tuple(self._students) return newStudents def numberOfStudents(self): number = len(self._students) return number def hasStudents(self): has = len(self._students) > 0 return has def indexOfStudent(self, aStudent): index = (-1 if not aStudent in self._students else self._students.index(aStudent)) return index # Code from template association_IsNumberOfValidMethod def isNumberOfStudentsValid(self): isValid = self.numberOfStudents() >= Mentor.minimumNumberOfStudents() and self.numberOfStudents() <= Mentor.maximumNumberOfStudents() return isValid # Code from template association_RequiredNumberOfMethod @staticmethod def requiredNumberOfStudents(): return 2 # Code from template association_MinimumNumberOfMethod @staticmethod def minimumNumberOfStudents(): return 2 # Code from template association_MaximumNumberOfMethod @staticmethod def maximumNumberOfStudents(): return 2 # Code from template association_AddManyToManyMethod def addStudent(self, aStudent): wasAdded = False if (aStudent) in self._students : return False if self.numberOfStudents() >= Mentor.maximumNumberOfStudents() : return wasAdded self._students.append(aStudent) if aStudent.indexOfMentor(self) != -1 : wasAdded = True else : wasAdded = aStudent.addMentor(self) if not wasAdded : self._students.remove(aStudent) return wasAdded # Code from template association_AddMNToMany def removeStudent(self, aStudent): wasRemoved = False if not (aStudent) in self._students : return wasRemoved if self.numberOfStudents() <= Mentor.minimumNumberOfStudents() : return wasRemoved oldIndex = (-1 if not aStudent in self._students else self._students.index(aStudent)) self._students.remove(oldIndex) if aStudent.indexOfMentor(self) == -1 : wasRemoved = True else : wasRemoved = aStudent.removeMentor(self) if not wasRemoved : self._students.insert(oldIndex, aStudent) return wasRemoved # Code from template association_SetMNToMany def setStudents(self, *newStudents): newStudents = list(newStudents) wasSet = False verifiedStudents = [] for aStudent in newStudents: if (aStudent) in verifiedStudents : continue verifiedStudents.append(aStudent) if len(verifiedStudents) != len(newStudents) or len(verifiedStudents) < Mentor.minimumNumberOfStudents() or len(verifiedStudents) > Mentor.maximumNumberOfStudents() : return wasSet oldStudents = self._students.copy() self._students.clear() for aNewStudent in verifiedStudents: self._students.append(aNewStudent) if (aNewStudent) in oldStudents : oldStudents.remove(aNewStudent) else : aNewStudent.addMentor(self) for anOldStudent in oldStudents: anOldStudent.removeMentor(self) wasSet = True return wasSet def delete(self): copyOfStudents = self._students.copy() self._students.clear() for aStudent in copyOfStudents: aStudent.removeMentor(self) def __str__(self): return str(super().__str__()) + "[" + "name" + ":" + str(self.getName()) + "]"