Question
Can you please modify the code Inventory.cpp to where it will display something similar to the sample output I provided? For example, if the sample
Can you please modify the code Inventory.cpp to where it will display something similar to the sample output I provided?
For example, if the sample data above is kept in files itemList-01.txt and inventoryList-01.txt, then to run this program, do:
./storage itemList-01.txt inventoryList-01.txt
(On a Windows system, you would omit the ./. If you are running from Code::Blocks or a similar development environment, you may need to review how to supply command-line parameters to a running program.)
The copyable codes i provided are all complete except for Inventory.cpp which needs to be modified.
COPYABLE CODES:
//Inventory.cpp (incomplete)
#include #include "Inventory.h" // Allow the compiler to define the remaining // comparison operators using namespace std::rel_ops; //------------------------------------------------------------------------------ Inventory::Node::Node() :data(Item(0, "Air"), 0) { this->next = nullptr; } //------------------------------------------------------------------------------ Inventory::Node::Node(ItemStack s) :data(s) { this->next = nullptr; } //------------------------------------------------------------------------------ Inventory::Inventory() { this->head = nullptr; this->tail = nullptr; this->slots = 10; this->occupied = 0; //std::cerr head = nullptr; this->tail = nullptr; this->slots = n; this->occupied = 0; } //------------------------------------------------------------------------------ Inventory::Inventory(const Inventory &src) { // @todo implement this function } //------------------------------------------------------------------------------ Inventory::~Inventory() { // @todo implement this function } //------------------------------------------------------------------------------ bool Inventory::isFull() const { // @todo implement this function // // If this is more than one line // in the form "return (boolean expression);" // you are overthinking the problem return true; // This line is a placeholder. Remove it. } //------------------------------------------------------------------------------ void Inventory::display(std::ostream &outs) const { outs data next; } } //------------------------------------------------------------------------------ Inventory& Inventory::operator=(Inventory rhs) { std::swap(*this, rhs); return *this; } //------------------------------------------------------------------------------ void swap(Inventory& lhs, Inventory& rhs) { using std::swap; swap(lhs.head, rhs.head); swap(lhs.tail, rhs.tail); swap(lhs.slots, rhs.slots); swap(lhs.occupied, rhs.occupied); } //------------------------------------------------------------------------------ Inventory::Node* Inventory::findMatchingItemStackNode(const ItemStack& itemStack) { // @todo implement this function return nullptr; } //------------------------------------------------------------------------------ void Inventory::mergeStacks(ItemStack& lhs, const ItemStack& rhs) { // Update lhs... remember rhs is read only } //------------------------------------------------------------------------------ void Inventory::addItemStackNoCheck(ItemStack itemStack) { // @todo implement this function }
//Inventory.h (complete)
#ifndef INVENTORY_H_INCLUDED #define INVENTORY_H_INCLUDED #include #include "ItemStack.h" /** * An Inventory is composed of n slots. Each slot may store only * one type of item--specified by *slots*. *
* Once all slots are filled, no additional Item types may be * stored. Individual slots may contain any number of the same * Item. */ class Inventory { private: /** * Each Node represents on Inventory slot--i.e., space */ struct Node { ItemStack data; /// 0 */ Inventory(int n); /** * Duplicate an existing Inventory */ Inventory(const Inventory& src); /** * Empty all Inventory slots. */ ~Inventory(); /** * Add one or more items to the inventory list * * @return true if *stack* was added and false otherwise */ bool addItems(ItemStack itemStack); /** * Check if this inventory is full * * @return (occupied
* I am using a friend function here and only here (under protest) *
* [Refer here](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) */ friend void swap(Inventory& lhs, Inventory& rhs); private: /** * Find Node containing and ItemStack with a matching id * * @param itemStack ItemStack for which we want a match * * @return pointer to a Node containing a matching ItemStack * or nullptr if no such Node exists */ Node* findMatchingItemStackNode(const ItemStack& itemStack); /** * Merge two item stacks. * * @param lhs item stack where items need to be `add`ed * @param rhs item stack with the *number* of items to add * * @pre lhs.id == rhs.id */ void mergeStacks(ItemStack& lhs, const ItemStack& rhs); /** * Append a Node. *
* This is the code we discussed in Review-01 * When this method is invoked all special cases * have already been covered in `addItems`. *
* Abstraction and Interfaces */ void addItemStackNoCheck(ItemStack itemStack); }; //------------------------------------------------------------------------------ inline bool Inventory::addItems(ItemStack itemStack) { Node* matchingNode = findMatchingItemStackNode(itemStack); // A match was found if(matchingNode != nullptr){ mergeStacks(matchingNode->data, itemStack); return true; } // There is no space for a new type of `ItemStack` if(this->isFull()) { return false; } // This is a new type of item and there is plenty of room addItemStackNoCheck(itemStack); return true; } /** * Print the Inventory through use of the display member function */ inline std::ostream& operator
//storage.cpp (complete)
#include #include #include #include #include #include #include "Inventory.h" #include "ItemStack.h" #include "Item.h" using namespace std; /** * Trim leading and trailing whitespace from a string. * * @param str string to prune * * @pre str is nonempty */ void trim(std::string& str); /** * Read file containing the list * of all possible items */ vector parseItemList(istream& inf); /** * Read inventory file and create all Inventory * instances. * * @param items collection of valid Item entries * * @pre items is non-empty */ vector parseInventoryFile(istream& inf, const vector& items); /** * Generate a summary of all valid items */ void printItems(const vector& items); /** * Generate a summary of Inventory utilization */ void printInventories(const vector& storage); /** * Assignment 1: Item Storage * * @param argv[1] items filename * @param argv[2] inventories filename */ int main(int argc, char** argv) { // Check Command Line Arguments if(argc != 3) { cerr > i) { getline(inf, n); trim(n); //items.push_back(Item(i, n)); items.emplace_back(i, n); } return items; } //------------------------------------------------------------------------------ vector parseInventoryFile(istream& inf, const vector& items) { vector storage; // Collection of Inventory instances Inventory* inv = nullptr; // Temporary Inventory pointer // First two values on a line char leading_char; int num_1; cout > leading_char >> num_1) { if (leading_char == '#') { if (inv != nullptr) { storage.push_back(*inv); delete inv; } inv = new Inventory(num_1); } else { Item key(num_1); // Read third value int num_2; inf >> num_2; vector::const_iterator it; it = find(items.begin(), items.end(), key); // Ignore any Item id not found in items if (it != items.end()) { ItemStack stack(*it, num_2); if (!(inv->addItems(stack))) { cout
UPDATE TO QUESTION:
Sorry for slow response but here are 1 of each of the provided text files that I was given.
//inventoryList-00.txt
# 3 - 1 10 - 2 5 - 3 2 # 6 - 4 3 - 5 27 - 6 44 - 7 55 - 8 1 - 9 4 - 4 3 # 5 - 2 5 - 9 4 - 8 1 - 5 2 - 7 5
//itemList-00.txt
0 Air 1 HP Potion 2 MP Potion 5 Iron Ore 3 Bow Tie 4 Dirt 6 Diamond Ore 7 Iron Ingot 8 Diamond 9 Diamond BlockYOUR TASK The key abstractions employed in this program are Inventory, Item, and ItemStack. Complete ADT implementations have been provided for the latter two. The Inventory class is partially implemented. Your task is to complete the Inventory ADT. The code provided will not run correctly... until you complete all the following methods: 1. Copy Constructor 2. Destructor 3. Assignment Operator Note this is already provided and complete. Once you have completed the Copy Constructor and Destructor, you are done with the Big-3. 4. Inventory::isFull - refer to documentation in Inventory.h. 5. Inventory::findMatchingItemStackNode - refer to documentation in Inventory.h. 6. Inventory::mergeStacks - refer to documentation in Inventory.h. 7. Inventory::addItemStackNoCheck - refer to documentation in Inventory.h. The partial implementation provided assumes that each Inventory will keep track of items in a linked list. You must not change this choice of data structure. SAMPLE OUTPUT Processing Log: Stored (10) HP Potion Stored (5) MP Potion Stored (2) Bow Tie Stored (3) Dirt Stored (27) Iron Ore Stored (44) Diamond Ore Stored (55) Iron Ingot Stored (1) Diamond Stored (4) Diamond Block Stored (3) Dirt Stored (5) MP Potion Stored ( 4) Diamond Block Discarded (1) Diamond Discarded ( 2) Iron Ore Item List: 0 Air 1 HP Potion 2 MP Potion 3 Bow Tie 4 Dirt 5 Iron Ore 6 Diamond Ore 7 Iron Ingot 8 Diamond 9 Diamond Block Storage Summary : - Used 3 of 5 slots (10) HP Potion (5) MP Potion (2) Bow Tie - Used 6 of 6 slots (6) Dirt (27) Iron Ore (44) Diamond Ore (55) Iron Ingot (1) Diamond (4) Diamond Block - Used 2 of 2 slots (5) MP Potion (4) Diamond Block YOUR TASK The key abstractions employed in this program are Inventory, Item, and ItemStack. Complete ADT implementations have been provided for the latter two. The Inventory class is partially implemented. Your task is to complete the Inventory ADT. The code provided will not run correctly... until you complete all the following methods: 1. Copy Constructor 2. Destructor 3. Assignment Operator Note this is already provided and complete. Once you have completed the Copy Constructor and Destructor, you are done with the Big-3. 4. Inventory::isFull - refer to documentation in Inventory.h. 5. Inventory::findMatchingItemStackNode - refer to documentation in Inventory.h. 6. Inventory::mergeStacks - refer to documentation in Inventory.h. 7. Inventory::addItemStackNoCheck - refer to documentation in Inventory.h. The partial implementation provided assumes that each Inventory will keep track of items in a linked list. You must not change this choice of data structure. SAMPLE OUTPUT Processing Log: Stored (10) HP Potion Stored (5) MP Potion Stored (2) Bow Tie Stored (3) Dirt Stored (27) Iron Ore Stored (44) Diamond Ore Stored (55) Iron Ingot Stored (1) Diamond Stored (4) Diamond Block Stored (3) Dirt Stored (5) MP Potion Stored ( 4) Diamond Block Discarded (1) Diamond Discarded ( 2) Iron Ore Item List: 0 Air 1 HP Potion 2 MP Potion 3 Bow Tie 4 Dirt 5 Iron Ore 6 Diamond Ore 7 Iron Ingot 8 Diamond 9 Diamond Block Storage Summary : - Used 3 of 5 slots (10) HP Potion (5) MP Potion (2) Bow Tie - Used 6 of 6 slots (6) Dirt (27) Iron Ore (44) Diamond Ore (55) Iron Ingot (1) Diamond (4) Diamond Block - Used 2 of 2 slots (5) MP Potion (4) Diamond Block
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