CS396-Projects/assignmentThree/inventory.cpp

262 lines
7.9 KiB
C++

/*
Program Name: Inventory
Process: Write a program that dynamically manages an inventory system for a roleplaying
game. The inventory should be a dynamic array of the following items {“Sword”,
“Health”, “Shield”, “Food”}. Each item in this array should have a quantity
quantity. The quantity should indicate how many of such items exist, for example
the inventory could have 3 swords, 2 healths, 1 shield and 0 food. The array is
dynamic since there is no requirement that all items have to be present in the array.
Starting empty, allow your user to pick the following options:
● Add an item to the inventory and continue adding till you have at least one
of each item.
● Display the inventory
● Use an item (reducing the quantity by 1)
● Delete an item from the inventory when the quantity is 0
Hint: Use new and delete operators to allocate and deallocate memory for the
array. Ensure that you handle the memory properly to avoid any memory leaks.
Function Input/Parameters: ???
Function Output/Parameters: ???
Function Output/Returned: ???
Device Input/Device: ???
Device Output/Monitor: ???
Dependencies: ???
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <random>
#include <algorithm>
#include <time.h>
using namespace std;
typedef enum
{
QUIT_INVENTORY = -1,
START_INVENTORY_MANAGEMENT,
KEEP_INVENTORY_MANAGING,
DISPLAY_INVENTORY
} MENU_OPTIONS;
// class definitions
class Inventory
{
private:
vector<string> inv;
public:
Inventory()
{
// default constructor
}
~Inventory()
{
// default destructor
}
// accessor functions
// need a find function (use vector find?)
int findItem(string itemFind)
{
// use the find function, then compare each raw value
auto vecIterator = find(inv.begin(), inv.end(), itemFind);
if(vecIterator != inv.end())
{
// return the index if found
return vecIterator - inv.begin();
}
// otherwise, return 'not found'
return -1;
}
// need a display function, iterate and print each?
void displayInventory()
{
// print out the entire inventory using a for loop.
int inventorySize = inv.size();
cout << "Inventory Items:" << endl;
for(int vecIterator = 0; vecIterator < inventorySize - 1; vecIterator++)
{
cout << "\tItem " << vecIterator + 1 << ": " << inv[vecIterator] << endl;
}
cout << endl;
}
// mutator functions
// need a use function (finds and removes the item)
void useItem()
{
// ask user which item they want to use.
int pickedItem;
bool validItem = false;
string itemToFind;
int foundItemIndex = 0;
// ask user which item they want to add.
cout << "Please pick an item to add to the inventory: " << endl;
cout << "\t1: Sword" << endl;
cout << "\t2: Health" << endl;
cout << "\t3: Shield" << endl;
cout << "\t4: Food" << endl;
cout << "Item Number: ";
while(!validItem)
{
cin >> pickedItem;
cin.ignore();
if(pickedItem == 1)
{
validItem = true;
itemToFind = "Sword";
}
else if(pickedItem == 2)
{
validItem = true;
itemToFind = "Health";
}
else if(pickedItem == 3)
{
validItem = true;
itemToFind = "Shield";
}
else if(pickedItem == 4)
{
validItem = true;
itemToFind = "Food";
}
else
{
cout << "Invalid Item. Please enter again:" << endl;
cout << "Item Number: ";
}
}
cout << endl;
// use the itemToFind with the find function
foundItemIndex = findItem(itemToFind);
// remove said item at index if found.
if(foundItemIndex > -1)
{
// remove item at index
inv.erase(inv.begin() + foundItemIndex);
}
}
// need an add function (prompts user to add an item, get num input to ask)
void addItem()
{
int pickedItem;
bool validItem = false;
// ask user which item they want to add.
cout << "Please pick an item to add to the inventory: " << endl;
cout << "\t1: Sword" << endl;
cout << "\t2: Health" << endl;
cout << "\t3: Shield" << endl;
cout << "\t4: Food" << endl;
cout << "Item Number: ";
while(!validItem)
{
cin >> pickedItem;
cin.ignore();
if(pickedItem == 1)
{
validItem = true;
inv.push_back("Sword");
}
else if(pickedItem == 2)
{
validItem = true;
inv.push_back("Health");
}
else if(pickedItem == 3)
{
validItem = true;
inv.push_back("Shield");
}
else if(pickedItem == 4)
{
validItem = true;
inv.push_back("Food");
}
else
{
cout << "Invalid Item. Please enter again:" << endl;
cout << "Item Number: ";
}
}
cout << endl;
}
};
// function definitions
// main function
int main()
{
int menuSelInt = 4;
int iterateCount = 0;
MENU_OPTIONS menuFunction = START_INVENTORY_MANAGEMENT;
Inventory currentInventory;
// menu functionality
do
{
// print out the menu with all of the options available.
printf("Inventory Manager Program\n");
printf(" Menu Options:\n");
printf(" 1: Add Item to Inventory\n");
printf(" 2: Display Items in Inventory\n");
printf(" 3: Use Item in Inventory\n");
printf(" 4: Quit Inventory Manager\n");
printf("Select a menu option: ");
// ask for the menu selection
scanf("%d", &menuSelInt);
// print a new line
printf("\n");
if(menuSelInt == 1)
{
if(iterateCount == 0)
{
menuFunction = START_INVENTORY_MANAGEMENT;
}
else
{
menuFunction = KEEP_INVENTORY_MANAGING;
}
iterateCount = iterateCount + 1;
}
else if(menuSelInt == 2)
{
// use the display function defined, increase the iterateCount
menuFunction = DISPLAY_INVENTORY;
iterateCount = iterateCount + 1;
currentInventory.displayInventory();
}
else if(menuSelInt == 3)
{
// use the 'useItem' function defined. increase the iterateCount,
// use the DISPLAY_INVENTORY code since I can't be bothered to make another one.
menuFunction = DISPLAY_INVENTORY;
iterateCount = iterateCount + 1;
currentInventory.useItem();
}
else
{
// decide to quit the inventory manager if all else fails.
menuFunction = QUIT_INVENTORY;
}
switch (menuFunction)
{
case START_INVENTORY_MANAGEMENT:
cout << "Starting Inventory Management." << endl;
// add item to inventory.
currentInventory.addItem();
break;
case KEEP_INVENTORY_MANAGING:
cout << "Continuing Inventory Management." << endl;
// add item to inventory.
currentInventory.addItem();
break;
default:
break;
}
} while (menuFunction != QUIT_INVENTORY);
return 0;
}