-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite.cpp
352 lines (321 loc) · 8.61 KB
/
website.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/******************************************************************************
# Program Desc.: This program is a website bookmarking program. The program
# implements a hash table using chaining (pointer array of
# linked lists) to store website information.
# File: website.cpp
# File Description: Implementation file for the Website ADT class. Contains
# the website data and functions to manipulate the data.
# Input: None
# Output: None
#******************************************************************************/
#include "website.h"
using namespace std;
// Default constructor
Website::Website()
{
topic = nullptr;
url = nullptr;
summary = nullptr;
review = nullptr;
rating = -1;
}
// Copy constructor
// Description: Copies the website data from the website passed in
// into the new website. Uses overloaded assignment
// operator.
Website::Website(const Website & website)
{
*this = website;
}
// Destructor
Website::~Website()
{
destroy();
}
// Destroy
// Description: Deallocates all memory associated with the website
// then sets all pointers to nullptr. First checks if
// the website has been initialized.
// Input: None
// Output: None
void Website::destroy()
{
if (topic)
{
delete [] topic;
topic = nullptr;
}
if (url)
{
delete [] url;
url = nullptr;
}
if (summary)
{
delete [] summary;
summary = nullptr;
}
if (review)
{
delete [] review;
review = nullptr;
}
rating = -1;
}
// Display
// Description: Displays all website data. Uses overloaded ostream
// operator to display the website data.
// Input: None
// Output: None
void Website::display()
{
cout << *this;
}
// ACCESSORS
// Set Topic
// Description: Sets the topic of the website. First checks if the
// topic has been initialized. If so, it deletes the
// topic and sets the pointer to nullptr. Then it
// allocates memory for the topic and copies the
// topic into the website.
// Input: char * topic
// Output: None
void Website::setTopic(char * topic)
{
if (this->topic) // if topic is not null
{
delete [] this->topic;
this->topic = nullptr;
}
this->topic = new char[strlen(topic) + 1];
strcpy(this->topic, topic);
}
// Set URL
// Description: Sets the URL of the website. First checks if the
// URL has been initialized. If so, it deletes the
// URL and sets the pointer to nullptr. Then it
// allocates memory for the URL and copies the
// URL into the website.
// Input: char * url
// Output: None
void Website::setURL(char * url)
{
if (this->url) // if url is not null
{
delete [] this->url;
this->url = nullptr;
}
this->url = new char[strlen(url) + 1];
strcpy(this->url, url);
}
// Set Summary
// Description: Sets the summary of the website. First checks if the
// summary has been initialized. If so, it deletes the
// summary and sets the pointer to nullptr. Then it
// allocates memory for the summary and copies the
// summary into the website.
// Input: char * summary
// Output: None
void Website::setSummary(char * summary)
{
if (this->summary) // if summary is not null
{
delete [] this->summary;
this->summary = nullptr;
}
this->summary = new char[strlen(summary) + 1];
strcpy(this->summary, summary);
}
// Set Review
// Description: Sets the review of the website. First checks if the
// review has been initialized. If so, it deletes the
// review and sets the pointer to nullptr. Then it
// allocates memory for the review and copies the
// review into the website.
// Input: char * review
// Output: None
void Website::setReview(char * review)
{
if (this->review) // if review is not null
{
delete [] this->review;
this->review = nullptr;
}
this->review = new char[strlen(review) + 1];
strcpy(this->review, review);
}
// Set Rating
// Description: Sets the rating of the website (1-5)
// Input: int rating
// Output: None
void Website::setRating(int rating)
{
this->rating = rating;
}
// ACCESSORS
// Get Topic
// Description: Returns the topic of the website. Const so
// it does not modify the website data.
// Input: None
// Output: char * topic
const char * Website::getTopic() const
{
return topic;
}
// Get URL
// Description: Returns the URL of the website. Const so
// it does not modify the website data.
// Input: None
// Output: char * url
const char * Website::getURL() const
{
return url;
}
// Get Summary
// Description: Returns the summary of the website. Const so
// it does not modify the website data.
// Input: None
// Output: char * summary
const char * Website::getSummary() const
{
return summary;
}
// Get Review
// Description: Returns the review of the website. Const so
// it does not modify the website data.
// Input: None
// Output: char * review
const char * Website::getReview() const
{
return review;
}
// Get Rating
// Description: Returns the rating of the website. Const so
// it does not modify the website data.
// Input: None
// Output: int rating
const int Website::getRating() const
{
return rating;
}
// assignment operator overload
// Description: Overloads the assignment operator to copy
// the data from one website to another.
// Input: const Website & website
// Output: Website & website
const Website & Website::operator=(const Website & website)
{
if (this != &website)
{
destroy();
setTopic(website.topic);
setURL(website.url);
setSummary(website.summary);
setReview(website.review);
setRating(website.rating);
}
return *this;
}
// ostream operator overload (NO OUTPUT FORMATTING)
// Description: Overloads the ostream operator to display
// the website data. Only displays cstring data
// if it is not nullptr. Else displays "N/A".
// Input: ostream & out, const Website & website
// Output: ostream & out
ostream & operator<<(ostream & out, const Website & website)
{
out << "Topic: ";
if (website.topic)
out << website.topic << endl;
else
out << "N/A" << endl;
out << "URL: ";
if (website.url)
out << website.url << endl;
else
out << "N/A" << endl;
out << "Summary: ";
if (website.summary)
out << website.summary << endl;
else
out << "N/A" << endl;
out << "Review: ";
if (website.review)
out << website.review << endl;
else
out << "N/A" << endl;
out << "Rating: ";
if (website.rating != -1)
out << website.rating << endl;
else
out << "N/A" << endl;
return out;
}
// ostream operator overload (WITH OUTPUT FORMATTING - FIX IF TIME)
// Description: Overloads the ostream operator to display
// the website data. Only displays cstring data
// if it is not nullptr. Else displays "N/A".
// Input: ostream & out, const Website & website
// Output: ostream & out
/*
ostream & operator<<(ostream & out, const Website & website)
{
int width = 30; // width of the output
out << "Topic: ";
if (website.topic)
{
out << setw(width) << left << website.topic << endl;
}
else
{
out << "N/A" << endl;
}
out << "URL: ";
if (website.url)
{
out << setw(width) << left << website.url << endl;
}
else
{
out << "N/A" << endl;
}
out << "Summary: ";
if (website.summary)
{
out << setw(width) << left << website.summary << endl;
}
else
{
out << "N/A" << endl;
}
out << "Review: ";
if (website.review)
{
out << setw(width) << left << website.review << endl;
}
else
{
out << "N/A" << endl;
}
out << "Rating: ";
if (website.rating != -1)
out << website.rating << endl;
else
out << "N/A" << endl;
return out;
}
*/
// equals operator overload
// Description: Overloads the equals operator to compare
// the data of two websites. Returns true if
// URL is equal. Returns false if not equal.
// Guards against nullptr.
// Input: const Website & lhs rhs
// Output: bool true or false if equal or not equal respectively
bool operator==(const Website & lhs, const Website & rhs)
{
if (lhs.url && rhs.url) // if both are not null
return (strcmp(lhs.url, rhs.url) == 0); // if equal return true
else
return false; // if not equal return false
}