CSE-8B / PA8 / starter / Box.java
Box.java
Raw
//File: Box.java
//Name: Trai Pham
//Date: 03/02/2020
//Course: CSE 8B
/**
The Box within the storage system.
**/

import java.util.*;
import java.util.ArrayList;


public class Box<T>{
//constants to used in toString() method
  private static final String BORDER     = "---------------------";
  private static final String DIVIDER    = "|";
  private static final String NEW_LINE   = "\n";
  private static final String EMPTY_POS  = "   ";
  private static final int MAX_ELEM_LINE = 5;
//constant used in in an argumment to an exception
  private static final String OUT_OF_BOUNDS_EXCEPTION = "Index: %s";
//variables that defines the functionality of the box and stores Pokemon

//List of the Position of object T
  private List<Position<T>> boxElements;
  private int maxSize;
  private int zero = 0;

/**
@param int contains the max Size of the box
@return nothing
Constructor
**/
  public Box(int maxSize){
    this.maxSize = maxSize;
//adding empty spaces with the boxes
    this.boxElements = new ArrayList<>();
    for(int i = 0; i < this.maxSize; i++){
      Position<T> pos = new Position<T>(null);
      this.boxElements.add(pos);
    }
  }

/**
@param nothing
@return String
toString method
**/
  @Override
  public String toString() {
    int counter = 0;

    StringBuilder boxPrintout = new StringBuilder();
    boxPrintout.append(BORDER);

    // Iterate through each element, print 5 at most on a line
    for(Position<T> element : boxElements) {
        if(counter == 0) {
            boxPrintout.append(NEW_LINE);
            boxPrintout.append(DIVIDER);
        }

        // Print EMPTY_POS if the spot is free (null)
        T pokemon = element.getPokemon();
        if(element.isOpen()) {
            boxPrintout.append(EMPTY_POS);
        }
        else {
            boxPrintout.append(pokemon.toString());
        }
        boxPrintout.append(DIVIDER);

        counter++;

        // Used so we only have 5 elements at most on a line
        if(counter == MAX_ELEM_LINE) {
            boxPrintout.append(NEW_LINE);
            boxPrintout.append(BORDER);
            counter = 0;
        }
    }
    boxPrintout.append(NEW_LINE);

    return boxPrintout.toString();
  }

/**
@param object T that reflect that of a pokemon
@return boolean
adding a new pokemon into the box, in which this depends if there is an
empty Space
**/
  public boolean deposit(T newPokemon){
//Iterate through box and check if position is open/null
    for(int i = 0; i < this.maxSize; i++){
//Check if there is an open space at the position of the box
      if(this.boxElements.get(i).isOpen()){
//adds the pokemon to a position object
        Position<T> pokePosition = new Position<T>(newPokemon);
//adds new Pokemon to index of boxElements where Position is null/open
        this.boxElements.add(i, pokePosition);
        return true;
      }
    }
    return false;
  }

/**
@param int index
@return Position<T>
This method gets the elements within the speicified position/index that can be
found in the box.
**/
  public Position<T> getPositionAtIndex(int index) throws OutOfBoundsException{
    if(index < zero || index >= this.maxSize){
      throw new OutOfBoundsException(String.format(
        OUT_OF_BOUNDS_EXCEPTION, index));
    }
    else{
      return this.boxElements.get(index);
    }
  }
}