Lab 3: Furniture Part 2

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

  1. The class will include a list of pointers to Furniture objects.
  2. 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 to Furniture objects. Each element of the array will store a pointer to a Furniture object (e.g., a Chair, 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 a Furniture* (i.e., a pointer to a furniture item).
  • 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 takes capacity as a parameter, which determines the size of the array. Inside the constructor (implementation not shown here), the items array would be dynamically allocated based on this capacity using new. For example:items = new Furniture*[capacity]; This dynamically allocates memory for an array of Furniture* pointers, with the size determined by capacity.
  • ~Inventory(): The destructor is responsible for releasing the dynamically allocated memory to avoid memory leaks. In the destructor, the items array will be deleted using:delete[] items; If the inventory stores dynamically allocated Furniture objects, 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 a Furniture object (via its pointer) to the inventory. Before adding, it would check if the array is full by calling isFull(). If there is space, the new item would be added to the array at the index itemCount, and itemCount would 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 the items array and call a display method (perhaps a print() method) on each Furniture object. Since this method is const, 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 uses const because it shouldn’t modify the Inventory.

3.4 Finding an Item by ID

Furniture* find(int furnID) const;
  • find(int furnID) const: This method searches the array for a Furniture item by its unique ID (assumed to be a property of each Furniture object). 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 returns nullptr.

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 provided filename, 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 each Furniture item), 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 comparing itemCount with capacity. If itemCount == 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:

  1. View current inventory. Should provide a formated list of all the items by name.
  2. 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.
  3. 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.
  4. Compute and display the value of all the items.
  5. Save the items to a file. User should provide a file name to write to.
  6. 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

RequirementsGrading CommentsPointsScore
Good Code Design30
Test suite for all functions including common and boundry cases.20
All required function implemented20
Quality User Interface for editing system information20
Comments10
Total100

Scroll to Top