Stock.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Created by jovian on 20/08/17.
  3. //
  4. #include "Stock.h"
  5. Stock::Stock() {
  6. setZero();
  7. }
  8. int Stock::set(Resource res, int val) {
  9. int prev(m_tab[res]);
  10. m_tab[res] = val;
  11. return prev;
  12. }
  13. int Stock::add(Resource res, int val) {
  14. m_tab[res] += val;
  15. return m_tab[res];
  16. }
  17. int Stock::get(Resource res) const {
  18. return m_tab[res];
  19. }
  20. void Stock::setZero() {
  21. for (int& res : m_tab) {
  22. res = 0;
  23. }
  24. }
  25. void Stock::setPositive() {
  26. for (int& res : m_tab) {
  27. if (res < 0)
  28. res = 0;
  29. }
  30. }
  31. Stock &Stock::operator+=(Stock const &stock) {
  32. for (int id(0); id < RESOURCE_NUMBER; ++id) {
  33. m_tab[id] += stock.m_tab[id];
  34. }
  35. return *this;
  36. }
  37. Stock &Stock::operator-=(Stock const &stock) {
  38. for (int id(0); id < RESOURCE_NUMBER; ++id) {
  39. m_tab[id] -= stock.m_tab[id];
  40. }
  41. return *this;
  42. }
  43. Stock &Stock::operator*=(int const& multi) {
  44. for (int id(0); id < RESOURCE_NUMBER; ++id) {
  45. m_tab[id] *= multi;
  46. }
  47. return *this;
  48. }
  49. Stock &Stock::operator/=(int const& multi) {
  50. for (int id(0); id < RESOURCE_NUMBER; ++id) {
  51. m_tab[id] /= multi;
  52. }
  53. return *this;
  54. }
  55. Resource Stock::mostResource() const {
  56. int highest(0);
  57. Resource most(ENERGY);
  58. for (int res(0); res < RESOURCE_NUMBER; ++res) {
  59. if (m_tab[res] > highest) {
  60. highest = m_tab[res];
  61. most = (Resource)res;
  62. }
  63. }
  64. return most;
  65. }
  66. Stock operator+(Stock const &a, Stock const &b) {
  67. Stock copy(a);
  68. copy += b;
  69. return copy;
  70. }
  71. Stock operator-(Stock const &a, Stock const &b) {
  72. Stock copy(a);
  73. copy -= b;
  74. return copy;
  75. }
  76. Stock operator*(Stock const &a, int const &b) {
  77. Stock copy(a);
  78. copy *= b;
  79. return copy;
  80. }
  81. Stock operator/(Stock const &a, int const &b) {
  82. Stock copy(a);
  83. copy /= b;
  84. return copy;
  85. }