* * TankFarmException.java * * Version: * $Id$ * * Revisions: * $Log$ */ import java.io.Serializable; /** * This class defines the object to be thrown by a TankFarm object when it * detects a constructor error. * * @author Ryan Modica */ public class TankFarmException{ // Constructor for the TankFarmException class public TankFarmException( String message ) { super(); } } // TankFarmException /* * TankFarm.java * * Version: * $Id: TankFarm.java,v 1.1 2007/08/16 19:10:01 vcss232 Exp rpm5732 $ * * Revisions: * $Log: TankFarm.java,v $ * Revision 1.1 2007/08/16 19:10:01 vcss232 * Initial revision * */ import java.lang.*; /** * A tank farm is a collection of tanks each having a different capacity. * The number of tanks and size of each one are provided as parameters to the * tank farm constructor. Tanks are maintained in the array tanks. The id * for a tank corresponds to the array index for the tank. * * @author Jim Vallino * */ public class TankFarm extends TankFarmException{ private Tank[] tanks; // the actual tank farm /** * Create a tank farm with the specified number of tanks having * the capacities specified in the cap parameter. * * @param howMany Number of tanks in tank farm * @param cap Array of capacities for each tank in tank farm * * @exception TankFarmException Thrown if the number of tanks in the * tank farm (i.e., the parameter howMany) * is less than 1. * Exception message field is: * "TankFarm constructor received 0 or * negative count." * @exception TankCreateException Thrown by the Tank constructor if an * invalid capacity is requested for a * tank. If the TankFarm receives this * exception the constructor will print the * exception message field to stderr and * rethrow the exception. */ public TankFarm( int howMany, int cap[] ) throws TankFarmException { // throw exception if( howMany < 1 ) { throw new TankFarmException( "TankFarm constructor received 0 or" + " received negative count." ); } // Allocate the actual array of tanks. tanks = new Tank[ howMany ]; // Initialize the tank array with new tanks. for ( int i = 0; i < howMany; i++ ) { tanks[ i ] = new Tank( i, cap[ i ] ); } } /** * Request to add material to a tank in the tank farm. *

* The operation of adding material to the specified tank is performed by * the tank farm object. TankWarningExceptions are handled by printing * the exception message to stderr. Other exceptions are allowed to pass up * the call stack. * * @param id Tank id for the tank in which new material will be added * @param in Number of units of material to add to tank * * @exception TankOverflowException Passed up the call stack if thrown * by Tank object. */ public void addMaterial( int id, int in ) throws TankOverflowException { tanks[ id ].fill( in ); // throw exception if( id > in ){ throw new TankOverflowException(); } } /** * Request to remove material from a tank in the tank farm. *

* The operation of removing material from the specified tank is performed * by the tank farm object. TankWarningExceptions are handled by printing * the exception message to stderr. * * @param id Tank id for the tank from which material will be removed * @param out Number of units of material to remove from the tank */ public void removeMaterial( int id, int out ) { tanks[ id ].empty( out ); } /** * Accessor function for tank level. * * @param id Id of the tank whose level will be returned. No check is * made that the parameter is valid. * * @return returns the current level of the tank. */ public int getTankLevel( int id ) { return tanks[ id ].getCurrent(); } /** * Accessor function for tank state. * * @param id Id of the tank whose state will be returned. No check is * made that the parameter is valid. * * @return returns the current state of the tank. */ public int getTankState( int id ) { return tanks[ id ].getState(); } public static void main( String[] args ) { TankFarm test = new TankFarm( 4, 6 ); } } // TankFarm /* * TankFarm.java * * Version: * $Id: TankFarm.java,v 1.1 2007/08/16 19:10:01 vcss232 Exp rpm5732 $ * * Revisions: * $Log: TankFarm.java,v $ * Revision 1.1 2007/08/16 19:10:01 vcss232 * Initial revision * */ import java.lang.*; /** * A tank farm is a collection of tanks each having a different capacity. * The number of tanks and size of each one are provided as parameters to the * tank farm constructor. Tanks are maintained in the array tanks. The id * for a tank corresponds to the array index for the tank. * * @author Jim Vallino * */ public class TankFarm extends TankFarmException{ private Tank[] tanks; // the actual tank farm /** * Create a tank farm with the specified number of tanks having * the capacities specified in the cap parameter. * * @param howMany Number of tanks in tank farm * @param cap Array of capacities for each tank in tank farm * * @exception TankFarmException Thrown if the number of tanks in the * tank farm (i.e., the parameter howMany) * is less than 1. * Exception message field is: * "TankFarm constructor received 0 or * negative count." * @exception TankCreateException Thrown by the Tank constructor if an * invalid capacity is requested for a * tank. If the TankFarm receives this * exception the constructor will print the * exception message field to stderr and * rethrow the exception. */ public TankFarm( int howMany, int cap[] ) throws TankFarmException { // throw exception if( howMany < 1 ) { throw new TankFarmException( "TankFarm constructor received 0 or" + " received negative count." ); } // Allocate the actual array of tanks. tanks = new Tank[ howMany ]; // Initialize the tank array with new tanks. for ( int i = 0; i < howMany; i++ ) { tanks[ i ] = new Tank( i, cap[ i ] ); } } /** * Request to add material to a tank in the tank farm. *

* The operation of adding material to the specified tank is performed by * the tank farm object. TankWarningExceptions are handled by printing * the exception message to stderr. Other exceptions are allowed to pass up * the call stack. * * @param id Tank id for the tank in which new material will be added * @param in Number of units of material to add to tank * * @exception TankOverflowException Passed up the call stack if thrown * by Tank object. */ public void addMaterial( int id, int in ) throws TankOverflowException { tanks[ id ].fill( in ); // throw exception if( id > in ){ throw new TankOverflowException(); } } /** * Request to remove material from a tank in the tank farm. *

* The operation of removing material from the specified tank is performed * by the tank farm object. TankWarningExceptions are handled by printing * the exception message to stderr. * * @param id Tank id for the tank from which material will be removed * @param out Number of units of material to remove from the tank */ public void removeMaterial( int id, int out ) { tanks[ id ].empty( out ); } /** * Accessor function for tank level. * * @param id Id of the tank whose level will be returned. No check is * made that the parameter is valid. * * @return returns the current level of the tank. */ public int getTankLevel( int id ) { return tanks[ id ].getCurrent(); } /** * Accessor function for tank state. * * @param id Id of the tank whose state will be returned. No check is * made that the parameter is valid. * * @return returns the current state of the tank. */ public int getTankState( int id ) { return tanks[ id ].getState(); } public static void main( String[] args ) { TankFarm test = new TankFarm( 4, 6 ); } } // TankFarm