-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibraryManagementwCSV.cpp
222 lines (203 loc) · 6.53 KB
/
LibraryManagementwCSV.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <queue>
using namespace std;
struct Book {
int id;
string title;
string author;
bool isIssued;
Book* next;
};
class BookList {
private:
Book* head;
public:
BookList() : head(nullptr) {}
void addBook(int id, string title, string author, bool isIssued = false) {
Book* newBook = new Book{id, title, author, isIssued, nullptr};
if (!head) {
head = newBook;
} else {
Book* temp = head;
while (temp->next) temp = temp->next;
temp->next = newBook;
}
cout << "Book added successfully!" << endl;
}
Book* searchBook(string title) {
Book* temp = head;
while (temp) {
if (temp->title == title) return temp;
temp = temp->next;
}
return nullptr;
}
void removeBook(int id) {
if (!head) return;
if (head->id == id) {
Book* temp = head;
head = head->next;
delete temp;
cout << "Book removed successfully!" << endl;
return;
}
Book* temp = head;
while (temp->next && temp->next->id != id) {
temp = temp->next;
}
if (temp->next) {
Book* toDelete = temp->next;
temp->next = toDelete->next;
delete toDelete;
cout << "Book removed successfully!" << endl;
}
}
void displayBooks() {
Book* temp = head;
if (!temp) {
cout << "No books available in the library." << endl;
return;
}
while (temp) {
cout << "ID: " << temp->id << ", Title: " << temp->title << ", Author: " << temp->author
<< ", Issued: " << (temp->isIssued ? "Yes" : "No") << endl;
temp = temp->next;
}
}
bool issueBook(string title) {
Book* book = searchBook(title);
if (book && !book->isIssued) {
book->isIssued = true;
cout << "Book \"" << title << "\" has been issued." << endl;
return true;
} else if (book && book->isIssued) {
cout << "Book \"" << title << "\" is already issued." << endl;
} else {
cout << "Book \"" << title << "\" not found." << endl;
}
return false;
}
void returnBook(string title) {
Book* book = searchBook(title);
if (book && book->isIssued) {
book->isIssued = false;
cout << "Book \"" << title << "\" has been returned." << endl;
} else if (book && !book->isIssued) {
cout << "Book \"" << title << "\" was not issued." << endl;
} else {
cout << "Book \"" << title << "\" not found." << endl;
}
}
void saveToCSV(string filename) {
ofstream file(filename);
if (!file) {
cout << "Error: Could not open file for writing." << endl;
return;
}
Book* temp = head;
while (temp) {
file << temp->id << "," << temp->title << "," << temp->author << "," << (temp->isIssued ? "1" : "0") << endl;
temp = temp->next;
}
file.close();
cout << "Books saved to file successfully!" << endl;
}
void loadFromCSV(string filename) {
ifstream file(filename);
if (!file) {
cout << "Error: Could not open file for reading." << endl;
return;
}
string line, title, author;
int id;
bool isIssued;
while (getline(file, line)) {
stringstream ss(line);
string issued;
getline(ss, line, ',');
id = stoi(line);
getline(ss, title, ',');
getline(ss, author, ',');
getline(ss, issued);
isIssued = (issued == "1");
addBook(id, title, author, isIssued);
}
file.close();
cout << "Books loaded from file successfully!" << endl;
}
};
void displayMenu() {
cout << "\nLibrary Management System Menu" << endl;
cout << "1. Display all books" << endl;
cout << "2. Add a book" << endl;
cout << "3. Remove a book" << endl;
cout << "4. Search for a book" << endl;
cout << "5. Issue a book" << endl;
cout << "6. Return a book" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
}
int main() {
BookList library;
int choice, id;
string title, author;
string filename = "books.csv";
library.loadFromCSV(filename);
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1:
library.displayBooks();
break;
case 2:
cout << "Enter book ID: ";
cin >> id;
cin.ignore();
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter book author: ";
getline(cin, author);
library.addBook(id, title, author);
break;
case 3:
cout << "Enter book ID to remove: ";
cin >> id;
library.removeBook(id);
break;
case 4:
cin.ignore();
cout << "Enter book title to search: ";
getline(cin, title);
if (library.searchBook(title)) {
cout << "Book found!" << endl;
} else {
cout << "Book not found." << endl;
}
break;
case 5:
cin.ignore();
cout << "Enter book title to issue: ";
getline(cin, title);
library.issueBook(title);
break;
case 6:
cin.ignore();
cout << "Enter book title to return: ";
getline(cin, title);
library.returnBook(title);
break;
case 0:
cout << "Saving books to file..." << endl;
library.saveToCSV(filename);
cout << "Exiting the system." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 0);
return 0;
}