1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <iostream>
#include <string>
#include <vector>
// class for music
class Track {
public:
int tId {};
struct CD {
std::string artist;
std::string title;
int year {};
std::string duration;
CD() {}
CD(const std::string& artist_, const std::string title_, int year_, int secs) :
artist(artist_), title(title_), year(year_), duration(sduration(secs)) {}
};
CD cd;
static std::string sduration(int d);
Track() {}
Track(int tId_, const std::string& artist_, const std::string& title_, int year_, int seconds_) :
tId(tId_), cd(artist_, title_, year_, seconds_) {}
};
std::string Track::sduration(int d) {
return std::to_string(d / 60) + "\'" + std::to_string(d % 60) + "\"";
}
// class for movies
class Divx {
public:
int dId {};
struct movie {
std::string director;
std::string title;
int year {};
movie() {}
movie(const std::string& direc, const std::string& title_, int y) :
director(direc), title(title_), year(y) {}
};
movie movie;
Divx() {}
Divx(int id, const std::string& director, const std::string& title, int year) :
dId(id), movie(director, title, year) {}
};
// a parent class item and its children
class Item : public Track, public Divx {
public:
enum Category {
Music, Movies, MusicAndMovies
};
Item(int id, const std::string& artist, const std::string& title, int year, int seconds);
Item(int id, const std::string& director, const std::string& title, int year);
static std::string displayCategory(Item::Category c) {
switch (c) {
case Item::Category::Music:
return "A new entry in the Music section";
case Item::Category::Movies:
return "A new entry in the Movies section";
case Item::Category::MusicAndMovies:
return "A new entry in the Music And Movies section";
default:
return "[Unknown]";
}
}
Category cat;
};
Item::Item(int ID, const std::string& artist, const std::string& title, int year, int seconds) :
Track { ID, artist, title, year, seconds }, cat { Category::Music } {}
Item::Item(int ID, const std::string& director, const std::string& title, int year) :
Divx { ID, director, title, year }, cat { Category::Movies } {}
int main() {
std::vector<Item> collect;
collect.emplace_back(1, "Jimi Hendrix", "Red House", 1967, 224);
collect.emplace_back(2, "Fleetwood Mac", "Little Lies", 1987, 216);
collect.emplace_back(3, "Green Day", "When I come Around", 1995, 178);
collect.emplace_back(4, "Prince", "Purple Rain", 1984, 245);
collect.emplace_back(1, "Albert Magnoli", "Purple Rain", 1984);
collect.emplace_back(2, "Ridley Scott", "Blade Runner", 1982);
std::cout << "\n*** Music section ***\n";
size_t records {}, movies {};
for (const auto& o : collect)
if (o.cat == Item::Category::Music) {
++records;
std::cout << o.cd.artist << '\n';
std::cout << o.cd.title << '\n';
std::cout << o.cd.year << '\n';
std::cout << o.cd.duration << '\n';
}
std::cout << "\n*** Movies section ***\n" << '\n';
for (const auto& o : collect)
if (o.cat == Item::Category::Movies) {
++movies;
std::cout << o.movie.director << '\n';
std::cout << o.movie.title << '\n';
std::cout << o.movie.year << '\n';
}
std::cout << "You have " << records << " tracks in your playlist\n";
std::cout << "You have " << movies << " movies in your folder\n";
}
|