-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemList.cpp
More file actions
237 lines (210 loc) · 7.52 KB
/
ItemList.cpp
File metadata and controls
237 lines (210 loc) · 7.52 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "ItemList.h"
// Default constructor
ItemList::ItemList() : head(nullptr), tail(nullptr), size(0), current(nullptr), isItemActivated(false) {}
// Add a new item to the list by prepending it (adding at the front)
void ItemList::addItem(const std::string& name, int cost) {
ItemNode* newItem = new ItemNode(name, cost); // Create new node
// If the list is empty, the new item is both the head and tail
if (head == nullptr) {
head = tail = newItem;
current = head; // Set current to the first item
}
else {
// If the list is not empty, insert the new item at the front
newItem->next = head; // New node points to current head
head->prev = newItem; // Current head points back to new node
head = newItem; // New node becomes the new head
}
++size; // Increment the size of the list
}
// Remove an item from the list by its name
void ItemList::removeItem(const std::string& itemName) {
ItemNode* current = head;
// Traverse the linked list to find the item with the specified name
while (current != nullptr) {
if (current->itemName == itemName) {
// Found the item, now remove it and break out of the loop
if (current->prev != nullptr) {
current->prev->next = current->next; // Update previous node's next pointer
}
else {
// Update head if removing the first item node
head = current->next;
}
if (current->next != nullptr) {
current->next->prev = current->prev; // Update next node's prev pointer
}
else {
// Update tail if removing the last item node
tail = current->prev;
}
delete current; // Free memory
--size; // Decrement list size
std::cout << "Removed item: " << itemName << std::endl;
return; // Exit after removing the first occurrence
}
current = current->next; // Move to the next node
}
std::cout << "Item not found: " << itemName << std::endl;
}
// Check if the list is empty
bool ItemList::isEmpty() const {
return size == 0;
}
// Get the size of the list
int ItemList::getSize() const {
return size;
}
// Function to read and print all items
void ItemList::listAllItems() const {
ItemNode* current = head; // Start from the head of the linked list
std::cout << "Items:" << std::endl;
// Iterate through the linked list until the end is reached
while (current != nullptr) { // Continue until the current node is null
// Print the item name and level of the current node
std::cout << "- " << current->itemName << " (Cost " << current->itemCost << ")" << std::endl;
current = current->next; // Move to the next node in the linked list
}
}
// Check if an item with a specific name exists in the list
bool ItemList::findItem(const std::string& itemName) const {
ItemNode* current = head; // Start from the head of the list
while (current != nullptr) {
if (current->itemName == itemName) {
return true; // Item found
}
current = current->next; // Move to the next node
}
return false; // Item not found
}
// Function to print the current item's details
void ItemList::getCurrentItem() const {
if (current != nullptr) {
std::cout << current->itemName << " (Cost: " << current->itemCost << ")" << std::endl;
}
else {
std::cout << "No current item selected." << std::endl;
}
}
// Move the current pointer to the previous item in the list
void ItemList::moveLeft() {
if (current == nullptr) {
std::cout << "The list is empty or no current item selected." << std::endl;
return;
}
if (current->prev != nullptr) {
current = current->prev; // Move to the previous node
}
else {
std::cout << "Already at the beginning of the list." << std::endl;
}
getCurrentItem();
promptRemoveCurrentItem();
promptActivateItem();
}
// Move the current pointer to the next item in the list
void ItemList::moveRight() {
if (current == nullptr) {
std::cout << "The list is empty or no current item selected." << std::endl;
return;
}
if (current->next != nullptr) {
current = current->next; // Move to the next node
}
else {
std::cout << "Already at the end of the list." << std::endl;
}
getCurrentItem();
promptRemoveCurrentItem();
promptActivateItem();
}
// Getter for item activation status
bool ItemList::getIsItemActivated() const {
return isItemActivated;
}
// Setter for item activation status
void ItemList::setIsItemActivated(bool status) {
isItemActivated = status;
}
// Activate the current item
void ItemList::activateItem() {
if (current != nullptr) {
isItemActivated = true; // Set the current item's activation status to true
std::cout << "Activated item: " << current->itemName << std::endl;
}
else {
std::cout << "No current item to activate." << std::endl;
}
}
// Function to prompt user if they want to activate the item
void ItemList::promptActivateItem() {
char choice;
std::cout << "Do you want to activate this item? (y/n): ";
std::cin >> choice;
if (choice == 'y' || choice == 'Y') {
activateItem();
}
else if (choice == 'n' || choice == 'N') {
return; // Exit the function early if the user chooses not to activate
}
else {
std::cout << "Invalid choice. Please enter 'y' or 'n'." << std::endl;
promptActivateItem(); // Recursively call to prompt again if the input is invalid
}
}
// Remove the current item from the list
void ItemList::removeCurrentItem() {
if (current == nullptr) {
std::cout << "No current item to remove." << std::endl;
return;
}
ItemNode* temp = current; // Store the current item in a temporary pointer
// If there's a previous item, set it as the new current item.
if (current->prev != nullptr) {
current = current->prev;
current->next = temp->next; // Update the previous item to point to the next item.
}
else {
// If the current item is the first item, update the head.
head = current->next;
if (head != nullptr) {
head->prev = nullptr; // Make sure the head points back to nullptr.
}
current = head;
}
// If there's a next item, make sure it points back to the previous item.
if (temp->next != nullptr) {
temp->next->prev = temp->prev;
}
else {
// If the current item is the last one, update the tail.
tail = temp->prev;
}
delete temp; // Free memory
--size; // Decrement list size
std::cout << "Removed the current item." << std::endl;
}
// Prompt the user to remove the current item
void ItemList::promptRemoveCurrentItem() {
char choice;
std::cout << "Do you want to remove this item? (y/n): ";
std::cin >> choice;
if (choice == 'y' || choice == 'Y') {
removeCurrentItem();
}
else if (choice == 'n' || choice == 'N') {
return; // Exit the function early if the user chooses not to remove
}
else {
std::cout << "Invalid choice. Please enter 'y' or 'n'." << std::endl;
promptRemoveCurrentItem(); // Recursively call to prompt again if the input is invalid
}
}
// Destructor
ItemList::~ItemList() {
while (head != nullptr) {
ItemNode* temp = head;
head = head->next;
delete temp;
}
}