-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.cpp
More file actions
152 lines (136 loc) · 4.47 KB
/
Copy pathLibrary.cpp
File metadata and controls
152 lines (136 loc) · 4.47 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Library.h"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iostream>
using namespace std;
Library::Library() {
loadBooks();
loadStudents();
}
void Library::loadBooks() {
ifstream file("books.txt");
string line, id, title, category;
while (getline(file, line)) {
stringstream ss(line);
getline(ss, id, ',');
getline(ss, title, ',');
getline(ss, category, ',');
books.push_back(Book(id, title, category));
}
file.close();
}
void Library::loadStudents() {
ifstream file("students.txt");
string line, rollNumber, name, password;
while (getline(file, line)) {
stringstream ss(line);
getline(ss, rollNumber, ',');
getline(ss, name, ',');
getline(ss, password, ',');
students.push_back(Student(rollNumber, name, password));
}
file.close();
}
void Library::saveStudent(const Student& student) {
ofstream file("students.txt", ios::app);
file << student.rollNumber << "," << student.name << "," << student.password << endl;
file.close();
}
void Library::registerStudent() {
string rollNumber, name, password;
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Password: ";
cin >> password;
Student student(rollNumber, name, password);
students.push_back(student);
saveStudent(student);
cout << "Student registered successfully!" << endl;
}
void Library::displayBooks() const {
for (const auto& book : books) {
book.display();
}
}
void Library::issueBook() {
string rollNumber, bookId;
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter Book ID: ";
cin >> bookId;
auto student = find_if(students.begin(), students.end(), [&rollNumber](const Student& s) { return s.rollNumber == rollNumber; });
if (student != students.end()) {
auto book = find_if(books.begin(), books.end(), [&bookId](const Book& b) { return b.id == bookId; });
if (book != books.end()) {
if (!book->isIssued) {
book->isIssued = true;
student->issuedBooks.push_back(bookId);
cout << "Book issued successfully to " << student->name << endl;
} else {
cout << "Book is already issued!" << endl;
}
} else {
cout << "Book not found!" << endl;
}
} else {
cout << "Student not found!" << endl;
}
}
void Library::returnBook() {
string rollNumber, bookId;
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter Book ID: ";
cin >> bookId;
auto student = find_if(students.begin(), students.end(), [&rollNumber](const Student& s) { return s.rollNumber == rollNumber; });
if (student != students.end()) {
auto it = find(student->issuedBooks.begin(), student->issuedBooks.end(), bookId);
if (it != student->issuedBooks.end()) {
student->issuedBooks.erase(it);
auto book = find_if(books.begin(), books.end(), [&bookId](Book& b) { return b.id == bookId; });
if (book != books.end()) {
book->isIssued = false;
cout << "Book returned successfully!" << endl;
}
} else {
cout << "Book not issued to this student!" << endl;
}
} else {
cout << "Student not found!" << endl;
}
}
void Library::searchBook() const {
string title;
cout << "Enter Book Title: ";
cin.ignore();
getline(cin, title);
auto book = find_if(books.begin(), books.end(), [&title](const Book& b) { return b.title == title; });
if (book != books.end()) {
book->display();
} else {
cout << "Book not found!" << endl;
}
}
void Library::deleteBook() {
string bookId;
cout << "Enter Book ID to delete: ";
cin >> bookId;
auto it = remove_if(books.begin(), books.end(), [&bookId](const Book& b) { return b.id == bookId; });
if (it != books.end()) {
books.erase(it, books.end());
cout << "Book deleted successfully!" << endl;
} else {
cout << "Book not found!" << endl;
}
}
void Library::displayIssuedBooks() const {
for (const auto& book : books) {
if (book.isIssued) {
book.display();
}
}
}