Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #pragma once
  2. #include "Collection.h"
  3. #include "Item.h"
  4. #include <functional>
  5. #include <list>
  6. using namespace std;
  7. class Inventory : public Collection<const Item>
  8. {
  9. public:
  10. const list<const Item*> getInventoryList()const {
  11.  
  12. return inventoryList;
  13. };
  14. bool compareGoldToWeight();
  15. virtual unsigned int getSize() const override
  16. {
  17. return size;
  18. };
  19.  
  20. void forEach(
  21. const function<void(const Item&)>& accept)
  22. const override
  23. {
  24. for (auto i : inventoryList) {
  25. accept(*i);
  26.  
  27. }
  28. };
  29.  
  30. void forEach(
  31. const function<void(const Item&)>& accept)override
  32. {
  33. for (auto i : inventoryList) {
  34. accept(*i);
  35.  
  36. }
  37. };
  38.  
  39.  
  40.  
  41. void add(const Item* item)
  42. {
  43.  
  44.  
  45. double itemGoldToWeight = (double)(*item).getGoldValue() / (*item).getWeight();
  46. auto invetoryIterator = inventoryList.begin();
  47. for (auto i: inventoryList) {
  48. double currentIRatio = (double)(i)->getGoldValue() / (i)->getWeight();
  49. if (itemGoldToWeight < currentIRatio) {
  50. invetoryIterator++;//iterate to figure out were to insert it into the list
  51.  
  52. }
  53.  
  54.  
  55. }
  56. inventoryList.insert(invetoryIterator, item);
  57.  
  58. ++size;
  59.  
  60. };
  61.  
  62.  
  63. const Item* getFirst() const
  64. {
  65. return inventoryList.front();
  66. };
  67. void dropItem(const Item* item)
  68. {
  69. bool isItemFound = false;
  70. for (auto i : inventoryList) {
  71. if (item == i) {
  72. //found the item
  73. isItemFound = true;
  74. break;
  75. }
  76. }
  77. if (isItemFound == true) {
  78.  
  79. inventoryList.remove(item);
  80. --size;
  81. }
  82. else {
  83.  
  84. throw logic_error("Item not found");
  85. }
  86. };
  87. private:
  88. unsigned int size = 0;
  89. list<const Item*> inventoryList;
  90.  
  91.  
  92. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement