Finish Assignment 3, inventory program has finished.

This commit is contained in:
Charles Descamps 2024-10-08 09:30:47 -07:00
parent 60572aefd9
commit 06bcc9edc9
2 changed files with 204 additions and 2 deletions

Binary file not shown.

View file

@ -33,11 +33,19 @@ Dependencies: ???
using namespace std; using namespace std;
typedef enum
{
QUIT_INVENTORY = -1,
START_INVENTORY_MANAGEMENT,
KEEP_INVENTORY_MANAGING,
DISPLAY_INVENTORY
} MENU_OPTIONS;
// class definitions // class definitions
class Inventory class Inventory
{ {
private: private:
// variables vector<string> inv;
public: public:
Inventory() Inventory()
@ -48,6 +56,132 @@ public:
{ {
// default destructor // 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 // function definitions
@ -55,6 +189,74 @@ public:
// main function // main function
int main() 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; return 0;
} }