Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

COPYABLE FILES (Inventory.cpp, Inventory.h, Item.cpp, Item.h, ItemStack.cpp, ItemStack.h) //Inventory.cpp (Incomplete, this is the only file that must be modified so that it works in order

image text in transcribed

image text in transcribed

image text in transcribed

COPYABLE FILES (Inventory.cpp, Inventory.h, Item.cpp, Item.h, ItemStack.cpp, ItemStack.h)

//Inventory.cpp (Incomplete, this is the only file that must be modified so that it works in order to complete the assignment.)

#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

//Item.cpp (complete):

#include  #include "Item.h" //------------------------------------------------------------------------------ Item::Item() :Item(0, "Air") { } //------------------------------------------------------------------------------ Item::Item(int id) :Item(id, "Air") { } //------------------------------------------------------------------------------ Item::Item(int id, std::string name) :name(name) { this->id = id; } //------------------------------------------------------------------------------ void Item::display(std::ostream &outs) const { outs  

//Item.h (complete):

#ifndef ITEM_H_INCLUDED #define ITEM_H_INCLUDED #include  #include  /** * Item represents an individual Item in an inventory. * This includes items such as potions, building materials, and food. * * Only one of each item can exist--i.e., no two items share the * same numeric id. */ class Item { private: int id; ///id; } //------------------------------------------------------------------------------ inline void Item::setID(int i) { this->id = i; } //------------------------------------------------------------------------------ inline std::string Item::getName() const { return this->name; } //------------------------------------------------------------------------------ inline void Item::setName(std::string n) { this->name = n; } //------------------------------------------------------------------------------ inline bool Item::operator==(const Item &rhs) const { return this->id == rhs.id; } //------------------------------------------------------------------------------ inline bool Item::operatorid  

//ItemStack.cpp (complete):

#include  #include "ItemStack.h" //------------------------------------------------------------------------------ ItemStack::ItemStack() :item(0, "Air") { this->quantity = 0; } //------------------------------------------------------------------------------ ItemStack::ItemStack(const Item &item, int s) :item(item) { this->quantity = s; } //------------------------------------------------------------------------------ std::ostream& operator 

//ItemStack.h (complete):

#ifndef ITEMSTACK_H_INCLUDED #define ITEMSTACK_H_INCLUDED #include  #include "Item.h" /** * A Homogeneous--i.e., uniform--stack of Items. */ class ItemStack { private: Item item; /// 0) */ ItemStack(const Item& item, int s); /** * Retrieve the Item out of which the stack is composed */ Item getItem() const; /** * Retrieve the size of the stack */ int size() const; /** * Increase the size of the stack * * @param a number of items to add * @pre a > 0 */ void addItems(int a); /** * Consider two stacks to be the same if * they contain the same type of Item */ bool operator==(const ItemStack& rhs) const; /** * Order stacks based on Item id */ bool operatoritem; } //------------------------------------------------------------------------------ inline int ItemStack::size() const { return this->quantity; } //------------------------------------------------------------------------------ inline void ItemStack::addItems(int a) { this->quantity += a; } //------------------------------------------------------------------------------ inline bool ItemStack::operator==(const ItemStack& rhs) const { return this->item == rhs.item; } //------------------------------------------------------------------------------ inline bool ItemStack::operatoritem  

PROBLEM DESCRIPTION This assignment deals with a program that places items into separate inventories. The program reads data from two files, itemsList-Ox.txt and inventoryList-Ox.txt. The first file, itemsList-Ox.txt, lists all possible items. Each line represents one item in the form id name. Example: Sample itemsList-01.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 Block *********************** The second file, inventory List-Ox.txt, lists each individual inventory followed by a list of items. Example: Sample inventoryList-01.txt : ***************************************** #5 - 1 10 -25 - 3 2 # 6 - 4 3 - 527 - 644 - 7 55 - 81 -94 - 4 3 #2 - 2 5 -94 -81 -52 - 105 *************************** Each line preceded by #denotes the start of a new inventory. Each line preceded by - denotes an item. The program creates a new inventory each time a #is encountered. When a - is encountered, a stack of items, ItemStack, is created. The ItemStack is placed in the Inventory based on the following rules: 1. If the Inventory is empty, store the ItemStack, and return true. 2. If the Inventory is not empty, examine the Inventory. If a matching ItemStack is found, merge the two ItemStacks and return true. o If no matching ItemStack is found, store the new ItemStack and return true. 3. If the Inventory is full, return false. 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 PROBLEM DESCRIPTION This assignment deals with a program that places items into separate inventories. The program reads data from two files, itemsList-Ox.txt and inventoryList-Ox.txt. The first file, itemsList-Ox.txt, lists all possible items. Each line represents one item in the form id name. Example: Sample itemsList-01.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 Block *********************** The second file, inventory List-Ox.txt, lists each individual inventory followed by a list of items. Example: Sample inventoryList-01.txt : ***************************************** #5 - 1 10 -25 - 3 2 # 6 - 4 3 - 527 - 644 - 7 55 - 81 -94 - 4 3 #2 - 2 5 -94 -81 -52 - 105 *************************** Each line preceded by #denotes the start of a new inventory. Each line preceded by - denotes an item. The program creates a new inventory each time a #is encountered. When a - is encountered, a stack of items, ItemStack, is created. The ItemStack is placed in the Inventory based on the following rules: 1. If the Inventory is empty, store the ItemStack, and return true. 2. If the Inventory is not empty, examine the Inventory. If a matching ItemStack is found, merge the two ItemStacks and return true. o If no matching ItemStack is found, store the new ItemStack and return true. 3. If the Inventory is full, return false. 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions