-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.cpp
290 lines (263 loc) · 9.04 KB
/
p.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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
bool search(string s) {
string x;
fstream user_file("user.txt");
while (getline(user_file, x)) {
if (x.find(s, 0) != string::npos)
return true;
}
return false;
}
class User {
private:
string password;
string first_name;
string last_name;
string birth_date;
public:
string user_name;
void setFirstName(string f) {
first_name = f;
}
void setLastName(string l) {
last_name = l;
}
void setBithDate(string b) {
birth_date = b;
}
void setPassword(string p) {
password = p;
}
string getPassword() {
return password;
}
bool sign_in() {
string d = user_name + " " + password;
if (search(d))
return true;
cout << "ther is no user with this charactristics" << "\n";
return false;
}
bool sign_up() {
if (search(user_name)) {
cout << "this username already exist" << "\n";
return false;
}
else {
ofstream file("user.txt", ios_base::app);
file << first_name << " " << last_name << " " << birth_date << " " << user_name << " " << password << "\n";
file.close();
return true;
}
}
};
class Books {
public:
string title;
string shelf_number;
string author;
string publlsher;
string subjects;
string edithion;
string publlshed_year;
string isbn;
string length;
void borrow(string username) {
string book;
User user;
bool existence = false;
ofstream newFile("new.txt", ios_base::app);
ifstream booksfile1("book.txt");
string myline = title + " " + shelf_number + " " + author + " " + edithion + " " +
publlsher + " " + publlshed_year + " " + isbn + " " + length + " " + subjects;
int lineNumber = 1;
int line = 1;
while (getline(booksfile1, book)) {
if (book.find(myline, 0) != string::npos && book.find("not_free", 0) == string::npos) {
existence = true;
break;
}
lineNumber++;
}
if (existence) {
string b;
booksfile1.close();
ifstream booksfile2("book.txt");
while (getline(booksfile2, b)) {
if (line != lineNumber)
newFile << b << "\n";
else if (line == lineNumber)
newFile << myline << " " << "not_free " + username << "\n";
line++;
}
remove("book.txt");
rename("new.txt", "book.txt");
cout << "enjoy your reading!" << "\n";
}
else
cout << "the book that you looking for does not exsit. please search again and make sure that you enter the information correctly and the book is not taken by someone else" << "\n";
}
void give_back(string username) {
string book;
bool existence = false;
ofstream newFile("new.txt", ios_base::app);
ifstream booksfile1("book.txt");
string myline = title + " " + shelf_number + " " + author + " " + edithion + " " +
publlsher + " " + publlshed_year + " " + isbn + " " + length + " " + subjects;
int lineNumber = 1;
int line = 1;
while (getline(booksfile1, book)) {
if (book.find(myline, 0) != string::npos && book.find(username, 0) != string::npos) {
existence = true;
break;
}
lineNumber++;
}
if (existence) {
string b;
booksfile1.close();
ifstream booksfile2("book.txt");
while (getline(booksfile2, b)) {
if (line != lineNumber)
newFile << b << "\n";
else if (line == lineNumber)
newFile << myline << " " << "free" << " NULL" << "\n";
line++;
}
remove("book.txt");
rename("new.txt", "book.txt");
cout << "thank you!!" << "\n";
}
else
cout << "the book that you are looking for does not exsit. please search again and make sure that you've entered the information correctly and it's not borrowed by someone else" << "\n";
}
};
bool N(string username) { //check the number of books that user borrowed before
ifstream booksFile("book.txt");
string book;
int n = 0;
while (getline(booksFile, book)) {
if (book.find(username, 0) != string::npos)
n++;
}
if (n < 2) {
return true;
}
return false;
}
void Mission(string username) {
string m;
Books books;
cout << "please choose your mission( search , borrow , give_back):" << "\n";
cin >> m;
if (m == "borrow") {
if (N(username)) {
cout << "please enter precise characteristic of the book that you want:" << "\n";
cin >> books.title >> books.shelf_number >> books.author >> books.edithion >>
books.publlsher >> books.publlshed_year >> books.isbn >> books.length >> books.subjects;
books.borrow(username);
}
else
cout << "you have already borrowd 2 books. give back at least one book to get another one" << "\n";
}
if (m == "give_back") {
cout << "please enter precise characteristic of the book that you have:" << "\n";
cin >> books.title >> books.shelf_number >> books.author >> books.edithion >>
books.publlsher >> books.publlshed_year >> books.isbn >> books.length >> books.subjects;
books.give_back(username);
}
if (m == "search") {
string information[9];
int data = 0;
cout << "please enter any of the following information that you have.(enter '-' if you didnt know!)" << "\n";
cout << "title " << "shelfnubmer " << "auther " << "edithion " << "publisher " << "publishedyear " << "isbn " << "length " << "subject " << "\n";
cin >> information[0] >> information[1] >> information[2] >> information[3] >>
information[4] >> information[5] >> information[6] >> information[7] >> information[8];
for (int i = 0; i < 9; i++) {
if (information[i] != "-")
data++;
}
string updat_info[data];
int d = 0;
for (int i = 0; i < 9; i++) {
if (information[i] != "-") {
updat_info[d] = information[i];
d++;
}
}
ifstream booksFile("book.txt");
string info;
bool b;
cout << "result" << "\n";
while (getline(booksFile, info)) {
if ((information[0] == "-" || info.find(information[0]) < 100)
&& (information[1] == "-" || info.find(information[1]) < 100)
&& (information[2] == "-" || info.find(information[2]) < 0)
&& (information[3] == "-" || info.find(information[3]) < 0)
&& (information[4] == "-" || info.find(information[4]) < 0)
&& (information[5] == "-" || info.find(information[5]) < 0)
&& (information[6] == "-" || info.find(information[6]) < 0)
&& (information[7] == "-" || info.find(information[7]) < 0)
&& (information[8] == "-" || info.find(information[8]) < 0))
cout << info << "\n";
}
}
int f = 0;
cout << "if you stil want to do mission enter 1 and if you want to exit enter 0" << "\n";
cin >> f;
if (f == 1) {
Mission(username);
}
}
int main() {
string enter;
string Password;
string firstName;
string lastName;
string birthDate;
User user;
string mytext;
bool c = true;
int f = 0;
cout << "enter sign_in or sign_up for start!:" << "\n";
cin >> enter;
while (f == 0) {
if (enter == "sign_in") {
cout << "please enter the following information:" << "\n";
cout << "username password" << "\n";
cin >> user.user_name >> Password;
user.setPassword(Password);
Password = user.getPassword();
c = user.sign_in();
if (!c) {
cout << "pleas sign_in again" << "\n";
f = 0;
}
else {
f = 1;
Mission(user.user_name);
}
}
if (enter == "sign_up") {
cout << "pleas inter the following information:" << "\n";
cout << "firstname lastname birthdate username password" << "\n";
cin >> firstName >> lastName >> birthDate >> user.user_name >> Password;
user.setFirstName(firstName);
user.setLastName(lastName);
user.setBithDate(birthDate);
user.setPassword(Password);
c = user.sign_up();
if (!c) {
cout << "please sign up again" << "\n";
f = 0;
}
else {
f = 1;
Mission(user.user_name);
}
}
}
}