-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.cpp
More file actions
185 lines (153 loc) · 4.24 KB
/
Product.cpp
File metadata and controls
185 lines (153 loc) · 4.24 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
/*
Product.cpp
Name: Yuansheng Lu
Student ID: 136654167
E-mail: ylu140@myseneca.ca
*/
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include "Product.h"
using namespace std;
namespace sict {
// constructors, destructor and copy operator
//
Product::Product() {
sku_[0] = '\0'; // set safe empty state
name_ = nullptr;
price_ = 0;
taxed_ = false;
quantity_ = 0;
qtyNeeded_ = 0;
}
Product::Product(const char* m_sku, const char* m_name, double m_price, int m_qtyNeeded, bool m_taxed) {
sku(m_sku);
name(m_name);
quantity(0); // set current quantity_ to zero
price(m_price);
qtyNeeded(m_qtyNeeded);
taxed(m_taxed);
}
Product::Product(const Product& src) {
// shallow copy non-resource variables
sku(src.sku_);
price(src.price_);
taxed(src.taxed_);
quantity(src.quantity_);
qtyNeeded(src.qtyNeeded_);
// allocate dynamic memory for name
name(src.name_);
}
Product& Product::operator=(const Product& src) {
// check for self-assignment
if (this != &src) {
// shallow copy non-resource variables
sku(src.sku_);
price(src.price_);
taxed(src.taxed_);
quantity(src.quantity_);
qtyNeeded(src.qtyNeeded_);
// deallocate previously allocated dynamic memory
delete [] name_;
// allocate new dynamic memory
name(src.name_);
}
return *this;
}
Product::~Product() {
delete [] name_;
}
// setters
//
void Product::sku(const char* sku) {
if (sku != nullptr) { // validate the received sku
strncpy(sku_, sku, MAX_SKU_LEN);
sku_[MAX_SKU_LEN] = '\0';
} else {
sku_[0] = '\0';
}
}
void Product::name(const char* name) {
if (name != nullptr) { // validate the received name
name_ = new char[strlen(name) + 1]; // allocate dynamic memory
strncpy(name_, name, strlen(name));
name_[strlen(name)] = '\0';
} else {
name_ = nullptr;
}
}
void Product::price(double price) {
price_ = price;
}
void Product::taxed(bool taxed) {
taxed_ = taxed;
}
void Product::quantity(int quantity) {
quantity_ = quantity;
}
void Product::qtyNeeded(int qtyNeeded) {
qtyNeeded_ = qtyNeeded;
}
// getters
//
const char* Product::sku() const {
return sku_;
}
double Product::price() const {
return price_;
}
const char* Product::name() const {
return name_;
}
bool Product::taxed() const {
return taxed_;
}
int Product::quantity() const {
return quantity_;
}
int Product::qtyNeeded() const {
return qtyNeeded_;
}
double Product::cost() const {
double cost_product;
if (taxed_)
cost_product = price_ * (TAX + 1);
else
cost_product = price_;
return cost_product;
}
bool Product::isEmpty() const {
bool rv;
if (sku_[0] == '\0')
rv = true;
else
rv = false;
return rv;
}
// member operators
//
bool Product::operator==(const char* sku) const {
return strcmp(sku_, sku) == 0;
}
int Product::operator+=(int quantity) {
quantity_ += quantity;
return quantity_;
}
int Product::operator-=(int quantity) {
quantity_ -= quantity;
return quantity_;
}
// helper operators
//
double operator+=(double& left_operand, const Product& product) {
left_operand += product.cost() * product.quantity();
return left_operand;
}
ostream& operator<<(ostream& ostr, const Product& product) {
product.write(ostr, true);
return ostr;
}
istream& operator>>(istream& istr, Product& product) {
product.read(istr);
return istr;
}
}