Advertisement
CR7CR7

privateLibrary

Apr 6th, 2023
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. // A class to represent a media item
  2. class Media {
  3.   public:
  4.     // A constructor to initialize the data members
  5.     Media(string type, string author, string title, int year, bool status) {
  6.       this->type = type;
  7.       this->author = author;
  8.       this->title = title;
  9.       this->year = year;
  10.       this->status = status;
  11.     }
  12.  
  13.     // A method to display the media information
  14.     void display() {
  15.       cout << "Type: " << type << endl;
  16.       cout << "Author: " << author << endl;
  17.       cout << "Title: " << title << endl;
  18.       cout << "Year: " << year << endl;
  19.       cout << "Status: " << (status ? "Free" : "Occupied") << endl;
  20.     }
  21.  
  22.     // Getters and setters for the data members
  23.     string getType() {
  24.       return type;
  25.     }
  26.  
  27.     void setType(string type) {
  28.       this->type = type;
  29.     }
  30.  
  31.     string getAuthor() {
  32.       return author;
  33.     }
  34.  
  35.     void setAuthor(string author) {
  36.       this->author = author;
  37.     }
  38.  
  39.     string getTitle() {
  40.       return title;
  41.     }
  42.  
  43.     void setTitle(string title) {
  44.       this->title = title;
  45.     }
  46.  
  47.     int getYear() {
  48.       return year;
  49.     }
  50.  
  51.     void setYear(int year) {
  52.       this->year = year;
  53.     }
  54.  
  55.     bool getStatus() {
  56.       return status;
  57.     }
  58.  
  59.     void setStatus(bool status) {
  60.       this->status = status;
  61.     }
  62.  
  63.   private:
  64.     // Data members to store the media information
  65.     string type; // The type of the media
  66.     string author; // The author of the media
  67.     string title; // The title of the media
  68.     int year; // The year of publication of the media
  69.     bool status; // The status of the media (free or occupied)
  70. };
  71.  
  72. // A class to represent a virtual library
  73. class Library {
  74.   public:
  75.     // A constructor to initialize the data member
  76.     Library() {
  77.       mediaList = vector<Media>(); // An empty vector of media items
  78.     }
  79.  
  80.     // A method to add a new media item to the library
  81.     void addMedia(Media media) {
  82.       mediaList.push_back(media); // Append the media item to the vector
  83.       cout << "Media added successfully." << endl;
  84.     }
  85.  
  86.     // A method to display all the media items in the library
  87.     void displayAll() {
  88.       cout << "The library contains " << mediaList.size() << " media items." << endl;
  89.       for (int i = 0; i < mediaList.size(); i++) { // Loop through the vector
  90.         cout << "Media #" << i + 1 << ":" << endl;
  91.         mediaList[i].display(); // Display each media item
  92.         cout << endl;
  93.       }
  94.     }
  95.  
  96.   private:
  97.     // A data member to store a vector of media items
  98.     vector<Media> mediaList;
  99. };
  100.  
  101. // A main function to test the classes
  102. int main() {
  103.   // Create a library object
  104.   Library library;
  105.  
  106.   // Create some media objects
  107.   Media m1("Book", "J.K. Rowling", "Harry Potter and the Philosopher's Stone", 1997, true);
  108.   Media m2("Magazine", "National Geographic", "The Future of Medicine", 2020, false);
  109.   Media m3("CD-ROM", "Microsoft", "Windows 10", 2015, true);
  110.  
  111.   // Add the media objects to the library
  112.   library.addMedia(m1);
  113.   library.addMedia(m2);
  114.   library.addMedia(m3);
  115.  
  116.   // Display all the media items in the library
  117.   library.displayAll();
  118.  
  119.   return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement