1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/**
* Description: Inventory Class for storing items
* Default Constructor: Does not require any parameters
* Build an empty inventory
*
*/
class Inventory
{
public:
/**
* Description: Default Constructor with no parameters
* Build an empty inventory
*/
Inventory() {}
/**
* Description: Add an item to the inventory
* @param Item item to be added
* @return none
* This function will check if the item already exists
* by comparing item name and item number. If the
* item already exists, the quantity will be updated.
*/
void addItem( const Item &itemToAdd )
{
// Search the existing item and see if this item already exists
// If the item already exists, simply update the quantity
for( auto &x : items ) {
if( x.getItemNumber() == itemToAdd.getItemNumber() &&
x.getItemName() == itemToAdd.getItemName() )
{
x.setQuantity( itemToAdd.getQuantity() + x.getQuantity() );
return;
}
}
// Add as a new item
items.push_back( itemToAdd );
}
/**
* Description: Search an item by name. If found, print item info
* No action taken if item is not found
* @param string Item Name
* @return none
*/
void printItemByName( std::string name ) const
{
for( auto x : items ) {
if( x.getItemName() == name ) {
std::cout << x;
}
}
}
/**
* Description: Search an item by item number. If found, print item info
* No Action taken if item is not found.
* @param int Item Number
* @return none
*/
void printItemByNumber( int itemNumber ) const
{
for( auto x : items ) {
if( x.getItemNumber() == itemNumber ) {
std::cout << x;
}
}
}
/**
* Description: Search an item by name. If found, return true
* @param string Item Name
* @return true if item is found
*/
bool searchItemByName( std::string name ) const
{
for( auto x : items ) {
if( x.getItemName() == name ) {
return true;
}
}
return false;
}
/**
* Description: Search an item by item number. If found, return true
* @param int Item Number
* @return true if item is found
*/
bool searchItemByNumber( int itemNumber ) const
{
for( auto x : items ) {
if( x.getItemNumber() == itemNumber ) {
return true;
}
}
return false;
}
/**
* Description: Print all Understocked Items
* @param none
* @return none
*/
void printAllUnderstocked() const
{
for( auto x : items ) {
if( x.requireRestock() ) {
std::cout << x;
}
}
}
/**
* Description: Print all Overstocked Items
* @param none
* @return none
*/
void printAllOverstocked() const
{
for( auto x : items ) {
if( x.isOverStocked() ) {
std::cout << x;
}
}
}
/**
* Description: Remove Item Info from Inventory.
* No action is taken if Item does not exist
* @param string Item Name
* @return none
*/
void removeItem( std::string itemToRemove )
{
std::vector<Item>::iterator itr = items.begin();
for( itr ; itr != items.end() ; itr++ ) {
if( itr->getItemName() == itemToRemove ) {
items.erase( itr );
return;
}
}
}
/**
* Description: Remove Item Info from Inventory.
* No action is taken if Item does not exist
* @param int Item Number
* @return none
*/
void removeItem( int itemToRemove )
{
std::vector<Item>::iterator itr = items.begin();
for( itr ; itr != items.end() ; itr++ ) {
if( itr->getItemNumber() == itemToRemove ) {
items.erase( itr );
return;
}
}
}
/**
* Description: Modify an Existing Item.
* Must provide an updated Item Object
* @param Item Updated Item Object. The name or item number
* has to match one of the existing item.
* @return true if the item was successfully modified.
*/
bool modifyItemInfo( const Item &newItem )
{
for( int i = 0 ; i < items.size() ; i++ ) {
if( items[i].getItemName() == newItem.getItemName()
|| items[i].getItemNumber() == newItem.getItemNumber() )
{
items[i] = newItem;
return true;
}
}
return false;
}
/**
* Description: Function to print all items in the inventory
* Will be used by overloaded operator<<
* @param std::ostream &out
* @return none
*/
void printItems( std::ostream &out ) const
{
for( auto x : items ) {
out << x;
}
}
private:
std::vector<Item> items;
};
/**
* Overloaded operator<< to print all items in the inventory
*/
std::ostream & operator<< ( std::ostream &out , const Inventory &inventory )
{
inventory.printItems( out );
return out;
}
int main()
{
Inventory storeInventory;
// Add some items to the inventory
// Parameter List - Name , Item Number , Quantity , Price
storeInventory.addItem( Item{ "Pencil" , 101 , 10 , 1.00} );
storeInventory.addItem( Item{ "Eraser" , 102 , 21 , 2.00} );
storeInventory.addItem( Item{ "Toothpaste" , 103 , 10 , 1.00} );
storeInventory.addItem( Item{ "Toothbrush" , 104 , 50 , 1.00} );
// Print all items in the inventory after adding items
std::cout << "\n***** This is the initial inventory *****" << storeInventory;
// Remove the item with 104 Item Number
storeInventory.removeItem( 104 );
// Remove the item with the name, "Pencil"
storeInventory.removeItem( "Pencil" );
// Change the item name, quantity, and price of an item w/ 102 item number
storeInventory.modifyItemInfo( Item{ "Pencil Eraser" , 102 , 50 , 0.25} );
// Change the item number, quantity, and price of an item w/ Toothpaste item name
storeInventory.modifyItemInfo( Item{ "Toothpaste" , 104 , 8 , 1.25} );
// Print Updated Inventory
std::cout << "\n\n***** This is the updated Inventory *****" << storeInventory;
// Print all overstocked items
std::cout << "\n\nHere are overstocked items";
storeInventory.printAllOverstocked();
// Print all understocked items
std::cout << "\n\nHere are items that need to be restocked";
storeInventory.printAllUnderstocked();
// Search an item. If the item exists, print its information
std::cout << "\n\n\nSearching for an item named Toothpaste ...";
if( storeInventory.searchItemByName("Toothpaste") ) {
std::cout << "\nItem was found, printing the item information...\n";
storeInventory.printItemByName( "Toothpaste" );
}
else {
std::cout << "\n\nThe Item was not found";
}
}
|