-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
276 lines (211 loc) · 5.71 KB
/
book.cpp
File metadata and controls
276 lines (211 loc) · 5.71 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
#include <iostream>
#include <time.h>
#include <fstream>
#include <stdlib.h>
#include <algorithm>
#include <cctype>
#include "book.hpp"
#define OPEN_FILE fstream file("data.txt", ios_base::in);
#define OPEN_TEMP_FILE ofstream temp("new_data.txt");
#define CLOSE_FILE file.close();
#define CLOSE_TEMP_FILE temp.close();
using namespace std;
/*DESCRIPTION: This will add the book to the
file name. During the process,
the genID is called, giving it
it's personal ID.
@param bookName is a string
that the book is being
manipulated.
@param bookPrice is the price
being made by the customer
*/
void addBook(string bookName, int bookPrice){
int lenght;
fstream file("data.txt", ios::out | ios_base::app);
bookName = upperString(bookName);
file.seekg(0, file.end);
lenght = file.tellg();
file.seekg(0, file.beg);
file << genID();
file << ' ';
file << bookName;
file << ' ';
file << "P" << to_string(bookPrice) << endl;
CLOSE_FILE;
}
/*DESCRIPTION: This will return the number
of books stored in the file.
It will be went through
a for loop by the number of
IDs stored in the file.
@return Integer
*/
int bookCount(){
int count = 0;
string tempString;
OPEN_FILE;
while (getline(file, tempString)){count++;}
CLOSE_FILE;
return count;
}
/*DESCRIPTION: This will remove the books. It
will remove the book name, ID,
and price from the file.
@param bookName is a string
that is being referred to.
*/
void removeBook(string bookName){
bookName = upperString(bookName);
string toTempFile;
char confirmation;
size_t found;
bool isAvail;
OPEN_FILE;
OPEN_TEMP_FILE;
isAvail = isBookAvail(bookName);
if (isAvail){
puts("The book is available");
do {
printf("Do you confirm? Y/N ");
scanf(" %c", &confirmation);
confirmation = upperChar(confirmation);
} while (confirmation != 'Y' && confirmation != 'N');
if (confirmation == 'Y'){
while (getline(file, toTempFile)){
found = toTempFile.find(bookName);
if(found == string::npos)
temp << toTempFile << endl;
}
CLOSE_TEMP_FILE;
CLOSE_FILE;
//Remove the files into a new one.
remove("data.txt");
rename("new_data.txt", "data.txt");
cout << "You bought the item! Thank you!" << endl;
}
CLOSE_TEMP_FILE;
CLOSE_FILE;
}
else {
cout << "Please input the correct name. Make sure it has no spaces" << endl;
CLOSE_FILE;
CLOSE_TEMP_FILE;
}
}
/*DESCRIPTION: This will search for the book.
A check must be passed if the
bookName is available
in the file. If it is not
available, it throw off
an error.
@param bookName is the string
that the book is being reffered
to.
*/
void searchBook(string bookName){
bookName = upperString(bookName);
bool isAvail;
isAvail = isBookAvail(bookName);
if (isAvail){
cout << "The book that you are looking for is available" << endl;
}
else{
cout << "Please enter the correct name. Make sure there are no spaces" << endl;
}
}
/*DESCRIPTION: This will check if the ID
is being used
@param ID - is the integer that will
check if it's avaialble.
@return An integer: (1) true,(0) false
*/
static bool isIdentificationUsed(int ID){
OPEN_FILE;
string referencedID;
bool result;
while (getline(file, referencedID, ' ')){
if (referencedID == to_string(ID)){
result = true;
break;
}
else {
result = false;
}
}
CLOSE_FILE;
return result;
}
/*DESCRIPTION: This will make the string into
an uppercase for uniformity.
@param bookName - is a string
that is being referred to.
@return A capatilized version of the
bookName
*/
string upperString(string bookName){
transform(bookName.begin(), bookName.end(),
bookName.begin(), ::toupper);
return bookName;
}
/* DESCRIPTION: This will generate ID's, and must
undergo some kind of check
if it's available; if not,
reloop.
@return A number generated ID
*/
static int genID(){
int ID; bool isUsed;
srand(time(NULL));
do{
ID = rand() % 1000;
isUsed = isIdentificationUsed(ID);
} while(isUsed == true);
return ID;
}
/*
DESCRIPTION: As like the upperString, it makes the
charactes to become uppercase;
this is to make the choices to be
made uniformly.
@param ch is a character.
@return An uppercase version of ch
*/
static char upperChar(char ch){
if (ch >= 'a' && 'z' >= ch){
ch -= 32;
}
return ch;
}
/*
This prints out the list of books available.
*/
void printListBooks(){
OPEN_FILE;
string printOut;
cout << "ID NAME PRICE" << endl;
while (getline(file, printOut)) cout << printOut << endl;
CLOSE_FILE;
}
/*
DESCRIPTION: This checks if the book is available.
@param bookName is the name being referred to
and will be passed through a getline thing.
@return A boolean value.
*/
bool isBookAvail(string bookName){
OPEN_FILE;
bookName = upperString(bookName);
string referredBookName;
size_t found;
bool result;
result = false;
while(getline(file, referredBookName)){
found = referredBookName.find(bookName);
if(found != string::npos){
result = true;
break;
}
}
return result;
}