-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerOrder.cpp
More file actions
186 lines (159 loc) · 4.13 KB
/
CustomerOrder.cpp
File metadata and controls
186 lines (159 loc) · 4.13 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
// Author: Kathleen Monks
// Purpose: Final Project for Seneca's OOP345
// Date of completion: 2021-11-27
// ==========================================
#include <iostream>
#include <algorithm>
#include "CustomerOrder.h"
using namespace std;
namespace sdds
{
// Initialize Class Variables
// ==========================
size_t CustomerOrder::m_widthField{ 0u };
// Class Constraints
// =================
const size_t serialNo__widthField{ 6 };
CustomerOrder::CustomerOrder(const std::string& record)
{
Utilities util;
size_t next_pos{ 0u };
bool more{ true };
std::string itemFields{ "" };
std::string itemField{ "" };
size_t i;
if (record.length() > 1) // ensure record contains data
{
m_name = util.extractToken(record, next_pos, more);
m_product = util.extractToken(record, next_pos, more);
itemFields = record.substr(next_pos, record.length() - next_pos);
m_cntItem = std::count(itemFields.begin(), itemFields.end(), util.getDelimiter()) + 1;
m_lstItem = new Item * [m_cntItem]; // create new instances of each item in resource
next_pos = 0u; // reset for itemFields
for (i = 0; i < m_cntItem && more; i++)
{
itemField = util.extractToken(itemFields, next_pos, more);
m_lstItem[i] = new Item(itemField);
}
m_widthField = std::max(util.getFieldWidth(), m_widthField);
}
}
CustomerOrder& CustomerOrder::operator=(CustomerOrder&& src)noexcept
{
size_t i;
if (this != &src)
{
if (m_lstItem)
{
for (i = 0u; i < m_cntItem; i++)
{
delete m_lstItem[i]; // deallocate current items
}
delete[] m_lstItem; // deallocate current array
}
m_lstItem = src.m_lstItem; // move src's array address
m_cntItem = src.m_cntItem; // move src count
src.m_cntItem = 0u;
for (i = 0u; i < m_cntItem; i++)
{
m_lstItem[i] = src.m_lstItem[i]; // move each Item address
}
src.m_lstItem = nullptr;
// swap instance variables
m_name = src.m_name;
src.m_name = "";
m_product = src.m_product;
src.m_product = "";
}
return *this;
}
CustomerOrder::~CustomerOrder()
{
size_t i;
for (i = 0u; i < m_cntItem; i++)
{
delete m_lstItem[i];
}
delete[] m_lstItem;
}
bool CustomerOrder::isFilled()const // modified for ms3, 2021-12-04
{
bool result = true;
size_t i;
for (i = 0u; i < m_cntItem && result; i++)
{
if (!m_lstItem[i]->m_isFilled)
{
result = false;
}
}
return result;
}
//***************************************************************
bool CustomerOrder::isItemFilled(const std::string& itemName) const // modified for ms3, 2021-12-04
{
bool result = true;
size_t i;
for (i = 0u; i < m_cntItem && result; i++)
{
if (m_lstItem[i]->m_itemName == itemName)
{
result = m_lstItem[i]->m_isFilled;
}
}
return result;
}
void CustomerOrder::fillItem(Station& station, std::ostream& os) // modified for ms3, 2021-12-04
{
size_t i;
bool available{true};
for (i = 0u; i < m_cntItem && available; i++)
{
if (m_lstItem[i]->m_itemName == station.getItemName()) // find item match
{
if (station.getQuantity() >= 1) // ensure station is not empty
{
station.updateQuantity(); // subtract item from station
m_lstItem[i]->m_serialNumber = station.getNextSerialNumber();
m_lstItem[i]->m_isFilled = true;
os << " Filled " << m_name << ", " << m_product << " [" << m_lstItem[i]->m_itemName << "]\n";
}
else
{
os << " Unable to fill " << m_name << ", " << m_product << " [" << station.getItemName() << "]\n";
}
}
}
}
void CustomerOrder::display(std::ostream& os) const
{
size_t i;
// title
os << m_name;
os << " - ";
os << m_product;
os << endl;
// item list
for (i = 0u; i < m_cntItem; i++)
{
// serial
os << '[';
os.fill('0');
os.width(serialNo__widthField);
os.setf(ios::right);
os << m_lstItem[i]->m_serialNumber;
os.unsetf(ios::right);
os << "] ";
// name
os.fill(' ');
os.width(m_widthField);
os.setf(ios::left);
os << m_lstItem[i]->m_itemName;
os.unsetf(ios::left);
os << " - ";
// status
m_lstItem[i]->m_isFilled ? os << "FILLED" : os << "TO BE FILLED";
os << endl;
}
}
}