Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
289 lines (269 loc) · 7.57 KB
/
Copy pathtest.cpp
File metadata and controls
289 lines (269 loc) · 7.57 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
#include <iostream>
#include <cstring>
using namespace std;
struct book
{
string title;
int price,pages;
string author_name;
long int ISBN;
};
struct book inLibrary[20];
struct book borrowed[20];
int n=5,b=0;
void search_bk()
{
int ch,i;
long int ISBN;
string title;
int pos = -1;
cout << "\nSearch using:\n";
cout << "1. Title\n2. ISBN number\n";
cin >> ch;
do
{
switch (ch)
{
case 1:
cout << "\nEnter the title of the book to be searched:\n";
fflush(stdin);
getline(cin,title);
for(i=0;i<n;i++)
{
if(inLibrary[i].title == title)
pos = i;
}
break;
case 2:
cout << "\nEnter the ISBN number of the book to be searched:\n";
cin >> ISBN;
for(i=0;i<n;i++)
{
if(ISBN == inLibrary[i].ISBN)
pos = i;
}
break;
default:
cout << "\nPlease enter a valid option!";
break;
}
}while (ch !=1 && ch !=2);
if(pos == -1)
cout << "\nBook not found!";
else
{
cout << "\nBook found at position " << pos + 1 << endl;
cout << "\nBook details:\n\n";
cout << "Title: " << inLibrary[pos].title << endl;
cout << "Author: " << inLibrary[pos].author_name << endl;
cout << "Price/week: " << inLibrary[pos].price << endl;
cout << "Pages: " << inLibrary[pos].pages << endl;
cout << "ISBN number: " << inLibrary[pos].ISBN << endl;
}
}
void sell_bk()
{
int pos = -1,i,pages,sell_price,expected_price,ch=2;
string title;
cout << "\nEnter the title of the book: ";
fflush(stdin);
getline(cin,title);
for(i=0;i<n;i++)
{
if(inLibrary[i].title == title)
pos = i;
if(borrowed[i].title == title)
pos = i;
}
if(pos == -1)
{
cout << "Enter the number of pages in the book: ";
cin >> pages;
expected_price = pages + 30;
do
{
cout << "Enter the selling price of the book: ";
cin >> sell_price;
if(sell_price <= expected_price)
{
cout << "\nThis book is eligible for purchase. Thank you.\n";
inLibrary[n].title = title;
cout << "\nEnter the name of Author: ";
fflush(stdin);
getline(cin,inLibrary[n].author_name);
cout << "Enter the ISBN number of the book: ";
cin >> inLibrary[n].ISBN;
inLibrary[n].price = expected_price/10;
inLibrary[n].pages = pages;
ch++;
n++;
}
else
{
cout << "\nThis price exceeds our maximum purchase rates for a for a book of " << pages << " pages." << endl;
cout << "\nPlease consider selling the book under " << expected_price << endl;
cout << "1. Bargain\n2. Cancel\n";
cin >> ch;
}
}while(ch == 1);
}
else
cout << "\nWe already possess this book!";
}
void borrow_bk()
{
int ch,i;
long int ISBN;
string title;
int pos = -1;
do
{
cout << "\nSearch for book using:\n";
cout << "1. Title\n2. ISBN number\n";
cin >> ch;
switch (ch)
{
case 1:
cout << "\nEnter the title of the book to be borrowed:\n";
fflush(stdin);
getline(cin,title);
for(i=0;i<n;i++)
{
if(inLibrary[i].title == title)
pos = i;
}
break;
case 2:
cout << "\nEnter the ISBN number of the book to be borrowed:\n";
cin >> ISBN;
for(i=0;i<n;i++)
{
if(ISBN == inLibrary[i].ISBN)
pos = i;
}
break;
default:
cout << "\nPlease enter a valid option!";
break;
}
}while (ch != 1 && ch !=2);
if(pos == -1)
cout << "\nBook not found!";
else
{
borrowed[b] = inLibrary[pos];
cout << "\n" << inLibrary[pos].title << " has been moved to borrowed books section!";
for(i=pos;i<n-1;i++)
{
inLibrary[i] = inLibrary[i+1];
}
b++;
n--;
}
}
void display_inLibrary()
{
int i;
if(n!=0)
{
for(i=0;i<n;i++)
{
cout << "\nBook " << i+1 << " Details:\n\n";
cout << "Title: " << inLibrary[i].title << endl;
cout << "Author: " << inLibrary[i].author_name << endl;
cout << "Price/week: " << inLibrary[i].price << endl;
cout << "Pages: " << inLibrary[i].pages << endl;
cout << "ISBN number: " << inLibrary[i].ISBN << endl;
}
}
else
cout << "\nNo books in the library!";
}
void display_borrowed()
{
int i;
if(b!=0)
{
for(i=0;i<b;i++)
{
cout << "\nBook " << i+1 << " Details:\n\n";
cout << "Title: " << borrowed[i].title << endl;
cout << "Author: " << borrowed[i].author_name << endl;
cout << "Price/week: " << borrowed[i].price << endl;
cout << "Pages: " << borrowed[i].pages << endl;
cout << "ISBN number: " << borrowed[i].ISBN << endl;
}
}
else
cout << "\nNo books borrowed at the moment!";
}
int main()
{
int ch,format = 0;
inLibrary[0].title = "Think and Grow Rich";
inLibrary[0].author_name = "Napoleon Hill";
inLibrary[0].ISBN = 337609265;
inLibrary[0].pages = 238;
inLibrary[0].price = (inLibrary[0].pages+30)/10;
inLibrary[1].title = "Atomic Habits";
inLibrary[1].author_name = "James Clear";
inLibrary[1].ISBN = 337609266;
inLibrary[1].pages = 320;
inLibrary[1].price = (inLibrary[1].pages+30)/10;
inLibrary[2].title = "Rich Dad Poor Dad";
inLibrary[2].author_name = "Robert Kiyosaki";
inLibrary[2].ISBN = 337609267;
inLibrary[2].pages = 207;
inLibrary[2].price = (inLibrary[2].pages+30)/10;
inLibrary[3].title = "Percy Jackson and the Lightning Thief";
inLibrary[3].author_name = "Rick Riordan";
inLibrary[3].ISBN = 337609268;
inLibrary[3].pages = 377;
inLibrary[3].price = (inLibrary[3].pages+30)/10;
inLibrary[4].title = "Harry Potter and the Deathly Hallows";
inLibrary[4].author_name = "J.K. Rowling";
inLibrary[4].ISBN = 337609269;
inLibrary[4].pages = 607;
inLibrary[4].price = (inLibrary[4].pages+30)/10;
do
{
if(format != 0)
cout << "\n\n";
cout << "***MENU***\n";
cout << "\nWhat action do you wish to perform?\n";
cout << "1. Search a book\n";
cout << "2. Sell us a book\n";
cout << "3. Borrow a book\n";
cout << "4. Display books in the library\n";
cout << "5. Display books borrowed at present\n";
cout << "6. Exit\n";
cout << "\nEnter your choice: ";
cin >> ch;
switch (ch)
{
case 1:
search_bk();
break;
case 2:
sell_bk();
break;
case 3:
borrow_bk();
break;
case 4:
display_inLibrary();
break;
case 5:
display_borrowed();
break;
case 6:
cout << "\nThank You for choosing us!\n";
break;
default:
cout << "\nChoose a valid action!";
break;
}
format++;
}while(ch!=6);
return 0;
}