-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.cpp
167 lines (114 loc) · 3 KB
/
Item.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**************************************************
Project: The Tamer, the Arigamo and the Fuhai Gem
Task: Assignment 2 + 3
Author: Sean Lee
Purpose: Item Class Definition File
The Definition file for the Item class which contains
the attributes and behaviours of the game's items.
**************************************************/
#include "Item.h"
//-------------------------------------//
// constructors and destructors //
//-------------------------------------//
Item::Item() {
itemName = "";
itemType = NOITEM;
itemAmount = 0;
otherData = "";
}
Item::Item(string name, ItemType type, int amount) {
itemName = name;
itemType = type;
itemAmount = amount;
otherData = "";
}
Item::~Item() {
}
//--------------------------------------//
// accessor methods //
//--------------------------------------//
string Item::getName() {
return itemName;
}
ItemType Item::getType() {
return itemType;
}
string Item::getTypeName() {
// returns the name of the item type
// adapted from Week 6 Lab notes
switch (getType()) {
case WEAPON:
return "Weapon";
case CONSUMABLE:
return "Consumable";
case NAVIGATION:
return "Map";
case MAGIC:
return "Magic";
default:
return "No item type has been assigned";
}
}
int Item::getAmount() {
return itemAmount;
}
string Item::getOtherData() {
return otherData;
}
string Item::getDetails() {
// returns the class details of the item as a formatted string
stringstream itemDetails;
itemDetails << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
itemDetails << " Item Name: " << getName() << "\n";
itemDetails << " Item Type: " << getTypeName() << "\n";
itemDetails << " Item Amount: " << getAmount() << "\n";
itemDetails << " Other Data: " << getOtherData() << "\n";
itemDetails << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
return itemDetails.str();
}
//-------------------------------------//
// mutator methods //
//-------------------------------------//
void Item::setName(string name) {
if (name.find_first_not_of(' ') != string::npos) {
itemName = name;
}
else {
displayString(" You have not entered a valid item name to set.\n");
}
}
void Item::setType(ItemType type) {
if (type != NULL) {
itemType = type;
}
else {
displayString(" You have not entered a valid item type to set.\n");
}
}
void Item::setAmount(int amount) {
if (amount >= 0) {
itemAmount = amount;
}
else {
displayString(" You have not entered a valid item amount to set.\n");
}
}
void Item::setOtherData(string data) {
if (data.find_first_not_of(' ') != string::npos) {
otherData = data;
}
else {
displayString(" You have not entered valid other item data to set.\n");
}
}
void Item::updateAmount(int amount) {
if (amount != 0) {
itemAmount += amount;
if (itemAmount < 0) {
itemAmount = 0;
}
}
else {
displayString(" You have not entered a valid amount to update the item's amount.\n");
}
}