-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComic.cpp
More file actions
185 lines (155 loc) · 5.71 KB
/
Copy pathComic.cpp
File metadata and controls
185 lines (155 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
// Blake Berry
// 03/08/2022
// Homework 4
// This file is an implimentation for the Comic class. The Comic class inherted
// from the collectible interface. The Comic class represents an item that can
// exist at a collectible store
//-----------------------------------------------------------------------------
#include "Comic.h"
//-------------------------- Constructor -----------------------------------
// Creates a completely empty comic with empty fields
// Postconditions: an empty comic is created
Comic::Comic(std::string id) :
Collectible(id),
publisher_(""),
title_(""),
year_(0),
grade_("")
{
}
//-------------------------- destructor -----------------------------------
// Frees any dynamic memory associated with the Comic
// Postconditions: The comic is freed of any dynamic memory
Comic::~Comic()
{
}
//-------------------------- Create --------------------------------------
// Parses a given string and created a comic from a given string returning
// itself
// PreConditions : The comic data must be formatted correctly in the string
// Postconditions: returns a constant pointer to the created Comic
// throws an exception if any of the information is invalid
const Comic* Comic::create(std::string toMakeFrom) const
{
Comic* dummy = new Comic();
// assumes that year comes first
dummy->year_ = std::stoi(Collectible::processConstruction(toMakeFrom));
if (dummy->year_ < 1000 || dummy->year_ > 2022) {
throw CollectiblesStoreError("Invalid Comic Year");
}
dummy->grade_ = Collectible::processConstruction(toMakeFrom);
dummy->title_ = Collectible::processConstruction(toMakeFrom);
dummy->publisher_ = Collectible::processConstruction(toMakeFrom);
return dummy;
}
//-------------------------- operator== ------------------------------------
// Checks if two Comics are equivilent. Equivilance is defined as each
// Comic having the same publisher, title year and grade.
// Postconditions: Returns true if both Comics are equivilent
// Returns false if the Comics are not equivilent.
bool Comic::operator==(const Comparable& right) const
{
const Comic& toCheck = static_cast<const Comic&>(right);
bool equiv = true;
if (publisher_ != toCheck.publisher_) {
return false;
}
if (title_ != toCheck.title_) {
return false;
}
if (year_ != toCheck.year_) {
return false;
}
if (grade_ != toCheck.grade_) {
return false;
}
return equiv;
}
//-------------------------- operator!= ------------------------------------
// Checks if two Comics are not equivilent. Equivilance is defined as each
// Comic having the same publisher, title year and grade.
// Preconditions : The right hand side comparable must be defined
// Postconditions: Returns true if both Comics are not equivilent
// Returns false if the Comics are equivilent.
bool Comic::operator!=(const Comparable& right) const
{
return !((*this) == right);
}
//-------------------------- operator> -------------------------------------
// Checks if two Comics have a greater than relationship. Comics are
// weighted such that we first consider them by publisher, then by title,
// then by year, then by grade. The fields are compared using defined >
// Postconditions: Returns true if the right hand side comic is smaller
// than the left hand side. Otherwise, false is returned
bool Comic::operator>(const Comparable& right) const
{
const Comic& toCheck = static_cast<const Comic&>(right);
if (publisher_ > toCheck.publisher_) {
return true;
}
else if (publisher_ == toCheck.publisher_) {
if (title_ > toCheck.title_) {
return true;
}
else if (title_ == toCheck.title_) {
if (year_ > toCheck.year_) {
return true;
}
else if (year_ == toCheck.year_) {
if (grade_ > toCheck.grade_) {
return true;
}
}
}
}
return false;
}
//-------------------------- operator< --------------------------------------
// Checks if two Comics have a less than relationship. Comics are
// weighted such that we first consider them by publisher, then by title,
// then by year, then by grade. The fields are compared using defined <
// Postconditions: Returns true if the right hand side comic is larger
// than the left hand side. Otherwise, false is returned
bool Comic::operator<(const Comparable& right) const
{
const Comic& toCheck = static_cast<const Comic&>(right);
if (publisher_ < toCheck.publisher_) {
return true;
}
else if (publisher_ == toCheck.publisher_) {
if (title_ < toCheck.title_) {
return true;
}
else if (title_ == toCheck.title_) {
if (year_ < toCheck.year_) {
return true;
}
else if (year_ == toCheck.year_) {
if (grade_ < toCheck.grade_) {
return true;
}
}
}
}
return false;
}
//-------------------------- print --------------------------------------
// Prints the comics publisher, title, year, and grade on one line
// Postconditions: prints to the console a representation of the Comic
void Comic::print() const
{
std::cout << "Comic : " << year_ << ", " << grade_ << ", " + publisher_
<< ", " + title_;
}
//-------------------------- copy ------------------------------------------
// creates a deep copy of the current comic and returns a non-modifyable
// pointer to it
// preconditions : The caller must means to deallocate the memory
// associated
// Postconditions: returns a constant pointer deep copy of the current
// comic
const Comic* Comic::copy() const
{
const Comic* copied = new Comic(*this);
return copied;
}