Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile the unmodified

One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile the unmodified code.

The key abstractions employed in this program are Item, ItemStack, and Inventory. Complete ADT implementations have been provided for the former two. A partial implementation has been provided for the Inventory. Your task is to finish the Inventory.

You can implement the Copy Constructor, findMatchingItemStack, and display with loops.

You can, alternatively, use the built-in C++ functions discussed in Review 03 Example 5 (if you like a challenge).

You must implement:

1. Copy Constructor

2. Assignment Operator

(Note this is already provided and complete. Refer to our discussions of the copy-and-swap method.

Once you have completed the Copy Constructor, you are done with the Big-3.)

3. Inventory::isFull - refer to documentation in Inventory.h.

4. Inventory::findMatchingItemStackIterator - refer to documentation in Inventory.h.

5. Inventory::addItemStackNoCheck - refer to documentation in Inventory.h.

6. Inventory::mergeStacks - refer to documentation in ItemStack.h.

7. Inventory::display. This must generate the Inventory summary.

Note there is no formatting applied with setw. All spacing consists of hard-coded spaces.

Employ your Head-to-Head Testing Skills from CS 250.

This is the code you need to modify

#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 << Node().data << " "; } //------------------------------------------------------------------------------ Inventory::Inventory(int n) { this->head = nullptr; this->tail = nullptr; this->slots = n; this->occupied = 0; } //------------------------------------------------------------------------------ Inventory::Inventory(const Inventory &src) :Inventory(src.slots) { for (Node* it = src.head; it!= nullptr; it = it->next) { this->addItemStackNoCheck(it->data); } /* Node* srcIt = src.head; while (srcIt != nullptr) { this->addItemStackNoCheck(srcIt->data); srcIt = srcIt->next; } */ } //------------------------------------------------------------------------------ Inventory::~Inventory() { Node* it = head; while(it!= nullptr){ Node* next = it->next; delete it; it = next; } } //------------------------------------------------------------------------------ bool Inventory::isFull() const { // If this is more than one line // in the form "return (boolean expression);" // you are overthinking the problem return (occupied == slots); } //------------------------------------------------------------------------------ void Inventory::display(std::ostream &outs) const { outs << " -Used " << occupied << " of " << slots << " slots" << " "; Node* it = head; while (it != nullptr) { outs << " " << it->data << " "; it = it->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) { Node* it = head; while (it != nullptr) { if ((it->data) == itemStack) { return it; } it = it->next; } return nullptr; } //------------------------------------------------------------------------------ void Inventory::mergeStacks(ItemStack& lhs, const ItemStack& rhs) { lhs.addItems(rhs.size()); } //------------------------------------------------------------------------------ void Inventory::addItemStackNoCheck(ItemStack itemStack) { if (head == nullptr) { head = new Node(itemStack); tail = head; } else { tail->next = new Node(itemStack); tail = tail->next; } occupied++; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions