-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.cpp
296 lines (269 loc) · 6.52 KB
/
Item.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
/*
*************************************************************************************************
* Name: Patel Bhavy Piyushkumar *
* Id: 121048219 *
* Email: [email protected] *
* course: OOP244 NDD *
* *
* Citation and Sources... *
* Final Project Milestone 4 *
* Module: Item *
* Filename: Item.cpp *
* Version 1.0 *
* Author: Bhavy Piyushkumar Patel *
* Revision History *
* ----------------------------------------------------------- *
* Date Reason *
* 2022/04/06 completed MS4 *
* ----------------------------------------------------------- *
* I have done all the coding by myself and only copied the code *
* that my professor provided to complete my workshops and assignments. *
* - Bhavy Piyushkumar Patel *
************************************************************************************************/
#include <cstring>
#include "Item.h"
#include "Utils.h"
using namespace std;
namespace sdds
{
bool Item::linear() const
{
return m_flag;
}
Item::Item()
{
m_itemDescription = nullptr;
m_flag = 0;
m_price = 0.0;
m_quantityInHand = 0;
m_quantityReq = 0;
m_sku = 0;
}
Item::Item(const Item& i)
{
operator=(i);
}
Item& Item::operator=(const Item& i)
{
if (this != &i)
{
ut.alocpy(m_itemDescription, i.m_itemDescription);
m_flag = i.m_flag;
m_price = i.m_price;
m_quantityInHand = i.m_quantityInHand;
m_quantityReq = i.m_quantityReq;
m_sku = i.m_sku;
}
return *this;
}
Item::~Item() //destructor
{
delete[] m_itemDescription;
}
int Item::qtyNeeded() const
{
return m_quantityReq;
}
int Item::qty() const
{
return m_quantityInHand;
}
Item::operator double() const
{
return m_price;
}
Item::operator bool() const
{
return m_state;
}
int Item::operator-=(int quantityInHand)
{
m_quantityInHand -= quantityInHand;
return 0;
}
int Item::operator+=(int quantityInHand)
{
m_quantityInHand += quantityInHand;
return 0;
}
void Item::linear(bool flag)
{
m_flag = flag;
}
Item& Item::clear()
{
m_state.clear();
return *this;
}
bool Item::operator==(int sku) const
{
return sku == m_sku ? 1 : 0;
}
bool Item::operator==(const char* description) const
{
bool done = 0;
if (description != NULL)
{
char* ptr;
ptr = strstr(m_itemDescription, description);
if (ptr)
{
done = 1;
}
}
return done;
}
std::ofstream& Item::save(std::ofstream& ofstr) const
{
if (m_state)
{
ofstr << m_sku << "\t";
ofstr << m_itemDescription << "\t";
ofstr << m_quantityInHand << "\t";
ofstr << m_quantityReq << "\t";
ofstr.setf(ios::fixed);
ofstr.precision(2);
ofstr << m_price;
ofstr.unsetf(ios::fixed);
}
return ofstr;
}
std::ifstream& Item::load(std::ifstream& ifstr)
{
int tempSKU;
char tempDescription[500];
int tempQuantityInHand;
int tempQuantityReq;
double tempPrice;
ifstr >> tempSKU;
ifstr.ignore();
ifstr.getline(tempDescription, 500, '\t');
ifstr >> tempQuantityInHand;
ifstr.ignore();
ifstr >> tempQuantityReq;
ifstr.ignore();
ifstr >> tempPrice;
ifstr.ignore();
m_sku = tempSKU;
ut.alocpy(m_itemDescription, tempDescription);
m_quantityInHand = tempQuantityInHand;
m_quantityReq = tempQuantityReq;
m_price = tempPrice;
if (ifstr.bad())
{
m_state = "Input file stream read failed!";
}
tempDescription[0] = '\0';
return ifstr;
}
std::ostream& Item::display(std::ostream& ostr) const
{
if (!m_state)
{
ostr << m_state;
}
else
{
if (linear())
{
ostr.setf(ios::fixed);
ostr.width(5);
ostr << m_sku;
ostr.unsetf(ios::fixed);
ostr << " | ";
ostr.setf(ios::fixed);
bool done = 0;
int i = 0;
for (i = 0; i < 35 && done != 1; i++)
{
if (m_itemDescription[i] != '\0')
{
ostr << m_itemDescription[i];
}
else
{
done = 1;
}
}
if (i < 35)
{
int tempLength = 35 - strlen(m_itemDescription);
for (int j = 0; j < tempLength; j++)
{
ostr << " ";
}
}
ostr.unsetf(ios::fixed);
ostr << " | ";
ostr.setf(ios::fixed);
ostr.width(4);
ostr.fill(' ');
ostr << right << m_quantityInHand;
ostr.unsetf(ios::fixed);
ostr << " | ";
ostr.setf(ios::fixed);
ostr.width(4);
ostr.fill(' ');
ostr << right << m_quantityReq;
ostr.unsetf(ios::fixed);
ostr << " | ";
ostr.setf(ios::fixed);
ostr.width(7);
ostr.fill(' ');
ostr.precision(2);
ostr << right << m_price;
ostr.unsetf(ios::fixed);
ostr << " |";
}
else
{
int tempQty = m_quantityReq - m_quantityInHand;
ostr << "AMA Item:" << endl;
ostr << m_sku << ": " << m_itemDescription << endl;
ostr << "Quantity Needed: " << m_quantityReq << endl;
ostr << "Quantity Available: " << m_quantityInHand << endl;
ostr.setf(ios::fixed);
ostr.precision(2);
ostr << "Unit Price: $" << m_price << endl;
ostr << "Needed Purchase Fund: $" << (m_price * tempQty) << endl;
ostr.unsetf(ios::fixed);
}
}
return ostr;
}
std::istream& Item::read(std::istream& istr)
{
//delete[] m_description;
m_state.clear();
char tempDescription[100];
int tempQuantityReq;
int tempQuantityInHand;
double tempPrice;
cout << "AMA Item:" << endl;
m_sku = ut.getint(m_minSkuNum, m_maxSkuNum, "SKU: ");
cout << "Description: ";
istr.ignore(1000, '\n');
istr.getline(tempDescription, 100);
tempQuantityReq = ut.getint(1, 9999, "Quantity Needed: ");
tempQuantityInHand = ut.getint(0, tempQuantityReq, "Quantity On Hand: ");
tempPrice = ut.getdouble(0.0, 9999.0, "Unit Price: $");
ut.alocpy(m_itemDescription, tempDescription);
m_quantityReq = tempQuantityReq;
m_quantityInHand = tempQuantityInHand;
m_price = tempPrice;
if (istr.fail())
{
m_state = "Console entry failed!";
}
m_minSkuNum = 40000;
m_maxSkuNum = 99999;
return istr;
}
int Item::readSku(std::istream& istr)
{
m_sku = ut.getint(m_minSkuNum, m_maxSkuNum, "SKU: ");
m_minSkuNum = 40000;
m_maxSkuNum = 99999;
return 0;
}
}