Sure, here's an example implementation of an inventory system in C++ that allows for adding, removing, and trading items. The items are defined in an ItemManager class, which stores information about each item's ID, name, and maximum capacity. The inventory itself is implemented as a std::vector of InventoryItem objects, which contain a pointer to the corresponding ItemManager item and the current quantity of that item in the inventory.
#include <iostream>
#include <vector>
#include <algorithm>
class ItemManager {
public:
struct Item {
int id;
std::string name;
int max_capacity;
};
ItemManager() {
items_ = {
{1, "Item 1", 10},
{2, "Item 2", 5},
{3, "Item 3", 20},
{4, "Item 4", 15},
{5, "Item 5", 8}
};
}
const Item* get_item(int id) const {
auto it = std::find_if(items_.begin(), items_.end(),
[id](const Item& item) { return item.id == id; });
return it != items_.end() ? &(*it) : nullptr;
}
private:
std::vector<Item> items_;
};
class Inventory {
public:
struct InventoryItem {
const ItemManager::Item* item;
int quantity;
};
void add_item(int id, int quantity) {
const ItemManager::Item* item = item_manager_.get_item(id);
if (!item) {
std::cout << "Error: item with ID " << id << " not found." << std::endl;
return;
}
auto it = std::find_if(items_.begin(), items_.end(),
[item](const InventoryItem& inv_item) { return inv_item.item == item; });
if (it != items_.end()) {
it->quantity += quantity;
} else {
items_.push_back({item, quantity});
}
}
void remove_item(int id, int quantity) {
const ItemManager::Item* item = item_manager_.get_item(id);
if (!item) {
std::cout << "Error: item with ID " << id << " not found." << std::endl;
return;
}
auto it = std::find_if(items_.begin(), items_.end(),
[item](const InventoryItem& inv_item) { return inv_item.item == item; });
if (it != items_.end()) {
if (it->quantity >= quantity) {
it->quantity -= quantity;
if (it->quantity == 0) {
items_.erase(it);
}
} else {
std::cout << "Error: not enough " << item->name << " in inventory." << std::endl;
}
} else {
std::cout << "Error: " << item->name << " not found in inventory." << std::endl;
}
}
void trade_item(int id, int quantity, Inventory& other_inventory) {
const ItemManager::Item* item = item_manager_.get_item(id);
if (!item) {
std::cout << "Error: item with ID " << id << " not found." << std::endl;
return;
}
auto it = std::find_if(items_.begin(), items_.end(),
[item](const InventoryItem& inv_item) { return inv_item.item == item; });
if (it != items_.end()) {
if (it->quantity >= quantity) {
it->quantity -= quantity;
if (it->quantity == 0) {
items_.erase(it);
}
other_inventory.add_item(id, quantity);
} else {
std::cout << "Error: not enough " << item->name << " in inventory." << std::endl;
}
} else {
std::cout << "Error: " << item->name << " not found in inventory." << std::endl;
}
}
void print_inventory() const {
std::cout << "Inventory:" << std::endl;
for (const auto& inv_item : items_) {
std::cout << " " << inv_item.item->name << ": " << inv_item.quantity << std::endl;
}
}
private:
ItemManager item_manager_;
std::vector<InventoryItem> items_;
};
int main() {
Inventory inventory1;
Inventory inventory2;
inventory1.add_item(1, 5);
inventory1.add_item(2, 3);
inventory1.add_item(3, 10);
inventory1.print_inventory();
inventory1.remove_item(1, 2);
inventory1.remove_item(2, 4);
inventory1.remove_item(4, 1);
inventory1.print_inventory();
inventory1.trade_item(3, 5, inventory2);
inventory1.trade_item(4, 3, inventory2);
inventory1.trade_item(5, 10, inventory2);
inventory1.print_inventory();
inventory2.print_inventory();
return 0;
}
In this implementation, the ItemManager class is responsible for storing information about each item, such as its ID, name, and maximum capacity. The Inventory class uses a std::vector of InventoryItem objects to store the current quantity of each item in the inventory, along with a pointer to the corresponding ItemManager item.
The add_item, remove_item, and trade_item methods allow items to be added, removed, and traded between inventories, respectively. These methods check that the item exists in the ItemManager and that there is enough quantity of the item in the inventory before making any changes.
The print_inventory method simply prints out the current contents of the inventory.
In the main function, we create two inventories and perform some operations on them to test the functionality of the inventory system.