Advertisement
CR7CR7

Virtual-Library

Apr 23rd, 2023
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. // A base class for media
  7. class Media {
  8.   private:
  9.     // Private instance variables to store the media data
  10.     string type; // The type of the media
  11.     string author; // The author of the media
  12.     string title; // The title of the media
  13.     int year; // The year of publication of the media
  14.     bool status; // The condition of the media (free or occupied)
  15.  
  16.   public:
  17.     // A default constructor that initializes the media data with default values
  18.     Media() {
  19.       type = "";
  20.       author = "";
  21.       title = "";
  22.       year = 0;
  23.       status = true; // Free by default
  24.     }
  25.  
  26.     // A parameterized constructor that initializes the media data with given values
  27.     Media(string t, string a, string ti, int y, bool s) {
  28.       type = t;
  29.       author = a;
  30.       title = ti;
  31.       year = y;
  32.       status = s;
  33.     }
  34.  
  35.     // Public getters and setters for the private instance variables
  36.  
  37.     // A getter for the type
  38.     string getType() {
  39.       return type;
  40.     }
  41.  
  42.     // A setter for the type
  43.     void setType(string t) {
  44.       type = t;
  45.     }
  46.  
  47.     // A getter for the author
  48.     string getAuthor() {
  49.       return author;
  50.     }
  51.  
  52.     // A setter for the author
  53.     void setAuthor(string a) {
  54.       author = a;
  55.     }
  56.  
  57.     // A getter for the title
  58.     string getTitle() {
  59.       return title;
  60.     }
  61.  
  62.     // A setter for the title
  63.     void setTitle(string ti) {
  64.       title = ti;
  65.     }
  66.  
  67.     // A getter for the year
  68.     int getYear() {
  69.       return year;
  70.     }
  71.  
  72.     // A setter for the year
  73.     void setYear(int y) {
  74.       year = y;
  75.     }
  76.  
  77.     // A getter for the status
  78.     bool getStatus() {
  79.       return status;
  80.     }
  81.  
  82.     // A setter for the status
  83.     void setStatus(bool s) {
  84.       status = s;
  85.     }
  86. };
  87.  
  88. // A derived class for books
  89. class Book : public Media {
  90.   private:
  91.     // Private instance variable to store the number of pages of the book
  92.     int pages;
  93.  
  94.   public:
  95.     // A default constructor that initializes the book data with default values
  96.     Book() : Media() {
  97.       pages = 0;
  98.     }
  99.  
  100.     // A parameterized constructor that initializes the book data with given values
  101.     Book(string t, string a, string ti, int y, bool s, int p) : Media(t, a, ti, y, s) {
  102.       pages = p;
  103.     }
  104.  
  105.      // Public getters and setters for the private instance variable
  106.  
  107.      // A getter for the pages
  108.      int getPages() {
  109.        return pages;
  110.      }
  111.  
  112.      // A setter for the pages
  113.      void setPages(int p) {
  114.        pages = p;
  115.      }
  116. };
  117.  
  118. // A derived class for magazines
  119. class Magazine : public Media {
  120.   private:
  121.    // Private instance variable to store the issue number of the magazine
  122.    int issue;
  123.  
  124.   public:
  125.    // A default constructor that initializes the magazine data with default values
  126.    Magazine() : Media() {
  127.      issue = 0;
  128.    }
  129.  
  130.    // A parameterized constructor that initializes the magazine data with given values
  131.    Magazine(string t, string a, string ti, int y, bool s, int i) : Media(t, a, ti, y, s) {
  132.      issue = i;
  133.    }
  134.  
  135.    // Public getters and setters for the private instance variable
  136.  
  137.    // A getter for the issue
  138.    int getIssue() {
  139.      return issue;
  140.    }
  141.  
  142.    // A setter for the issue
  143.    void setIssue(int i) {
  144.      issue = i;
  145.    }
  146. };
  147.  
  148. // A derived class for audio CDs
  149. class AudioCD : public Media {
  150.   private:
  151.    // Private instance variable to store the duration of the audio CD in minutes
  152.    int duration;
  153.  
  154.   public:
  155.    // A default constructor that initializes the audio CD data with default values
  156.    AudioCD() : Media() {
  157.      duration = 0;
  158.    }
  159.  
  160.    // A parameterized constructor that initializes the audio CD
  161. // A parameterized constructor that initializes the audio CD data with given values
  162.    AudioCD(string t, string a, string ti, int y, bool s, int d) : Media(t, a, ti, y, s) {
  163.      duration = d;
  164.    }
  165.  
  166.    // Public getters and setters for the private instance variable
  167.  
  168.    // A getter for the duration
  169.    int getDuration() {
  170.      return duration;
  171.    }
  172.  
  173.    // A setter for the duration
  174.    void setDuration(int d) {
  175.      duration = d;
  176.    }
  177. };
  178.  
  179. // A derived class for CD-ROMs
  180. class CDROM : public Media {
  181.   private:
  182.    // Private instance variable to store the capacity of the CD-ROM in GB
  183.    double capacity;
  184.  
  185.   public:
  186.    // A default constructor that initializes the CD-ROM data with default values
  187.    CDROM() : Media() {
  188.      capacity = 0.0;
  189.    }
  190.  
  191.    // A parameterized constructor that initializes the CD-ROM data with given values
  192.    CDROM(string t, string a, string ti, int y, bool s, double c) : Media(t, a, ti, y, s) {
  193.      capacity = c;
  194.    }
  195.  
  196.    // Public getters and setters for the private instance variable
  197.  
  198.    // A getter for the capacity
  199.    double getCapacity() {
  200.      return capacity;
  201.    }
  202.  
  203.    // A setter for the capacity
  204.    void setCapacity(double c) {
  205.      capacity = c;
  206.    }
  207. };
  208.  
  209. // A derived class for tapes
  210. class Tape : public Media {
  211.   private:
  212.    // Private instance variable to store the length of the tape in meters
  213.    double length;
  214.  
  215.   public:
  216.    // A default constructor that initializes the tape data with default values
  217.    Tape() : Media() {
  218.      length = 0.0;
  219.    }
  220.  
  221.    // A parameterized constructor that initializes the tape data with given values
  222.    Tape(string t, string a, string ti, int y, bool s, double l) : Media(t, a, ti, y, s) {
  223.      length = l;
  224.    }
  225.  
  226.    // Public getters and setters for the private instance variable
  227.  
  228.    // A getter for the length
  229.    double getLength() {
  230.      return length;
  231.    }
  232.  
  233.    // A setter for the length
  234.    void setLength(double l) {
  235.      length = l;
  236.    }
  237. };
  238.  
  239. // A derived class for videotapes
  240. class Videotape : public Media {
  241.   private:
  242.     // Private instance variable to store the quality of the videotape (HD or SD)
  243.     string quality;
  244.  
  245.   public:
  246.     // A default constructor that initializes the videotape data with default values
  247.     Videotape() : Media() {
  248.       quality = "";
  249.     }
  250.  
  251.     // A parameterized constructor that initializes the videotape data with given values
  252.     Videotape(string t, string a, string ti, int y, bool s, string q) : Media(t, a, ti, y, s) {
  253.       quality = q;
  254.     }
  255.  
  256.     // Public getters and setters for the private instance variable
  257.  
  258.     // A getter for the quality
  259.     string getQuality() {
  260.       return quality;
  261.     }
  262.  
  263.     // A setter for the quality
  264.     void setQuality(string q) {
  265.       quality = q;
  266.     }
  267. };
  268.  
  269. // A class for library
  270. class Library {
  271.   private:
  272.     // Private instance variable to store a vector of media pointers
  273.     vector<Media*> mediaList;
  274.  
  275.   public:
  276.     // A default constructor that initializes an empty library
  277.     Library() {
  278.       mediaList.clear();
  279.     }
  280.  
  281.     // A function to add a media pointer to the library
  282.     void addMedia(Media* m) {
  283.       mediaList.push_back(m);
  284.     }
  285.  
  286.     // A function to search for free media in the library
  287.     void searchFreeMedia() {
  288.       cout << "The free media in the library are: \n";
  289.       for (int i = 0; i < mediaList.size(); i++) {
  290.         if (mediaList[i]->getStatus()) { // If the media is free
  291.           cout << "Type: " << mediaList[i]->getType() << "\n";
  292.           cout << "Author: " << mediaList[i]->getAuthor() << "\n";
  293.           cout << "Title: " << mediaList[i]->getTitle() << "\n";
  294.           cout << "Year: " << mediaList[i]->getYear() << "\n";
  295.           cout << "\n";
  296.         }
  297.       }
  298.     }
  299.  
  300.     // A function to search for borrowed media in the library
  301.     //void searchBorrowedMedia()
  302. // A function to search for borrowed media in the library
  303.     void searchBorrowedMedia() {
  304.       cout << "The borrowed media in the library are: \n";
  305.       for (int i = 0; i < mediaList.size(); i++) {
  306.         if (!mediaList[i]->getStatus()) { // If the media is occupied
  307.           cout << "Type: " << mediaList[i]->getType() << "\n";
  308.           cout << "Author: " << mediaList[i]->getAuthor() << "\n";
  309.           cout << "Title: " << mediaList[i]->getTitle() << "\n";
  310.           cout << "Year: " << mediaList[i]->getYear() << "\n";
  311.           cout << "\n";
  312.         }
  313.       }
  314.     }
  315.  
  316.     // A function to input any number of different media and their data
  317.     void inputMedia() {
  318.       int n; // The number of media to input
  319.       cout << "How many media do you want to input? \n";
  320.       cin >> n;
  321.       for (int i = 0; i < n; i++) {
  322.         string type; // The type of the media
  323.         string author; // The author of the media
  324.         string title; // The title of the media
  325.         int year; // The year of publication of the media
  326.         bool status; // The condition of the media (free or occupied)
  327.         Media* m; // A pointer to a media object
  328.  
  329.         cout << "Enter the type of the media (book, magazine, audio CD, CD-ROM, tape, videotape): \n";
  330.         cin >> type;
  331.  
  332.         cout << "Enter the author of the media: \n";
  333.         cin >> author;
  334.  
  335.         cout << "Enter the title of the media: \n";
  336.         cin >> title;
  337.  
  338.         cout << "Enter the year of publication of the media: \n";
  339.         cin >> year;
  340.  
  341.         cout << "Enter the condition of the media (1 for free, 0 for occupied): \n";
  342.         cin >> status;
  343.  
  344.         // Creating a specific media object based on the type and assigning it to m
  345.         if (type == "book") {
  346.           int pages; // The number of pages of the book
  347.           cout << "Enter the number of pages of the book: \n";
  348.           cin >> pages;
  349.           m = new Book(type, author, title, year, status, pages);
  350.         }
  351.         else if (type == "magazine") {
  352.           int issue; // The issue number of the magazine
  353.           cout << "Enter the issue number of the magazine: \n";
  354.           cin >> issue;
  355.           m = new Magazine(type, author, title, year, status, issue);
  356.         }
  357.         else if (type == "audio CD") {
  358.           int duration; // The duration of the audio CD in minutes
  359.           cout << "Enter the duration of the audio CD in minutes: \n";
  360.           cin >> duration;
  361.           m = new AudioCD(type, author, title, year, status, duration);
  362.         }
  363.         else if (type == "CD-ROM") {
  364.           double capacity; // The capacity of the CD-ROM in GB
  365.           cout << "Enter the capacity of the CD-ROM in GB: \n";
  366.           cin >> capacity;
  367.           m = new CDROM(type, author, title, year, status, capacity);
  368.         }
  369.         else if (type == "tape") {
  370.           double length; // The length of the tape in meters
  371.           cout << "Enter the length of the tape in meters: \n";
  372.           cin >> length;
  373.           m = new Tape(type, author, title, year, status, length);
  374.         }
  375.         else if (type == "videotape") {
  376.           string quality; // The quality of the videotape (HD or SD)
  377.           cout << "Enter the quality of the videotape (HD or SD): \n";
  378.           cin >> quality;
  379.           m = new Videotape(type, author, title, year, status, quality);
  380.         }
  381.         else {
  382.           // Invalid type
  383.           cout << "Invalid type. Please try again. \n";
  384.           i--; // Decrementing i to repeat this iteration
  385.           continue; // Skipping to next iteration
  386.         }
  387.  
  388.         // Adding m to the library
  389.         addMedia(m);
  390.       }
  391.     }
  392. };
  393.  
  394. // The main function
  395. int main() {
  396.   // Creating a library object
  397.   Library lib;
  398.  
  399.   // Inputting some media and their data
  400.   lib.inputMedia();
  401.  
  402.   // Searching for free media in the library
  403.   lib.searchFreeMedia();
  404.  
  405.   // Searching for borrowed media in the library
  406.   lib.searchBorrowedMedia();
  407.  
  408.   // Ending the program
  409.   return 0;
  410. }
  411.  
  412.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement