This will use the same groups a Lab 2
Github Classroom code: link
Goal
Create a class Inventory to manage the collection of furniture items.
Step 1 – Create the Inventory class
- The class will include a list of pointers to
Furnitureobjects. - Implement methods to add new items, display all items, calculate the total inventory value, find and return and item by ID, write the inventory to a file (CSV), and read the inventory from a file:
#ifndef INVENTORY_H
#define INVENTORY_H
#include <string>
#include "Furniture.h" // Assuming Furniture class is declared in Furniture.h
class Inventory {
public:
// Constructor to initialize the inventory with a specific size
Inventory(int capacity);
// Destructor to clean up dynamically allocated memory
~Inventory();
// Method to add a furniture item to the inventory
void addItem(Furniture* item);
// Method to display all the furniture items in the inventory
void displayInventory() const;
// Method to compute and return the total retail value of all items in the inventory
double calculateTotalValue() const;
// Method to search the inventory for a furniture item by its ID
Furniture* find(int furnID) const;
// Method to write the inventory details to a file
void writeToFile(const std::string& filename) const;
// Method to read the inventory details from a file
void readFromFile(const std::string& filename);
private:
Furniture** items; // Pointer to dynamically allocated array of Furniture pointers
int capacity; // Maximum number of items the inventory can hold
int itemCount; // Current number of items in the inventory
// Method to check if inventory is full
bool isFull() const;
};
#endif // INVENTORY_H
Explanation:
1. Data Members
private:
Furniture** items; // Pointer to dynamically allocated array of Furniture pointers
int capacity; // Maximum number of items the inventory can hold
int itemCount; // Current number of items in the inventory
Furniture** items: This is a pointer to a C-style array of pointers toFurnitureobjects. Each element of the array will store a pointer to aFurnitureobject (e.g., aChair,Table, etc.).- The
Furniture*is a pointer to an object. - The
Furniture**is an array of such pointers, where each element in the array is aFurniture*(i.e., a pointer to a furniture item).
- The
int capacity: This represents the maximum number of items that the inventory can store. The size of the array is determined by this capacity.int itemCount: This keeps track of the actual number of items currently in the inventory. It helps us determine if the array is full and how many items are stored.
2. Constructor and Destructor
public:
Inventory(int capacity); // Constructor
~Inventory(); // Destructor
Inventory(int capacity): The constructor takescapacityas a parameter, which determines the size of the array. Inside the constructor (implementation not shown here), theitemsarray would be dynamically allocated based on this capacity usingnew. For example:items = new Furniture*[capacity];This dynamically allocates memory for an array ofFurniture*pointers, with the size determined bycapacity.~Inventory(): The destructor is responsible for releasing the dynamically allocated memory to avoid memory leaks. In the destructor, theitemsarray will be deleted using:delete[] items;If the inventory stores dynamically allocatedFurnitureobjects, those objects would need to be deleted as well, to free their memory.
3. Methods
3.1 Adding an Item
void addItem(Furniture* item);
addItem(Furniture* item): This method adds aFurnitureobject (via its pointer) to the inventory. Before adding, it would check if the array is full by callingisFull(). If there is space, the new item would be added to the array at the indexitemCount, anditemCountwould be incremented.
3.2 Displaying the Inventory
void displayInventory() const;
displayInventory() const: This method displays all the items in the inventory. It would loop through theitemsarray and call a display method (perhaps aprint()method) on eachFurnitureobject. Since this method isconst, it cannot modify the object state.
3.3 Calculating the Total Value
double calculateTotalValue() const;
calculateTotalValue() const: This method calculates the total value of the inventory by iterating through the array and summing the prices (or some other value property) of all the items. It also usesconstbecause it shouldn’t modify theInventory.
3.4 Finding an Item by ID
Furniture* find(int furnID) const;
find(int furnID) const: This method searches the array for aFurnitureitem by its unique ID (assumed to be a property of eachFurnitureobject). It loops through the array, checking each item’s ID. If it finds a match, it returns the pointer to that item. If not, it returnsnullptr.
3.5 Writing to a File
void writeToFile(const std::string& filename) const;
writeToFile(const std::string& filename) const: This method writes the inventory data to a file. It opens a file with the providedfilename, and writes each item’s details (e.g., type, price, ID) in a format that can later be read back.
3.6 Reading from a File
void readFromFile(const std::string& filename);
readFromFile(const std::string& filename): This method reads inventory data from a file. It opens the file, reads in data (such as type, price, ID for eachFurnitureitem), and adds them to the inventory.
4. Helper Method
private:
bool isFull() const;
isFull() const: This private method checks if the inventory is full by comparingitemCountwithcapacity. IfitemCount == capacity, the array is full, and new items cannot be added.
The readFromFile method must correctly deal with the nextID properly. The code to find the maximum ID in the file as it reads them in, and set nextID to the next higest number so there will be no duplicate IDs.
Step 2 – Write test code to test each of the functions above.
Cerate a test program to test each function of the inventory class. You can use AI to help write this code, and to come up with test data. You should reuse the LoadFurniture from the previous lab to help with the testing.
Step 3 – Create an interactive store inventory management program
Write an interactive program that start up and gives the user a menu of options:
- View current inventory. Should provide a formated list of all the items by name.
- Look up item aned update item by ID. Given an item it, output a complete description of the item. Give the user option to update the quantity of that item or the cost of the item.
- Add a new item to the inventory. You should first ask for the type of the item to add , ask for all the fields associated with that item type, then create it and add it to the inventory. Hint: add a constructor to each item class that prompts the user for the data needed for that type of furniture.
- Compute and display the value of all the items.
- Save the items to a file. User should provide a file name to write to.
- Read the items from a file. User should provide a file name to read from.
Step 4
Create a plan for how to test this program. Since this is a user interface you do cannot exactly automate it (though it is possible with scripts). But you should just write up a test plan of things you should do, and then do it.
Step 5
Turn in the output of all your tests, and your test plan from step 4. on moodle. Submit your code to the github classroom.
Grading
| Requirements | Grading Comments | Points | Score |
|---|---|---|---|
| Good Code Design | 30 | ||
| Test suite for all functions including common and boundry cases. | 20 | ||
| All required function implemented | 20 | ||
| Quality User Interface for editing system information | 20 | ||
| Comments | 10 | ||
| Total | 100 |
