Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Item {
- private:
- unsigned long itemID;
- string itemName;
- float itemCost;
- int quantity;
- public:
- static bool isNoItem(Item& item)
- {
- return item.getID() == -1 && item.getCost() == -1 && item.getName() == "No Item" && item.getQuant() == -1;
- }
- Item(int id, string name, int cost, int quantity) : itemID(id), itemName(name), itemCost(cost), quantity(quantity) { }
- void setID(unsigned long itemID) { this->itemID = itemID; };
- void setName(string itemName) { this->itemName = itemName; };
- void setCost(float itemCost) { this->itemCost = itemCost; };
- void setQuant(int quantity) { this->quantity = quantity; };
- unsigned long getID(void) { return itemID; };
- string getName(void) { return itemName; };
- float getCost(void) { return itemCost; };
- int getQuant(void) { return quantity; };
- };
- class ItemManager
- {
- vector<Item> items;
- public:
- ItemManager() { items = vector<Item>(); }
- int itemsLength() { return items.size(); }
- void addItem(int id, string name, int cost, int quantity)
- {
- items.push_back(Item(id, name, cost, quantity));
- }
- Item getItemByIndex(int index)
- {
- return items[index];
- }
- Item getItemByID(int id)
- {
- for (int i = 0; i < itemsLength(); i++)
- if (items[i].getID() == id)
- return items[i];
- return Item(-1, "No Item", -1, -1);
- }
- Item getItemByName(string name)
- {
- for (int i = 0; i < itemsLength(); i++)
- if (items[i].getName() == name)
- return items[i];
- return Item(-1, "No Item", -1, -1);
- }
- };
- void printItem(Item item)
- {
- if (Item::isNoItem(item))
- {
- cout << "No Item Found." << endl;
- return;
- }
- cout << endl;
- cout << "ID: " << item.getID() << endl;
- cout << "Name: " << item.getName() << endl;
- cout << "Cost: " << item.getCost() << endl;
- cout << "Quantity: " << item.getQuant() << endl;
- cout << endl;
- }
- int main(void) {
- ItemManager itemManager = ItemManager();
- int sel = 0;
- int index = 0;
- itemManager.addItem(1, "Sword", 50, 1);
- itemManager.addItem(2, "Spear", 25, 1);
- cout << "Inventory Management System Menu";
- cout << "\n--------------------------------\n";
- do
- {
- cout << "1. Add a new item\n2. Print item list\n3. Find item by ID\n4. Find item by name\n5. Quit";
- cout << "\nPlease select an option: ";
- cin >> sel;
- if (sel == 1)
- {
- int id = -1;
- string name = "";
- int cost;
- int quantity;
- cout << "Enter Item ID: ";
- cin >> id;
- cout << "Enter Item Name: ";
- cin >> name;
- cout << "Enter Item Cost: ";
- cin >> cost;
- cout << "Enter Item Quantity: ";
- cin >> quantity;
- itemManager.addItem(id, name, cost, quantity);
- }
- else if (sel == 2)
- {
- for (int i = 0; i < itemManager.itemsLength(); i++)
- {
- Item item = itemManager.getItemByIndex(i);
- printItem(item);
- }
- }
- else if (sel == 3)
- {
- int idLookup = -1;
- cout << "Enter Item ID: ";
- cin >> idLookup;
- Item item = itemManager.getItemByID(idLookup);
- printItem(item);
- }
- else if (sel == 4)
- {
- string itemName = "";
- cout << "Enter Item Name: ";
- cin >> itemName;
- Item item = itemManager.getItemByName(itemName);
- printItem(item);
- }
- }
- while (sel != 5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement