Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WHY IS MY CODE NOT BUILDING? PROMPT: Introduction Hi and welcome to team Gilded Rose. As you know, we are a small inn with a

WHY IS MY CODE NOT BUILDING?

PROMPT:

Introduction

Hi and welcome to team Gilded Rose. As you know, we are a small inn with a prime location in a prominent city ran by a friendly innkeeper named Allison. We also buy and sell only the finest goods.

You are provided a listing of items from the latest shipment to the Gilded Rose with each items name, sell in value, and quality. The sell in value is based on gold coins (there is no fraction of a coin). The store wants you to build a program that is able to store each item in an internal list and later retrieve the information in a number of ways.

Objective

You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class.

Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate data structure to maintain this inventory with an unknown size known only at runtime. Your code is tested in the provided main.cpp.

You will need to implement the following functions:

  • Constructors/Destructors - Initialize your data. Allocate memory if using a native array. The destructor should deallocate memory if using a native array.

  • size() - This should return the number of items currently for sale (this is different from the max).

  • get(size_t) - This should return the item with the matching index. For example if given an index of 3, you should return the item at index 3 in the list.

  • add(Item) - This should add another item for sale in the Gilded Rose by adding it to your inventory.

  • operator[](size_t) - This should perform identical to the get(size_t) function.

Initially the given code will not compile. As you complete the code, the tests should start to pass in main.cpp.

MY CODE:

//GildedRose.h

#ifndef GildedRose_H #define GildedRose_H #include #include

using namespace std;

// This is already done for you...

class Item {

public:

string name; int sellIn; int quality; Item(string, int, int);

};

Item::Item(string new_name, int new_sellIn, int new_quality) : name(new_name), sellIn(new_sellIn), quality(new_quality) {

}

class GildedRose {

private:

vector items;

public:

GildedRose(); ~GildedRose(); size_t size() const; Item& get(size_t); void add(const Item&); Item& operator[](size_t);

};

#endif

//GildedRose.cpp

#include "GildedRose.h"

//constructor

GildedRose::GildedRose() {

//since we are using vector, there is nothing to initialize

}

//destructor

GildedRose::~GildedRose() {

//since we are using vector, there is nothing to deallocate

}

//returns the size

size_t GildedRose::size() const {

return items.size();

}

//returns the item at given index

//causes exception if the index is invalid

Item& GildedRose::get(size_t idx) {

if (idx >= 0 && idx < items.size()) {

return items[idx];

}

throw "Invalid index!";

}

//adds an item to the end of vector

void GildedRose::add(const Item& itm) {

items.push_back(itm);

}

//fetches an item with given index

Item& GildedRose::operator[](size_t idx) {

return get(idx);

}

//main.cpp

#include #include "GildedRose.h" using std::cout; using std::endl; using std::string;

const size_t NUM_TESTS = 9; size_t NUM_PASSED = 0;

// Helper Functions template void assertEquals(string, T, T);

// Main Testing int main(int argc, char const *argv[]) { GildedRose store; store.add(Item("+5 Dexterity Vest", 10, 20)); store.add(Item("Aged Brie", 2, 0)); store.add(Item("Elixir of the Mongoose", 5, 7)); store.add(Item("Sulfuras, Hand of Ragnaros", 0, 80));

assertEquals("GildedRose.size()", static_cast(4), store.size()); assertEquals("GildedRose[0].sellIn", 10, store[0].sellIn); assertEquals("GildedRose.get(3).quality", 80, store.get(3).quality);

store.add(Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)); store.add(Item("Backstage passes to a TAFKAL80ETC concert", 10, 49)); store.add(Item("Backstage passes to a TAFKAL80ETC concert", 5, 49));

assertEquals("GildedRose.size()", static_cast(7), store.size()); assertEquals("GildedRose[6].quality", 49, store[6].quality); assertEquals("GildedRose.get(4).sellIn", 15, store.get(4).sellIn);

store.add(Item("Conjured Mana Cake", 3, 6));

assertEquals("GildedRose.size()", static_cast(8), store.size()); assertEquals("GildedRose[8].name", static_cast("Conjured Mana Cake"), store[7].name); assertEquals("GildedRose.get(8).name", static_cast("Conjured Mana Cake"), store.get(7).name);

cout << endl << "Tests passed: " << NUM_PASSED << "/" << NUM_TESTS << endl;

return 0; }

template void assertEquals(string test_name, T expected, T actual) { if (actual == expected) { cout << "[PASSED] " << test_name << endl; NUM_PASSED++; } else { cout << "[FAILED] " << test_name << " - Expected: " << expected << ", Actual: " << actual << endl; } }

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

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions