-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.cpp
440 lines (332 loc) · 10.6 KB
/
Product.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
***********************************
Student Name:<Badal Sarkar>
Student#: <137226189>
Student e-mail: [email protected]
Subject: OOP244
Section: <SAA>
Topic: Assignment MS 5
***********************************
*/
#include<iostream>
#include <cstring>
#include<string>
#include <iomanip>
using namespace std;
#include"Product.h"
#include "ErrorState.h"
#define _CRT_SECURE_NO_WARNINGS
namespace ama {
//default constructor
Product::Product(char prodType):type(prodType),productName(nullptr){
//empty
}
//custom constructor with 7 argument
Product::Product(const char * prodSKU, const char * prodName, const char * prodUnit,
double price, int qtNeeded, int qtOnHand, bool taxStatus):type('N'), ProductError(){
//check if the data is valid
//call init function to store data
if (dataIsValid(prodSKU, prodName, prodUnit, price, qtNeeded, qtOnHand)) {
init(prodSKU, prodName, prodUnit, price, qtNeeded, qtOnHand, taxStatus);
}
else {
//if data is not valid, store safe empty
(*this) = Product();
}
}
//copy constructor
Product::Product(const Product& src):type(src.type) {
productName = nullptr;
init(src.sku, src.productName, src.unit, src.unitPrice, src.quantityNeeded, src.quantityOnHand, src.taxStatus);
}
//destructor
Product::~Product() {
delete[]productName;
productName = nullptr;
}
//copy assignment
Product& Product::operator=(const Product& rhs) {
//check for self assignment
if (this != &rhs) {
delete[]productName;
init(rhs.sku, rhs.productName, rhs.unit, rhs.unitPrice, rhs.quantityNeeded, rhs.quantityOnHand, rhs.taxStatus);
}
return *this;
}
//Overloaded operator +=
//this function adds cnt to the qty on hand
int Product::operator+=(int cnt) {
if (cnt > 0) {
quantityOnHand += cnt;
}
return quantityOnHand;
}
//overloaded operator ==
//returns true if sku matches
bool Product::operator==(const char* sku)const {
return (strcmp(this->sku, sku) == 0 ? true : false);
}
//overloaded operator >
//returns true if current sku is greater than sku
bool Product::operator>(const char* sku)const {
return(strcmp(this->sku, sku) > 0 ? true : false);
}
//overloaded operator<
//returns true if current name is greater that rhs name
bool Product::operator>(const iProduct& rhs)const {
return(strcmp(productName, rhs.name()) > 0 ? true : false);
}
//this function query the available quantity
int Product::qtyAvailable()const {
return quantityOnHand;
}
//this function query the quantity needed
int Product::qtyNeeded()const {
return quantityNeeded;
}
//this function returns total after tax price of
//all available unit
double Product::total_cost()const {
double totalCost;
if (taxStatus) {
totalCost=(quantityOnHand*unitPrice)*(1 + tax_rate);
}
else {
totalCost = (quantityOnHand*unitPrice);
}
return totalCost;
}
//function to check if object is empty
bool Product::isEmpty()const {
return(productName == nullptr ? true : false);
}
const char * Product::name() const{
return productName;
}
//function init
//This function copies the parameter into the data members
void Product::init(const char * prodSKU, const char * prodName, const char * prodUnit,
double price, int qtNeeded, int qtHand, bool tax){
//copy static members
strncpy(sku, prodSKU, max_length_sku);
sku[max_length_sku] = '\0';
strncpy(unit, prodUnit, max_length_unit);
unit[max_length_unit] = '0';
unitPrice = price;
quantityOnHand = qtHand;
quantityNeeded = qtNeeded;
taxStatus = tax;
//resource allocation
int totalElement = strlen(prodName);
productName = new(nothrow) char[totalElement+1];
//memory allocation successful
if (productName != nullptr) {
strncpy(productName, prodName, totalElement);
productName[totalElement] = '\0';
}
else {
//if memory allocation fails,
//safe empty state
productName =nullptr;
}
}
//function to validate data
bool Product::dataIsValid(const char* prodSKU, const char* prodName,const char* prodUnit, double priceBeforeTax,
int quantityNeeded, int quantOnHand){
bool result;
if (prodName == nullptr || prodName[0] == '\0') {
result = false;
}
else {
result = (prodSKU == nullptr || prodUnit == nullptr || priceBeforeTax < 0 || quantityNeeded < 0 || quantOnHand < 0) ? false : true;
}
return result;
}
//this function stores the error status
//into the errorstate object of product object
void Product::message(const char* pText) {
ProductError.message(pText);
}
//this function returns true if the object has no error message
//false otherwise
bool Product::isClear()const {
return((ProductError) ? false : true);
}
//read function
//this function reads from stream
std::istream& Product::read(std::istream& in, bool interractive) {
//declare temporary variable
char tempSku[max_length_sku + 1]{ '\0' },
tempUnit[max_length_unit + 1]{ '\0' },
tempTax,*tempName = nullptr;
string name;
double tempPrice=-1;
int tempQtyOnHand=-1, tempQtyNeed=-1, tax;
bool taxStatus=true;
//if it is interactive
if (interractive == true) {
//get SKU from stream
cout << setw(max_length_label) <<right<< "Sku: ";
in.get(tempSku, max_length_sku + 1);
//read name and store dynamically
if (!in.fail()) {
in.ignore(2000, '\n');
//get name from stream
cout << setw(max_length_label) <<right<< "Name (no spaces): ";
in >> name;
if (!in.fail()) {
in.ignore(2000, '\n');
//get unit from stream
cout << setw(max_length_label) <<right<< "Unit: ";
in.get(tempUnit, max_length_sku + 1);
if (!in.fail()) {
in.ignore(2000, '\n');
//get tax from stream
cout << setw(max_length_label) <<right<< "Taxed? (y/n): ";
in.get(tempTax);
//validate input
if (!in.fail() && (tempTax == 'y' || tempTax == 'Y' || tempTax == 'n' || tempTax == 'N')) {
in.ignore(2000, '\n');
//get price
cout << setw(max_length_label) <<right<< "Price: ";
in >> tempPrice;
if (!in.fail() && tempPrice >= 0) {
in.ignore(2000, '\n');
//get qt on hand
cout << setw(max_length_label) <<right<< "Quantity on hand: ";
in >> tempQtyOnHand;
if (!in.fail() && tempQtyOnHand >= 0) {
in.ignore(2000, '\n');
//get qt needed
cout << setw(max_length_label) <<right<< "Quantity needed: ";
in>>tempQtyNeed;
if (!in.fail() && tempQtyNeed >= 0) {
//store name into a cstyle string
tempName = new(nothrow) char[name.length() + 1];
if (tempName != nullptr) {
strcpy(tempName, name.c_str());
}
//determine tax status
taxStatus = (tempTax == 'y' || tempTax == 'Y') ? true : false;
//copying to the object
delete[] productName;
init(tempSku, tempName, tempUnit, tempPrice, tempQtyNeed, tempQtyOnHand, taxStatus);
}
else {
//error state for qty needed
in.setstate(ios::failbit);
message("Invalid Quantity Needed Entry!");
}
}
else {
//error state for qty on hand
in.setstate(ios::failbit);
message("Invalid Quantity Available Entry!");
}
}
else {
//error state for price
in.setstate(ios::failbit);
message("Invalid Price Entry!");
}
}
else {
//error state for tax
in.setstate(ios::failbit);
message("Only (Y)es or (N)o are acceptable!");
}
}
}
}
}
else {
//if not interractive
in.getline(tempSku, max_length_sku + 1, ',');
//read the name from the stream
//dynamically allocate memory for c style string
getline(in, name, ',');
//delete::://readingStat = in.fail();
tempName = new char[name.length() + 1];
if (tempName != nullptr) {
strcpy(tempName, name.c_str());
}
//read unit from stream
in.getline(tempUnit, max_length_unit + 1, ',');
//read price from unit
in >> tempPrice;
in.ignore();
//read tax from stream
in >> tax;
taxStatus = (tax > 0) ? true : false;
in.ignore();
in >> tempQtyOnHand;
in.ignore();
in >> tempQtyNeed;
//store into the object
if (!in.fail()) {
if (dataIsValid(tempSku, tempName, tempUnit, tempPrice, tempQtyNeed, tempQtyOnHand)) {
//deallocate existing resource
delete[] productName;
init(tempSku, tempName, tempUnit, tempPrice, tempQtyNeed, tempQtyOnHand, taxStatus);
}
}
}
//deallocate temporary resource
delete[] tempName;
tempName = nullptr;
return in;
}
//function write
//this function write to stream
std::ostream& Product::write(std::ostream& out, int writeMode)const {
//check error message
if (isClear()){
//if empty don't do anything
if(!isEmpty()){
switch (writeMode) {
case write_condensed:
out << type << "," << sku << "," << productName << ","
<< unit << "," << fixed<<setprecision(2) << unitPrice << ","
<< taxStatus << "," << quantityOnHand << ","
<< quantityNeeded;
break;
case write_table:
if (strlen(productName) > 16) {
out << " " << setw(max_length_sku) << right << sku << " | ";
for (int i = 0; i < 13; i++) {
out << left << productName[i];
}
out << "... | ";
out<<setw(10)<<left<<unit<<" | "
<<setw(7)<<fixed<<setprecision(2)<<right<<unitPrice<<" | "
<<setw(3)<<right<<(taxStatus==0?"no":"yes")<<" | "
<<setw(6)<<right<<quantityOnHand<<" | "
<<setw(6)<<right<<quantityNeeded<<" |";
}
else {
out << " " << setw(max_length_sku) << right << sku << " | ";
out <<setw(16)<< left << productName<<" | "
<< setw(10) <<left<<unit << " | "
<< setw(7) << setprecision(2) <<right<< unitPrice << " | "
<< setw(3) <<right<< (taxStatus == 0 ? "no" : "yes") << " | "
<< setw(6) << right<<quantityOnHand << " | "
<< setw(6) << right<<quantityNeeded << " |";
}
break;
case write_human:
out << setw(max_length_label) <<right<< "Sku: " << sku << endl;
out << setw(max_length_label) <<right<< "Name: " << productName << endl;
out << setw(max_length_label) <<"Price: " <<fixed<<setprecision(2)<< right<<unitPrice << endl;
out << setw(max_length_label) << "Price after Tax: " <<fixed<<setprecision(2)<<right<<(taxStatus?(unitPrice*(1+tax_rate)):unitPrice)<< endl;
out << setw(max_length_label) <<right<< "Quantity Available: " << quantityOnHand <<" "<<unit<< endl;
out << setw(max_length_label) <<right<< "Quantity Needed: " << quantityNeeded <<" "<<unit<< endl;
}//end of switch
}
}
else {
//print the error message
out << this->ProductError;
}
return out;
}
}