-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary2.cpp
More file actions
317 lines (285 loc) · 9.88 KB
/
library2.cpp
File metadata and controls
317 lines (285 loc) · 9.88 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include<iostream>
#include<vector>
using namespace std;
//1. static variables are inherited
//and works among the parent class and the children classes too
// some instructions to work on it
// library --> parent--> static variable (count books)--> view library--> info book--> show info book as it is common
//(call the function that shows weather ebooks printed)--> view virtual one as according to which one
// printed books --> child--> view them
// e-books --> child-->view them
// vector<ptrs of library>
//borrow -->erase from the vector
//return --> back again to the vector
// return and adding take care it is not here so overloading
// for operator of ==
// borrow take care from borrowing something it not valid by map
//view book not virtual function
// the code
// functions of repeated validations
bool numeric(string s){
for(auto ch: s){
if(!isdigit(ch)){ return false; }
}
return true;
}
bool person(string s){
for(auto ch :s){
if(isdigit(ch))
return false;
}
return true;
}
//classes
class library {
protected:
string name, description,editor,writer;
bool type;//ebook -->1 printed -->0
int price;
static int counter;
public:
virtual void book_info()=0;
friend istream& operator>>(istream& in, library& e);
bool operator== ( library & l );
~library(){
counter--;
}
static void view_lib(const vector<library*> & v,int target) ;
string name_book(){
return name;
}
};
int library::counter=0;
class EBook:public library{
public:
void book_info() override{
cout<<"Name : "<<name<<'\n'
<<"Type : E-book"<<'\n'
<<"Editor : "<<editor<<'\n'
<<"Writer : "<<writer<<'\n'
<<"Price : "<<price<<'\n'<<"Description : "<<description<<"\n\n";
}
EBook() {
type=true;
counter++;
}
};
class PrintedBook:public library{
public:
void book_info() override{
cout<<"Name : "<<name<<'\n'
<<"Type : Printed-book"<<'\n'
<<"Editor : "<<editor<<'\n'
<<"Writer : "<<writer<<'\n'
<<"Price : "<<price<<'\n'<<"Description : "<<description<<"\n\n";
}
PrintedBook(){
type=false;
counter++;
}
};
//functions of some classes
istream& operator>>(istream& in, library& e){
in.ignore();
cout<<"Enter the name : ";getline(in,e.name) ;//in.ignore();
cout<<"Enter the description : "; getline(in,e.description);
cout<<"Enter the editor name : ";getline(in,e.editor);
while (!person(e.editor)){
cout<<"Enter a valid name : ";
getline(in,e.editor);
}
cout<<"Enter the writer name : ";getline(in,e.writer);
while (!person(e.writer)){
cout<<"Enter a valid name : ";
getline(in,e.writer) ;
}
string price;
cout<<"Enter the price : ";getline(in,price);
while (!numeric(price)){
cout<<"Enter a valid price : ";getline(in,price);
}
e.price=stoi(price);
return in;
}
bool library:: operator== ( library& l){
return (l.type==type&&l.name==name&&l.price==price&&l.writer==writer&&l.editor==editor&&
l.description==description);
}
void library::view_lib(const vector<library*> & v,int target) {
if(target!=2){
int cntr = 1;
cout << "\n\n==============================\n\n";
for (int i = 0; i < v.size(); ++i) {
if (v[i]->type == target) {
cout << cntr << "." << v[i]->name << '\n';
cntr++;
}
}
if (cntr == 1)
cout << "The library now is empty of that type :(\n";
cout << "\n\n==============================\n\n";
}else{
int cntr=1;
for (int i = 0; i < v.size(); ++i) {
cout << cntr << "." << v[i]->name << '\n';
cntr++;
}
}
return ;
}
int main()
{
cout<<"\n\n\n ############ Welcome to The library system ############ \n\n\n";
vector<library *>books;// carry the info of the library
vector<library*>borrow;
while (true){
cout<<"##### The Menu #####\n\n";
cout<<"1.Add a book\n";
if(!borrow.empty()){
cout<<"2.Return a book\n"; }
if(!books.empty()){
cout<<"3.Borrow a book\n4.View a book's info\n5.View the library's info\n";
}
cout<<"6.Exit\n";
cout<<"Please choose from the menu : ";
string choice;cin>>choice;
//validation part
if(!books.empty()){
if(!borrow.empty()){
while (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "5" &&
choice != "6") {
cout << "Please Enter a valid choice : ";
cin >> choice;
}
}else{
while (choice != "1" && choice != "3" && choice != "4" && choice != "5" &&
choice != "6") {
cout << "Please Enter a valid choice : ";
cin >> choice;
}
}
}else{
if(borrow.empty()){
while (choice != "1" && choice != "6") {
cout << "Please Enter a valid choice : ";
cin >> choice;
}
}else{
while (choice != "1"&&choice!="2" && choice != "6") {
cout << "Please Enter a valid choice : ";
cin >> choice;
}
}
}
if(choice=="1"){//add a book
repeat:
string type;
cout<<"Choose from the following the type of the book :\n1.E-Book\n2.Printed-Book\n\n";
cout<<"Enter the choice : ";cin>>type;
while (type!="1"&&type!="2"){
cout<<"Enter a valid choice : ";
cin>>type;
}
if(type=="1")
{
library * e= new EBook() ; cin>> *e;
for (int i = 0; i < books.size(); ++i) {
if(*(books[i])==*e){
cout<<"This book is already in the library!\n "
"Please Enter a new book\n\n";
delete e;
goto repeat;
}
}
books.push_back(e);
}
else {
library * p=new PrintedBook();cin>>*p;
for (int i = 0; i < books.size(); ++i) {
if(*(books[i])==*p){
cout<<"This book is already in the library!\n "
"Please Enter a new book\n\n";
delete p;
goto repeat;
}
}
books.push_back(p);
}
}
else if(choice=="2"){// return a book
cout<<"Which Book you want to return ?\n";
library::view_lib(borrow,2);
cout<<"Enter your choice : ";string choose;
again3:
cin>>choose;
if(!numeric(choose)){
cout<<"Please Enter a valid choice : ";
goto again3;
}
if(stoi(choose)<1||stoi(choose)>borrow.size()){
cout<<"Please Enter a valid choice : \n";
goto again3;
}
books.push_back(borrow[stoi(choose)-1]);
borrow.erase(stoi(choose)-1+borrow.begin());
cout<<"Return operation is done successfully :) \n";
}
else if(choice=="3"){// borrow
cout<<"Which Book you want to borrow ?\n";
library::view_lib(books,2);
cout<<"Enter your choice : ";
string choose;
//validation part
again2:
cin>>choose;
if(!numeric(choose)) {
cout<<"Please Enter a valid choice : ";
goto again2;
}
if (stoi(choose)<1||stoi(choose)>books.size()){
cout<<"Please Enter a valid choice : ";
goto again2;
}
//rest of the code
borrow.push_back(books[stoi(choose)-1]);
books.erase(stoi(choose)-1+books.begin());
cout<<"Borrow operation is done successfully :) \n";
}
else if(choice=="4"){// view book info
cout<<"Which Book do you Want ?\n\n";
library::view_lib(books,2);
cout<<"Enter your choice : ";
//validation part
again:
string choose;cin>>choose;
if(!numeric(choose)) {
cout<<"Please Enter a valid choice : ";
goto again;
}
if (stoi(choose)<1||stoi(choose)>books.size()){
cout<<"Please Enter a valid choice : ";
goto again;
}
//rest of the code
books[stoi(choose)-1]->book_info();
}
else if(choice=="5"){//view the library info
cout<<"Which section you want to see ?\n1.E-books\n2.Printed-books\nEnter your choice : ";
string choose;cin>>choose;
//validation part
while (choose!="1"&& choose!="2"){
cout<<"Please Enter a valid choice : ";cin>>choose;
}
//implementation part
if(choose=="1"){
library::view_lib(books,true);
}else{
library::view_lib(books,false);
}
}
else{// close
cout<<"\n\n ######## Thanks for using that system :) ######## \n\n";
break;
}
}
}