-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliberarybook.cpp
More file actions
130 lines (115 loc) · 3.23 KB
/
liberarybook.cpp
File metadata and controls
130 lines (115 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//this is for automate library operations such as book cataloging, issue tracking, and fine calculation.
// Including graphical user interfaces (GUIs) using libraries like QT.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Define a structure to represent a book
struct Book {
string title;
string author;
int id;
bool isAvailable;
};
// Define a structure to represent a member
struct Member {
string name;
int id;
vector<Book*> borrowedBooks;
};
// Function to add a new book to the library
void addBook(vector<Book>& library) {
Book newBook;
cout << "Enter book title: ";
getline(cin >> ws, newBook.title);
cout << "Enter author name: ";
getline(cin >> ws, newBook.author);
cout << "Enter book ID: ";
cin >> newBook.id;
newBook.isAvailable = true;
library.push_back(newBook);
cout << "Book added successfully!" << endl;
}
// Function to lend a book to a member
void lendBook(vector<Book>& library, vector<Member>& members) {
int memberId, bookId;
cout << "Enter member ID: ";
cin >> memberId;
cout << "Enter book ID: ";
cin >> bookId;
// Search for the member
Member* member = nullptr;
for (auto& m : members) {
if (m.id == memberId) {
member = &m;
break;
}
}
if (!member) {
cout << "Member not found." << endl;
return;
}
// Search for the book
Book* book = nullptr;
for (auto& b : library) {
if (b.id == bookId) {
book = &b;
break;
}
}
if (!book) {
cout << "Book not found." << endl;
return;
}
if (book->isAvailable) {
book->isAvailable = false;
member->borrowedBooks.push_back(book);
cout << "Book successfully lent to " << member->name << "." << endl;
} else {
cout << "Book is already lent out." << endl;
}
}
// Function to display all books in the library
void displayBooks(const vector<Book>& library) {
cout << "Library Books:" << endl;
for (const auto& book : library) {
cout << "Title: " << book.title << ", Author: " << book.author << ", ID: " << book.id;
if (book.isAvailable) {
cout << " (Available)" << endl;
} else {
cout << " (Not Available)" << endl;
}
}
}
int main() {
vector<Book> library;
vector<Member> members;
int choice;
do {
cout << "\nLibrary Management System\n";
cout << "1. Add Book\n";
cout << "2. Lend Book\n";
cout << "3. Display Books\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cin.ignore(); // Ignore the newline character left by cin
addBook(library);
break;
case 2:
lendBook(library, members);
break;
case 3:
displayBooks(library);
break;
case 4:
cout << "Exiting...";
break;
default:
cout << "Invalid choice. Please enter a number from 1 to 4." << endl;
}
} while (choice != 4);
return 0;
}