Class StudentContainer

java.lang.Object
it.polimi.ingsw.eriantys.model.StudentContainer
Direct Known Subclasses:
Bag, ContainerCharacterCard, DiningRoom, IslandGroup

public class StudentContainer extends Object
This class provides a container for student discs. It is meant to model every game object on which student discs can be placed. It can be used as is, or it can be extended in order to include additional methods and attributes. It exposes various methods to move students between two StudentContainer objects and a method returning the number of students of a given Color in the container.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a StudentContainer with maximum size equal to the total number of student discs in the game (130).
    StudentContainer(int maxSize)
    Constructs an empty StudentContainer with the specified maximum size.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    A method to state if the object has no students.
    protected void
    Fills the container with MAX_STUDENTS_PER_COLOR (26) students of each color.
    int
    A getter for the amount of students of color color currently in the container.
    A helper-getter method to fulfill the BoardStatus creation process.
    void
    Moves all the students from this to the destination container dest.
    void
    moveTo(StudentContainer dest, int amount)
    Moves amount students of random colors from this to the destination container dest.
    void
    Moves a student of color color from this to the destination container dest.
    void
    Refills this to its maximum capacity by picking random students from source.
    protected int
    Returns the container's remaining capacity for the specified Color, an integer between 0 and MAX_STUDENTS_PER_COLOR, or -1 if color is null.
    void
    swap(StudentContainer that, Color thisColor, Color thatColor)
    Swaps two student discs between this and that: one student of color thisColor is removed from this and added to that, and one student of color thatColor is removed from that and added to this.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StudentContainer

      public StudentContainer()
      Constructs a StudentContainer with maximum size equal to the total number of student discs in the game (130). This constructor is to be used only to construct a StudentContainer with no specified maximum size.
    • StudentContainer

      public StudentContainer(int maxSize)
      Constructs an empty StudentContainer with the specified maximum size. The size has an upper bound of 130.
      Parameters:
      maxSize - the maximum number of students allowed in the container at any moment.
  • Method Details

    • getQuantity

      public int getQuantity(Color color)
      A getter for the amount of students of color color currently in the container.
      Parameters:
      color - the color of students whose amount is requested.
      Returns:
      the amount of students of color color currently in the container, or -1 if color is null
    • moveTo

      public void moveTo(StudentContainer dest, Color color) throws InvalidArgumentException, NoMovementException
      Moves a student of color color from this to the destination container dest. No movement occurs if this is empty or the destination container has no remaining capacity.
      Parameters:
      dest - the destination StudentContainer
      color - the Color of the student to be moved
      Throws:
      InvalidArgumentException - if any argument is null
      NoMovementException - if this is empty or the destination container is full
    • moveTo

      public void moveTo(StudentContainer dest, int amount) throws InvalidArgumentException, NoMovementException
      Moves amount students of random colors from this to the destination container dest. Students are moved one by one until amount is reached or no colors that can be both removed from this and added to dest can be found, at which point no subsequent movements occur.
      Parameters:
      dest - the destination StudentContainer
      amount - the amount of students to be moved
      Throws:
      InvalidArgumentException - if dest is null
      NoMovementException - if no colors that can be both removed from this and added to dest can be found
    • moveAllTo

      public void moveAllTo(StudentContainer dest) throws InvalidArgumentException, NoMovementException
      Moves all the students from this to the destination container dest. Students are moved color by color until the source container this is empty or the destination container has no remaining capacity, at which point no subsequent movements occur. Generally, the source container this should be emptied as a result of this method.
      Parameters:
      dest - the destination StudentContainer
      Throws:
      InvalidArgumentException - if dest is null
      NoMovementException - if this contains more students than the destination container can add
    • refillFrom

      public void refillFrom(StudentContainer source) throws InvalidArgumentException, NoMovementException
      Refills this to its maximum capacity by picking random students from source. Students are moved one by one until this is full or source is empty, at which point no subsequent movements occur.
      Parameters:
      source - the source StudentContainer
      Throws:
      InvalidArgumentException - if source is null
      NoMovementException - if source is emptied before this can be filled
      See Also:
    • swap

      public void swap(StudentContainer that, Color thisColor, Color thatColor) throws InvalidArgumentException, NoMovementException
      Swaps two student discs between this and that: one student of color thisColor is removed from this and added to that, and one student of color thatColor is removed from that and added to this. If any container does not have enough students or enough capacity to perform the swap, the operation is aborted.
      Parameters:
      that - the second StudentContainer
      thisColor - the Color of the student being moved from this to that
      thatColor - the Color of the student being moved from that to this
      Throws:
      InvalidArgumentException - if any argument is null
      NoMovementException - if any container does not have enough students or enough capacity to perform the swap
    • getRepresentation

      public Map<String,Integer> getRepresentation()
      A helper-getter method to fulfill the BoardStatus creation process.
      Returns:
      a representation for the object, ordered by Color enum declarations
    • remainingCapacity

      protected int remainingCapacity(Color color)
      Returns the container's remaining capacity for the specified Color, an integer between 0 and MAX_STUDENTS_PER_COLOR, or -1 if color is null.
      Parameters:
      color - the color whose remaining capacity is returned.
      Returns:
      the container's remaining capacity for the specified Color, an integer between 0 and MAX_STUDENTS_PER_COLOR, or -1 if color is null.
      See Also:
      • remainingCapacity()
    • fill

      protected void fill()
      Fills the container with MAX_STUDENTS_PER_COLOR (26) students of each color. This method is only to be called while constructing a Bag object.
    • empty

      protected boolean empty()
      A method to state if the object has no students.
      Returns:
      true if and only if the container is empty