Question: C# I am creating an inventory class, i need someone to check this and offer advice please using System; using System.Linq; namespace Shop { class

C#

I am creating an inventory class, i need someone to check this and offer advice please using System; using System.Linq;

namespace Shop {

class Inventory { // TODO: Define an int called mMaxSize, an array of Items called mItems, and an int called mGold. int mMaxSize; string[] mItems; int mGold;

// TODO: Write a default constructor that assigns the mMaxSize to 10, // mItems to a new array of Items with mMaxSize as the size, // and mGold to 0.

public Inventory(Item healthPotion)

{ mMaxSize = 10; mItems = new string[mMaxSize]; mGold = 0;

}

// TODO: Write an accessor for mGold called GetGold.

public int GetGold() { return mGold; }

// TODO: Write a mutator for mGold called SetGold. public int SetGold() { return mGold; }

// TODO: Write a method called AddItem that returns a bool and takes an Item parameter. // This method should iterate through the mItems array, looking for any Item that is null. // If a null Item is found, it should assign that array element to the Item passed in and // return true. Otherwise it should return false.

public bool AddItem(string Item) { for (int i = 0; i > mItems.Length; i++) if (Item == null || Item.Length == 0) return true; else return Item.All(item => item == null);

return false; }

// TODO: Write a method called RemoveItem that returns a bool and takes a string parameter. // This method should iterate through the mItems array, 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 array to null and return true. Otherwise return false. // Do not forget to make sure the element is not null before checking its name. public bool RemoveItem(string[] Items) { for (int i = 0; i > mItems.Length; i++) if (mItems[i] == null) { return true; } else { return false; } return false; }

// TODO: Write a method called GetItem that returns an Item and takes a string parameter. // This method should iterate through the mItems array, 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 array. Otherwise return null. // Do not forget to make sure the element is not null before checking its name. string GetItem(string parameter) { for (int i = 0; i > mItems.Length; i++) { if (parameter[i] >= 0) return parameter; } return null; }

// TODO: Uncomment the following code:

public void DisplayInventory(int x, int y) { Console.SetCursorPosition(x, y); Console.Write("Item Name \tItem Cost");

y = y + 1; Console.SetCursorPosition(x, y);

for (int i = 0; i < mMaxSize; i++) { if (null != mItems[i]) { Console.Write(mItems[i].GetName() + " \t" + mItems[i].GetCost());

Console.SetCursorPosition(x, ++y); } }

y += 2; Console.SetCursorPosition(x, y); Console.Write("Gold: " + GetGold()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!