Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Online C++ compiler to run C++ program online
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Media {
- private:
- // Common attributes for all media
- string type;
- string author;
- string title;
- int year;
- bool free; // true if free, false if occupied
- public:
- // Default constructor
- Media() {
- type = "";
- author = "";
- title = "";
- year = 0;
- free = true;
- }
- // Parameterized constructor
- Media(string t, string a, string ti, int y, bool f) {
- type = t;
- author = a;
- title = ti;
- year = y;
- free = f;
- }
- // Getters and setters for private attributes
- string getType() {
- return type;
- }
- void setType(string t) {
- type = t;
- }
- string getAuthor() {
- return author;
- }
- void setAuthor(string a) {
- author = a;
- }
- string getTitle() {
- return title;
- }
- void setTitle(string ti) {
- title = ti;
- }
- int getYear() {
- return year;
- }
- void setYear(int y) {
- year = y;
- }
- bool isFree() {
- return free;
- }
- void setFree(bool f) {
- free = f;
- }
- };
- // A derived class for books
- class Book : public Media {
- private:
- // Additional attribute for books
- int pages;
- public:
- // Default constructor
- Book() : Media() {
- pages = 0;
- }
- // Parameterized constructor
- Book(string a, string ti, int y, bool f, int p) : Media("Book", a, ti, y, f) {
- pages = p;
- }
- // Getter and setter for pages
- int getPages() {
- return pages;
- }
- void setPages(int p) {
- pages = p;
- }
- };
- // A derived class for magazines
- class Magazine : public Media {
- private:
- // Additional attribute for magazines
- int issue;
- public:
- // Default constructor
- Magazine() : Media() {
- issue = 0;
- }
- // Parameterized constructor
- Magazine(string a, string ti, int y, bool f, int i) : Media("Magazine", a, ti, y, f) {
- issue = i;
- }
- // Getter and setter for issue
- int getIssue() {
- return issue;
- }
- void setIssue(int i) {
- issue = i;
- }
- };
- // A derived class for audio CDs
- class AudioCD : public Media {
- private:
- // Additional attribute for audio CDs
- int tracks;
- public:
- // Default constructor
- AudioCD() : Media() {
- tracks = 0;
- }
- // Parameterized constructor
- AudioCD(string a, string ti, int y, bool f, int t) : Media("Audio CD", a, ti, y, f) {
- tracks = t;
- }
- // Getter and setter for tracks
- int getTracks() {
- return tracks;
- }
- void setTracks(int t) {
- tracks = t;
- }
- };
- // A derived class for CD-ROMs
- class CDROM : public Media {
- private:
- // Additional attribute for CD-ROMs
- string content;
- public:
- // Default constructor
- CDROM() : Media() {
- content = "";
- }
- // Parameterized constructor
- CDROM(string a, string ti, int y, bool f, string c) : Media("CD-ROM", a, ti, y, f) {
- content = c;
- }
- // Getter and setter for content
- string getContent() {
- return content;
- }
- void setContent(string c) {
- content = c;
- }
- };
- // A derived class for tapes
- class Tape : public Media {
- private:
- // Additional attribute for tapes
- double length;
- public:
- // Default constructor
- Tape() : Media() {
- length = 0.0;
- }
- // Parameterized constructor
- Tape(string a, string ti, int y, bool f, double l) : Media("Tape", a, ti, y, f) {
- length = l;
- }
- // Getter and setter for length
- double getLength() {
- return length;
- }
- void setLength(double l) {
- length = l;
- }
- };
- // A derived class for videotapes
- class VideoTape : public Media {
- private:
- // Additional attribute for videotapes
- string genre;
- public:
- // Default constructor
- VideoTape() : Media() {
- genre = "";
- }
- // Parameterized constructor
- VideoTape(string a, string ti, int y, bool f, string g) : Media("Video Tape", a, ti, y, f) {
- genre = g;
- }
- // Getter and setter for genre
- string getGenre() {
- return genre;
- }
- void setGenre(string g) {
- genre = g;
- }
- };
- // A class for the virtual library
- class Library {
- private:
- // A vector of pointers to media objects
- vector<Media*> mediaList;
- public:
- // Default constructor
- Library() {
- mediaList.clear();
- }
- // A function to add a new media to the library
- void addMedia(Media* m) {
- mediaList.push_back(m);
- }
- // A function to search for free media by type
- void searchFreeMedia(string type) {
- cout << "Free media of type " << type << ":\n";
- for (int i = 0; i < mediaList.size(); i++) {
- if (mediaList[i]->getType() == type && mediaList[i]->isFree()) {
- cout << mediaList[i]->getTitle() << " by " << mediaList[i]->getAuthor() << "\n";
- }
- }
- }
- // A function to search for occupied media by type
- void searchOccupiedMedia(string type) {
- cout << "Occupied media of type " << type << ":\n";
- for (int i = 0; i < mediaList.size(); i++) {
- if (mediaList[i]->getType() == type && !mediaList[i]->isFree()) {
- cout << mediaList[i]->getTitle() << " by " << mediaList[i]->getAuthor() << "\n";
- }
- }
- }
- };
- // A function to validate the input of an integer
- int inputInt(string prompt) {
- int x;
- cout << prompt;
- cin >> x;
- while (cin.fail()) {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Invalid input. Please enter an integer.\n";
- cout << prompt;
- cin >> x;
- }
- return x;
- }
- // A function to validate the input of a double
- double inputDouble(string prompt) {
- double x;
- cout << prompt;
- cin >> x;
- while (cin.fail()) {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Invalid input. Please enter a double.\n";
- cout << prompt;
- cin >> x;
- }
- return x;
- }
- // A function to validate the input of a boolean
- bool inputBool(string prompt) {
- char x;
- cout << prompt;
- cin >> x;
- while (cin.fail() || (x != 'y' && x != 'n')) {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Invalid input. Please enter y or n.\n";
- cout << prompt;
- cin >> x;
- }
- return x == 'y';
- }
- // A function to validate the input of a string
- string inputString(string prompt) {
- string x;
- cout << prompt;
- getline(cin, x);
- while (cin.fail()) {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Invalid input. Please enter a string.\n";
- cout << prompt;
- getline(cin, x);
- }
- return x;
- }
- // The main function
- int main() {
- // Create a library object
- Library lib;
- // A loop to input any number of different media and their data
- char choice = 'y';
- while (choice == 'y') {
- // Input the type of the media
- string type = inputString("Enter the type of the media (Book/Magazine/Audio CD/CD-ROM/Tape/Video Tape): ");
- // Input the common attributes for all media
- string author = inputString("Enter the author of the media: ");
- string title = inputString("Enter the title of the media: ");
- int year = inputInt("Enter the year of publication of the media: ");
- bool free = inputBool("Is the media free? ");
- // Create a media object based on the type and input the additional attributes
- Media* m;
- if (type == "Book") {
- int pages = inputInt("Enter the number of pages of the book: ");
- m = new Book(author, title, year, free, pages);
- }
- else if (type == "Magazine") {
- int issue = inputInt("Enter the issue number of the magazine: ");
- m = new Magazine(author, title, year, free, issue);
- }
- else if (type == "Audio CD") {
- int tracks = inputInt("Enter the number of tracks of the audio CD: ");
- m = new AudioCD(author, title, year, free, tracks);
- }
- else if (type == "CD-ROM") {
- string content = inputString("Enter the content of the CD-ROM: ");
- m = new CDROM(author, title, year, free, content);
- }
- else if (type == "Tape") {
- double length = inputDouble("Enter the length of the tape in minutes: ");
- m = new Tape(author, title, year, free, length);
- }
- else if (type == "Video Tape") {
- string genre = inputString("Enter the genre of the video tape: ");
- m = new VideoTape(author, title, year, free, genre);
- }
- else {
- cout << "Invalid type. Please enter one of the valid types.\n";
- continue;
- }
- // Add the media object to the library
- lib.addMedia(m);
- // Ask if the user wants to add more media
- choice = inputBool("Do you want to add more media? (y/n): ");
- }
- // A loop to search for free and borrowed media by type
- choice = 'y';
- while (choice == 'y') {
- // Input the type of the media to search
- string type = inputString("Enter the type of the media to search (Book/Magazine/Audio CD/CD-ROM/Tape/Video Tape): ");
- // Search for free media by type
- lib.searchFreeMedia(type);
- // Search for occupied media by type
- lib.searchOccupiedMedia(type);
- // Ask if the user wants to search more media
- choice = inputBool("Do you want to search more media? (y/n): ");
- }
- // End the program
- cout << "Thank you for using the virtual library application.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement