#include "stdstuff.h" #include "Inventory.h" // creates a new item in the inventory called "item" with 0 items on hand. Inventory::Inventory(String2002 item) { Inventory::item = item; stock = 0; minOnHand = 0; numSold = 0; } // creates a new item in the inventory called "item" with number items on hand. // Throws invalid_argument if number is less than zero. Inventory::Inventory(String2002 item, int number) { if ( number < 0 ) { Inventory::item = item; // set up like in default constructor stock = 0; minOnHand = 0; numSold = 0; throw invalid_argument( "Inventory::Inventory -- number cannot be negative"); } Inventory::item = item; stock = number; minOnHand = number; numSold = 0; } // reduces the inventory by the amount sold. // If the amount is negative or will cause the number of items on hand to become // negative, an invalid_argument is thrown. void Inventory::sell(int amount) { if ( ( amount < 0 ) || ( stock-amount < 0 ) ) throw invalid_argument( "Inventory::sell -- amount cannot be negative or cause number on hand to be negative"); stock -= amount; numSold += amount; if ( stock < minOnHand ) minOnHand = stock; } // stocks up on the item, if required. // If the number of items on hand is less than the amount given, 20 more items are // added to the inventory. // If the amount is negative an invalid_argument is thrown. void Inventory::stockUp(int amount) { if ( amount < 0 ) throw invalid_argument( "Inventory::stockUp -- amount cannot be negative"); if ( stock < amount ) stock += 20; } // returns the number of items in the warehouse. int Inventory::getStock() const { return stock; } // returns the name of the item. String2002 Inventory::getItem() const { return item; } // returns the total number of this item sold to date. int Inventory::totalSold() const { return numSold; } // prints the minimum number of this item in the warehouse to date. void Inventory::printMinInventory() const { cout << minOnHand; }