-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.cpp
294 lines (276 loc) · 11.9 KB
/
user.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "user.h"
#include "library.h"
#include "cctype"
#include "algorithm"
extern Log rec;
extern Library lib;
extern string account;
extern string book_address;
extern string cash_address;
extern int user_index;
void User::PrintUserArray() // Output user information to User.txt
{
ofstream fout;
fout.open("User.csv"); // Open and cover User.txt
if (!fout) // If can't open
{
cerr << "User.csv can't open" << endl;
abort(); // Exit
}
for (size_t i = 0; i < lib.UserArray.size(); i++ ) // Output each user's information
fout << lib.UserArray[i].GetUsername() << "," << lib.UserArray[i].GetPassword() << "," << lib.UserArray[i].GetStatus() << "," << lib.UserArray[i].GetVip() << endl;
fout.close(); // Close User.txt
}
void User::PrintBookArray() // Output book information to Book.txt
{
ofstream fos;
fos.open(book_address); // Open and cover Book.txt
if (!fos) { // If can't open
cerr << book_address << " can't open" << endl;
abort(); // Exit
}
for (size_t i = 0; i < lib.BookArray.size(); i++ ) // Output each book's information
fos << lib.BookArray[i].GetName() << "," << lib.BookArray[i].GetIsbn() << "," << lib.BookArray[i].GetAuthor()
<< "," << lib.BookArray[i].GetNumber() << "," << lib.BookArray[i].GetCategory()
<< "," << lib.BookArray[i].GetPrice() << "," << lib.BookArray[i].GetInformation()
<< "," << lib.BookArray[i].GetNumberOfSale() << endl;
fos.close(); // Close Book.txt
}
void User::PrintCash() // Output cash information to Cash.txt
{
ofstream fout;
fout.open(cash_address); // Open and cover Cash.txt
if (!fout) // If can't open
{
cerr << cash_address << " can't open" << endl;
abort(); // Exit
}
fout << lib.GetIncome() << endl; // Output income
fout << lib.GetOutcome() << endl; // Output outcome
fout.close(); // Close Book.txt
}
int User::IsAllDigit(const string& str) // Check if str is all made of digital number
{
int i ;
for(i = 0; i != str.length(); i++)
if (!isdigit(str[i]) && (str[i]!='.')) return 0; // '.' for double number
return 1;
}
void User::ListBookAll() // List all book
{
system("cls");
int temp;
cout << "This is the list of all the book:" << endl;
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << "| ISBN | Name | Category | Quantity | Unit Price |" << endl;
cout << "-----------------------------------------------------------------------------------------" << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++) // Output each book information
{
cout << "|" << setw(18) << lib.BookArray[i].GetIsbn() << "|" << setw(27) << lib.BookArray[i].GetName() << "|" << setw(14) << lib.BookArray[i].GetCategoryName()<< "|"
<< setw(10) << lib.BookArray[i].GetNumber() << "|"<< setw(14) << setiosflags(ios::fixed) << setprecision(2) <<lib.BookArray[i].GetPrice() << "|" << endl;
}
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << endl;
while(1)
{
cout << "Input 0 to exit, 1 to display the books on web: " << endl;
cin >> temp;
if (temp == 1)
{
ofstream doc("html_file.html");
if (doc.fail())
{
cout << "ERROR: there was an error exporting the file" << endl;
abort(); // Exit
}
else
{
cout << "please wait..." << endl;
doc << "<!DOCTYPE html><html><head><title>Departmental Store Management System</title><style>td{border: solid 2px #ddd;}tr:hover{background:#ddd;}</style></head><body><h2>Books List</h2><table><tr> <td>ISBN</td> <td>Book name</td> <td>Category </td> <td>Quantity </td> <td>Unit price </td> <td>Information</td></tr> " << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++)
doc << "<tr>" << "<td>" << lib.BookArray[i].GetIsbn() << " " << "</td>" << "<td>" << lib.BookArray[i].GetName() << " " << "</td>" << "<td>"<< lib.BookArray[i].GetCategoryName() << " " << "</td>" << "<td>" << lib.BookArray[i].GetNumber() << "</td>" << "<td>" << lib.BookArray[i].GetPrice() << "</td>" << "<td>" << lib.BookArray[i].GetInformation() << " " << "</td>" << "</tr>";
doc << "</table></body></html>";
doc.close();
system("start html_file.html");
}
}
else if (temp == 0)
break;
else
cout << "Please input 0 or 1!" << endl;
}
}
void User::ListBookByC() // List book by category
{
system("cls");
int temp, category;
cout << "Category List:" << endl;
cout << "1. Art" << endl;
cout << "2. Engineering" << endl;
cout << "3. Science" << endl;
cout << "Please input the number of the category of the book:" << endl;
while (1)
{
cin >> category; // Input category number
if (category == 1 || category == 2 || category == 3) break;
cout << "Pleass input a number between 1 and 3!" << endl;
}
system("cls");
string categoryList[3] = {"Art", "Engineering", "Science"};
cout << "This is the list of all the book in category " << categoryList[category-1] << " :" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << "| ISBN | Name | Quantity | Unit Price |" << endl;
cout << "-------------------------------------------------------------------------" << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++) // Output each book information in this category
{ if (lib.BookArray[i].GetCategory() == category)
{
cout << "|" << setw(18) << lib.BookArray[i].GetIsbn() << "|" << setw(27) << lib.BookArray[i].GetName() << "|"
<< setw(10) << lib.BookArray[i].GetNumber() << "|"<< setw(13) << setiosflags(ios::fixed) << setprecision(2) <<lib.BookArray[i].GetPrice() << "|" << endl;
}
}
cout << "-------------------------------------------------------------------------" << endl;
cout << endl;
while(1)
{
cout << "Input 0 to exit, 1 to display the books on web: " << endl;
cin >> temp;
if (temp == 1)
{
ofstream doc("html_file.html");
if (doc.fail())
{
cout << "ERROR: there was an error exporting the file" << endl;
abort(); // Exit
}
else
{
cout << "please wait..." << endl;
doc << "<!DOCTYPE html><html><head><title>Departmental Store Management System</title><style>td{border: solid 2px #ddd;}tr:hover{background:#ddd;}</style></head><body><h2>Books List</h2><table><tr> <td>ISBN</td> <td>Book name</td> <td>Category </td> <td>Quantity </td> <td>Unit price </td> <td>Information</td></tr> " << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++)
if (lib.BookArray[i].GetCategory() == category)
doc << "<tr>" << "<td>" << lib.BookArray[i].GetIsbn() << " " << "</td>" << "<td>" << lib.BookArray[i].GetName() << " " << "</td>" << "<td>"<< lib.BookArray[i].GetCategoryName() << " " << "</td>" << "<td>" << lib.BookArray[i].GetNumber() << "</td>" << "<td>" << lib.BookArray[i].GetPrice() << "</td>" << "<td>" << lib.BookArray[i].GetInformation() << " " << "</td>" << "</tr>";
doc << "</table></body></html>";
doc.close();
system("start html_file.html");
}
}
else if (temp == 0)
break;
else
cout << "Please input 0 or 1!" << endl;
}
}
;
bool findByName(string fullName, string searchName)
{
transform(fullName.begin(), fullName.end(), fullName.begin(), toupper);
transform(searchName.begin(), searchName.end(), searchName.begin(), toupper);
return fullName.find(searchName) != string::npos;
}
void User::ListBookByN() // List book by name
{
system("cls");
int temp;
string name;
cout << "Please input the name that you want to find: " << endl;
cin >> name;
system("cls");
cout << "This is the list of all the book that name contains string \"" << name << "\" :" << endl;
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << "| ISBN | Name | Category | Quantity | Unit Price |" << endl;
cout << "-----------------------------------------------------------------------------------------" << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++) // Output each book information
if (findByName(lib.BookArray[i].GetName(), name))
cout << "|" << setw(18) << lib.BookArray[i].GetIsbn() << "|" << setw(27) << lib.BookArray[i].GetName() << "|" << setw(14) << lib.BookArray[i].GetCategoryName()<< "|"
<< setw(10) << lib.BookArray[i].GetNumber() << "|"<< setw(14) << setiosflags(ios::fixed) << setprecision(2) <<lib.BookArray[i].GetPrice() << "|" << endl;
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << endl;
while(1)
{
cout << "Input 0 to exit, 1 to display the books on web: " << endl;
cin >> temp;
if (temp == 1)
{
ofstream doc("html_file.html");
if (doc.fail())
{
cout << "ERROR: there was an error exporting the file" << endl;
abort(); // Exit
}
else
{
cout << "please wait..." << endl;
doc << "<!DOCTYPE html><html><head><title>Departmental Store Management System</title><style>td{border: solid 2px #ddd;}tr:hover{background:#ddd;}</style></head><body><h2>Books List</h2><table><tr> <td>ISBN</td> <td>Book name</td> <td>Category </td> <td>Quantity </td> <td>Unit price </td> <td>Information</td></tr> " << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++)
if (findByName(lib.BookArray[i].GetName(), name))
doc << "<tr>" << "<td>" << lib.BookArray[i].GetIsbn() << " " << "</td>" << "<td>" << lib.BookArray[i].GetName() << " " << "</td>" << "<td>"<< lib.BookArray[i].GetCategoryName() << " " << "</td>" << "<td>" << lib.BookArray[i].GetNumber() << "</td>" << "<td>" << lib.BookArray[i].GetPrice() << "</td>" << "<td>" << lib.BookArray[i].GetInformation() << " " << "</td>" << "</tr>";
doc << "</table></body></html>";
doc.close();
system("start html_file.html");
}
}
else if (temp == 0)
break;
else
cout << "Please input 0 or 1!" << endl;
}
}
void User::BookInfor() // See more information of one book
{
system("cls");
string temp, isbn;
cout << "Please input the ISBN number of the book that you want to find: " << endl;
cin >> isbn;
for (size_t i = 0; i < lib.BookArray.size(); i++)
if (lib.BookArray[i].GetIsbn() == isbn ) // If exist, list its detail information
{
system("cls");
cout << "Detail information of the book with ISBN number " << isbn << " : " << endl;
cout << "Name : " << lib.BookArray[i].GetName() << endl;
cout << "Author : " << lib.BookArray[i].GetAuthor() << endl;
cout << "Number : " << lib.BookArray[i].GetNumber() << endl;
cout << "Category : " << lib.BookArray[i].GetCategoryName() << endl;
cout << "Price : " << setiosflags(ios::fixed) << setprecision(2) << lib.BookArray[i].GetPrice() << endl;
cout << "Information : " << lib.BookArray[i].GetInformation() << endl;
cout << endl;
cout << "Input 0 to exit" << endl;
cin >> temp;
while (temp != "0")
cin >> temp;
return;
}
cout << "Not found this book! " << endl;
Sleep(1000);
}
void User::ChangePsw() // User choose to change his or her password
{
system("cls");
string temp, password;
cout << "Please input the new password that you want: " << endl;
cin >> password;
cout << "Please repeat it: " << endl;
cin >> temp;
if (temp != password) // If two input are not match then exit
{
cout << "Two password doesn't match!! Change password failed... " << endl;
Sleep(1500);
return;
}
else
{
for (size_t i = 0; i < lib.BookArray.size(); i++)
if (lib.UserArray[i].GetUsername() == account )
lib.UserArray[i].SetPassword(password); // Change password
cout << "Password change successfully !" << endl;
rec.UserLog(account, account, "psw", 0); // Update UserLog.txt with "psw" operation
PrintUserArray(); // Output user information to User.txt
Sleep(2000);
}
}
void User::LoadUser() // Load the user's vip level
{
SetUsername(lib.UserArray[user_index].GetUsername());
SetPassword(lib.UserArray[user_index].GetPassword());
SetStatus(lib.UserArray[user_index].GetStatus());
SetVip(lib.UserArray[user_index].GetVip());
}