-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.cpp
277 lines (255 loc) · 7.97 KB
/
library.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "library.h"
library::library() {
std::string file_name = "./books.txt";
std::ifstream ifs(file_name);
uint64_t allBooks;
uint64_t allStudents;
if (!ifs.is_open()) {
std::cout << "can't open this file " << file_name << std::endl;
} else {
ifs >> allBooks; //第一行存储图书管理系统中一共的书籍数量
for (uint64_t i = 0; i < allBooks; i++) {
Books temp;
ifs >> temp.id_ >> temp.name_;
books.push_back(temp);
}
ifs.close();
};
file_name = "./students.txt";
ifs.open(file_name);
if (!ifs.is_open()) {
std::cout << "can't open this file" << file_name << std::endl;
} else {
ifs >> allStudents;
for (uint64_t i = 0; i < allStudents; i++) {
Students temp;
ifs >> temp.id_ >> temp.name_ >> temp.borrow_nums_ >> temp.total_nums_;
students.push_back(temp);
}
ifs.close();
}
}
library::~library() {
std::string file_name = "./books.txt";
std::ofstream ofs(file_name);
std::cout << "Deinit class library" << std::endl;
if (!ofs.is_open()) {
std::cout << "can't open this file" << file_name << std::endl;
return;
}
ofs << books.size() << "\n";
for (auto &book : books) {
ofs << book.id_ << " " << book.name_ << "\n";
}
ofs.close();
file_name = "./students.txt";
ofs.open(file_name);
if (!ofs.is_open()) {
std::cout << "can't open this file " << file_name << std::endl;
return;
}
ofs << students.size() << "\n";
for (auto &temp : students) {
ofs << temp.id_ << " " << temp.name_ << " "
<< temp.borrow_nums_ << " " << temp.total_nums_ << "\n";
}
ofs.close();
}
void library::addBooks(Books newbook) {
books.push_back(newbook);
}
void library::addBooks() {
Books newbook;
std::cin.clear();
std::cin.sync();
newbook.id_ = books.size();
std::cout << "Please input books name:";
std::cin >> newbook.name_;
std::cout << "Please input books author:";
std::cin >> newbook.author_;
std::cout << "Please input books num:";
std::cin >> newbook.sum_;
newbook.rest_num_ = newbook.sum_;
books.push_back(newbook);
}
uint64_t library::findBooks(std::string bookName, std::string author) {
for (auto &book : books) {
if (book.name_ == bookName && book.author_ == author) {
book.print();
return book.id_;
}
}
return -1;
}
bool library::borrowBooks(std::string bookName, std::string author) {
uint64_t id = findBooks(bookName, author);
if (id != -1) {
uint64_t tmp;
std::cout << "Please input your usr_id:";
std::cin >> tmp;
if (tmp < students.size()) {
if (students[tmp].borrow_nums_ >= 10) {
std::cout << "sorry , you have already borrowed 10 books" << std::endl;
return false;
}
students[tmp].book_list_.push_back(id);
students[tmp].borrow_nums_++;
students[tmp].total_nums_++;
books[id].rest_num_--;
books[id].can_borrow_ = (books[id].rest_num_ == 0) ? false : true;
books[id].borrow_count_++; //redundent
saveBooksInfo();
saveStudentsInfo();
} else {
std::cout << "wrong usr_id" << std::endl;
return false;
}
} else {
std::cout << "Sorry, can't find this book:" << bookName << std::endl;
}
return true;
}
void library::returnBooks(std::string bookname, std::string author) {
int64_t user_id = -1;
bool flag = false;
std::cout << "please input your id." << std::endl;
std::cin >> user_id;
for (int i = 0; i < books.size(); i++) {
if (books[i].name_ == bookname && books[i].author_ == author) {
books[i].rest_num_++;
books[i].can_borrow_=bool(books[i].rest_num_);
books[i].borrow_count_--;
students[user_id].borrow_nums_--;
//auto it = books.begin() + books[i].id_;
//students[user_id].book_list_.erase(book_list_.begin() + books[i].id_);
total_return_nums++;
flag = true;
}
}
if (!flag) {
std::cout << "Sorry cannot find this book under your credit" << std::endl;
}
void saveBooksInfo(); //保存图书信息
void saveStudentsInfo(); //保存学生信息
}
bool library::compareBook(const Books &A, const Books &B) {
return A.borrow_count_ > B.borrow_count_;
}
bool library::compareReader(const Students &A, const Students &B) {
return A.borrow_nums_ > B.borrow_nums_;
}
void library::bookStatics() {
std::sort(books.begin(), books.end(), compareBook);
std::cout << "Books Borrow Ranks" << std::endl;
for (auto book : books) {
std::cout << "id: " << book.id_ << ", bookname: " << book.name_ << ", borrow_count: " << book.borrow_count_
<< std::endl;
}
}
void library::readerStatics() {
sort(students.begin(), students.end(), compareReader);
std::cout << "Readers Ranks" << std::endl;
for (auto student : students) {
std::cout << "id: " << student.id_ << ", name: " << student.name_ << ", borrow_count_: " << student.borrow_nums_
<< std::endl;
}
}
void library::saveBooksInfo() {
std::string file_name = "./books.txt";
std::ofstream ofs(file_name);
if (!ofs.is_open()) {
std::cout << "Open failed :" << file_name;
}
ofs << books.size() << "\n"; //first line is book nums
for (int i = 0; i < books.size(); i++) {
ofs << books[i].id_ << " " << books[i].name_ << " "
<< books[i].author_ << " " << books[i].sum_ << " "
<< books[i].rest_num_ << " " << books[i].can_borrow_ << "\n";
}
ofs.close();
}
void library::saveStudentsInfo() {
std::string file_name = "./students.txt";
std::ofstream ofs(file_name);
if (!ofs.is_open()) {
std::cout << "Open failed :" << file_name;
}
ofs << students.size() << "\n";
for (int i = 0; i < students.size(); i++) {
ofs << students[i].id_ << " " << students[i].name_ << " "
<< students[i].total_nums_ << " " << students[i].borrow_nums_;
for (int j = 0; j < students[i].book_list_.size(); j++) {
ofs << " " << students[i].book_list_[j];
}
ofs << "\n";
}
ofs.close();
}
void library::printAllMsg() {
for (auto &book : books) {
book.print();
}
}
bool library::findStudents(uint64_t user_id) {
uint32_t size = students.size();
for (uint32_t i = 0; i < size; i++) {
if(students[i].id_ == user_id) {
return true;
}
}
return false;
}
void library::deleteBooks() {
std::cin.sync();
std::cin.clear();
uint64_t temp = 0;
std::cout << "Delete Books by id(0) or name(1): " << std::endl;
std::cin >> temp;
switch (temp) {
case 0:
{
uint64_t books_id;
std::cout << "Please input book id: " << std::endl;
std::cin >> books_id;
deleteBooks(books_id);
break;
}
case 1:
{
std::string books_name;
std::string books_author;
std::cout << "Please input book name: " << std::endl;
std::cin >> books_name;
std::cout << "Please input book author: " << std::endl;
std::cin >> books_author;
deleteBooks(books_name, books_author);
break;
}
default:
break;
}
}
void library::deleteBooks(std::string bookname, std::string author) {
auto it = books.begin();
while (it != books.end()) {
if ((*it).name_ == bookname && (*it).author_ == author) {
books.erase(it);
saveBooksInfo();
continue;
}
it++;
}
return;
}
void library::deleteBooks(uint64_t id) {
auto it = books.begin();
while(it != books.end()) {
if ((*it).id_ == id) {
books.erase(it); //erase后it迭代器会自动后移这个时候不再需要自增了
saveBooksInfo();
continue;
}
it++;
}
return;
}