Question
A Cpp program with three files provided InventoryItem.h , InventoryItem.cpp and Inventorymain.cpp . Begin by defining the private attributes of the InventoryItem class: itemName: a
A Cpp program with three files provided InventoryItem.h, InventoryItem.cpp and Inventorymain.cpp. Begin by defining the private attributes of the InventoryItem class:
- itemName: a std::string representing the name of the item.
- itemCode: an int to hold a unique identifier for the item.
- stockQuantity: an int indicating how many units of the item are in stock.
- itemPrice: a double representing the price of the item before tax.
- taxRate: a constant double for the tax rate (13%).
- restockFeePercent: a constant double for the restocking fee percentage (5%).
Setter Functions
Create setter functions for each attribute except the constants. These functions should accept a value, validate it, and if valid, set the attribute's value. For example:
bool setName(const std::string& name):
- Purpose: To validate and set the item's name.
- Parameters: const std::string& name - the new name to be assigned to the item.
- Return Type: bool - indicates whether the operation was successful (true) or not (false).
- Functionality: Check if the provided name is not empty and not just whitespace. If valid, it sets the itemName and returns true; otherwise, it returns false.
bool setItemCode(int code):
- Purpose: To validate and set the item's unique code.
- Parameters: int code - the new code to be assigned to the item.
- Return Type: bool - success (true) or failure (false).
- Functionality: Verifies the code is a positive integer with 8 digits. If valid, it assigns the itemCode and returns true; otherwise, it returns false.
bool setStockQuantity(int quantity):
- Purpose: To validate and set the stock quantity of the item.
- Parameters: int quantity - the new stock quantity.
- Return Type: bool - indicating success or failure.
- Functionality: Ensures the quantity is zero or positive since the negative stock is not logical. If valid, it updates stockQuantity and returns true; if not, it returns false.
bool setItemPrice(double price):
- Purpose: To validate and set the item's price.
- Parameters: double price - the new price for the item.
- Return Type: bool - indicating the outcome of the operation.
- Functionality: Checks if the price is non-negative. If the price is valid, it sets itemPrice and returns true; otherwise, it provides false.
In each function, the return value is used as a simple form of error checking; it enables the calling code to determine if the operation succeeded and respond accordingly (e.g., by informing the user of an invalid input).
Getter Functions
Implement getter functions for each attribute. These functions return the current value of the attribute.
std::string getName() const:
- Purpose: Retrieves the name of the inventory item.
- Return Type: std::string
- Parameters: None.
- Const Correctness: This function is const because it promises not to modify any member variables of the class.
int getItemCode() const:
- Purpose: Retrieves the unique item code for the inventory item.
int getStockQuantity() const:
- Purpose: Obtains the current stock quantity of the inventory item.
double getItemPrice() const:
- Purpose: Fetches the price of the inventory item.
Each function has const qualifier at the end of the member function declaration, indicating that the function is read-only and does not change any member data of the class. This is an essential aspect of const correctness in C++, which guarantees that getters will not alter the object's state, making it safe to call them on const instances of the class.
Business Logic Methods
Define and implement the following methods:
- computeRestockFee: Returns the restock fee, calculated as a percentage of the item's price times the stock quantity. This method is crucial for calculating the additional fee required when the inventory is being restocked. This fee is based on a percentage of the item's price. Calculate the fee by multiplying itemPrice with restockFeePercent.
double InventoryItem::computeRestockFee() const;
- restockItem: Accepts a quantity and a reference to a double for the restock fee. It increases stockQuantity and sets the restock fee by calling computeRestockFee. This function is used to update the inventory level of an item by adding a specified quantity to the current stock. It also calculates the total restocking fee for the added items by invoking the computeRestockFee() method. The calculated restocking fee is then output through a reference parameter. The function assumes that the quantity parameter is positive. If a non-positive quantity is passed, it will still add the quantity to the stock, which could reduce the stock quantity (in case of negative numbers). It's the caller's responsibility to ensure the parameter is a positive number. The restockFee parameter will be modified to reflect the total fee for restocking the provided quantity of items. restockFee a reference to a double where the total restock fee will be stored. After the function executes, this will contain the calculated fee for restocking the given quantity of items.
void InventoryItem::restockItem(int quantity, double& restockFee)
- sellItem: Accepts the quantity to be sold and a reference to a double for the selling price. If enough items are in stock, it reduces stockQuantity and computes the selling price, including tax. Returns a bool indicating success. This function attempts to sell a given quantity of an inventory item. It checks if the desired quantity is available in stock. If sufficient stock is present, it deducts the quantity from the stock and calculates the total sell price, including tax. The total price is then output through a reference parameter. If the quantity requested exceeds the stock available, the function returns false and no transaction occurs. The sellPrice parameter is modified to reflect the total cost, including the tax for the given quantity of items. This function affects the object's state by potentially decreasing the stockQuantity if the sale is successful.
bool InventoryItem::sellItem(int quantity, double& sellPrice)
Implementing the Main Function for Inventory Management
A main function in C++ that prompts the user for inventory item details and prints a summary.
Requirements:
- Prompt for the number of inventory items (up to 5).
- Validate the input and allow the user to re-enter if incorrect.
- For each item, prompt and validate the name, item code, stock quantity, and price.
- Display the entered information in a formatted manner.
Implementation Steps:
- Start by defining the number of maximum items, const int MAX_ITEMS = 5;.
- An array of InventoryItem objects, InventoryItem items[MAX_ITEMS];.
- Use a do-while loop to get a valid number of items from the user.
- Check that the number is between 1 and MAX_ITEMS.
- Use input validation techniques like cin.fail() and cin.ignore() to handle incorrect input.
- Use a for-loop to iterate over the number of items.
- For each item, use a series of do-while loops to validate and set each property (name, item code, stock quantity, and price).
- After all items are inputted, print out a formatted inventory list.
- Use std::setw and std::setprecision to format the output for readability.
- Calculate and display the total inventory value at the end. Check the sample run/output.
SAMPLE OUTPUT Enter the number of inventory items (5 max): 3 Enter the name of the inventory item: Item1 Enter the 8-digit item code (positive integer): 45632897 Enter the stock quantity (non-negative integer): 5 Enter the item price (non-negative value): $3.99 Enter the name of the inventory item: Item2 Enter the 8-digit item code (positive integer): 852147 Invalid item code. Please enter an 8-digit positive integer. Enter the 8-digit item code (positive integer): 4521 Invalid item code. Please enter an 8-digit positive integer. Enter the 8-digit item code (positive integer): 89654712 Enter the stock quantity (non-negative integer): -9 Invalid stock quantity. Quantity cannot be negative. Enter the stock quantity (non-negative integer): 8 Enter the item price (non-negative value): $-85 Invalid item price. Price cannot be negative. Enter the item price (non-negative value): $8.99 Enter the name of the inventory item: Item3 Enter the 8-digit item code (positive integer): 85214796 Enter the stock quantity (non-negative integer): 5 Enter the item price (non-negative value): $7.29 Name: Item1 Code: 45632897 Quantity: 5 Price: $3.99 Name: Item2 Code: 89654712 Quantity: 8 Price: $8.99 Name: Item3 Code: 85214796 Quantity: 5 Price: $7.29 ------------------------------------- Total inventory value: $128.32 ======================================
Sample Output for Testing Scenario One Item
====================================== Testing Scenario: One Item ====================================== Item Name: Test Item Item Code: 12345678 Stock Quantity: 50 Item Price: $29.99 Restock 20 items Restocked items. Restock fee: $29.99 New Stock Quantity: 70 Sell 10 items Sold items. Total sell price: $338.89 New Stock Quantity: 60
Sample Output for Testing Scenario Two Items
====================================== Testing Scenario: Two Items ====================================== Item 1 Details: Name: Test Item 1 Code: 12345678 Stock: 30 Price: $19.99 Item 2 Details: Name: Test Item 2 Code: 87654321 Stock: 20 Price: $29.99 Restock 15 items for Item 1 Restocked Item 1. Restock fee: $14.9925 New Stock Quantity: 45 Restock 10 items for Item 2 Restocked Item 2. Restock fee: $14.995 New Stock Quantity: 30 Sell 5 items for Item 1 Sold Item 1. Total sell price: $112.944 New Stock Quantity: 40 Sell 3 items for Item 2 Sold Item 2. Total sell price: $101.666 New Stock Quantity: 27
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started