#PLEASE DO NOT EDIT THIS CODE
#This code was generated using the UMPLE 1.33.0.6934.a386b0a58 modeling language!
# line 3 "../OneToMNTest.ump"

class Mentor():
    #------------------------
    # MEMBER VARIABLES
    #------------------------
    #Mentor Attributes
    #Mentor Associations
    #------------------------
    # CONSTRUCTOR
    #------------------------
    def __init__(self, aName):
        self._pupils = None
        self._name = None
        self._name = aName
        self._pupils = []

    #------------------------
    # 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 getPupil(self, index):
        aPupil = self._pupils[index]
        return aPupil

    def getPupils(self):
        newPupils = tuple(self._pupils)
        return newPupils

    def numberOfPupils(self):
        number = len(self._pupils)
        return number

    def hasPupils(self):
        has = len(self._pupils) > 0
        return has

    def indexOfPupil(self, aPupil):
        index = (-1 if not aPupil in self._pupils else self._pupils.index(aPupil))
        return index

    # Code from template association_IsNumberOfValidMethod 
    def isNumberOfPupilsValid(self):
        isValid = self.numberOfPupils() >= Mentor.minimumNumberOfPupils() and self.numberOfPupils() <= Mentor.maximumNumberOfPupils()
        return isValid

    # Code from template association_MinimumNumberOfMethod 
    @staticmethod
    def minimumNumberOfPupils():
        return 5

    # Code from template association_MaximumNumberOfMethod 
    @staticmethod
    def maximumNumberOfPupils():
        return 7

    # Code from template association_AddMNToOnlyOne 
    def addPupil1(self, aNumber):
        from Pupil import Pupil
        if self.numberOfPupils() >= Mentor.maximumNumberOfPupils() :
            return None
        else :
            return Pupil(aNumber, self)

    def addPupil2(self, aPupil):
        wasAdded = False
        if (aPupil) in self._pupils :
            return False
        if self.numberOfPupils() >= Mentor.maximumNumberOfPupils() :
            return wasAdded
        existingMentor = aPupil.getMentor()
        isNewMentor = not (existingMentor is None) and not self == existingMentor
        if isNewMentor and existingMentor.numberOfPupils() <= Mentor.minimumNumberOfPupils() :
            return wasAdded
        if isNewMentor :
            aPupil.setMentor(self)
        else :
            self._pupils.append(aPupil)
        wasAdded = True
        return wasAdded

    def removePupil(self, aPupil):
        wasRemoved = False
        #Unable to remove aPupil, as it must always have a mentor
        if self == aPupil.getMentor() :
            return wasRemoved
        #mentor already at minimum (5)
        if self.numberOfPupils() <= Mentor.minimumNumberOfPupils() :
            return wasRemoved
        self._pupils.remove(aPupil)
        wasRemoved = True
        return wasRemoved

    # Code from template association_AddIndexControlFunctions 
    def addPupilAt(self, aPupil, index):
        wasAdded = False
        if self.addPupil(aPupil) :
            if index < 0 :
                index = 0
            if index > self.numberOfPupils() :
                index = self.numberOfPupils() - 1
            self._pupils.remove(aPupil)
            self._pupils.insert(index, aPupil)
            wasAdded = True
        return wasAdded

    def addOrMovePupilAt(self, aPupil, index):
        wasAdded = False
        if (aPupil) in self._pupils :
            if index < 0 :
                index = 0
            if index > self.numberOfPupils() :
                index = self.numberOfPupils() - 1
            self._pupils.remove(aPupil)
            self._pupils.insert(index, aPupil)
            wasAdded = True
        else :
            wasAdded = self.addPupilAt(aPupil, index)
        return wasAdded

    def delete(self):
        i = len(self._pupils)
        while i > 0 :
            aPupil = self._pupils[i - 1]
            aPupil.delete()
            i -= 1

    def __str__(self):
        return str(super().__str__()) + "[" + "name" + ":" + str(self.getName()) + "]"

    def addPupil(self, *argv):
        from Pupil import Pupil
        if len(argv) == 1 and isinstance(argv[0], str) :
            return self.addPupil1(argv[0])
        if len(argv) == 1 and isinstance(argv[0], Pupil) :
            return self.addPupil2(argv[0])
        raise TypeError("No method matches provided parameters")


#PLEASE DO NOT EDIT THIS CODE
#This code was generated using the UMPLE 1.33.0.6934.a386b0a58 modeling language!
# line 9 "../OneToMNTest.ump"
import os

class Pupil():
    #------------------------
    # MEMBER VARIABLES
    #------------------------
    #Pupil Attributes
    #Pupil Associations
    #------------------------
    # CONSTRUCTOR
    #------------------------
    def __init__(self, aNumber, aMentor):
        self._mentor = None
        self._number = None
        self._number = aNumber
        didAddMentor = self.setMentor(aMentor)
        if not didAddMentor :
            raise RuntimeError ("Unable to create pupil due to mentor. See http://manual.umple.org?RE002ViolationofAssociationMultiplicity.html")

    #------------------------
    # INTERFACE
    #------------------------
    def setNumber(self, aNumber):
        wasSet = False
        self._number = aNumber
        wasSet = True
        return wasSet

    def getNumber(self):
        return self._number

    # Code from template association_GetOne 
    def getMentor(self):
        return self._mentor

    # Code from template association_SetOneToAtMostN 
    def setMentor(self, aMentor):
        from Mentor import Mentor
        wasSet = False
        #Must provide mentor to pupil
        if aMentor is None :
            return wasSet
        #mentor already at maximum (7)
        if aMentor.numberOfPupils() >= Mentor.maximumNumberOfPupils() :
            return wasSet
        existingMentor = self._mentor
        self._mentor = aMentor
        if not (existingMentor is None) and not existingMentor == aMentor :
            didRemove = existingMentor.removePupil(self)
            if not didRemove :
                self._mentor = existingMentor
                return wasSet
        self._mentor.addPupil(self)
        wasSet = True
        return wasSet

    def delete(self):
        placeholderMentor = self._mentor
        self._mentor = None
        if not (placeholderMentor is None) :
            placeholderMentor.removePupil(self)

    def __str__(self):
        return str(super().__str__()) + "[" + "number" + ":" + str(self.getNumber()) + "]" + str(os.linesep) + "  " + "mentor = " + ((format(id(self.getMentor()), "x")) if not (self.getMentor() is None) else "null")