// ballane.cpp Implementation file for the LaneType class /******************************************************************************* // MODULE NAME: LaneType // INTERFACE FILE: ballane.h // IMPLEMENTATION FILE: ballane.cpp // // PURPOSE: To provide a LaneType useful in implementing the BAL program, // allowing the user to create a new LaneType object, store new values into it, // test those values for validity, and print the current or default value of // the object // // FUNCTIONS: Name Purpose // LaneType Parameterless Constructor // LaneType Constructor w/specified lane numbers // isValid Test whether lane numbers are valid // isDefault Test whether lane numbers contain default values // Print Print out the current values // valueIs Print out the current values // Default Print out the default values // Limits Report the limits on lane number values // Store Store 2 new int values into the lane numbers // Store Store a string of int values into the object // Reset Change the values of the lane numbers to their // defaults // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ #include "ballane.h" // class interface file /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: LaneType // // DESCRIPTION OF FUNCTION: Creates a LaneType object // // DESCRIPTION OF ALGORITHM: Assigns firstLand and secondLane the value of // DEFAULT_LANE // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Object has been created w/default lane values // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ LaneType::LaneType() { firstLane=secondLane=DEFAULT_LANE; } /******************************************************************************* // FUNCTION NAME: LaneType // // DESCRIPTION OF FUNCTION: Constructor w/specified lane numbers // // DESCRIPTION OF ALGORITHM: Assigns the value of lane1 to firstLand, lane2 to // second // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / lane1 - int value of the first lane's number // / in / lane2 - int value of the second lane's number // // PRECONDITIONS: none // // POSTCONDITIONS: A LaneType object has been created w/the specified lane #'s // as the values of lane1 and lane2. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ LaneType::LaneType(int lane1,int lane2) { firstLane=lane1; secondLane=lane2; } /*********************************OBSERVERS************************************/ /******************************************************************************* // FUNCTION NAME: isValid // // DESCRIPTION OF FUNCTION: Determines whether the values stored in the // LaneType object are valid lane numbers. // // DESCRIPTION OF ALGORITHM: Using arithmetic comparison and boolean operators, // determine if both lanes are betwen the upper and lower bounds for acceptable // lane numbers. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if both firstLane and secondLane are greater // than or equal to LANE_MIN and less than or equal LANE_MAX, and returns // false otherwise // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool LaneType::isValid() { return (((firstLane>=LANE_MIN)&&(firstLane<=LANE_MAX))&& ((secondLane>=LANE_MIN)&&(secondLane<=LANE_MAX))); } /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Determines if the values stored in the LaneType // object are the default values for the object // // DESCRIPTION OF ALGORITHM: Uses arithmetic comparison and boolean operators // to compare firstLane and secondLane for exact equality with DEFAULT_LANE // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if both firstLane and secondLane are exactly // equal to the value of DEFAULT_LANE // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool LaneType::isDefault() { return ((firstLane==DEFAULT_LANE)&&(secondLane==DEFAULT_LANE)); } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Creates a string that contains the lane numbers // properly formatted for output. // // DESCRIPTION OF ALGORITHM: Converts each lane number to a string and pads the // string with zeroes if it is to small to fit the space allotted to each lane // number, then creates a string holding the first lane number, a delimiter, & // the second lane number // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: Neither firstLane nor secondLane is negative // // POSTCONDITIONS: A string has been created which contains two int values of // width LANE_FLD_WIDTH, each of which contain the first LANE_FLD_WIDTH // characters of the respective lane numbers, separated by a hyphen // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LaneType::Print() { ostringstream lane1String; // For conversion to string ostringstream lane2String; // For converting int to string string outputString; // to hold string for output int counter; // to control for-loop lane1String << firstLane; // Convert ints to strings lane2String << secondLane; // if ((lane1String.str()).length()Print() // // CALLED BY: client // CALLS: Print // // PARAMETERS: none // // PRECONDITIONS: Neither firstLane nor secondLane is negative // // POSTCONDITIONS: A string has been created which contains two int values of // width LANE_FLD_WIDTH, each of which contain the first LANE_FLD_WIDTH // characters of the respective lane numbers, separated by a hyphen // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LaneType::valueIs() { return (this->Print()); } /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Creates a string that contains the default value // for a LaneType object, formatted appropriately. // // DESCRIPTION OF ALGORITHM: Creates a default LaneType object, then calls the // Print the member function to return the string that contains that object's // contents // // CALLED BY: client // CALLS: LaneType constructor, Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been returned which contains the default value // of a LaneType object, formatted appropriately // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LaneType::Default() { LaneType defaultLane; return (defaultLane.Print()); } /******************************************************************************* // FUNCTION NAME: Limits // // DESCRIPTION OF FUNCTION: Describes the limits on a LaneType object // // DESCRIPTION OF ALGORITHM: Returns a string which contains a literal message // to the caller. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been created which contains the following // message: "Must be two positive integers in ##-## format" // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LaneType::Limits() { return ("Must be two positive integers in ##-## format"); } /********************************TRANSFORMERS**********************************/ /******************************************************************************* // FUNCTION NAME: Store // // DESCRIPTION OF FUNCTION: Stores new lane numbers into the object // // DESCRIPTION OF ALGORITHM: Assigns the value of lane1 to firstLane, and the // value of lane2 to secondLane // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / lane1 - int value of first lane's number // / in / lane2 - int value of second lane's number // // PRECONDITIONS: none // // POSTCONDITIONS: The provided ints have been stored into the LaneType object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LaneType::Store(int lane1,int lane2) { firstLane=lane1; secondLane=lane2; return; } /******************************************************************************* // FUNCTION NAME: Store // // DESCRIPTION OF FUNCTION: Stores lane information contained in a string into // the LaneType object. // // DESCRIPTION OF ALGORITHM: Creates an input stringstream, then extracts the // elements of the stringstream into firstLane, an extra char, and secondLane // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / laneString - string that contains the new lane // information in ##-## format // // PRECONDITIONS: laneString must be properly formatted // // POSTCONDITIONS: The values of firstLane and secondLane have been changed to // the value before the hyphen and the value after the hyphen, respectively. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LaneType::Store(string laneString) { istringstream inputString(laneString); char extraChar; inputString >> firstLane >> extraChar >> secondLane; return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Resets the values stored in the object to their // default values // // DESCRIPTION OF ALGORITHM: Assigns firstLane and secondLane the value of // DEFAULT_LANE // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The values of firstLane and secondLane have been changed to // DEFAULT_LANE // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LaneType::Reset() { firstLane=secondLane=DEFAULT_LANE; return; }