fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. #include <memory>
  5.  
  6. /**************** Item: defined as a pure abstract class ************************ **/
  7. class Item
  8. {
  9. protected:
  10. int m_code;
  11. int m_price;
  12. int m_quantity;
  13. public:
  14. Item() = default;
  15.  
  16. void setCode(const int& code) { m_code = code; }
  17. void setPrice(const int& price) { m_price = price; }
  18. void setQuantity(const int& quantity) { m_quantity = quantity; }
  19.  
  20. /*const int& getCode()const { return m_code; }
  21.   const int& getPrice()const { return m_price; }
  22.   const int& getQuantity()const { return m_quantity; }*/
  23.  
  24. virtual int calculateStockValue() = 0;
  25. virtual ~Item(){ };
  26. };
  27.  
  28. /** ItemTable : inherits from Item, to store code - price - quantity to hash table **/
  29. class ItemTable: public Item
  30. {
  31. private:
  32. std::unordered_map<int, std::pair<int,int>> m_itemTable;
  33. public:
  34. ItemTable() = default;
  35. //(ii) to display the data members according to the item code
  36. void displayItem(const int& code)
  37. {
  38. if(m_itemTable.find(code) != m_itemTable.cend())
  39. {
  40. auto it = m_itemTable.find(code);
  41. std::cout << "Item code = " << it->first;
  42. std::cout << "\t price = " << it->second.first;
  43. std::cout << "\t quantity = " << it->second.second << std::endl;
  44. }
  45. else
  46. std::cout << "Invalid Item code!" << std::endl;
  47. }
  48. // (iii) appending Item to the list
  49. void appendItem(const int& code, const int& price, const int& quantity)
  50. {
  51. //(i) input the data members
  52. setCode(code);
  53. setPrice(price);
  54. setQuantity(quantity);
  55. m_itemTable.emplace(m_code, std::make_pair(m_price, m_quantity));
  56. }
  57. // (iv) delete the Item from the list
  58. void deleteItem(const int& code)
  59. {
  60. if(m_itemTable.find(code) != m_itemTable.cend())
  61. {
  62. std::cout << "The item with code: " << code
  63. << " has been deleted successfully!" <<std::endl;
  64. m_itemTable.erase(m_itemTable.find(code));
  65. }
  66. else
  67. std::cout << "Invalid Item code!" << std::endl;
  68. }
  69. // (v) total value of the stock
  70. virtual int calculateStockValue() override
  71. {
  72. int m_totalStock = 0;
  73. for(const auto &it:m_itemTable)
  74. m_totalStock += (it.second.first * it.second.second);
  75.  
  76. return m_totalStock;
  77. }
  78. virtual ~ItemTable(){ };
  79. };
  80. /********************* main ******************************/
  81. int main()
  82. {
  83. int number;
  84. int code;
  85. std::unique_ptr<ItemTable> obj = std::make_unique<ItemTable>();
  86.  
  87. std::cout << "Enter total number of Items : "; std::cin >> number;
  88.  
  89. for(int i = 0; i<number; ++i)
  90. {
  91. int code, price, quantity;
  92. std::cout << i+1 << ": Enter the Item code = "; std::cin >> code ;
  93. std::cout << i+1 << ": Enter the Item price = "; std::cin >> price ;
  94. std::cout << i+1 << ": Enter the Item quantity = "; std::cin >> quantity ;
  95. obj->appendItem(code, price, quantity);
  96. }
  97.  
  98. std::cout << "Total Value of stock = " << obj->calculateStockValue() << std::endl;
  99.  
  100. std::cout << "Enter code of the Item to be displayed: "; std::cin >> code;
  101. obj->displayItem(code);
  102.  
  103. std::cout << "Enter code to delete the Item : "; std::cin >> code;
  104. obj->deleteItem(code);
  105.  
  106. std::cout << "Total Value of stock = " << obj->calculateStockValue() << std::endl;
  107.  
  108. return 0;
  109. }
  110.  
Success #stdin #stdout 0s 4164KB
stdin
3
1 20 2
2 40 2
3 80 2
2
3
stdout
Enter total number of Items : 1: Enter the Item code = 1: Enter the Item price = 1: Enter the Item quantity = 2: Enter the Item code = 2: Enter the Item price = 2: Enter the Item quantity = 3: Enter the Item code = 3: Enter the Item price = 3: Enter the Item quantity = Total Value of stock = 280
Enter code of the Item to be displayed: Item code = 2	 price = 40	 quantity = 2
Enter code to delete the Item : The item with code: 3 has been deleted successfully!
Total Value of stock = 120