//////////////////////////////////////////////////////////////////////////////// // Truck.C // // CMSC341 Fall 2001 -Project 1 // // Kyu Bae 212-04-5868 Section 201 // // kyu_bae@yahoo.com /kbae1@umbc.edu // // created: 1 September 2001 // // current: 16 September 2001 // //////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Truck carries a homogeneous collection of goods. It will have unlimited // capacity, the number of goods. the actual number of goods increases as // goods are loaded and decreses as they are delivered. The Truck has three // data members, totalMiles, listOfGoods, currentLocation. several operatons // are done to truck , load, deliver, drive etc.. // Truck was templatized so that when called with particular choice either // perishable or equipment, Truck class will be instantiated. Template was // used so that avoid writing two identical functions. // Author: kyu bae // Version: 11 september 2001 ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // default constructor: set totalMiles of data member to 0 and // currentLocation to Depot ///////////////////////////////////////////////////////////////////////// template Truck::Truck() { totalMiles = 0; currentLocation = string("Depot"); } //////////////////////////////////////////////////////////////////////// // copy constructor: // copy object of Truck to another object of Truck. // Param rhs: another Truck to copy //////////////////////////////////////////////////////////////////////// template Truck::Truck( const Truck & rhs) { operator= ( rhs ); } /////////////////////////////////////////////////////////////////////// // Destructor: ////////////////////////////////////////////////////////////////////// template Truck::~Truck() { } ////////////////////////////////////////////////////////////////////// // Assignment operator: // assgin tr to this Truck. copy data members of truck class object // to this data members // Param tr: tr is the Truck to assign the rvalue // return: this Truck after assigning the data members ////////////////////////////////////////////////////////////////////// template const Truck & Truck::operator= ( const Truck & tr) { if (this != & tr) { totalMiles = tr.totalMiles; listOfGoods = tr.listOfGoods; currentLocation = tr.currentLocation; } return *this; } ////////////////////////////////////////////////////////////////////// // load items to truck. Check if they are same item and if they are // not same add the item to Truck. // Param item: item of Object to Truck // Pre-cond: item has to be different, one of three data types has // be different. //////////////////////////////////////////////////////////////////// template void Truck::load(const Object &item) { int cSize = listOfGoods.size(); for (int i = 0; i < cSize; ++i) { if (item == listOfGoods[i]) { cout << "**Error: Truck already has this Item on...**" << endl; return; } } listOfGoods.resize(cSize+1); listOfGoods[cSize] = item; } ///////////////////////////////////////////////////////////////////// // deliver item to destination and delete the item from the Truck // Param item: item of Object to Truck // Pre-cond: item has to be on the TRuck in order to deliver ///////////////////////////////////////////////////////////////////// template void Truck::deliver (const Object & item) { int cSize = listOfGoods.size(); if(cSize == 0){ cout << "**Error: Truck is Empty....Nothing to DELIVER!!**" << endl; return; } for( int i= 0; i < cSize; ++i) { if (item == listOfGoods[i]) { int j=0; vector tmpListOfGoods(cSize-1); for (int c=0; c < cSize; c++) { if(!(item == listOfGoods[c])) tmpListOfGoods[j++] = listOfGoods[c]; //vector assignment operator// } listOfGoods.resize(cSize-1); listOfGoods = tmpListOfGoods;//vector copy constructor return; } } cout << "**Error: Truck doesn't contain the Item.**" << endl; } ////////////////////////////////////////////////////////////////////// // drive to destination and keep track of how many miles you have // driven and currentLocation. // Param destination : assign destination to current location // Param miles : miles to totalmiles // Pre-cond: if asked to go same place, tell you are there already. ////////////////////////////////////////////////////////////////////// template void Truck::drive(string destination, int miles) { totalMiles += miles; currentLocation = destination; } ///////////////////////////////////////////////////////////////////// // accessor: checking if the vector is empty. // return: boolean for empty or not.. //////////////////////////////////////////////////////////////////// template bool Truck::isEmpty() const { int cSize = listOfGoods.size(); if (cSize == 0) return true; else return false; } /////////////////////////////////////////////////////////////////// // mutator: making the vector empty. deleting all the items /////////////////////////////////////////////////////////////////// template void Truck::MakeEmpty() { cout << "** making Truck empty**" << endl; listOfGoods.resize(0); } ///////////////////////////////////////////////////////////////////// // print three data members: tell its current location and total // miles of truck drove. // Param out: ostream's object to write and call operator << ///////////////////////////////////////////////////////////////////// template void Truck::print(ostream &out) const { int cSize = listOfGoods.size(); if(cSize == 0) { cout << " The truck is empty" << endl; return; } out << "The truck contains the following goods:" << endl; for (int i = 0; i ostream &operator << (ostream &out, const Truck &rTruck) { rTruck.print(out); return out; }