//Inventory
//Manages a player's inventory
#include <iostream>
#include <string>
using namespace std;
//Initializes a container
void initialize(const string* const container[], int capacity);
//Tests filled status of a container
bool isFull(const string* const container[], int capacity);
//Tests empty status of a container
bool isEmpty(const string* const container[], int capacity);
//Tests if an item is in a container
bool contains(const string* const pItem, const string* container[], int capacity);
//Displays the item in a container
void display(const string* const container[], int capacity);
//Adds an item to a container
void add(const string* const pItem, const string* container[], int capacity);
//Removes an item from a container
void remove(const string* const pItem, const string* container[], int capacity);
int main()
{
cout << "\tWelcome to Inventory!" << endl;
//Items
const int NUM_ITEMS = 5;
const string items[NUM_ITEMS] = {"sword", "axe", "shield", "gold", "potion"};
//Inventory
const int INVENTORY_CAPACITY = 3;
const string* inventory[INVENTORY_CAPACITY];
initialize(inventory, INVENTORY_CAPACITY);
int choice; //Menu Choice
int itemNumber; //Number of item to add or remove
do
{
cout << endl << "Inventory" << endl;
cout << "---------" << endl << endl;
cout << "0 - Quit" << endl;
cout << "1 - Display items in the inventory" << endl;
cout << "2 - Add an item to the inventory" << endl;
cout << "3 - Remove an item from the inventory" << endl;
cout << endl << endl << "Choice: ";
cin >> choice;
cout << endl;
switch(choice)
{
case 0:
cout << "Goodbye." << endl;
break;
case 1:
cout << "Inventory:" << endl;
display(inventory, INVENTORY_CAPACITY);
break;
case 2:
for (int i = 0; i < NUM_ITEMS; i++)
{
cout << i << " - " << items[i] << endl;
}
do
{
cout << "Enter the number of the item to add: ";
cin >> itemNumber;
} while(itemNumber < 0 || itemNumber >= NUM_ITEMS);
//Pass address of element items[itemNumber]
add(&items[itemNumber], inventory, INVENTORY_CAPACITY);
break;
case 3:
for (int i = 0; i < NUM_ITEMS; i++)
{
cout << i << " - " << items[i] << endl;
}
do
{
cout << "Enter the number of the item to remove: ";
cin >> itemNumber;
} while(itemNumber < 0 || itemNumber >= NUM_ITEMS);
//Pass address of element items[itemNumber]
remove(&items[itemNumber], inventory, INVENTORY_CAPACITY);
break;
default:
cout << "Sorry, " << choice << " isn't a valid choice." << endl;
}
} while(choice !=0);
return 0;
}
//Initialize Function
void initialize(const string* container[], int capacity)
{
//Set all elements to NULL
for (int i = 0; i < capacity; i++)
{
container[i] = NULL;
}
}
//isFull Function
bool isFull(const string* container[], int capacity)
{
bool full = true;
int i = 0;
while (full && i < capacity)
{
//If there's at least one empty slot...
if(container[i] == NULL)
{
//Then the container isn't full
full = false;
}
++i;
}
return full;
}
//isEmpty Function
bool isEmpty(const string* container[], int capacity)
{
bool empty = true;
int i = 0;
while(empty && i < capacity)
{
//If a slot isn't empty
if(container[i] != NULL)
{
//Then the container isn't empty
empty = false;
}
++i;
}
return empty;
}
//Containers Function
bool contains(const string* const pItem, const string* container[], int capacity)
{
bool has = false;
int i = 0;
while(!has && i < capacity)
{
if(container[i] == pItem)
{
has = true;
}
i++;
}
return has;
}
//Display Function
void display(const string* const container[], int capacity)
{
//If container is empty, display message saying so and return
if (isEmpty(container, capacity))
{
cout << "<Empty>" << endl;
return;
}
//Otherwise, send all strings pointed to by elements of container to cout
for (int i = 0; i < capacity; ++i)
{
if(container[i] != NULL)
{
cout << *(container[i]) << endl;
}
}
}
//Add Function
void add(const string* const pItem, const string* container[], int capacity)
{
if (pItem == NULL)
{
return;
}
if(contains(pItem, container, capacity))
{
cout << "Item already present. Can't add." << endl;
return;
}
if (isFull(container, capacity))
{
cout << "Container full. Cant add." << endl;
return;
}
//Find first null pointer in container, point it to string pItem points to
bool found = false;
int i = 0;
while (!found && i < capacity)
{
if(container[i] == NULL)
{
container[i] = pItem; //Add pointer
found = true;
}
i++;
}
}
//Remove Function
void remove(const string* const pItem, const string* container[], int capacity)
{
if(pItem == NULL)
{
return;
}
//Set element that is equal to pItem to NULL
bool found = false;
int i = 0;
while (!found && i < capacity)
{
if(container[i] == pItem)
{
container[i] = NULL; //Remove pointer
found = true;
}
++i;
}
//If item wasn't found, say so
if(!found)
{
cout << "Item not found. Can't remove." << endl;
}
}