Advertisement
Frost2312

POO Final 2011

Dec 21st, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Exception {
  8.     string _message;
  9. public:
  10.     Exception(string message) : _message(message) { }
  11.     string message() const { return _message; }
  12. };
  13.  
  14.  
  15. class Duration {
  16.     int _h;
  17.     int _m;
  18.     int _s;
  19. public:
  20.     Duration() : _h(0), _m(0), _s(0) { }
  21.  
  22.     Duration(int s)
  23.     {
  24.         if (s < 0)
  25.             throw Exception("Value must be positive integer");
  26.  
  27.         _s = s % 60;
  28.         _m = (s / 60) % 60;
  29.         _h = s / 3600;
  30.     }
  31.  
  32.     Duration(int m, int s)
  33.     {
  34.         if (s < 0 || m < 0)
  35.             throw Exception("Value must be positive integer");
  36.  
  37.         _s = s % 60;
  38.         _m = m + (s / 60);
  39.         _h = _m / 60;
  40.         _m = _m - _h * 60;
  41.     }
  42.  
  43.     Duration(int h, int m, int s)
  44.     {
  45.         if (s < 0 || m < 0 || h < 0)
  46.             throw Exception("Value must be positive integer");
  47.  
  48.         _s = s % 60;
  49.         _m = m + (s / 60);
  50.         _h = h + _m / 60;
  51.         _m = _m % 60;
  52.     }
  53.  
  54.     Duration operator+(Duration const &D) const { return Duration(_h + D._h, _m  + D._m, _s + D._s); }
  55.  
  56.     int hours() const { return _h; }
  57.  
  58.     int minutes() const { return _m; }
  59.  
  60.     int seconds() const { return _s; }
  61.  
  62.     int toSeconds() const { return _h * 3600 + _m * 60 + _s; }
  63.  
  64.     bool operator<(Duration const &D) const
  65.     {
  66.         if (_h < D._h || _h == D._h && _m < D._m || _h == D._h && _m == D._m && _s < D._s)
  67.             return true;
  68.  
  69.         return false;
  70.     }
  71.  
  72.     ostream &operator<<(ostream &out) const
  73.     {
  74.         if (_h != 0)
  75.             out << _h << "h";
  76.  
  77.         if (_m != 0)
  78.             out << _m << "m";
  79.  
  80.         out << _s << "s";
  81.  
  82.         return out;
  83.     }
  84.  
  85. };
  86.  
  87.  
  88. ostream &operator<<(ostream &out, Duration const &D)
  89. {
  90.     return D << out;
  91. }
  92.  
  93.  
  94. class Track {
  95.     Duration d;
  96.     int nbCD = 0;
  97. public:
  98.     Track(Duration _d) : d(_d) { }
  99.  
  100.     int refCount() { return nbCD; }
  101.  
  102.     void retain() { nbCD++; }
  103.  
  104.     void release() { nbCD--; }
  105.  
  106.     Duration duration() { return d; }
  107.  
  108.     virtual string description() = 0;
  109.  
  110.     virtual int sizeOnDisk() = 0;
  111.  
  112. };
  113.  
  114.  
  115. class CD {
  116.     string _title;
  117.     int _capacity;
  118.     vector<Track *> _tracks;
  119.  
  120. public:
  121.     CD(string title, int capacity) : _title(title)
  122.     {
  123.         if (capacity < 0)
  124.             throw Exception("Capacity must be a positive integer");
  125.  
  126.         _capacity = capacity;
  127.     }
  128.  
  129.     int tracks() const { return _tracks.size(); }
  130.  
  131.     Track *track(int i) const
  132.     {
  133.         if (i < _tracks.size())
  134.             throw Exception("Index out of range");
  135.  
  136.         return _tracks[i];
  137.     }
  138.  
  139.     Duration duration() const
  140.     {
  141.         Duration D;
  142.  
  143.         for (int i = 0; i < _tracks.size(); i++)
  144.         {
  145.             D = D + _tracks[i]->duration();
  146.         }
  147.  
  148.         return D;
  149.     }
  150.  
  151.     int freeSpace() const
  152.     {
  153.         int used = 0;
  154.  
  155.         for (int i = 0; i < _tracks.size(); i++)
  156.         {
  157.             used += _tracks[i]->sizeOnDisk();
  158.         }
  159.  
  160.         return _capacity - used;
  161.     }
  162.  
  163.     void addTrack(Track *T)
  164.     {
  165.         if (T->sizeOnDisk() > freeSpace())
  166.             throw Exception("Disk full");
  167.  
  168.         _tracks.push_back(T);
  169.         T->retain();
  170.     }
  171.  
  172.     virtual ~CD()
  173.     {
  174.         for (int i = 0; i < _tracks.size(); i++)
  175.         {
  176.             _tracks[i]->refCount() == 1 ? delete _tracks[i] : _tracks[i]->release();
  177.         }
  178.     }
  179.  
  180. };
  181.  
  182.  
  183. class AudioTrack : public Track {
  184.     string title;
  185.     string artist;
  186. public:
  187.     AudioTrack(Duration d, string _title, string _artist) : Track (d), title(_title), artist(_artist) { }
  188.  
  189.     string description()
  190.     {
  191.         stringstream s;
  192.         s << title << ", by " << artist << " (" << duration() << ")";
  193.         return s.str();
  194.     }
  195.  
  196.     int sizeOnDisk() { return duration().toSeconds() * 150000; }
  197.  
  198. };
  199.  
  200. class VideoTrack : public Track {
  201.     string title;
  202.     int _w;
  203.     int _h;
  204. public:
  205.     VideoTrack(Duration d, string _title, int width, int height) : Track(d), title(_title)
  206.     {
  207.         if (width < 0)
  208.             throw Exception("Width must be a positive integer");
  209.  
  210.         if (height < 0)
  211.             throw Exception("Height must be a positive integer");
  212.  
  213.         _w = width;
  214.         _h = height;
  215.     }
  216.  
  217.     string description()
  218.     {
  219.         stringstream s;
  220.         s << title << " (" << _w << " x " << _h <<" x " << duration() << ")";
  221.         return s.str();
  222.     }
  223.  
  224.     int sizeOnDisk() { return 3 * duration().toSeconds() * _w * _h * 24; }
  225.  
  226. };
  227.  
  228. int main()
  229. {
  230.     Duration d1(6101);
  231.     Duration d2(3, 23, 6021);
  232.     cout << d1 << endl << d2 << endl;
  233.     cout << (d1 + d2) << endl;
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement