-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin.cpp
More file actions
183 lines (150 loc) · 5.45 KB
/
Coin.cpp
File metadata and controls
183 lines (150 loc) · 5.45 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
// Blake Berry
// 03/08/2022
// Homework 4
// This file is an implimentation for the Coin class. The Coin class inherted
// from the collectible interface. The Coin class represents an item that can
// exist at a collectible store
//-----------------------------------------------------------------------------
#include "Coin.h"
//-------------------------- Constructor -----------------------------------
// Creates a completely empty coin with empty fields
// Postconditions: an empty coin is created
Coin::Coin(std::string type) :
Collectible(type),
year_(0),
grade_(0),
type_("")
{
}
//-------------------------- destructor -----------------------------------
// Frees any dynamic memory associated with the Coin
// Postconditions: The coin is freed of any dynamic memory
Coin::~Coin()
{
}
//-------------------------- Create --------------------------------------
// Parses a given string and created a coin from a given string returning
// itself
// PreConditions : The coin data must be formatted correctly in the string
// Postconditions: returns a constant pointer to the created Coin
// throws an exception if any of the information is invalid
const Coin* Coin::create(std::string toMakeFrom) const
{
// creates a new coin
Coin* dummy = new Coin();
// assumes that year is first
dummy->year_ = std::stoi(Collectible::processConstruction(toMakeFrom));
// validates year
if (dummy->year_ < 1000 || dummy->year_ > 2022) {
throw CollectiblesStoreError("Invalid Coin Year");
}
// assumes grade is second
dummy->grade_ = std::stoi(Collectible::processConstruction(toMakeFrom));
if (dummy->grade_ < 0) {
throw CollectiblesStoreError("Invalid Coin Grade");
}
// assumes is third
dummy->type_ = Collectible::processConstruction(toMakeFrom);
return dummy;
}
//-------------------------- operator== --------------------------------------
// Checks if two Coins are equivilent. Equivilance is defined as each
// Coin having the sametype year and grade
// Postconditions: Returns true if both Coins are equivilent
// Returns false if the Coins are not equivilent.
bool Coin::operator==(const Comparable& right) const
{
const Coin& toCheck = static_cast<const Coin&>(right);
bool equiv = true;
if (year_ != toCheck.year_) {
return false;
}
if (grade_ != toCheck.grade_) {
return false;
}
if(type_ != toCheck.type_){
return false;
}
return equiv;
}
//-------------------------- operator!= --------------------------------------
// Checks if two Coins are not equivilent. Equivilance is defined as each
// Coin having the sametype year and grade
// Postconditions: Returns true if both Coins are not equivilent
// Returns false if the Coins are equivilent.
bool Coin::operator!=(const Comparable& right) const
{
return !((*this) == right);
}
//-------------------------- operator> --------------------------------------
// Checks if two Coins have a greater than relationship. Coins are weighted
// such that we first consider them by type, then by year, then by grade.
// The string type is compared then the following ints
// Postconditions: Returns true if the right hand side coin is smaller
// than the left hand side. Otherwise, false is returned
bool Coin::operator>(const Comparable& right) const
{
const Coin& toCheck = static_cast<const Coin&>(right);
if (type_ > toCheck.type_) {
return true;
}
else if (type_ == toCheck.type_) {
// a smaller year is greater
if (year_ > toCheck.year_) {
return true;
}
else if (grade_ == toCheck.grade_) {
if (grade_ > toCheck.grade_) {
return true;
}
}
// a higher grade is greater
}
return false;
}
//-------------------------- operator< --------------------------------------
// Checks if two Coins have a less than relationship. Coins are weighted
// such that we first consider them by type, then by year, then by grade.
// The string type is compared then the following ints
// Postconditions: Returns false if the right hand side Coin is smaller
// than the left hand side. Otherwise, true is returned
bool Coin::operator<(const Comparable& right) const
{
const Coin& toCheck = static_cast<const Coin&>(right);
if (type_ < toCheck.type_) {
return true;
}
else if (type_ == toCheck.type_) {
// a smaller year is greater
if (year_ < toCheck.year_) {
return true;
}
else if (grade_ == toCheck.grade_) {
if (grade_ < toCheck.grade_) {
return true;
}
}
// a higher grade is greater
}
return false;
}
//-------------------------- print --------------------------------------
// Prints the coins type year and grade on one line
// Postconditions: prints to the console a representation of the Coin
void Coin::print() const
{
std::cout << "Coin : " << year_ << ", " << grade_ << ", " << type_;
}
//-------------------------- copy ------------------------------------------
// creates a deep copy of the current coin 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
// coin
const Coin* Coin::copy() const
{
// default copy as coin contains only primatives
const Coin* toCopy = new Coin(*this);
return toCopy;
}