-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.cpp
More file actions
268 lines (232 loc) · 6.6 KB
/
Copy pathManager.cpp
File metadata and controls
268 lines (232 loc) · 6.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
#include "Manager.h"
#include <iostream>
#include <string>
using namespace std;
Manager::Manager()
{
}
Manager::~Manager()
{
}
void Manager::LOAD()
{
// Check if memberQueue is already populated
if (!memberQueue.isEmpty())
{
PrintErrorCode(100); // Error code 300: Data already loaded
return;
}
// Open data file
ifstream fdata("data.txt");
if (!fdata)
{
PrintErrorCode(100); // Error code 200: Fail to open data file
return;
}
string name, date, terms;
int age;
// Read data from file and populate memberQueue
while (fdata >> name >> age >> date >> terms)
{
// Add data to memberQueue
MemberQueueNode newNode(name, age, date, terms);
memberQueue.enqueue(newNode);
}
fdata.close();
// Print loaded data to log file
flog << "===== LOAD =====" << endl;
while (!memberQueue.isEmpty())
{
MemberQueueNode node = memberQueue.pop();
flog << node.getName() << "/" << node.getAge() << "/" << node.getDate() << "/" << node.getTerms() << endl;
}
flog << "===============" << endl << endl;
PrintSuccess("LOAD");
}
void Manager::ADD(const string& name, int age, const string& date, const string& terms)
{
if (name.empty() || age < 10 || age > 99 || date.empty() || terms.empty())
{
PrintErrorCode(200); // Error code 100: Invalid input parameters
return;
}
// Check for duplicate names
MemberQueue tempQueue = memberQueue; // Create a temporary queue to preserve original data
while (!tempQueue.isEmpty())
{
MemberQueueNode node = tempQueue.pop();
if (node.getName() == name)
{
PrintErrorCode(200); // Error code 400: Duplicate patient name
return;
}
}
// Add data to memberQueue
MemberQueueNode newNode(name, age, date, terms);
memberQueue.enqueue(newNode);
// Print added data to log file
flog << "===== ADD =====" << endl;
flog << newNode.getName() << "/" << newNode.getAge() << "/" << newNode.getDate() << "/" << newNode.getTerms() << endl;
flog << "===============" << endl << endl;
PrintSuccess("ADD");
}
void Manager::QPOP()
{
if (memberQueue.isEmpty())
{
PrintErrorCode(300); // 에러 코드 300: Member_Queue에 데이터가 없음
return;
}
// Process data from Member_Queue
while (!memberQueue.isEmpty())
{
MemberQueueNode node = memberQueue.pop();
termsList.insert(node.getTerms(), 1, &termsBST);
nameBST.insert(node.getName(), node.getAge(), node.getDate(), node.getDate(), node.getTerms());
// Print processed data to log file
flog << "===== QPOP =====" << endl;
flog << "Success" << endl;
flog << "===============" << endl << endl;
}
PrintSuccess("QPOP");
}
void Manager::SEARCH(const string& name)
{
// Search for the member in Name_BST
NameBSTNode* result = nameBST.search(name);
if (result != nullptr)
{
// Member found, print the information
flog << "===== SEARCH =====" << endl;
flog << result->getMemberName() << "/" << result->getAge() << "/" << result->getCollectionDate()
<< "/" << result->getExpirationDate() << endl;
flog << "===============" << endl << endl;
}
else
{
// Member not found, print error code
PrintErrorCode(400);
}
}
void Manager::PRINT(const string& arg)
{
if (arg == "D")
{
flog << "===== PRINT =====" << endl;
flog << "Terms_BST D" << endl;
flog << "================" << endl;
termsBST.printInOrder(termsBST.getRoot());
flog << "================" << endl << endl;
}
else if (arg == "NAME")
{
flog << "===== PRINT =====" << endl;
flog << "Name_BST" << endl;
flog << "================" << endl;
nameBST.printInOrder(nameBST.getRoot());
flog << "================" << endl << endl;
}
else
{
PrintErrorCode(500);
}
}
void Manager::DELETE(const string& arg1, const string& arg2)
{
if (arg1 == "DATE")
{
// Delete by DATE
if (arg2.empty())
{
PrintErrorCode(600);
return;
}
string date = arg2;
TermsBSTNode* deletedNode = termsBST.deleteByExpirationDate(date);
if (deletedNode != nullptr)
{
nameBST.deleteNode(deletedNode->getMemberName(), termsBST);
flog << "===== DELETE =====" << endl;
flog << "Success" << endl;
flog << "================" << endl << endl;
}
else
{
PrintErrorCode(600);
}
}
else if (arg1 == "NAME")
{
// Delete by NAME
if (arg2.empty())
{
PrintErrorCode(600);
return;
}
string name = arg2;
NameBSTNode* deletedNode = nameBST.deleteByName(name, termsBST);
if (deletedNode != nullptr)
{
flog << "===== DELETE =====" << endl;
flog << "Success" << endl;
flog << "================" << endl << endl;
}
else
{
PrintErrorCode(600);
}
}
else
{
PrintErrorCode(600);
}
}
void Manager::run(const char* command) {
// 명령어 파일 열기
fcmd.open(command);
if (!fcmd) {
PrintErrorCode(200); // 에러 코드 200: 명령어 파일 열기 실패
return;
}
string cmd, name, date, terms;
int age;
// command.txt에서 명령어 읽고 처리하기
while (fcmd >> cmd) {
if (cmd == "LOAD") {
LOAD();
}
else if (cmd == "ADD") {
fcmd >> name >> age >> date >> terms;
ADD(name, age, date, terms);
}
else if (cmd == "QPOP") {
QPOP();
}
else if (cmd == "SEARCH") {
fcmd >> name;
SEARCH(name);
}
else if (cmd == "PRINT") {
fcmd >> cmd; // PRINT 명령어의 매개변수 얻기
PRINT(cmd);
}
else if (cmd == "DELETE") {
fcmd >> cmd >> name; // DELETE 명령어의 매개변수 얻기
DELETE(cmd, name);
}
else if (cmd == "EXIT") {
// 로그 파일에 종료 메시지 출력
flog << "===== EXIT =====" << endl;
flog << "Success" << endl;
flog << "===============" << endl << endl;
// 명령어 파일 닫기
fcmd.close();
return; // 함수 종료 및 프로그램 종료
}
else {
PrintErrorCode(300); // 에러 코드 300: 잘못된 명령어
}
}
// 명령어 파일 닫기
fcmd.close();
}