container.cpp 823 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "container.hpp"
  2. #include <assert.h>
  3. Container::Container(unsigned short capacity, unsigned short content = 0):
  4. m_capacity(capacity), m_content(content)
  5. {
  6. assert(m_capacity>=m_content && m_content>=0&&"Контейнер сломался");
  7. }
  8. void Container::setContent(int value)
  9. {
  10. if(value>m_capacity)
  11. {
  12. m_content = m_capacity;
  13. }
  14. else if (value<0)
  15. {
  16. m_content = 0;
  17. }
  18. }
  19. int Container::getCapacity() const
  20. {
  21. return m_capacity;
  22. }
  23. const int& Container::getContent() const
  24. {
  25. return m_content;
  26. }
  27. bool Container::isEmpty() const
  28. {
  29. return m_content<=0;
  30. }
  31. int Container::take(unsigned short value)
  32. {
  33. if(m_content>=value)
  34. {
  35. m_content-=value;
  36. return value;
  37. }
  38. return 0;
  39. }
  40. void Container::add(unsigned short value)
  41. {
  42. m_content += value;
  43. if(m_content > m_capacity)
  44. {
  45. m_content = m_capacity;
  46. }
  47. }