-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockitem.cpp
109 lines (87 loc) · 2.3 KB
/
stockitem.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
// File: stockitem.cpp
// Date: 2016-02-27
// Description: Implementation of a StockItem class
#include "stockitem.h"
// Default constructor
StockItem::StockItem() {
sku = 0;
description = "";
price = 0;
stock = 0;
}
// Parameterized constructor
// Need to specify SKU, description, and price.
// Stock is defaulted to 0;
// Assume parameters are valid
StockItem::StockItem(int skuid, string desc, double p) {
sku = skuid;
if (sku > 99999) sku = sku % 100000;
if (sku < 10000) sku += 10000; // force sku to 5 digits
if (desc.length() > 30)
description = desc.substr(0, 29);
else
description = desc;
price = p;
stock = 0;
}
// Accessors
int StockItem::GetSKU() const {
return sku;
}
string StockItem::GetDescription() const {
return description;
}
double StockItem::GetPrice() const {
return price;
}
int StockItem::GetStock() const {
return stock;
}
// Mutators
// boolean return values - return true for successful update, false if argument is invalid (i.e. negative price/stock/SKU)
bool StockItem::SetDescription(string newdesc) {
if (newdesc.length() > 30)
description = newdesc.substr(0, 29);
else
description = newdesc;
return true;
}
bool StockItem::SetPrice(double newprice) {
if (newprice >= 0) {
price = newprice;
return true;
} else return false;
}
bool StockItem::SetStock(int amount) {
if (amount >= 0) {
stock = amount;
return true;
} else return false;
}
bool StockItem::operator==(const StockItem& item) const {
return (sku == item.GetSKU());
}
bool StockItem::operator!=(const StockItem& item) const {
return !(*this == item);
}
bool StockItem::operator>(const StockItem& item) const {
return (sku > item.GetSKU());
}
bool StockItem::operator<(const StockItem& item) const {
return (sku < item.GetSKU());
}
bool StockItem::operator>=(const StockItem& item) const {
return !(*this < item);
}
bool StockItem::operator<=(const StockItem& item) const {
return !(*this > item);
}
StockItem& StockItem::operator=(const StockItem& item) {
if (this != &item) {
this->sku = item.sku;
this->description = item.description;
this->price = item.price;
this->stock = item.stock;
}
return *this;
}