Advertisement
AshTurner67

Online 21 A

Oct 21st, 2024 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Book
  7. {
  8.     string title;
  9.     string author;
  10.     string availability;
  11. public:
  12.     Book(){
  13.         availability = "Available";
  14.     }
  15.     Book(string t, string a){
  16.         title = t;
  17.         author = a;
  18.         availability = "Available";
  19.     }
  20.     ~Book(){}
  21.     void setTitle(string t){
  22.         title = t;
  23.     }
  24.     void setAuthor(string a){
  25.         author = a;
  26.     }
  27.     void setAvailability(string a){
  28.         availability = a;
  29.     }
  30.     string getTitle(){
  31.         return title;
  32.     }
  33.     string getAuthor(){
  34.         return author;
  35.     }
  36.    
  37.     string getAvaibality(){
  38.         return availability;
  39.     }
  40.     void display(){
  41.         cout << "\tTitle: " << title << '\n';
  42.         cout << "\tAuthor: " << author << '\n';
  43.         cout << "\tAvailability: " << availability << '\n';
  44.     }
  45. };
  46.  
  47. class LibraryMember
  48. {
  49.     string name;
  50.     int libraryCardNumber;
  51. public:
  52.     LibraryMember(){}
  53.     LibraryMember(string s, int n){
  54.         name = s;
  55.         libraryCardNumber = n;
  56.     }
  57.     ~LibraryMember(){}
  58.     void setName(string s){
  59.         name = s;
  60.     }
  61.     void setNumber(int n){
  62.         libraryCardNumber = n;
  63.     }
  64.     string getName(){
  65.         return name;
  66.     }
  67.     int getNumber(){
  68.         return libraryCardNumber;
  69.     }
  70.     void display(){
  71.         cout << "\tName: " << name << '\n';
  72.         cout << "\tLibrary Card Number: " << libraryCardNumber << '\n';
  73.     }
  74.  
  75. };
  76.  
  77. class Library
  78. {
  79.     Book books[100];
  80.     LibraryMember members[100];
  81.     int bookCount;
  82.     int memberCount;
  83. public:
  84.     Library(){
  85.         bookCount = 0;
  86.         memberCount = 0;
  87.     }
  88.     void addBook(string t, string s){
  89.         books[bookCount].setTitle(t);
  90.         books[bookCount].setAuthor(s);
  91.         bookCount++;
  92.     }
  93.     void displayAllBooks(){
  94.         cout << "Books in the library:\n";
  95.         for(int i = 0; i < bookCount; i++){
  96.             books[i].display();
  97.             cout << '\n';
  98.         }
  99.     }
  100.     void registerMember(string s, int n){
  101.         members[memberCount].setName(s);
  102.         members[memberCount].setNumber(n);
  103.         memberCount++;
  104.     }
  105.     void displayAllMembers(){
  106.         cout << "Members in the library:\n";
  107.         for(int i = 0; i < memberCount; i++){
  108.             members[i].display();
  109.             cout << '\n';
  110.         }
  111.     }
  112.     void borrowBook(string t, int id){
  113.         bool bookFound = false;
  114.         for(int i = 0; i < bookCount; i++){
  115.             if(books[i].getTitle() == t){
  116.                 bookFound = true;
  117.                 int j;
  118.                 bool memberFound = false;
  119.                 for(j = 0; j < memberCount; j++){
  120.                     if(members[j].getNumber() == id){
  121.                         memberFound = true;
  122.                         break;
  123.                     }
  124.                 }
  125.                 if(!memberFound){
  126.                     cout << "Member not found\n\n";
  127.                     break;
  128.                 }
  129.                 books[i].setAvailability("Not Available");
  130.                 cout << "Book '" << books[i].getTitle() << "' has been borrowed by\n";
  131.                 members[j].display();
  132.                 cout << '\n';
  133.             }
  134.         }
  135.         if(!bookFound)
  136.             cout << "Book not found\n";      
  137.     }
  138.     void returnBook(string t, int id){
  139.         bool bookFound = false;
  140.         for(int i = 0; i < bookCount; i++){
  141.             if(books[i].getTitle() == t){
  142.                 bookFound = true;
  143.                 int j;
  144.                 bool memberFound = false;
  145.                 for(j = 0; j < memberCount; j++){
  146.                     if(members[j].getNumber() == id){
  147.                         memberFound = true;
  148.                         break;
  149.                     }
  150.                 }
  151.                 if(!memberFound){
  152.                     cout << "Member not found\n\n";
  153.                     break;
  154.                 }
  155.                 books[i].setAvailability("Available");
  156.                 cout << "Book '" << books[i].getTitle() << "' has been returned by\n";
  157.                 members[j].display();
  158.                 cout << '\n';
  159.             }
  160.         }
  161.         if(!bookFound)
  162.             cout << "Book not found\n";
  163.     }
  164.     void removeBook(string t){
  165.         bool found = false;
  166.         string name = t;
  167.         for(int i = 0; i < bookCount; i++){
  168.             if(books[i].getTitle() == t) found = true;
  169.             if(found && i + 1 < bookCount){
  170.                 books[i] = books[i+1];
  171.             }
  172.         }
  173.         if(!found) cout << "Book not found\n";
  174.         else{
  175.             cout << "Book '" << name << "' has been removed from the library.\n\n";
  176.             bookCount--;
  177.         }
  178.     }
  179.     void removeMember(int t){
  180.         bool found = false;
  181.         string name;
  182.         for(int i = 0; i < memberCount; i++){
  183.             if(members[i].getNumber() == t) found = true, name = members[i].getName();
  184.             if(found && i + 1 < memberCount){
  185.                 members[i] = members[i+1];
  186.             }
  187.         }
  188.         if(!found) cout << "Member not found\n";
  189.         else{
  190.             cout << "Member '" << name << "' has been removed from the library.\n\n";
  191.             memberCount--;
  192.         }
  193.     }
  194.  
  195. };
  196.  
  197. int main()
  198. {
  199.     Library library;
  200.     // Adding books
  201.     library.addBook("The Great Gatsby", "F. Scott Fitzgerald");
  202.     library.addBook("To Kill a Mockingbird", "Harper Lee");
  203.     library.addBook("Pride and Prejudice", "Jane Austen");
  204.     // Displaying all books
  205.     library.displayAllBooks();
  206.     // Registering members
  207.     library.registerMember("John Doe", 1234);
  208.     library.registerMember("Jane Smith", 5678);
  209.     // Displaying all members
  210.     library.displayAllMembers();
  211.     // Borrowing books
  212.     library.borrowBook("The Great Gatsby", 1234);
  213.     library.borrowBook("To Kill a Mockingbird", 5678);
  214.     // Displaying all books (after borrowing)
  215.     library.displayAllBooks();
  216.     // Returning a book
  217.     library.returnBook("The Great Gatsby", 1234);
  218.     // Displaying all books (after returning)
  219.     library.displayAllBooks();
  220.     // Removing a book
  221.     library.removeBook("Pride and Prejudice");
  222.     // Displaying all books (after removal)
  223.     library.displayAllBooks();
  224.     // Removing a member
  225.     library.removeMember(5678);
  226.     // Displaying all members (after removal)
  227.     library.displayAllMembers();
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement