/********************************************************************* C201 Computer Programming II Fall 2006 Dana Vrajitoru & A class to represent the inventory of a shop. **********************************************************************/ #include "inventory.h" #include #include using namespace std; // *********************** Constructors - Destructor ****************** // Constructor with specified size and default value so that we can // use it as default constructor too. Inventory::Inventory(int nr_items) : size(0), capacity(0), items(NULL) { if (nr_items < SIZE_DEF) Init(SIZE_DEF); else Init(nr_items); } // Destructor: must delete the array items if it's not NULL. Inventory::~Inventory() { Clear(); } // **************** Initialize, reset, resize functions *************** // Initialize the array items with a given capacity. If the array is // not empty it must delete it. It does not copy the previous objects // in the array. void Inventory::Init(int nr_items) { if (capacity) Clear(); capacity = nr_items; items = new Product[capacity]; size = nr_items; } // Delete the array items and reset all values to 0. We're creating a // separate function for this because we may need it in more than one // place. void Inventory::Clear() { if (capacity) { delete [] items; items = NULL; size = 0; capacity = 0; } } // Double the size of the array items and copy all the items from the // previous one to the new one. No need to change the size: the number // of product objects in use is the same. void Inventory::Resize() { // Allocate a new array and copy the elements from the old one. Product *temp = new Product[capacity*2]; for (int i=0; i> input_name; for (int i=0; i> amount; items[i].Add(amount); return true; } // If the loop ends without a return it means we didn't find the // name. cout << "Item doesn't exist. Add a new item instead." << endl; return false; } // Sells a quantity of an existing item identified by name. If there // is no item with that name or if the quantity of the item is less // than the required amount to sell then the function should print an // error message and return false. If the operation is successful the // function should output the total price for those items (quantity // sold times the price). bool Inventory::Sell_by_name() { // To be copied from homework 7. } // ****************** General purpose functions *********************** // Print a list of the entire inventory. void Inventory::Output_all() { cout << "There are " << size << " items in the inventory." << endl; for (int i=0; i> nr_items; // Prepare the memory for these items. Init(nr_items); for(int i=0; i> items[i].name >> items[i].barcode >> ty >> items[i].quantity >> items[i].price; items[i].type = Category(ty); if (!fin.good()) { cout << "Could read only " << i << " items from the file" << endl; // reset the size to the number of items // we were actually able to read from the file size = i; } } fin .close(); return true; } }