-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceUsage.cpp
More file actions
320 lines (290 loc) · 11.6 KB
/
Copy pathServiceUsage.cpp
File metadata and controls
320 lines (290 loc) · 11.6 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "ServiceUsage.h"
// Static Element
int ServiceUsage::total = 0;
int ServiceUsage::currentNumber = 0;
LinkedList<ServiceUsage> ServiceUsage::usageList;
bool ServiceUsage::is_header_printed = false;
void ServiceUsage::resetHeader() { is_header_printed = false; }
// Constructor
ServiceUsage::ServiceUsage() {}
ServiceUsage::ServiceUsage(const string& roomId, const string& servId, const string& tenantId, bool status)
: room_ID(roomId), service_ID(servId), tenantID(tenantId), status(status), quantity(1) {
if (servId == "S.005" || servId == "S.006") quantity = 0;
usage_ID = generateID(++currentNumber);
}
ServiceUsage::~ServiceUsage() {}
// ID Generate
string ServiceUsage::generateID(int number) {
stringstream ss;
ss << "SU." << setw(3) << setfill('0') << number;
return ss.str();
}
// Load function
void ServiceUsage::load(const string& filename) { usageList.load(filename); }
void ServiceUsage::updateFile(const string& filename) { usageList.updateFile(filename); }
// Get function
string ServiceUsage::getID() const { return usage_ID; }
string ServiceUsage::getRoomID() const { return room_ID; }
string ServiceUsage::getTenantID() const { return tenantID; }
string ServiceUsage::getServiceID() const { return service_ID; }
bool ServiceUsage::getStatus() const { return status; }
void ServiceUsage::setStatus(bool newStatus) { status = newStatus; }
void ServiceUsage::setQuantity(int qty) { quantity = qty; }
// Ham bien doi nham doc du lieu tu file (moi du lieu se co 1 fromstring khac nhau)
void ServiceUsage::fromString(const string& line) {
string usageDatestr;
stringstream ss(line);
getline(ss, usage_ID, ',');
getline(ss, room_ID, ',');
getline(ss, tenantID, ',');
getline(ss, service_ID, ',');
ss >> quantity;
ss.ignore(1);
ss >> status;
total++;
}
string ServiceUsage::toString() const {
stringstream ss;
ss << usage_ID << ',' << room_ID << ',' << tenantID << ','
<< service_ID << ',' << quantity << ',' << status;
return ss.str();
}
// Chuc nang co ban (Basic Function)
void ServiceUsage::addServiceUsage() {
resetHeader();
Room::searchRoomByTenantID(Account::currentTenantID);
string room_ID, service_ID;
if (Service::serviceList.begin() == NULL) {
cout << "None of Service! " << endl;
return;
}
bool found1 = false;
do {
resetHeader();
cout << "Room ID (0 to exit): "; cin >> room_ID;
if (room_ID == "0") return;
Room* room = Room::roomList.searchID(room_ID);
if (room != NULL && room->getStatus() == 1) { found1 = true; }
else { cout << "Room ID not found or no owner. Please enter again! "<< endl; }
} while(!found1);
bool found2 = false;
do {
resetHeader();
Service::serviceList.show();
cout << "Service ID (0 to exit): "; cin >> service_ID;
if (service_ID == "0") return;
// Kiểm tra xem dịch vụ này đã được đăng ký chưa
LinkedList<ServiceUsage>::Node* current = usageList.begin();
bool serviceAlreadyActive = false;
while (current) {
if (current->data.getTenantID() == Account::currentTenantID &&
current->data.getRoomID() == room_ID &&
current->data.getServiceID() == service_ID) {
if (current->data.getStatus() == true) {
cout << "This service is already active for your account!" << endl;
serviceAlreadyActive = true;
break;
} else {
// Nếu dịch vụ đã bị hủy, cập nhật trạng thái thành active
current->data.setStatus(true);
cout << "Service usage reactivated successfully!" << endl;
return; // Kết thúc hàm sau khi cập nhật
}
}
current = current->next;
}
if (serviceAlreadyActive) continue;
Service* service = Service::serviceList.searchID(service_ID);
if (service != NULL) {
if (service->isMandatory()) {
cout << "This is a mandatory service. You cannot register/unregister it manually." << endl;
return;
}
found2 = true;
} else {
cout << "Service ID not found. Please enter again! "<< endl;
}
} while(!found2);
// Nếu không tìm thấy dịch vụ đã tồn tại, tạo mới
ServiceUsage newUsage(room_ID, service_ID, Account::currentTenantID, true);
usageList.add(newUsage);
cout << "Service usage added successfully!" << endl;
total++;
}
void ServiceUsage::deleteServiceUsage(string& usageID) {
usageList.deleteNode(usageID);
total--;
}
// Search Function
void ServiceUsage::searchByID() {
resetHeader();
string usageID;
cout << "Nhap Usage ID de tim kiem: "; cin >> usageID;
ServiceUsage* usage = usageList.searchID(usageID);
if (usage) { cout << "Da tim thay: " << *usage; }
else { cout << "Khong tim thay su dung dich vu voi ID: " << usageID << endl; }
}
void ServiceUsage::searchByRoomID() {
resetHeader();
string roomID;
cout << "Nhap RoomID de tim kiem dich vu da su dung: "; cin >> roomID;
LinkedList<ServiceUsage>::Node* current = usageList.begin();
bool found = false;
while (current) {
if (current->data.room_ID == roomID) {
cout << current->data;
found = true;
}
current = current->next;
}
if (!found) {
cout << "Khong tim thay su dung dich vu voi Room ID: " << roomID << endl;
}
}
void ServiceUsage::searchAll() {
resetHeader();
int choice;
do {
cout << " 1. Search by RoomID" << endl
<< " 2. Search by ServiceUsageID" << endl
<< " 0. Exit" << endl;
cout << "Please enter your option: "; cin >> choice;
switch (choice) {
case 1: searchByRoomID(); break;
case 2: searchByID(); break;
case 0: cout << "Exiting search menu." << endl; break;
default: cout << "Invalid choice. Please try again." << endl; break;
}
} while (choice != 0);
}
// Show Function
void ServiceUsage::showAllServiceUsages() {
resetHeader();
cout << "Danh sach tat ca cac su dung dich vu:" << endl;
usageList.show();
cout << "1. Sap xep tang ID" << endl
<< "2. Sap xep giam ID" << endl
<< "3. Tim kiem lich su su dung dich vu" << endl
<< "0. Thoat!" << endl;
int choice;
cout << "Lua chon cua ban: "; cin >> choice;
switch (choice) {
case 1: usageList.sortByID(true); showAllServiceUsages(); break;
case 2: usageList.sortByID(false); showAllServiceUsages(); break;
case 3: searchAll(); break;
default: break;
}
}
// Da nang hoa ham xuat
ostream& operator<<(ostream& os, const ServiceUsage& su) {
const int w_id = 15, w_room = 15, w_service = 15, w_tenant = 15, w_status = 10, w_qty = 10;
if (!ServiceUsage::is_header_printed) {
os << left << setw(w_id) << "Usage ID" << " | "
<< setw(w_room) << "Room ID" << " | "
<< setw(w_tenant) << "Tenant ID" << " | "
<< setw(w_service) << "Service ID" << " | "
<< setw(w_qty) << "Quantity" << " | "
<< setw(w_status) << "Status" << endl
<< string(w_id + w_room + w_service + w_tenant + w_qty + w_status + 16, '-') << endl;
ServiceUsage::is_header_printed = true;
}
os << left << setw(w_id) << su.usage_ID << " | "
<< setw(w_room) << su.room_ID << " | "
<< setw(w_tenant) << su.tenantID << " | "
<< setw(w_service) << su.service_ID << " | "
<< setw(w_qty) << su.quantity << " | "
<< setw(w_status) << (su.status ? "Active" : "Inactive") << endl;
return os;
}
void ServiceUsage::stopService() {
string serviceID, roomID;
searchByTenantID(Account::currentTenantID);
cout << "Nhap phong muon tuong tac: "; cin >> roomID;
cout << "Nhap Service ID muon dung su dung: "; cin >> serviceID;
// Kiểm tra xem dịch vụ có phải là dịch vụ bắt buộc không
Service* service = Service::serviceList.searchID(serviceID);
if (service && service->isMandatory()) {
cout << "Khong the dung dich vu bat buoc!" << endl;
return;
}
LinkedList<ServiceUsage>::Node* current = usageList.begin();
while (current) {
if (current->data.getServiceID() == serviceID &&
current->data.getTenantID() == Account::currentTenantID &&
current->data.getRoomID() == roomID &&
current->data.getStatus() == true) {
current->data.setStatus(false);
cout << "Da dung su dung dich vu thanh cong!" << endl;
return;
}
current = current->next;
}
cout << "Khong tim thay dich vu dang su dung!" << endl;
}
void ServiceUsage::searchByTenantID(string tenantID) {
resetHeader();
bool found = false;
cout << "\n\t\t\t\t\t==========SEARCH RESULT==========\n";
for (int i = 0; i < usageList.size(); i++) {
if (usageList[i].getTenantID() == tenantID && usageList[i].getStatus()) {
cout << usageList[i];
found = true;
}
}
if (!found) {
cout << "\t\t\t\t\tNo service usage found!\n";
}
cout << "\t\t\t\t\t==============================\n";
searchAll();
}
// Thêm hàm mới
void ServiceUsage::registerServices(const string& roomID, const string& tenantID) {
LinkedList<Service>::Node* current = Service::serviceList.begin();
while (current) {
ServiceUsage newUsage(roomID, current->data.getID(), tenantID, current->data.isMandatory());
usageList.add(newUsage);
total++;
current = current->next;
}
}
// Thêm các hàm mới vào ServiceUsage class
double ServiceUsage::calculateServiceAmountForRoom(const string& roomID, const string& tenantID, char usageChoice) {
double serviceAmount = 0;
for (LinkedList<ServiceUsage>::Node* current = usageList.begin(); current != nullptr; current = current->next) {
ServiceUsage& usage = current->data;
if (usage.getStatus() &&
roomID == usage.getRoomID() &&
tenantID == usage.getTenantID()) {
Service* service = Service::serviceList.searchID(usage.getServiceID());
if (service != nullptr) {
if (service->getID() == "S.005" || service->getID() == "S.006") {
double quantity = (usageChoice == '0') ? 100 :
promptServiceQuantity(service->getName(), roomID);
usage.setQuantity(quantity);
serviceAmount += service->getUnitPrice() * quantity;
} else {
serviceAmount += service->getUnitPrice();
}
}
}
}
return serviceAmount;
}
double ServiceUsage::promptServiceQuantity(const string& serviceName, const string& roomID) {
double quantity;
cout << "Nhap so luong " << serviceName << " cho phong " << roomID << ": ";
cin >> quantity;
return quantity;
}
void ServiceUsage::deleteServiceUsageByRoomAndTenant(const string& roomID, const string& tenantID) {
LinkedList<ServiceUsage>::Node* current = usageList.begin();
while (current != nullptr) {
if (current->data.getRoomID() == roomID && current->data.getTenantID() == tenantID) {
LinkedList<ServiceUsage>::Node* toDelete = current;
current = current->next;
usageList.deleteNode(toDelete->data.getID());
} else {
current = current->next;
}
}
}