-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerOrder.cpp
149 lines (121 loc) · 3.41 KB
/
CustomerOrder.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
// Name: Jagbir Singh
// Seneca Student ID: 144019221
// Seneca email: [email protected]
// Date of completion: 23/03/2024
//
// I confirm that I am the only author of this file
// and the content was created entirely by me.
#include "CustomerOrder.h"
using namespace std;
namespace seneca
{
size_t CustomerOrder::m_widthField = 0;
CustomerOrder::CustomerOrder(const string& line)
{
Utilities ut;
size_t startPos = 0u;
bool more = true;
m_name = ut.extractToken(line, startPos, more);
m_product = ut.extractToken(line, startPos, more);
size_t currentPos = startPos;
while (more)
{
ut.extractToken(line, startPos, more);
++m_cntItem;
}
m_lstItem = new Item*[m_cntItem];
more = true;
for (size_t i = 0; i < m_cntItem && more; ++i)
{
m_lstItem[i] = new Item(ut.extractToken(line, currentPos, more));
}
if (m_widthField < ut.getFieldWidth())
m_widthField = ut.getFieldWidth();
}
CustomerOrder::CustomerOrder(const CustomerOrder& src)
{
throw "Error";
}
CustomerOrder::~CustomerOrder()
{
for (size_t i = 0; i < m_cntItem; ++i)
{
delete m_lstItem[i];
}
delete[] m_lstItem;
}
CustomerOrder::CustomerOrder(CustomerOrder&& src)noexcept
{
m_name = src.m_name;
m_product = src.m_product;
m_cntItem = src.m_cntItem;
src.m_cntItem = 0;
m_lstItem = src.m_lstItem;
src.m_lstItem = nullptr;
}
CustomerOrder& CustomerOrder::operator=(CustomerOrder&& src)noexcept
{
if (this != &src)
{
for (size_t i = 0; i < m_cntItem; i++)
delete m_lstItem[i];
delete[] m_lstItem;
m_name = src.m_name;
m_product = src.m_product;
m_cntItem = src.m_cntItem;
src.m_cntItem = 0;
m_lstItem = src.m_lstItem;
src.m_lstItem = nullptr;
}
return *this;
}
bool CustomerOrder::isOrderFilled() const
{
bool res = true;
for (size_t i = 0; i < m_cntItem && res; ++i)
{
if (!m_lstItem[i]->m_isFilled)
res = false;
}
return res;
}
bool CustomerOrder::isItemFilled(const std::string& itemName) const
{
bool res = true;
for (size_t i = 0; i < m_cntItem && res; ++i)
{
if (m_lstItem[i]->m_itemName == itemName && !m_lstItem[i]->m_isFilled)
res = false;
}
return res;
}
void CustomerOrder::fillItem(Station& station, std::ostream& os) {
if (m_lstItem == nullptr) {
os << "Customer order has no items." << std::endl;
return;
}
for (size_t i = 0; i < m_cntItem; ++i) {
Item* item = m_lstItem[i];
if (item->m_itemName == station.getItemName() && !item->m_isFilled) {
if (station.getQuantity() > 0) {
station.updateQuantity();
item->m_serialNumber = station.getNextSerialNumber();
item->m_isFilled = true;
os << " Filled " << m_name << ", " << m_product << " [" << item->m_itemName << "]" << std::endl;
break;
}
else {
os << " Unable to fill " << m_name << ", " << m_product << " [" << item->m_itemName << "]" << std::endl;
}
}
}
}
void CustomerOrder::display(std::ostream& os) const
{
os << m_name << " - " << m_product << endl;
for (size_t i = 0; i < m_cntItem; i++)
{
os << "[" << setfill('0') << right << setw(6) << m_lstItem[i]->m_serialNumber << "] " << left << setfill(' ') << setw(m_widthField) << m_lstItem[i]->m_itemName << " - " << (m_lstItem[i]->m_isFilled ? "FILLED" : "TO BE FILLED") << endl;
}
}
}