-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cpp
More file actions
83 lines (77 loc) · 1.96 KB
/
Copy pathInventory.cpp
File metadata and controls
83 lines (77 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/************************************************************
** Author: Peter Nguyen
** CS162-400, Final project
** Date: 6/6/15
** Description: Implementation file for Inventory class
************************************************************/
#include "Inventory.hpp"
#include <iostream>
using std::cout;
// Constructor
Inventory::Inventory()
{
maxItems = 5;
}
/************************************************************
** Inventory::addItem
** Description: Adds an item to the inventory
** Parameters: string itm
************************************************************/
void Inventory::addItem(std::string itm)
{
if (contents.size() == maxItems)
cout << "The bag is full.\n";
else
contents.push_back(itm);
}
/************************************************************
** Inventory::useItem
** Description: Finds item and removes it from Inventory.
** Returns true if the item was used.
** Parameters: string itm
************************************************************/
bool Inventory::useItem(std::string itm)
{
bool itemFound = false;
if (contents.empty())
{
cout << "The bag is empty.\n";
return itemFound;
}
else
{
for (int i = 0; i < contents.size(); i++)
{
if (contents[i] == itm)
{
contents.erase(contents.begin() + i);
itemFound = true;
cout << "You used the " << itm << "\n";
return itemFound;
}
}
if (!itemFound)
{
cout << "You don't have the " << itm << "\n";
return itemFound;
}
}
}
/************************************************************
** Inventory::printContents
** Description: Prints contents of inventory
** Parameters: none
************************************************************/
void Inventory::printContents()
{
if (contents.empty())
cout << "The bag is empty.\n";
else
{
cout << "Bag contents:\n";
for (int i = 0; i < contents.size(); i++)
{
cout << contents[i] << "\n";
}
}
}