/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

package example;
import java.util.*;

// line 3 "../OneToMNTest.ump"
public class Mentor
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Mentor Attributes
  private String name;

  //Mentor Associations
  private List<Pupil> pupils;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public Mentor(String aName)
  {
    name = aName;
    pupils = new ArrayList<Pupil>();
  }

  //------------------------
  // INTERFACE
  //------------------------

  public boolean setName(String aName)
  {
    boolean wasSet = false;
    name = aName;
    wasSet = true;
    return wasSet;
  }

  public String getName()
  {
    return name;
  }
  /* Code from template association_GetMany */
  public Pupil getPupil(int index)
  {
    Pupil aPupil = pupils.get(index);
    return aPupil;
  }

  public List<Pupil> getPupils()
  {
    List<Pupil> newPupils = Collections.unmodifiableList(pupils);
    return newPupils;
  }

  public int numberOfPupils()
  {
    int number = pupils.size();
    return number;
  }

  public boolean hasPupils()
  {
    boolean has = pupils.size() > 0;
    return has;
  }

  public int indexOfPupil(Pupil aPupil)
  {
    int index = pupils.indexOf(aPupil);
    return index;
  }
  /* Code from template association_IsNumberOfValidMethod */
  public boolean isNumberOfPupilsValid()
  {
    boolean isValid = numberOfPupils() >= minimumNumberOfPupils() && numberOfPupils() <= maximumNumberOfPupils();
    return isValid;
  }
  /* Code from template association_MinimumNumberOfMethod */
  public static int minimumNumberOfPupils()
  {
    return 5;
  }
  /* Code from template association_MaximumNumberOfMethod */
  public static int maximumNumberOfPupils()
  {
    return 7;
  }
  /* Code from template association_AddMNToOnlyOne */
  public Pupil addPupil(String aNumber)
  {
    if (numberOfPupils() >= maximumNumberOfPupils())
    {
      return null;
    }
    else
    {
      return new Pupil(aNumber, this);
    }
  }

  public boolean addPupil(Pupil aPupil)
  {
    boolean wasAdded = false;
    if (pupils.contains(aPupil)) { return false; }
    if (numberOfPupils() >= maximumNumberOfPupils())
    {
      return wasAdded;
    }

    Mentor existingMentor = aPupil.getMentor();
    boolean isNewMentor = existingMentor != null && !this.equals(existingMentor);

    if (isNewMentor && existingMentor.numberOfPupils() <= minimumNumberOfPupils())
    {
      return wasAdded;
    }

    if (isNewMentor)
    {
      aPupil.setMentor(this);
    }
    else
    {
      pupils.add(aPupil);
    }
    wasAdded = true;
    return wasAdded;
  }

  public boolean removePupil(Pupil aPupil)
  {
    boolean wasRemoved = false;
    //Unable to remove aPupil, as it must always have a mentor
    if (this.equals(aPupil.getMentor()))
    {
      return wasRemoved;
    }

    //mentor already at minimum (5)
    if (numberOfPupils() <= minimumNumberOfPupils())
    {
      return wasRemoved;
    }
    pupils.remove(aPupil);
    wasRemoved = true;
    return wasRemoved;
  }
  /* Code from template association_AddIndexControlFunctions */
  public boolean addPupilAt(Pupil aPupil, int index)
  {  
    boolean wasAdded = false;
    if(addPupil(aPupil))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfPupils()) { index = numberOfPupils() - 1; }
      pupils.remove(aPupil);
      pupils.add(index, aPupil);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMovePupilAt(Pupil aPupil, int index)
  {
    boolean wasAdded = false;
    if(pupils.contains(aPupil))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfPupils()) { index = numberOfPupils() - 1; }
      pupils.remove(aPupil);
      pupils.add(index, aPupil);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addPupilAt(aPupil, index);
    }
    return wasAdded;
  }

  public void delete()
  {
    for(int i=pupils.size(); i > 0; i--)
    {
      Pupil aPupil = pupils.get(i - 1);
      aPupil.delete();
    }
  }


  public String toString()
  {
    return super.toString() + "["+
            "name" + ":" + getName()+ "]";
  }
}
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

package example;

// line 9 "../OneToMNTest.ump"
public class Pupil
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Pupil Attributes
  private String number;

  //Pupil Associations
  private Mentor mentor;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public Pupil(String aNumber, Mentor aMentor)
  {
    number = aNumber;
    boolean didAddMentor = setMentor(aMentor);
    if (!didAddMentor)
    {
      throw new RuntimeException("Unable to create pupil due to mentor. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
  }

  //------------------------
  // INTERFACE
  //------------------------

  public boolean setNumber(String aNumber)
  {
    boolean wasSet = false;
    number = aNumber;
    wasSet = true;
    return wasSet;
  }

  public String getNumber()
  {
    return number;
  }
  /* Code from template association_GetOne */
  public Mentor getMentor()
  {
    return mentor;
  }
  /* Code from template association_SetOneToAtMostN */
  public boolean setMentor(Mentor aMentor)
  {
    boolean wasSet = false;
    //Must provide mentor to pupil
    if (aMentor == null)
    {
      return wasSet;
    }

    //mentor already at maximum (7)
    if (aMentor.numberOfPupils() >= Mentor.maximumNumberOfPupils())
    {
      return wasSet;
    }
    
    Mentor existingMentor = mentor;
    mentor = aMentor;
    if (existingMentor != null && !existingMentor.equals(aMentor))
    {
      boolean didRemove = existingMentor.removePupil(this);
      if (!didRemove)
      {
        mentor = existingMentor;
        return wasSet;
      }
    }
    mentor.addPupil(this);
    wasSet = true;
    return wasSet;
  }

  public void delete()
  {
    Mentor placeholderMentor = mentor;
    this.mentor = null;
    if(placeholderMentor != null)
    {
      placeholderMentor.removePupil(this);
    }
  }


  public String toString()
  {
    return super.toString() + "["+
            "number" + ":" + getNumber()+ "]" + System.getProperties().getProperty("line.separator") +
            "  " + "mentor = "+(getMentor()!=null?Integer.toHexString(System.identityHashCode(getMentor())):"null");
  }
}