Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having issues with my inventory class maybe in the member fields or maybe how im setting up one of the methods... i am

I am having issues with my inventory class maybe in the member fields or maybe how im setting up one of the methods... i am including what i have for my inventory class as well as my item class class Item
{
/* TODO: Create an Item class
*===================================
* The class should contain 2 data members:
*- A string called name
*- An int called cost.
*
* This class should have a default constructor
* that sets name to "" and cost to 0.
*
* It should also have an overloaded constructor
* that accepts 2 parameters:
*- A string
*- An int.
*
* Write getters/accessors for each data member.
* They should be called GetName and GetCost.
*
* Write setters/mutators for each data member.
* They should be called SetName and SetCost.
*/
//define attributes
string name;
int cost;
public:
//default construcor
Item(){
//set default values to "" for name and 0 for cost
name ="";
cost =0;
}
//overloaded constuctor
Item(string name, int cost){
//set paramaters to this->
this->name = name;
this->cost = cost;
}
//method to get name
string GetName(){
//return name
return name;
}
//method to get cost
int GetCost(){
//return cost
return cost;
}
//method to set name with name parameter
void SetName(string name){
//set parameter to this->
this->name = name;
}
//method to set cost with cost parameter
void SetCost(int cost){
//set parameter to this->
this->cost = cost;
}
};class Inventory
{
/*
* TODO: Define three member fields
*=================================
*- An int called mMaxSize
*- An vector of Items called mItems
*- An int called mGold
*/
int mMaxSize;
int mGold;
vectormItems;
public:
/*
* TODO: Write a default constructor
*======================================
* By default, it should:
*- Assigns the mMaxSize to 10,
*- Assign mItems to a new vector of Items with mMaxSize as the size
*- Set mGold to 50.
*/
Inventory(){
mMaxSize =10;
mGold =50;
vectormItems{ mMaxSize };
}
// TODO: Create a Getter and a Setter for the mGold field named "GetGold" and "SetGold"
int GetGold(){
return mGold;
}
int SetGold(int gold){
mGold = gold;
}
/* TODO: Write a method called AddItem
*======================================
* The method should return a bool returns
* a bool and takes an Item parameter.
*
* This method should iterate through the
* mItems vector, looking for any vector element
* that is default.
*
* If a default Item is found, it should assign
* that vector element to the Item passed in and
* return true. Otherwise it should return false.
*/
bool AddItem(Item newItem){
bool result;
for (int i =0; i < mMaxSize; i++){
if (mItems[i]= Item()){
mItems[i]= newItem;
result = true;
}
else {
result = false;
}
}return result;
}
/* TODO: Write a method called RemoveItem
*==========================================
* This method should return a bool and takes
* a string parameter.
*
* This method should iterate through the
* mItems vector, looking for an Item that
* has the same name as the parameter.
*
* If it finds a match it should set that
* element of the mItems vector to a default
* item and return true. Otherwise return false.
*/
bool RemoveItem(string itemName){
bool result;
for (int i =0; i < mMaxSize; i++){
if (mItems[i]!= Item() && mItems[i].GetName()== itemName){
mItems[i]= Item();
result = true;
}
else {
result = false;
}
}return result;
}
/* TODO: Write a method called GetItem
*=======================================
* This method should return an Item and takes
* a string parameter.
*
* This method should iterate through the mItems
* vector, looking for an Item that has the same
* name as the parameter.
*
* If it finds a match it should return that element
* of the mItems vector. Otherwise return a default item.
*/
Item GetItem(string itemName){
for (int i =0; i < mMaxSize; i++){
if (mItems[i]!= Item() && mItems[i].GetName()== itemName){
return mItems[i];
}
}
}
//TODO: Uncomment the following code:
void DisplayInventory(int x, int y)
{
Console::SetCursorPosition(x,++y);
Console::Write("Item Name");
Console::SetCursorPosition(x +17, y);
Console::Write("Item Cost");
for (int i =0; i < mMaxSize; i++)
{
if (!mItems[i].GetName().empty())
{
Console::SetCursorPosition(x,++y);
Console::Write(mItems[i].GetName());
Console::SetCursorPosition(x +17, y);
Console::Write(std::to_string(mItems[i].GetCost()));
}
}
y +=2;
Console::SetCursorPosition(x,++y);

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

Students also viewed these Databases questions