-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomerOrder.cpp
146 lines (116 loc) · 3.44 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
//Name: Chaoyi Wu
// Seneca Student ID: 154330179
// Seneca email: [email protected]
// Date of completion: Nov. 29th
//
// I confirm that the content of this file is created by me,
// with the exception of the parts provided to me by my professor.
#include <algorithm>
#include <vector>
#include <iomanip>
#include "Utilities.h"
#include "CustomerOrder.h"
size_t CustomerOrder::m_widthField = 1;
CustomerOrder::CustomerOrder() {
m_name = "";
m_product = "";
m_cntItem = 0;
m_lstItem = nullptr;
}
CustomerOrder::CustomerOrder(std::string& record) {
Utilities utility;
size_t next_pos = 0;
bool more = true;
m_name = utility.extractToken(record, next_pos, more); // customer name
m_product = utility.extractToken(record, next_pos, more); // order name
// list of items making up the order
std::vector<std::string> items;
while (more) {
items.push_back(utility.extractToken(record, next_pos, more));
}
m_widthField = std::max(m_widthField, utility.getFieldWidth()); // update CustomerOrder::m_widthField
m_cntItem = items.size();
m_lstItem = new ItemInfo*[m_cntItem];
for (size_t i = 0; i < m_cntItem; i++) {
m_lstItem[i] = new ItemInfo(items[i]);
}
}
CustomerOrder::~CustomerOrder() {
for (size_t i = 0; i < m_cntItem; i++) {
delete m_lstItem[i];
}
delete[] m_lstItem;
}
CustomerOrder::CustomerOrder(CustomerOrder&& other) noexcept {
m_lstItem = nullptr;
m_cntItem = 0;
*this = std::move(other);
}
CustomerOrder& CustomerOrder::operator=(CustomerOrder&& other) {
if (this != &other) {
for (size_t i = 0; i < m_cntItem; i++) {
delete m_lstItem[i];
}
delete[] m_lstItem;
m_name = other.m_name;
m_product = other.m_product;
m_cntItem = other.m_cntItem;
m_lstItem = other.m_lstItem;
other.m_cntItem = 0;
other.m_lstItem = nullptr;
}
return *this;
}
bool CustomerOrder::getItemFillState(std::string item) const {
bool state = true;
for (size_t i = 0; i < m_cntItem; i++) {
if (m_lstItem[i]->m_itemName == item) {
state = m_lstItem[i]->m_fillState;
break;
}
}
return state;
}
bool CustomerOrder::getOrderFillState() const {
bool state = true;
for (size_t i = 0; i < m_cntItem; i++) {
if (!m_lstItem[i]->m_fillState) {
state = false;
}
}
return state;
}
void CustomerOrder::fillItem(Item& item, std::ostream& os) {
// if item cannot be found in the current order
for (size_t i = 0; i < m_cntItem; i++) {
if (m_lstItem[i]->m_itemName == item.getName()) {
if (item.getQuantity() > 0) {
item.updateQuantity(); // update inventory
m_lstItem[i]->m_serialNumber = item.getSerialNumber(); // update ItemInfo::m_serialNumber
m_lstItem[i]->m_fillState = true; // update ItemInfo::m_fillState
os << "Filled ";
}
// inventory is empty
else {
os << "Unable to fill ";
}
os << m_name << ", ";
os << m_product;
os << "[" << item.getName() << "]" << std::endl;
}
}
}
void CustomerOrder::display(std::ostream& os) const {
os << m_name << " - " << m_product << std::endl;
for (size_t i = 0; i < m_cntItem; i++) {
os << "[" << std::setw(6) << std::setfill('0') << std::right << m_lstItem[i]->m_serialNumber << "] ";
os << std::setw(m_widthField) << std::setfill(' ') << std:: left << m_lstItem[i]->m_itemName << " - ";
if (m_lstItem[i]->m_fillState) {
os << "FILLED";
}
else {
os << "MISSING";
}
os << std::endl;
}
}