Advertisement
Guest User

Untitled

a guest
Nov 7th, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Item {
  7. private:
  8.     unsigned long itemID;
  9.     string itemName;
  10.     float itemCost;
  11.     int quantity;
  12. public:
  13.     static bool isNoItem(Item& item)
  14.     {
  15.         return item.getID() == -1 && item.getCost() == -1 && item.getName() == "No Item" && item.getQuant() == -1;
  16.     }
  17.  
  18.     Item(int id, string name, int cost, int quantity) : itemID(id), itemName(name), itemCost(cost), quantity(quantity) { }
  19.  
  20.     void setID(unsigned long itemID) { this->itemID = itemID; };
  21.     void setName(string itemName) { this->itemName = itemName; };
  22.     void setCost(float itemCost) { this->itemCost = itemCost; };
  23.     void setQuant(int quantity) { this->quantity = quantity; };
  24.     unsigned long getID(void) { return itemID; };
  25.     string getName(void) { return itemName; };
  26.     float getCost(void) { return itemCost; };
  27.     int getQuant(void) { return quantity; };
  28. };
  29.  
  30. class ItemManager
  31. {
  32.     vector<Item> items;
  33.  
  34. public:
  35.     ItemManager() { items = vector<Item>(); }
  36.  
  37.     int itemsLength() { return items.size(); }
  38.  
  39.     void addItem(int id, string name, int cost, int quantity)
  40.     {
  41.         items.push_back(Item(id, name, cost, quantity));
  42.     }
  43.  
  44.     Item getItemByIndex(int index)
  45.     {
  46.         return items[index];
  47.     }
  48.  
  49.     Item getItemByID(int id)
  50.     {
  51.         for (int i = 0; i < itemsLength(); i++)
  52.             if (items[i].getID() == id)
  53.                 return items[i];
  54.  
  55.         return Item(-1, "No Item", -1, -1);
  56.     }
  57.  
  58.     Item getItemByName(string name)
  59.     {
  60.         for (int i = 0; i < itemsLength(); i++)
  61.             if (items[i].getName() == name)
  62.                 return items[i];
  63.  
  64.         return Item(-1, "No Item", -1, -1);
  65.     }
  66. };
  67.  
  68. void printItem(Item item)
  69. {
  70.     if (Item::isNoItem(item))
  71.     {
  72.         cout << "No Item Found." << endl;
  73.         return;
  74.     }
  75.  
  76.     cout << endl;
  77.     cout << "ID: " << item.getID() << endl;
  78.     cout << "Name: " << item.getName() << endl;
  79.     cout << "Cost: " << item.getCost() << endl;
  80.     cout << "Quantity: " << item.getQuant() << endl;
  81.     cout << endl;
  82. }
  83.  
  84. int main(void) {
  85.     ItemManager itemManager = ItemManager();
  86.     int sel = 0;
  87.     int index = 0;
  88.  
  89.     itemManager.addItem(1, "Sword", 50, 1);
  90.     itemManager.addItem(2, "Spear", 25, 1);
  91.  
  92.     cout << "Inventory Management System Menu";
  93.     cout << "\n--------------------------------\n";
  94.     do
  95.     {
  96.         cout << "1. Add a new item\n2. Print item list\n3. Find item by ID\n4. Find item by name\n5. Quit";
  97.         cout << "\nPlease select an option: ";
  98.         cin >> sel;
  99.  
  100.         if (sel == 1)
  101.         {
  102.             int id = -1;
  103.             string name = "";
  104.             int cost;
  105.             int quantity;
  106.  
  107.             cout << "Enter Item ID: ";
  108.             cin >> id;
  109.             cout << "Enter Item Name: ";
  110.             cin >> name;
  111.             cout << "Enter Item Cost: ";
  112.             cin >> cost;
  113.             cout << "Enter Item Quantity: ";
  114.             cin >> quantity;
  115.  
  116.             itemManager.addItem(id, name, cost, quantity);
  117.         }
  118.  
  119.         else if (sel == 2)
  120.         {
  121.             for (int i = 0; i < itemManager.itemsLength(); i++)
  122.             {
  123.                 Item item = itemManager.getItemByIndex(i);
  124.                 printItem(item);
  125.             }
  126.         }
  127.  
  128.         else if (sel == 3)
  129.         {
  130.             int idLookup = -1;
  131.             cout << "Enter Item ID: ";
  132.             cin >> idLookup;
  133.             Item item = itemManager.getItemByID(idLookup);
  134.             printItem(item);
  135.         }
  136.  
  137.         else if (sel == 4)
  138.         {
  139.             string itemName = "";
  140.             cout << "Enter Item Name: ";
  141.             cin >> itemName;
  142.             Item item = itemManager.getItemByName(itemName);
  143.             printItem(item);
  144.         }
  145.     }
  146.  
  147.     while (sel != 5);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement