-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookSystem.h
More file actions
281 lines (256 loc) · 9.96 KB
/
BookSystem.h
File metadata and controls
281 lines (256 loc) · 9.96 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
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
278
279
280
281
#ifndef INC_1_6BOOKSTORE_BOOKSYSTEM_H
#define INC_1_6BOOKSTORE_BOOKSYSTEM_H
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <iomanip>
#include "AccountSystem.h"
#include "UnrolledLinkedList_double.h"
class Book {
public:
int tag;
char ISBN[21];
char BookName[61];
char Author[61];
char Keyword[61];
int Quantity = 0;
double Price = 0.0;
double TotalCost = 0.0;
Book() {
tag = -1;
memset(ISBN, 0, sizeof(ISBN));
memset(BookName, 0, sizeof(BookName));
memset(Author, 0, sizeof(Author));
memset(Keyword, 0, sizeof(Keyword));
}
Book(const int &tag_, const char (&ISBN_)[21], const char (&BookName_)[61], const char (&Author_)[61],
const char (&Keyword_)[61]) {
tag = tag_;
memset(ISBN, 0, sizeof(ISBN));
memset(BookName, 0, sizeof(BookName));
memset(Author, 0, sizeof(Author));
memset(Keyword, 0, sizeof(Keyword));
strcpy(ISBN, ISBN_);
strcpy(BookName, BookName_);
strcpy(Author, Author_);
strcpy(Keyword, Keyword_);
}
Book(const Book &rhs) {
tag = rhs.tag;
memset(ISBN, 0, sizeof(ISBN));
memset(BookName, 0, sizeof(BookName));
memset(Author, 0, sizeof(Author));
memset(Keyword, 0, sizeof(Keyword));
strcpy(ISBN, rhs.ISBN);
strcpy(BookName, rhs.BookName);
strcpy(Author, rhs.Author);
strcpy(Keyword, rhs.Keyword);
}
Book &operator=(const Book &rhs) {
if (this == &rhs) return *this;
tag = rhs.tag;
memset(ISBN, 0, sizeof(ISBN));
memset(BookName, 0, sizeof(BookName));
memset(Author, 0, sizeof(Author));
memset(Keyword, 0, sizeof(Keyword));
strcpy(ISBN, rhs.ISBN);
strcpy(BookName, rhs.BookName);
strcpy(Author, rhs.Author);
strcpy(Keyword, rhs.Keyword);
return *this;
}
};
class BookSystem {
public:
BlockList<char[21]> ISBN_map;
double_BlockList<char[61], char[21]> BookName_map;
double_BlockList<char[61], char[21]> Author_map;
double_BlockList<char[61], char[21]> Keyword_map;
std::fstream Book_inf;
std::string filename_ = "Book_inf";
int amount = 0;
BookSystem() : ISBN_map("ISBNBody", "ISBNHead"), BookName_map("BookNameBody", "BookNameHead"),
Author_map("AuthorBody", "AuthorHead"), Keyword_map("KeywordBody", "KeywordHead") {
Book_inf.open(filename_);
if (!Book_inf.good()) { // 是否成功打开
Book_inf.open(filename_, std::fstream::out); // 新建
Book_inf.close();
Book_inf.open(filename_); // 变为可读可写
} else {
Book_inf.seekg(0);
Book_inf.read(reinterpret_cast <char *> (&amount), sizeof(amount));
}
}
~BookSystem() {
Book_inf.seekp(0);
Book_inf.write(reinterpret_cast <char *> (&amount), sizeof(amount));
Book_inf.close();
}
void readFile(Book &read_, const int &location) {
Book_inf.seekg(sizeof(Book) * location + sizeof(amount));
Book_inf.read(reinterpret_cast<char *>(&read_), sizeof(Book));
}
void writeFile(Book &write_, const int &location) {
Book_inf.seekp(sizeof(Book) * location + sizeof(amount));
Book_inf.write(reinterpret_cast<char *>(&write_), sizeof(Book));
}
void show(AccountSystem &account) { // 检索所有图书
for (auto &iter: ISBN_map.HeadList) {
BlockList<char[21]>::Block tmp_;
ISBN_map.readFile(tmp_, iter.tag);
for (int i = 0; i < iter.size; ++i) {
Book tmp;
readFile(tmp, tmp_[i].value);
std::cout << tmp.ISBN << '\t' << tmp.BookName << '\t' << tmp.Author << '\t' << tmp.Keyword << '\t';
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(2) << tmp.Price << '\t';
std::cout << tmp.Quantity << '\n';
}
}
}
void ISBN_show(const char (&ISBN_)[21], AccountSystem &account) { // 检索图书
int index_ = ISBN_map.search(ISBN_);
if (index_ == -1) {
std::cout << '\n';
return;
} else {
Book tmp;
readFile(tmp, index_);
std::cout << tmp.ISBN << '\t' << tmp.BookName << '\t' << tmp.Author << '\t' << tmp.Keyword << '\t';
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(2) << tmp.Price << '\t';
std::cout << tmp.Quantity << '\n';
}
}
void BookName_show(const char (&BookName_)[61], AccountSystem &account) {
BookName_map.search(BookName_);
if (BookName_map.indexes.empty()) {
std::cout << '\n';
return;
} else {
for (auto &iter: BookName_map.indexes) {
Book tmp;
readFile(tmp, iter);
std::cout << tmp.ISBN << '\t' << tmp.BookName << '\t' << tmp.Author << '\t' << tmp.Keyword << '\t';
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(2) << tmp.Price << '\t';
std::cout << tmp.Quantity << '\n';
}
}
}
void Author_show(const char (&Author_)[61], AccountSystem &account) {
Author_map.search(Author_);
if (Author_map.indexes.empty()) {
std::cout << '\n';
return;
} else {
for (auto &iter: Author_map.indexes) {
Book tmp;
readFile(tmp, iter);
std::cout << tmp.ISBN << '\t' << tmp.BookName << '\t' << tmp.Author << '\t' << tmp.Keyword << '\t';
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(2) << tmp.Price << '\t';
std::cout << tmp.Quantity << '\n';
}
}
}
void Keyword_show(const char (&Keyword_)[61], AccountSystem &account) {
for (int i = 0; i < 61; ++i) {
if (Keyword_[i] == '|') throw std::string("Invalid\n");
}
Keyword_map.search(Keyword_);
if (Keyword_map.indexes.empty()) {
std::cout << '\n';
return;
} else {
for (auto &iter: Keyword_map.indexes) {
Book tmp;
readFile(tmp, iter);
std::cout << tmp.ISBN << '\t' << tmp.BookName << '\t' << tmp.Author << '\t' << tmp.Keyword << '\t';
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(2) << tmp.Price << '\t';
std::cout << tmp.Quantity << '\n';
}
}
}
void buy(const char (&ISBN_)[21], const int &Quantity_, AccountSystem &account, double &cost) { // 购买图书
if (Quantity_ <= 0) throw std::string("Invalid\n");
int index_ = ISBN_map.search(ISBN_);
if (index_ == -1) throw std::string("Invalid\n");
else {
Book tmp;
readFile(tmp, index_);
if (tmp.Quantity < Quantity_) throw std::string("Invalid\n");
else tmp.Quantity -= Quantity_;
writeFile(tmp, index_);
cost = tmp.Price * double(Quantity_);
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(2) << cost << '\n';
}
}
void select(const char (&ISBN_)[21], AccountSystem &account) { // 选择图书
int index_ = ISBN_map.search(ISBN_);
if (index_ == -1) {
ISBN_map.insert(ISBN_, amount);
Book tmp;
tmp.tag = amount;
strcpy(tmp.ISBN, ISBN_);
writeFile(tmp, amount);
account.User_select.pop();
account.User_select.push(amount);
++amount;
} else {
account.User_select.pop();
account.User_select.push(index_);
}
}
void ISBN_modify(const char (&ISBN_)[21], AccountSystem &account) { // 修改图书信息
int index_ = account.User_select.top();
Book tmp;
readFile(tmp, index_);
ISBN_map.erase(tmp.ISBN, tmp.tag);
strcpy(tmp.ISBN, ISBN_);
writeFile(tmp, index_);
ISBN_map.insert(tmp.ISBN, tmp.tag);
}
void BookName_modify(const char (&BookName_)[61], AccountSystem &account) {
int index_ = account.User_select.top();
Book tmp;
readFile(tmp, index_);
char empty[61];
memset(empty, 0, sizeof(empty));
if (strcmp(empty, tmp.BookName) != 0) BookName_map.erase(tmp.BookName, tmp.ISBN, tmp.tag);
strcpy(tmp.BookName, BookName_);
writeFile(tmp, index_);
BookName_map.insert(tmp.BookName, tmp.ISBN, tmp.tag);
}
void Author_modify(const char (&Author_)[61], AccountSystem &account) {
int index_ = account.User_select.top();
Book tmp;
readFile(tmp, index_);
char empty[61];
memset(empty, 0, sizeof(empty));
if (strcmp(empty, tmp.Author) != 0) Author_map.erase(tmp.Author, tmp.ISBN, tmp.tag);
strcpy(tmp.Author, Author_);
writeFile(tmp, index_);
Author_map.insert(tmp.Author, tmp.ISBN, tmp.tag);
}
void Price_modify(const double &Price_, AccountSystem &account) {
int index_ = account.User_select.top();
Book tmp;
readFile(tmp, index_);
tmp.Price = Price_;
writeFile(tmp, index_);
}
void import(const int &Quantity_, const double &TotalCost_, AccountSystem &account) { // 图书进货
if (Quantity_ <= 0 || TotalCost_ <= 0) throw std::string("Invalid\n");
int index_ = account.User_select.top();
Book tmp;
readFile(tmp, index_);
tmp.Quantity += Quantity_;
tmp.TotalCost += TotalCost_;
writeFile(tmp, index_);
}
};
#endif //INC_1_6BOOKSTORE_BOOKSYSTEM_H