Library Management System
Library Management System
#include<iostream>
using namespace std;
int main() {
int choice, index;
bool is_found;
int iban, student_roll_number;
do {
cout << "1. Issue a book to a student" << endl;
cout << "2. Student returns a book" << endl;
cout << "3. Display details of all books sorted by price" << endl;
cout << "4. Display details of all students sorted by age" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
choice = getNumericInput();
switch (choice) {
case 1:
cout << "Enter student roll number: ";
student_roll_number = getNumericInput();
if (is_found) {
cout << "Book \"" << book_names[index] << "\" by " << book_authors[index] << " (Edition: " <<
book_editions[index] << ") has been issued to student with roll number " << student_roll_number << endl;
book_quantities[index]--;
book_issued[index] = true;
}
else {
cout << "Book not available, invalid IBAN, or already issued by this library." << endl;
}
break;
case 2:
cout << "Enter student roll number: ";
student_roll_number = getNumericInput();
is_found = false;
for (int i = 0; i < BOOK_COUNT; i++) {
if (iban == book_iban_arr[i] && book_quantities[i] < 5 && book_issued[i] == true) {
is_found = true;
index = i;
break;
}
}
if (is_found) {
cout << "Book \"" << book_names[index] << "\" by " << book_authors[index] << " (Edition: " <<
book_editions[index] << ") has been returned by student with roll number " << student_roll_number << endl;
book_quantities[index]++;
book_issued[index] = false;
}
else {
cout << "Invalid IBAN, the book was not issued by this library, or the book was not issued at all." << endl;
}
break;
case 3:
// Bubble sort for displaying details of all books sorted by price
for (int i = 0; i < BOOK_COUNT; i++) {
for (int j = 0; j < BOOK_COUNT - i - 1; j++) {
if (book_prices[j] > book_prices[j + 1]) {
swap(book_iban_arr[j], book_iban_arr[j + 1]);
swap(book_names[j], book_names[j + 1]);
swap(book_authors[j], book_authors[j + 1]);
swap(book_editions[j], book_editions[j + 1]);
swap(book_prices[j], book_prices[j + 1]);
swap(book_quantities[j], book_quantities[j + 1]);
swap(book_issued[j], book_issued[j + 1]);
}
}
}
cout << "Sorted book details by price:" << endl;
for (int i = 0; i < BOOK_COUNT; i++) {
cout << "IBAN: " << book_iban_arr[i] << " | Name: " << book_names[i] << " | Author: " << book_authors[i] <<
" | Edition: " << book_editions[i] << " | Price: " << book_prices[i] << " | Quantity: " << book_quantities[i] << " | Issued: "
<< (book_issued[i] ? "Yes" : "No") << endl;
}
break;
case 4:
// Bubble sort for displaying details of all students sorted by age
for (int i = 0; i < STUDENT_COUNT; i++) {
for (int j = 0; j < STUDENT_COUNT - i - 1; j++) {
if (student_ages[j] > student_ages[j + 1]) {
swap(student_roll_numbers[j], student_roll_numbers[j + 1]);
swap(student_ages[j], student_ages[j + 1]);
}
}
}
cout << "Sorted student details by age:" << endl;
for (int i = 0; i < STUDENT_COUNT; i++) {
cout << "Roll Number: " << student_roll_numbers[i] << " | Age: " << student_ages[i] << endl;
}
break;
case 5:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice" << endl;
break;
}
return 0;
}