fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Book {
  6. private:
  7. int id;
  8. string title;
  9. string author;
  10. bool available;
  11. int patronId;
  12. int numCopies;
  13.  
  14. public:
  15. Book(int id, const string& title, const string& author, int numCopies) {
  16. this->id = id;
  17. this->title = title;
  18. this->author = author;
  19. this->available = true;
  20. this->patronId = -1;
  21. this->numCopies = numCopies;
  22. }
  23.  
  24.  
  25.  
  26. int getId() const {
  27. return id;
  28. }
  29.  
  30. string getTitle() const {
  31. return title;
  32. }
  33.  
  34. string getAuthor() const {
  35. return author;
  36. }
  37.  
  38. bool isAvailable() const {
  39. return available;
  40. }
  41.  
  42. void setAvailable(bool available) {
  43. this->available = available;
  44. }
  45.  
  46. int getPatronId() const {
  47. return patronId;
  48. }
  49.  
  50. void setPatronId(int patronId) {
  51. this->patronId = patronId;
  52. }
  53. };
  54.  
  55. class LibraryInventory {
  56. private:
  57. static const int MAX_BOOKS = 100; // maximum number of books in inventory
  58. Book* books[MAX_BOOKS]; // array of Book pointers
  59. int numBooks; // number of books in inventory
  60.  
  61. struct Node {
  62. Book* book;
  63. Node* next;
  64. Node(Book* b, Node* n = NULL) : book(b), next(n) {}
  65. };
  66. Node* head; // pointer to the first node in the linked list
  67.  
  68. public:
  69. LibraryInventory() {
  70. numBooks = 0;
  71. head = NULL;
  72. }
  73.  
  74. ~LibraryInventory() {
  75. for (int i = 0; i < numBooks; i++) {
  76. delete books[i];
  77. }
  78.  
  79. Node* curr = head;
  80. while (curr != NULL) {
  81. Node* temp = curr;
  82. curr = curr->next;
  83. delete temp->book;
  84. delete temp;
  85. }
  86. }
  87.  
  88. void addBook(int id, const string& title, const string& author, int numCopies) {
  89. for (int i = 0; i < numCopies; i++) {
  90. Book* book = new Book(id, title, author, numCopies);
  91. books[numBooks] = book;
  92. numBooks++;
  93. head = new Node(book, head);
  94. }
  95.  
  96. cout << numCopies << " copies of book added successfully." << endl;
  97. }
  98.  
  99. void removeBook(int id) {
  100. int index = findBookIndex(id);
  101. if (index == -1) {
  102. cout << "Book not found." << endl;
  103. return;
  104. }
  105. // shift remaining books to fill the gap
  106.  
  107.  
  108.  
  109.  
  110. Node* prev = NULL;
  111. Node* curr = head;
  112.  
  113. while (curr != NULL && curr->book->getId() != id) {
  114. prev = curr;
  115. curr = curr->next;
  116. }
  117.  
  118. if (curr == NULL) {
  119. cout << "Book not found." << endl;
  120. return;
  121. }
  122.  
  123. if (prev == NULL) {
  124. head = curr->next;
  125. }
  126. else {
  127. prev->next = curr->next;
  128. }
  129.  
  130. for (int i = index; i < numBooks; i++) {
  131. books[i] = books[i + 1];
  132. }
  133. delete books[numBooks - 1];
  134. numBooks--;
  135.  
  136. cout << "Book removed successfully." << endl;
  137. }
  138.  
  139. void searchBook(int id) {
  140. int index = findBookIndex(id);
  141. if (index == -1) {
  142. cout << "Book not found." << endl;
  143. return;
  144. }
  145.  
  146. Book* book = books[index];
  147.  
  148. cout << "Book found:" << endl;
  149. cout << "ID: " << book->getId() << endl;
  150. cout << "Title: " << book->getTitle() << endl;
  151. cout << "Author: " << book->getAuthor() << endl;
  152. cout << "Availability: " << (book->isAvailable() ? "Available" : "Not available") << endl;
  153. }
  154.  
  155. void checkOutBook(int id, int patronId) {
  156. int numCheckedOut = 0;
  157. int index = findBookIndex(id);
  158. if (index == -1) {
  159. cout << "Book not found." << endl;
  160. return;
  161. }
  162.  
  163. Book* book = books[index];
  164.  
  165. book->setAvailable(true);
  166. book->setPatronId(patronId);
  167. if (!book->isAvailable()) {
  168. cout << "Book is already checked out." << endl;
  169. return;
  170. }
  171.  
  172. book->setAvailable(false);
  173. book->setPatronId(patronId);
  174.  
  175. cout << "Book checked out successfully." << endl;
  176. numBooks--;
  177. numCheckedOut++;
  178.  
  179. }
  180.  
  181. void checkInBook(int id) {
  182.  
  183. int index = findBookIndex(id);
  184. if (index == -1) {
  185. cout << "Book not found." << endl;
  186. return;
  187. }
  188.  
  189. Book* book = books[index];
  190.  
  191. book->setAvailable(false);
  192. book->setPatronId(-1);
  193.  
  194. if (book->isAvailable()) {
  195. cout << "Book is already checked in." << endl;
  196. return;
  197. }
  198.  
  199. book->setAvailable(true);
  200. book->setPatronId(-1);
  201.  
  202. cout << "Book checked in successfully." << endl;
  203. numBooks++;
  204. }
  205.  
  206. void displayInventoryStatus() {
  207. int numAvailable = 0;
  208. int numCheckedOut = 0;
  209. int numCopies = 0;
  210. int i=0;
  211. cout << "Inventory:" << endl;
  212. Book* book = books[i];
  213. for ( i ; i < numBooks; i++){
  214. numCopies++;
  215. if(books[i]->isAvailable()){
  216. numAvailable++;
  217. }else{
  218. numCheckedOut++;
  219. }
  220. if(i==numBooks-1||books[i]->getId()!=books[i+1]->getId()){
  221. cout << "Book ID :" << books[i]->getId()<< ", " << numCopies << " copies, " << ", Title: " << books[i]->getTitle() << " , Author : "<< books[i]->getAuthor() << endl;
  222. numCopies = 0;
  223. numAvailable=0;
  224. }
  225. }
  226. cout << "Inventory status:" << endl;
  227. cout << "Total books: " << numBooks << endl;
  228. cout << "Available books: " << numAvailable << endl;
  229. cout << "Checked out books: " << numCheckedOut << endl;
  230. }
  231.  
  232. int numCopies(int id) {
  233. int count = 0;
  234.  
  235. for (int i = 0; i < numBooks; i++) {
  236. if (books[i]->getId() == id) {
  237. count++;
  238. }
  239. }
  240.  
  241. return count;
  242. }
  243.  
  244. int numCheckedOut(int patronId) {
  245. int count = 0;
  246.  
  247. for (int i = 0; i < numBooks; i++) {
  248. if (books[i]->getPatronId() == patronId && !books[i]->isAvailable()) {
  249. count++;
  250. }
  251. }
  252.  
  253. return count;
  254. }
  255.  
  256. void inventoryStatus() {
  257. int numAvailable = 0;
  258. int numCheckedOut = 0;
  259.  
  260. for (int i = 0; i < numBooks; i++) {
  261. if (books[i]->isAvailable()) {
  262. numAvailable++;
  263. }
  264. else {
  265. numCheckedOut++;
  266. }
  267. }
  268.  
  269. cout << "Inventory status:" << endl;
  270. cout << "Total books: " << numBooks << endl;
  271. cout << "Available books: " << numAvailable << endl;
  272. cout << "Checked out books: " << numCheckedOut << endl;
  273. }
  274.  
  275. private:
  276. int findBookIndex(int id) {
  277. for (int i = 0; i < numBooks; i++) {
  278. if (books[i]->getId() == id) {
  279. return i;
  280. }
  281. }
  282.  
  283. return -1;
  284. }
  285. };
  286.  
  287. int main() {
  288. LibraryInventory inventory;
  289.  
  290. int choice;
  291. do {
  292. cout << "Library Inventory Management System" << endl;
  293. cout << "1. Add a book" << endl;
  294. cout << "2. Remove a book" << endl;
  295. cout << "3. Search for a book" << endl;
  296. cout << "4. Check out a book" << endl;
  297. cout << "5. Check in a book" << endl;
  298. cout << "6. Display inventory" << endl;
  299. cout << "7. Number of books checked out by a patron" << endl;
  300. cout << "0. Exit" << endl;
  301. cout << "Enter your choice: ";
  302. cin >> choice;
  303.  
  304. switch (choice) {
  305. case 1: {
  306. int id;
  307. string title, author;
  308. int numCopies;
  309. cout << "Enter book ID: ";
  310. cin >> id;
  311. cout << "Enter book title: ";
  312. cin.ignore();
  313. getline(cin, title);
  314. cout << "Enter book author: ";
  315. getline(cin, author);
  316. cout << "Enter number of copies: ";
  317. cin >> numCopies;
  318. inventory.addBook(id, title, author, numCopies);
  319. break;
  320. }
  321. case 2: {
  322. int id;
  323. cout << "Enter book ID: ";
  324. cin >> id;
  325. inventory.removeBook(id);
  326. break;
  327. }
  328. case 3: {
  329. int id;
  330. cout << "Enter book ID: ";
  331. cin >> id;
  332. inventory.searchBook(id);
  333. break;
  334. }
  335. case 4: {
  336. int id, patronId;
  337. cout << "Enter book ID: ";
  338. cin >> id;
  339. cout << "Enter patron ID: ";
  340. cin >> patronId;
  341. inventory.checkOutBook(id, patronId);
  342. break;
  343. }
  344. case 5: {
  345. int id;
  346. cout << "Enter book ID: ";
  347. cin >> id;
  348. inventory.checkInBook(id);
  349. break;
  350. }
  351. case 6:
  352. inventory.displayInventoryStatus();
  353. break;
  354.  
  355. case 7: {
  356. int patronId;
  357. cout << "Enter patron ID: ";
  358. cin >> patronId;
  359. int numCheckedOut = inventory.numCheckedOut(patronId);
  360. cout << "Number of books checked out by patron who has patronID : " << patronId << " Number of books :" << numCheckedOut << endl;
  361. break;
  362. }
  363.  
  364. case 0:
  365. cout << "Exiting program..." << endl;
  366. break;
  367. default:
  368. cout << "Invalid choice. Please try again." << endl;
  369. break;
  370. }
  371.  
  372. cout << endl;
  373.  
  374. } while (choice != 0);
  375.  
  376. return 0;
  377. inventory.~LibraryInventory();
  378. }
Success #stdin #stdout 0.01s 5548KB
stdin
Standard input is empty
stdout
Library Inventory Management System
1. Add a book
2. Remove a book
3. Search for a book
4. Check out a book
5. Check in a book
6. Display inventory
7. Number of books checked out by a patron
0. Exit
Enter your choice: Exiting program...