Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- // A base class for media
- class Media {
- private:
- // Private instance variables to store the media data
- string type; // The type of the media
- string author; // The author of the media
- string title; // The title of the media
- int year; // The year of publication of the media
- bool status; // The condition of the media (free or occupied)
- public:
- // A default constructor that initializes the media data with default values
- Media() {
- type = "";
- author = "";
- title = "";
- year = 0;
- status = true; // Free by default
- }
- // A parameterized constructor that initializes the media data with given values
- Media(string t, string a, string ti, int y, bool s) {
- type = t;
- author = a;
- title = ti;
- year = y;
- status = s;
- }
- // Public getters and setters for the private instance variables
- // A getter for the type
- string getType() {
- return type;
- }
- // A setter for the type
- void setType(string t) {
- type = t;
- }
- // A getter for the author
- string getAuthor() {
- return author;
- }
- // A setter for the author
- void setAuthor(string a) {
- author = a;
- }
- // A getter for the title
- string getTitle() {
- return title;
- }
- // A setter for the title
- void setTitle(string ti) {
- title = ti;
- }
- // A getter for the year
- int getYear() {
- return year;
- }
- // A setter for the year
- void setYear(int y) {
- year = y;
- }
- // A getter for the status
- bool getStatus() {
- return status;
- }
- // A setter for the status
- void setStatus(bool s) {
- status = s;
- }
- };
- // A derived class for books
- class Book : public Media {
- private:
- // Private instance variable to store the number of pages of the book
- int pages;
- public:
- // A default constructor that initializes the book data with default values
- Book() : Media() {
- pages = 0;
- }
- // A parameterized constructor that initializes the book data with given values
- Book(string t, string a, string ti, int y, bool s, int p) : Media(t, a, ti, y, s) {
- pages = p;
- }
- // Public getters and setters for the private instance variable
- // A getter for the pages
- int getPages() {
- return pages;
- }
- // A setter for the pages
- void setPages(int p) {
- pages = p;
- }
- };
- // A derived class for magazines
- class Magazine : public Media {
- private:
- // Private instance variable to store the issue number of the magazine
- int issue;
- public:
- // A default constructor that initializes the magazine data with default values
- Magazine() : Media() {
- issue = 0;
- }
- // A parameterized constructor that initializes the magazine data with given values
- Magazine(string t, string a, string ti, int y, bool s, int i) : Media(t, a, ti, y, s) {
- issue = i;
- }
- // Public getters and setters for the private instance variable
- // A getter for the issue
- int getIssue() {
- return issue;
- }
- // A setter for the issue
- void setIssue(int i) {
- issue = i;
- }
- };
- // A derived class for audio CDs
- class AudioCD : public Media {
- private:
- // Private instance variable to store the duration of the audio CD in minutes
- int duration;
- public:
- // A default constructor that initializes the audio CD data with default values
- AudioCD() : Media() {
- duration = 0;
- }
- // A parameterized constructor that initializes the audio CD
- // A parameterized constructor that initializes the audio CD data with given values
- AudioCD(string t, string a, string ti, int y, bool s, int d) : Media(t, a, ti, y, s) {
- duration = d;
- }
- // Public getters and setters for the private instance variable
- // A getter for the duration
- int getDuration() {
- return duration;
- }
- // A setter for the duration
- void setDuration(int d) {
- duration = d;
- }
- };
- // A derived class for CD-ROMs
- class CDROM : public Media {
- private:
- // Private instance variable to store the capacity of the CD-ROM in GB
- double capacity;
- public:
- // A default constructor that initializes the CD-ROM data with default values
- CDROM() : Media() {
- capacity = 0.0;
- }
- // A parameterized constructor that initializes the CD-ROM data with given values
- CDROM(string t, string a, string ti, int y, bool s, double c) : Media(t, a, ti, y, s) {
- capacity = c;
- }
- // Public getters and setters for the private instance variable
- // A getter for the capacity
- double getCapacity() {
- return capacity;
- }
- // A setter for the capacity
- void setCapacity(double c) {
- capacity = c;
- }
- };
- // A derived class for tapes
- class Tape : public Media {
- private:
- // Private instance variable to store the length of the tape in meters
- double length;
- public:
- // A default constructor that initializes the tape data with default values
- Tape() : Media() {
- length = 0.0;
- }
- // A parameterized constructor that initializes the tape data with given values
- Tape(string t, string a, string ti, int y, bool s, double l) : Media(t, a, ti, y, s) {
- length = l;
- }
- // Public getters and setters for the private instance variable
- // A getter for the length
- double getLength() {
- return length;
- }
- // A setter for the length
- void setLength(double l) {
- length = l;
- }
- };
- // A derived class for videotapes
- class Videotape : public Media {
- private:
- // Private instance variable to store the quality of the videotape (HD or SD)
- string quality;
- public:
- // A default constructor that initializes the videotape data with default values
- Videotape() : Media() {
- quality = "";
- }
- // A parameterized constructor that initializes the videotape data with given values
- Videotape(string t, string a, string ti, int y, bool s, string q) : Media(t, a, ti, y, s) {
- quality = q;
- }
- // Public getters and setters for the private instance variable
- // A getter for the quality
- string getQuality() {
- return quality;
- }
- // A setter for the quality
- void setQuality(string q) {
- quality = q;
- }
- };
- // A class for library
- class Library {
- private:
- // Private instance variable to store a vector of media pointers
- vector<Media*> mediaList;
- public:
- // A default constructor that initializes an empty library
- Library() {
- mediaList.clear();
- }
- // A function to add a media pointer to the library
- void addMedia(Media* m) {
- mediaList.push_back(m);
- }
- // A function to search for free media in the library
- void searchFreeMedia() {
- cout << "The free media in the library are: \n";
- for (int i = 0; i < mediaList.size(); i++) {
- if (mediaList[i]->getStatus()) { // If the media is free
- cout << "Type: " << mediaList[i]->getType() << "\n";
- cout << "Author: " << mediaList[i]->getAuthor() << "\n";
- cout << "Title: " << mediaList[i]->getTitle() << "\n";
- cout << "Year: " << mediaList[i]->getYear() << "\n";
- cout << "\n";
- }
- }
- }
- // A function to search for borrowed media in the library
- //void searchBorrowedMedia()
- // A function to search for borrowed media in the library
- void searchBorrowedMedia() {
- cout << "The borrowed media in the library are: \n";
- for (int i = 0; i < mediaList.size(); i++) {
- if (!mediaList[i]->getStatus()) { // If the media is occupied
- cout << "Type: " << mediaList[i]->getType() << "\n";
- cout << "Author: " << mediaList[i]->getAuthor() << "\n";
- cout << "Title: " << mediaList[i]->getTitle() << "\n";
- cout << "Year: " << mediaList[i]->getYear() << "\n";
- cout << "\n";
- }
- }
- }
- // A function to input any number of different media and their data
- void inputMedia() {
- int n; // The number of media to input
- cout << "How many media do you want to input? \n";
- cin >> n;
- for (int i = 0; i < n; i++) {
- string type; // The type of the media
- string author; // The author of the media
- string title; // The title of the media
- int year; // The year of publication of the media
- bool status; // The condition of the media (free or occupied)
- Media* m; // A pointer to a media object
- cout << "Enter the type of the media (book, magazine, audio CD, CD-ROM, tape, videotape): \n";
- cin >> type;
- cout << "Enter the author of the media: \n";
- cin >> author;
- cout << "Enter the title of the media: \n";
- cin >> title;
- cout << "Enter the year of publication of the media: \n";
- cin >> year;
- cout << "Enter the condition of the media (1 for free, 0 for occupied): \n";
- cin >> status;
- // Creating a specific media object based on the type and assigning it to m
- if (type == "book") {
- int pages; // The number of pages of the book
- cout << "Enter the number of pages of the book: \n";
- cin >> pages;
- m = new Book(type, author, title, year, status, pages);
- }
- else if (type == "magazine") {
- int issue; // The issue number of the magazine
- cout << "Enter the issue number of the magazine: \n";
- cin >> issue;
- m = new Magazine(type, author, title, year, status, issue);
- }
- else if (type == "audio CD") {
- int duration; // The duration of the audio CD in minutes
- cout << "Enter the duration of the audio CD in minutes: \n";
- cin >> duration;
- m = new AudioCD(type, author, title, year, status, duration);
- }
- else if (type == "CD-ROM") {
- double capacity; // The capacity of the CD-ROM in GB
- cout << "Enter the capacity of the CD-ROM in GB: \n";
- cin >> capacity;
- m = new CDROM(type, author, title, year, status, capacity);
- }
- else if (type == "tape") {
- double length; // The length of the tape in meters
- cout << "Enter the length of the tape in meters: \n";
- cin >> length;
- m = new Tape(type, author, title, year, status, length);
- }
- else if (type == "videotape") {
- string quality; // The quality of the videotape (HD or SD)
- cout << "Enter the quality of the videotape (HD or SD): \n";
- cin >> quality;
- m = new Videotape(type, author, title, year, status, quality);
- }
- else {
- // Invalid type
- cout << "Invalid type. Please try again. \n";
- i--; // Decrementing i to repeat this iteration
- continue; // Skipping to next iteration
- }
- // Adding m to the library
- addMedia(m);
- }
- }
- };
- // The main function
- int main() {
- // Creating a library object
- Library lib;
- // Inputting some media and their data
- lib.inputMedia();
- // Searching for free media in the library
- lib.searchFreeMedia();
- // Searching for borrowed media in the library
- lib.searchBorrowedMedia();
- // Ending the program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement