Advertisement
CR7CR7

virtualLibrary

Apr 8th, 2023
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.43 KB | None | 0 0
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class Media {
  8.   private:
  9.     // Common attributes for all media
  10.     string type;
  11.     string author;
  12.     string title;
  13.     int year;
  14.     bool free; // true if free, false if occupied
  15.  
  16.   public:
  17.     // Default constructor
  18.     Media() {
  19.       type = "";
  20.       author = "";
  21.       title = "";
  22.       year = 0;
  23.       free = true;
  24.     }
  25.  
  26.     // Parameterized constructor
  27.     Media(string t, string a, string ti, int y, bool f) {
  28.       type = t;
  29.       author = a;
  30.       title = ti;
  31.       year = y;
  32.       free = f;
  33.     }
  34.  
  35.     // Getters and setters for private attributes
  36.     string getType() {
  37.       return type;
  38.     }
  39.  
  40.     void setType(string t) {
  41.       type = t;
  42.     }
  43.  
  44.     string getAuthor() {
  45.       return author;
  46.     }
  47.  
  48.     void setAuthor(string a) {
  49.       author = a;
  50.     }
  51.  
  52.     string getTitle() {
  53.       return title;
  54.     }
  55.  
  56.     void setTitle(string ti) {
  57.       title = ti;
  58.     }
  59.  
  60.     int getYear() {
  61.       return year;
  62.     }
  63.  
  64.     void setYear(int y) {
  65.       year = y;
  66.     }
  67.  
  68.     bool isFree() {
  69.       return free;
  70.     }
  71.  
  72.     void setFree(bool f) {
  73.       free = f;
  74.     }
  75. };
  76.  
  77. // A derived class for books
  78. class Book : public Media {
  79.   private:
  80.     // Additional attribute for books
  81.     int pages;
  82.  
  83.   public:
  84.     // Default constructor
  85.     Book() : Media() {
  86.       pages = 0;
  87.     }
  88.  
  89.     // Parameterized constructor
  90.     Book(string a, string ti, int y, bool f, int p) : Media("Book", a, ti, y, f) {
  91.       pages = p;
  92.     }
  93.  
  94.     // Getter and setter for pages
  95.     int getPages() {
  96.       return pages;
  97.     }
  98.  
  99.     void setPages(int p) {
  100.       pages = p;
  101.     }
  102. };
  103.  
  104. // A derived class for magazines
  105. class Magazine : public Media {
  106.   private:
  107.     // Additional attribute for magazines
  108.     int issue;
  109.  
  110.   public:
  111.     // Default constructor
  112.     Magazine() : Media() {
  113.       issue = 0;
  114.     }
  115.  
  116.     // Parameterized constructor
  117.     Magazine(string a, string ti, int y, bool f, int i) : Media("Magazine", a, ti, y, f) {
  118.       issue = i;
  119.     }
  120.  
  121.     // Getter and setter for issue
  122.     int getIssue() {
  123.       return issue;
  124.     }
  125.  
  126.     void setIssue(int i) {
  127.       issue = i;
  128.     }
  129. };
  130.  
  131. // A derived class for audio CDs
  132. class AudioCD : public Media {
  133.   private:
  134.    // Additional attribute for audio CDs
  135.    int tracks;
  136.  
  137.   public:
  138.    // Default constructor
  139.    AudioCD() : Media() {
  140.      tracks = 0;
  141.    }
  142.  
  143.    // Parameterized constructor
  144.    AudioCD(string a, string ti, int y, bool f, int t) : Media("Audio CD", a, ti, y, f) {
  145.      tracks = t;
  146.    }
  147.  
  148.    // Getter and setter for tracks
  149.    int getTracks() {
  150.      return tracks;
  151.    }
  152.  
  153.    void setTracks(int t) {
  154.      tracks = t;
  155.    }
  156. };
  157.  
  158. // A derived class for CD-ROMs
  159. class CDROM : public Media {
  160.   private:
  161.    // Additional attribute for CD-ROMs
  162.    string content;
  163.  
  164.   public:
  165.    // Default constructor
  166.    CDROM() : Media() {
  167.      content = "";
  168.    }
  169.  
  170.    // Parameterized constructor
  171.    CDROM(string a, string ti, int y, bool f, string c) : Media("CD-ROM", a, ti, y, f) {
  172.      content = c;
  173.    }
  174.  
  175.    // Getter and setter for content
  176.    string getContent() {
  177.      return content;
  178.    }
  179.  
  180.    void setContent(string c) {
  181.      content = c;
  182.    }
  183. };
  184.  
  185. // A derived class for tapes
  186. class Tape : public Media {
  187.   private:
  188.    // Additional attribute for tapes
  189.    double length;
  190.  
  191.   public:
  192.    // Default constructor
  193.    Tape() : Media() {
  194.      length = 0.0;
  195.    }
  196.  
  197.    // Parameterized constructor
  198.    Tape(string a, string ti, int y, bool f, double l) : Media("Tape", a, ti, y, f) {
  199.      length = l;
  200.    }
  201.  
  202.    // Getter and setter for length
  203.    double getLength() {
  204.      return length;
  205.    }
  206.  
  207.    void setLength(double l) {
  208.      length = l;
  209.    }
  210. };
  211. // A derived class for videotapes
  212. class VideoTape : public Media {
  213.   private:
  214.    // Additional attribute for videotapes
  215.    string genre;
  216.  
  217.   public:
  218.    // Default constructor
  219.    VideoTape() : Media() {
  220.      genre = "";
  221.    }
  222.  
  223.    // Parameterized constructor
  224.    VideoTape(string a, string ti, int y, bool f, string g) : Media("Video Tape", a, ti, y, f) {
  225.      genre = g;
  226.    }
  227.  
  228.    // Getter and setter for genre
  229.    string getGenre() {
  230.      return genre;
  231.    }
  232.  
  233.    void setGenre(string g) {
  234.      genre = g;
  235.    }
  236. };
  237.  
  238. // A class for the virtual library
  239. class Library {
  240.   private:
  241.     // A vector of pointers to media objects
  242.     vector<Media*> mediaList;
  243.  
  244.   public:
  245.     // Default constructor
  246.     Library() {
  247.       mediaList.clear();
  248.     }
  249.  
  250.     // A function to add a new media to the library
  251.     void addMedia(Media* m) {
  252.       mediaList.push_back(m);
  253.     }
  254.  
  255.     // A function to search for free media by type
  256.     void searchFreeMedia(string type) {
  257.       cout << "Free media of type " << type << ":\n";
  258.       for (int i = 0; i < mediaList.size(); i++) {
  259.         if (mediaList[i]->getType() == type && mediaList[i]->isFree()) {
  260.           cout << mediaList[i]->getTitle() << " by " << mediaList[i]->getAuthor() << "\n";
  261.         }
  262.       }
  263.     }
  264.  
  265.     // A function to search for occupied media by type
  266.     void searchOccupiedMedia(string type) {
  267.       cout << "Occupied media of type " << type << ":\n";
  268.       for (int i = 0; i < mediaList.size(); i++) {
  269.         if (mediaList[i]->getType() == type && !mediaList[i]->isFree()) {
  270.           cout << mediaList[i]->getTitle() << " by " << mediaList[i]->getAuthor() << "\n";
  271.         }
  272.       }
  273.     }
  274. };
  275.  
  276. // A function to validate the input of an integer
  277. int inputInt(string prompt) {
  278.   int x;
  279.   cout << prompt;
  280.   cin >> x;
  281.   while (cin.fail()) {
  282.     cin.clear();
  283.     cin.ignore(1000, '\n');
  284.     cout << "Invalid input. Please enter an integer.\n";
  285.     cout << prompt;
  286.     cin >> x;
  287.   }
  288.   return x;
  289. }
  290.  
  291. // A function to validate the input of a double
  292. double inputDouble(string prompt) {
  293.   double x;
  294.   cout << prompt;
  295.   cin >> x;
  296.   while (cin.fail()) {
  297.     cin.clear();
  298.     cin.ignore(1000, '\n');
  299.     cout << "Invalid input. Please enter a double.\n";
  300.     cout << prompt;
  301.     cin >> x;
  302.   }
  303.   return x;
  304. }
  305.  
  306. // A function to validate the input of a boolean
  307. bool inputBool(string prompt) {
  308.   char x;
  309.   cout << prompt;
  310.   cin >> x;
  311.   while (cin.fail() || (x != 'y' && x != 'n')) {
  312.     cin.clear();
  313.     cin.ignore(1000, '\n');
  314.     cout << "Invalid input. Please enter y or n.\n";
  315.     cout << prompt;
  316.     cin >> x;
  317.   }
  318.   return x == 'y';
  319. }
  320.  
  321. // A function to validate the input of a string
  322. string inputString(string prompt) {
  323.   string x;
  324.   cout << prompt;
  325.   getline(cin, x);
  326.   while (cin.fail()) {
  327.     cin.clear();
  328.     cin.ignore(1000, '\n');
  329.     cout << "Invalid input. Please enter a string.\n";
  330.     cout << prompt;
  331.     getline(cin, x);
  332.   }
  333.   return x;
  334. }
  335.  
  336. // The main function
  337. int main() {
  338.   // Create a library object
  339.   Library lib;
  340.  
  341.   // A loop to input any number of different media and their data
  342.   char choice = 'y';
  343.   while (choice == 'y') {
  344.     // Input the type of the media
  345.     string type = inputString("Enter the type of the media (Book/Magazine/Audio CD/CD-ROM/Tape/Video Tape): ");
  346.    
  347.     // Input the common attributes for all media
  348.     string author = inputString("Enter the author of the media: ");
  349.     string title = inputString("Enter the title of the media: ");
  350.     int year = inputInt("Enter the year of publication of the media: ");
  351.     bool free = inputBool("Is the media free? ");
  352.     // Create a media object based on the type and input the additional attributes
  353.     Media* m;
  354.     if (type == "Book") {
  355.       int pages = inputInt("Enter the number of pages of the book: ");
  356.       m = new Book(author, title, year, free, pages);
  357.     }
  358.     else if (type == "Magazine") {
  359.       int issue = inputInt("Enter the issue number of the magazine: ");
  360.       m = new Magazine(author, title, year, free, issue);
  361.     }
  362.     else if (type == "Audio CD") {
  363.       int tracks = inputInt("Enter the number of tracks of the audio CD: ");
  364.       m = new AudioCD(author, title, year, free, tracks);
  365.     }
  366.     else if (type == "CD-ROM") {
  367.       string content = inputString("Enter the content of the CD-ROM: ");
  368.       m = new CDROM(author, title, year, free, content);
  369.     }
  370.     else if (type == "Tape") {
  371.       double length = inputDouble("Enter the length of the tape in minutes: ");
  372.       m = new Tape(author, title, year, free, length);
  373.     }
  374.     else if (type == "Video Tape") {
  375.       string genre = inputString("Enter the genre of the video tape: ");
  376.       m = new VideoTape(author, title, year, free, genre);
  377.     }
  378.     else {
  379.       cout << "Invalid type. Please enter one of the valid types.\n";
  380.       continue;
  381.     }
  382.  
  383.     // Add the media object to the library
  384.     lib.addMedia(m);
  385.  
  386.     // Ask if the user wants to add more media
  387.     choice = inputBool("Do you want to add more media? (y/n): ");
  388.   }
  389.  
  390.   // A loop to search for free and borrowed media by type
  391.   choice = 'y';
  392.   while (choice == 'y') {
  393.     // Input the type of the media to search
  394.     string type = inputString("Enter the type of the media to search (Book/Magazine/Audio CD/CD-ROM/Tape/Video Tape): ");
  395.  
  396.     // Search for free media by type
  397.     lib.searchFreeMedia(type);
  398.  
  399.     // Search for occupied media by type
  400.     lib.searchOccupiedMedia(type);
  401.  
  402.     // Ask if the user wants to search more media
  403.     choice = inputBool("Do you want to search more media? (y/n): ");
  404.   }
  405.  
  406.   // End the program
  407.   cout << "Thank you for using the virtual library application.\n";
  408.   return 0;
  409. }
  410.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement