fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. class user {
  9. protected:
  10. string name, username, password, phone_number, email_address, role;
  11.  
  12. public:
  13. user(string name, string username, string password, string phone_number, string email_address, string role)
  14. : name(name), username(username), password(password), phone_number(phone_number), email_address(email_address), role(role) {}
  15.  
  16. virtual void login() {
  17. cout << "Logging in as " << role << endl;
  18. }
  19.  
  20. void logout() {
  21. cout << "Logging out...\n";
  22. }
  23.  
  24. void change_password(string new_password) {
  25. password = new_password;
  26. cout << "Password changed successfully.\n";
  27. }
  28. };
  29.  
  30. class book {
  31. private:
  32. string title, author, genre;
  33. int number_of_copies;
  34.  
  35. public:
  36. book(string title, string author, string genre, int number_of_copies)
  37. : title(title), author(author), genre(genre), number_of_copies(number_of_copies) {}
  38.  
  39. bool is_available() {
  40. return number_of_copies > 0;
  41. }
  42.  
  43. void checkout() {
  44. if (is_available()) {
  45. number_of_copies--;
  46. cout << "Checked out: " << title << endl;
  47. } else {
  48. cout << "No copies available for checkout.\n";
  49. }
  50. }
  51.  
  52. void check_in() {
  53. number_of_copies++;
  54. cout << "Checked in: " << title << endl;
  55. }
  56.  
  57. string get_title() const {
  58. return title;
  59. }
  60. };
  61.  
  62. class magazine {
  63. private:
  64. string title, publisher, publication_date;
  65. bool available;
  66.  
  67. public:
  68. magazine(string title, string publisher, string publication_date, bool available)
  69. : title(title), publisher(publisher), publication_date(publication_date), available(available) {}
  70.  
  71. bool is_available() {
  72. return available;
  73. }
  74.  
  75. void checkout() {
  76. if (is_available()) {
  77. available = false;
  78. cout << "Checked out: " << title << endl;
  79. } else {
  80. cout << "Magazine not available for checkout.\n";
  81. }
  82. }
  83.  
  84. void check_in() {
  85. available = true;
  86. cout << "Checked in: " << title << endl;
  87. }
  88.  
  89. string get_title() const {
  90. return title;
  91. }
  92. };
  93.  
  94. class patron : public user {
  95. private:
  96. string library_card_number, address;
  97.  
  98. public:
  99. patron(string name, string username, string password, string phone_number, string email_address, string library_card_number, string address)
  100. : user(name, username, password, phone_number, email_address, "Patron"), library_card_number(library_card_number), address(address) {}
  101.  
  102. void search_and_checkout_book(vector<book>& books, string title) {
  103. for (auto& b : books) {
  104. if (b.get_title() == title) {
  105. b.checkout();
  106. return;
  107. }
  108. }
  109. cout << "Book not available.\n";
  110. }
  111.  
  112. void search_and_checkout_magazine(vector<magazine>& magazines, string title) {
  113. for (auto& m : magazines) {
  114. if (m.get_title() == title) {
  115. m.checkout();
  116. return;
  117. }
  118. }
  119. cout << "Magazine not available.\n";
  120. }
  121. };
  122.  
  123. class librarian : public user {
  124. private:
  125. string employee_id, department;
  126.  
  127. public:
  128. librarian(string name, string username, string password, string phone_number, string email_address, string employee_id, string department)
  129. : user(name, username, password, phone_number, email_address, "Librarian"), employee_id(employee_id), department(department) {}
  130.  
  131. void add_book(vector<book>& books, string title, string author, string genre, int number_of_copies) {
  132. books.emplace_back(title, author, genre, number_of_copies);
  133. cout << "Book added: " << title << endl;
  134. }
  135.  
  136. void delete_book(vector<book>& books, string title) {
  137. auto it = find_if(books.begin(), books.end(), [&title](const book& b) { return b.get_title() == title; });
  138. if (it != books.end()) {
  139. books.erase(it);
  140. cout << "Book deleted: " << title << endl;
  141. } else {
  142. cout << "Book not found.\n";
  143. }
  144. }
  145.  
  146. void add_magazine(vector<magazine>& magazines, string title, string publisher, string publication_date, bool available) {
  147. magazines.emplace_back(title, publisher, publication_date, available);
  148. cout << "Magazine added: " << title << endl;
  149. }
  150.  
  151. void delete_magazine(vector<magazine>& magazines, string title) {
  152. auto it = find_if(magazines.begin(), magazines.end(), [&title](const magazine& m) { return m.get_title() == title; });
  153. if (it != magazines.end()) {
  154. magazines.erase(it);
  155. cout << "Magazine deleted: " << title << endl;
  156. } else {
  157. cout << "Magazine not found.\n";
  158. }
  159. }
  160. };
  161.  
  162. int main() {
  163. vector<book> books;
  164. vector<magazine> magazines;
  165.  
  166. string name, username, password, phone_number, email_address, role;
  167. cout << "Enter your name: ";
  168. getline(cin, name);
  169. cout << "Enter your username: ";
  170. getline(cin, username);
  171. cout << "Enter your password: ";
  172. getline(cin, password);
  173. cout << "Enter your phone number: ";
  174. getline(cin, phone_number);
  175. cout << "Enter your email address: ";
  176. getline(cin, email_address);
  177. cout << "Enter your role (Patron or Librarian): ";
  178. getline(cin, role);
  179.  
  180. if (role == "Patron") {
  181. string library_card_number, address;
  182. cout << "Enter your library card number: ";
  183. getline(cin, library_card_number);
  184. cout << "Enter your address: ";
  185. getline(cin, address);
  186.  
  187. patron user(name, username, password, phone_number, email_address, library_card_number, address);
  188.  
  189. string search_title;
  190. cout << "Enter the title of the book or magazine you're looking for: ";
  191. getline(cin, search_title);
  192.  
  193. user.search_and_checkout_book(books, search_title);
  194. user.search_and_checkout_magazine(magazines, search_title);
  195.  
  196. } else if (role == "Librarian") {
  197. string employee_id, department;
  198. cout << "Enter your employee ID: ";
  199. getline(cin, employee_id);
  200. cout << "Enter your department: ";
  201. getline(cin, department);
  202.  
  203. librarian user(name, username, password, phone_number, email_address, employee_id, department);
  204.  
  205. int choice;
  206. while (true) {
  207. cout << "Choose an option:\n1. Add Book\n2. Delete Book\n3. Add Magazine\n4. Delete Magazine\n5. Exit\n";
  208. cin >> choice;
  209. cin.ignore();
  210.  
  211. if (choice == 1) {
  212. string title, author, genre;
  213. int copies;
  214. cout << "Enter book title: ";
  215. getline(cin, title);
  216. cout << "Enter book author: ";
  217. getline(cin, author);
  218. cout << "Enter book genre: ";
  219. getline(cin, genre);
  220. cout << "Enter number of copies: ";
  221. cin >> copies;
  222. cin.ignore();
  223. user.add_book(books, title, author, genre, copies);
  224. } else if (choice == 2) {
  225. string title;
  226. cout << "Enter book title to delete: ";
  227. getline(cin, title);
  228. user.delete_book(books, title);
  229. } else if (choice == 3) {
  230. string title, publisher, publication_date;
  231. bool available;
  232. cout << "Enter magazine title: ";
  233. getline(cin, title);
  234. cout << "Enter publisher: ";
  235. getline(cin, publisher);
  236. cout << "Enter publication date: ";
  237. getline(cin, publication_date);
  238. cout << "Is it available (1 for Yes, 0 for No): ";
  239. cin >> available;
  240. cin.ignore();
  241. user.add_magazine(magazines, title, publisher, publication_date, available);
  242. } else if (choice == 4) {
  243. string title;
  244. cout << "Enter magazine title to delete: ";
  245. getline(cin, title);
  246. user.delete_magazine(magazines, title);
  247. } else if (choice == 5) {
  248. break;
  249. } else {
  250. cout << "Invalid choice. Try again.\n";
  251. }
  252. }
  253. } else {
  254. cout << "Invalid role! Please enter either 'Patron' or 'Librarian'.\n";
  255. }
  256.  
  257. return 0;
  258. }
  259.  
Success #stdin #stdout 0s 5256KB
stdin
Standard input is empty
stdout
Enter your name: Enter your username: Enter your password: Enter your phone number: Enter your email address: Enter your role (Patron or Librarian): Invalid role! Please enter either 'Patron' or 'Librarian'.