//File: Box.java //Name: Trai Pham //Date: 03/02/2020 //Course: CSE 8B /** This code represents the storage system, in which it consists of a number of Box. Each Box has a number of position, positions are the elements within the Box. **/ import java.util.ArrayList; import java.util.List; public class PokemonStorageSystem{ private static final int MAX_BOXES = 8; private static final int MAX_ITEMS = 30; private static final String OUT_OF_BOUNDS_EXCEPTION = "Box: %d, Pos: %d"; private List> storage; private T partyMember; private int zero = 0; /** @param nothing @return nothing Constructor **/ public PokemonStorageSystem(){ //adding new boxes to the storage with a max size of MAX_ITEMS this.storage = new ArrayList>(); for(int i = 0; i < MAX_BOXES; i++){ this.storage.add(new Box(MAX_ITEMS)); } } /** @param T type @return nothing This sets the party member to that of a pokemon of type T **/ public void setPartyMember(T partyMember){ this.partyMember = partyMember; } public void deposit(T newPokemon) throws NoStorageSpaceException{ //iterates through the storage and check if there's an empty position in the //box for(int i = 0; i < MAX_BOXES; i++){ if(this.storage.get(i).deposit(newPokemon) == true){ return; } else{ throw new NoStorageSpaceException(); } } } /** @param int box, int pos @return T type This releases the pokemon within the storage System based on the box number and the position within the box **/ public T release(int box, int pos) throws OutOfBoundsException{ //Exception check if(box < zero || box >= MAX_BOXES || pos < zero || pos >= MAX_ITEMS){ throw new OutOfBoundsException(String.format( OUT_OF_BOUNDS_EXCEPTION, box, pos)); } else{ //creating a new T object that takes the pokemon from the specified box and //position T pokemon = this.storage.get(box).getPositionAtIndex(pos).getPokemon(); //setting the space of pokemon planning to be released to null this.storage.get(box).getPositionAtIndex(pos).setPokemon(null); return pokemon; } } /** @param int initial box, int initial position, int move to box, int move to position @return nothing Moves a pokemon at a specific box and position to another box and position. **/ public void move(int boxFrom, int posFrom, int boxTo, int posTo) throws OutOfBoundsException{ //checking the exceptions if(boxFrom < zero || boxFrom >= MAX_BOXES || posFrom < zero || posFrom >= MAX_ITEMS){ throw new OutOfBoundsException(String.format( OUT_OF_BOUNDS_EXCEPTION, boxFrom, posFrom)); } else if(boxTo < zero || boxTo >= MAX_BOXES || posTo < zero || posTo >= MAX_ITEMS){ throw new OutOfBoundsException(String.format( OUT_OF_BOUNDS_EXCEPTION, boxTo, posTo)); } else{ //saving the pokemons from the specify boxes and positions //this will help with switching the pokemons' location T pokemon1 = this.storage.get(boxFrom).getPositionAtIndex(posFrom).getPokemon(); T pokemon2 = this.storage.get(boxTo).getPositionAtIndex(posTo).getPokemon(); this.storage.get(boxFrom).getPositionAtIndex( posFrom).setPokemon(pokemon2); this.storage.get(boxTo).getPositionAtIndex( posTo).setPokemon(pokemon1); } } /** @param int a box number @return String a getter method that gets the user a specific box based on the box number and it returns the content of the box. **/ public String getBox(int boxNumber) throws OutOfBoundsException{ //Checking exception if(boxNumber < zero || boxNumber >= MAX_BOXES){ throw new OutOfBoundsException(String.format( OUT_OF_BOUNDS_EXCEPTION, boxNumber, zero)); } else{ //converting content of the box to that of a string return this.storage.get(boxNumber).toString(); } } }