-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddressbook.cpp
More file actions
344 lines (275 loc) · 6.83 KB
/
Copy pathaddressbook.cpp
File metadata and controls
344 lines (275 loc) · 6.83 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "addressbook.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
addressbook::addressbook() {
this->list = new linkedlist;
}
addressbook::~addressbook() {
this->list->deleteAll();
delete list;
}
void addressbook::printMainMenu() {
cout << "1. 주소정보를 입력한다." << endl;
cout << "2. 주소정보를 삭제한다." << endl;
cout << "3. 주소정보를 수정한다." << endl;
cout << "4. 주소정보를 검색한다." << endl;
cout << "5. 전체 주소록을 출력한다." << endl;
cout << "6. 주소록 파일에 저장한다." << endl;
cout << "7. 주소록 파일을 불러온다." << endl;
cout << "0. 종료한다." << endl;
}
void addressbook::print_finish() {
cout << "프로그램을 종료합니다." << endl;
}
void addressbook::print_error(int error) {
switch (error) {
case ERROR_NO_MENU:
cout << "없는 번호를 입력하였습니다." << endl;
break;
case ERROR_FULL:
cout << "주소록이 가득 찼습니다." << endl;
break;
case ERROR_EMPTY:
cout << "주소록이 비어있습니다." << endl;
break;
case ERROR_NOT_SEARCH:
cout << "주소정보를 찾을 수 없습니다." << endl;
break;
case ERROR_LOAD_FAIL:
cout << "addressbook.dat 파일을 불러올수 없습니다." << endl;
break;
default:
break;
}
}
bool addressbook::isEmpty() {
bool isEmpty = true;
if (this->list->get_length() > 0) {
isEmpty = false;
}
return isEmpty;
}
void addressbook::input() {
personInfo info;
char yesno;
string name;
string phone;
string address;
cout << "이름 : ";
cin >> name;
info.name(name);
cout << "전화번호 : ";
cin >> phone;
info.phone(phone);
cout << "주소 : ";
cin >> address;
info.address(address);
cout << "입력된 정보" << endl;
this->printHeader(-1);
this->printPersonInfo(info, -1);
cin.sync();
cout << "추가하시겠습니까 ? (y/n) :";
cin.get(yesno);
if (yesno == 'y') {
this->list->append(info);
cout << "추가되었습니다." << endl;
}
}
void addressbook::remove() {
string name;
int removeIndex = -1;
char yesno;
if ( this->isEmpty() == true ) {
this->print_error(ERROR_EMPTY);
return ;
}
cout << "삭제할 이름: ";
cin >> name;
removeIndex = this->list->findName(name);
if ( removeIndex == -1 ) {
this->print_error(ERROR_NOT_SEARCH);
return ;
}
this->printHeader(-1);
this->printPersonInfo(this->list->viewAt(removeIndex), -1);
cin.sync();
cout << "삭제하겠습니까? (y/n) :";
cin.get(yesno);
if ( yesno == 'y' ) {
this->list->deleteNode(removeIndex);
cout << "삭제되었습니다." << endl;
}
}
void addressbook::modify() {
string name;
string phone;
string address;
int modifyIndex = -1;
char yesno;
node *target;
if ( this->isEmpty() == true ) {
this->print_error(ERROR_EMPTY);
return ;
}
cout << "수정할 이름: ";
cin >> name;
modifyIndex = this->list->findName(name);
if ( modifyIndex == -1 ) {
this->print_error(ERROR_NOT_SEARCH);
return ;
}
this->printHeader(-1);
this->printPersonInfo(this->list->viewAt(modifyIndex), -1);
cin.sync();
cout << "수정하겠습니까? (y/n) :";
cin.get(yesno);
if ( yesno == 'y' ) {
cout << "이름 : ";
cin >> name;
cout << "전화번호 : ";
cin >> phone;
cout << "주소 : ";
cin >> address;
cout << "수정된 정보" << endl;
target = this->list->getNode(modifyIndex);
target->info->name(name);
target->info->phone(phone);
target->info->address(address);
this->printHeader(-1);
this->printPersonInfo(*(target->info), -1);
cout << "수정되었습니다." << endl;
}
}
void addressbook::search() {
string name;
int foundIndex = -1;
if ( this->isEmpty() == false ) {
this->print_error(ERROR_EMPTY);
return ;
}
cout << "검색할 이름: ";
cin >> name;
foundIndex = this->list->findName(name);
if ( foundIndex == -1 ) {
this->print_error(ERROR_NOT_SEARCH);
return ;
}
this->printHeader(-1);
this->printPersonInfo(this->list->viewAt(foundIndex), -1);
}
void addressbook::printHeader(int printNum) {
cout << "----------------------------------------------" << endl;
if ( printNum != -1 ) {
cout << "번호 이름 전화번호 주소" << endl;
} else {
cout << "이름 전화번호 주소" << endl;
}
cout << "----------------------------------------------" << endl;
}
void addressbook::printPersonInfo(personInfo &info, int printNum) {
if ( printNum != -1 ) {
cout << left << setw(4);
}
cout << info << endl;
}
void addressbook::printAll() {
int number = 1;
node *target;
if ( this->isEmpty() == true ) {
this->print_error(ERROR_EMPTY);
return ;
}
this->printHeader(1);
target = this->list->moveFirst();
while ( this->list->isTail() != true ) {
this->printPersonInfo(*(target->info), number);
target = this->list->moveNext();
number++;
}
}
void addressbook::save() {
ofstream fcout;
node *target;
if ( this->isEmpty() == true ) {
this->print_error(ERROR_EMPTY);
return ;
}
fcout.open("addressbook.dat", ios::out);
target = this->list->moveFirst();
if (fcout.is_open()) {
while ( this->list->isTail() != true ) {
fcout << target->info->name() << endl;
fcout << target->info->phone() << endl;
fcout << target->info->address() << endl;
target = this->list->moveNext();
}
fcout.close();
}
cout << "addressbook.dat 파일에 저장하였습니다." << endl;
}
void addressbook::load() {
ifstream fcin;
personInfo info;
string name;
string phone;
string address;
this->list->deleteAll();
fcin.open("addressbook.dat", ios::in);
if (fcin.is_open() == false) {
this->print_error(ERROR_LOAD_FAIL);
return ;
}
while (!fcin.eof()) {
getline(fcin, name);
info.name(name);
getline(fcin, phone);
info.phone(phone);
getline(fcin, address);
info.address(address);
if ( name == "" ) {
break;
}
this->list->append(info);
}
fcin.close();
cout << "addressbook.dat 파일을 불러왔습니다." << endl;
}
void addressbook::testSetup() {
int index = 0;
personInfo sample[10];
sample[0].name("aaa");
sample[0].phone("0001112222");
sample[0].address("acb");
sample[1].name("bbb");
sample[1].phone("0001113333");
sample[1].address("abc");
sample[2].name("ccc");
sample[2].phone("0001114444");
sample[2].address("abc");
sample[3].name("ddd");
sample[3].phone("0001115555");
sample[3].address("abc");
sample[4].name("eee");
sample[4].phone("0001116666");
sample[4].address("abc");
sample[5].name("fff");
sample[5].phone("0001117777");
sample[5].address("abc");
sample[6].name("ggg");
sample[6].phone("0001118888");
sample[6].address("abc");
sample[7].name("hhh");
sample[7].phone("0001119999");
sample[7].address("abc");
sample[8].name("iii");
sample[8].phone("0001110000");
sample[8].address("abc");
sample[9].name("jjj");
sample[9].phone("0002220000");
sample[9].address("abc");
for ( index = 0; index < 10; index++ ) {
this->list->append(sample[index]);
}
}