-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBook.cpp
321 lines (266 loc) · 7.56 KB
/
Book.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Book.cpp
Author: M00734132
Created: 30/03/2021
Updated: 16/04/2021
*/
#include "Book.h"
#include <iostream>
#include <string>
#include <vector>
#include<list>
#include <fstream>
#include <sstream>
#include<iterator>
Book :: Book(){
this->next = nullptr;
}
Book :: Book(std::string title,std::string author, std::string isbn, std::string qty){
this->title = title;
this->author = author;
this->isbn = isbn;
this->qty = qty;
this->next = nullptr;
}
std::string Book :: get_title(){
return this->title;
}
std::string Book :: get_author(){
return this->author;
}
std::string Book :: get_isbn(){
return this->isbn;
}
std::string Book :: get_qty(){
return this->qty;
}
Book* Book :: get_next(){
return this->next;
}
void Book :: set_title(std::string title){
this->title = title;
}
void Book :: set_author(std::string author){
this->author = author;
}
void Book :: set_isbn(std::string isbn){
this->isbn = isbn;
}
void Book :: set_qty(std::string qty){
this->qty = qty;
}
void Book :: set_next(Book * next){
this->next = next;
}
Hash_Table :: Hash_Table(){
}
//Hash table constructor
Hash_Table :: Hash_Table(unsigned int size) {
unsigned int i;
if (!size)
throw "author must be over 0";
this->size = size;
this->internal_array = new Book*[size];
for (i = 0; i < size; i++)
this->internal_array[i] = nullptr;
}
//Hash table destructor
Hash_Table :: ~Hash_Table(){
this->delete_table();
}
//Hash function returns hash code
unsigned long int Hash_Table::hash_Func(std::string str)
{
unsigned long int hash;
int c;
hash = 5381;
for (auto x: str)
{
c = x;
hash = ((hash << 5) + hash) + c;
/* hash * 33 + c */
}
return (hash % this->size);
}
// insert the book into the hash table
bool Hash_Table::addBook(std::string title, std::string author, std::string isbn, std::string qty) {
Book *curr;
unsigned long int idx;
Book *n = new Book(title, author, isbn, qty);
idx = this->hash_Func(title);
curr = this->internal_array[idx];
if (curr == nullptr) {
this->internal_array[idx] = n;
return true;
}
while (true)
{
if (title == curr->get_title())
{
// author found in the chain (so modify)
curr->set_author(author);
curr->set_isbn(isbn);
curr->set_qty(qty);
return true;
}
if (curr->get_next() && curr->get_next()->get_next())
{
curr = curr->get_next();
continue;
}else{
break;
}
}
curr->set_next(n);
return true;
}
// search the book from the hash table
bool Hash_Table :: searchBook(std::string title){
unsigned long int idx;
std::string str;
Book *curr;
idx = this->hash_Func(title);
curr = this->internal_array[idx];
while (curr)
{
if (title == curr->get_title())
{
// author found in the chain (retrieve book details)
curr->get_author();
curr->get_isbn();
curr->get_qty();
str = "\nAuthor : " + curr->get_author() + "\nISBN : " + curr->get_isbn() + "\nQuantity : " + curr->get_qty();
std::cout << str;
return true;
}
curr = curr->get_next();
}
std::cout << "The title: " << title << " is not present in hash table. Please enter a valid title\n" << std::endl;
return false;
}
// remove the book from the hash table
bool Hash_Table :: removeBook(std::string title){
unsigned long int idx;
std::string str;
Book *curr;
Book *prev;
idx = this->hash_Func(title);
for (curr = this->internal_array[idx]; curr != nullptr; prev = curr, curr = curr->get_next())
{
if (title == curr->get_title())
{
// we found the title in our chain
break; // break out so we can delete this later on
}
}
// if curr == nullptr we have gone through the entire chain
// and found nothing (or the initial hash gave us nothing).
if(!curr)
{
str = "The title '" + title + "' was not found in the hash table. Enter valid title\n";
std::cout << str;
return false;
}
// if the node to be deleted has a next
if (curr->get_next() != nullptr)
{
prev->set_next(curr->get_next());
}
delete curr;
str = "The title: '" + title + "' has been removed from the hash table Successfully\n";
std::cout << str;
return true;
}
//Put the string into a vector
std::vector<std::string> Hash_Table:: StringToVector(std::string title, char separator){
std::vector<std::string> vecsWords;
std::stringstream ss (title);
std::string sIndivStr;
while(getline(ss, sIndivStr, separator)){
vecsWords.push_back(sIndivStr);
}
return vecsWords;
}
//Delete Hash table destructor
bool Hash_Table::delete_table()
{
std::string str = " ";
Book *tmp;
Book *curr;
unsigned long int i;
for (i = 0; i < this->size; i++)
{
if ((curr = this->internal_array[i]))
{
while (curr)
{
tmp = curr;
curr = curr->get_next();
delete tmp;
}
}
}
delete[] this->internal_array;
str = "\nThe Hash Table has been deleted Successfully";
std::cout << str << std::endl;
return true;
}
// Read provided file
std::string Hash_Table::ReadFile(std::string file_name){
std::string str = " ";
std::ifstream f;
std::string lineFromFile = " ";
f.open(file_name, std::ios_base::in);
if(f.is_open())
{
while(f.good())
{
getline(f, lineFromFile);
std::vector<std::string> vect = StringToVector(lineFromFile, '\t');
if(f.good())
{
this->addBook(vect[0], vect[1], vect[2], (vect[3]));
}
}
f.close();
str = "\nEntered File : " + file_name + " Read Successfully!";
return str;
}else
{
str = "\nEntered File : " + file_name + " Not read, Enter with proper file name/format";
return str;
}
}
// Insert book into provided file
std::string Hash_Table::insertContentToFile(std::string title,std::string author, std::string isbn, std::string qty, std::string file_name) {
std::string str = "";
std::fstream f;
f.open(file_name,std::ios_base::out|std::ios::app);
f << title << '\t' << author << '\t' << isbn << '\t' << qty << '\n';
f.close();
str = "\nEntered data : Title- '" + title + "' Author- '" + author + "' ISBN- '" + isbn + "' Quantity- '" + qty + "' added to '" + file_name + "' Successfully!\n" ;
return str;
}
// Delete book from provided file
std::string Hash_Table::deleteFileContent (std::string title, std::string file_name){
std::string str = " ";
std::string lineFromFile = " ";
std::ifstream f;
f.open(file_name);
std::ofstream temp;
temp.open("temp.txt");
while (getline(f, lineFromFile))
{
if (lineFromFile.substr(0, title.size()) != title)
{
temp << lineFromFile << std::endl;
}
}
str = "\nEntered data : Title- '" + title + "' deleted from '" + file_name + "' Successfully!";
std::cout << str << std::endl;;
f.close();
temp.close();
std::remove(file_name.c_str());
rename("temp.txt", file_name.c_str());
return str;
}