-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.cpp
More file actions
204 lines (172 loc) · 5.49 KB
/
Copy patha.cpp
File metadata and controls
204 lines (172 loc) · 5.49 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Contact {
private:
string name;
string phoneNumber;
string email;
string address;
public:
//생성자 디폴트 값으로 빈 문자열 생성
Contact(const string& n = "", const string& phone = "",
const string& em = "", const string& addr = "")
:name(n), phoneNumber(phone), email(em), address(addr) {}
//연락처 정보 변경 함수
void setName(const string& n) { name = n; }
void setPhoneNumber(const string& phone) { phoneNumber = phone; }
void setEmail(const string& em) { email = em; }
void setAddress(const string& addr) { address = addr; }
// 연락처 정보 조회 함수
string getName() const { return name; }
string getPhoneNumber() const { return phoneNumber; }
string getEmail() const { return email; }
string getAddress() const { return address; }
//연락처 정보 출력 함수
void displayContact() const {
std::cout << "이름: " << name << "\n"
<< "전화번호: " << phoneNumber << "\n"
<< "이메일: " << email << "\n"
<< "주소: " << address << "\n";
}
};
class AddressBook {
private:
vector<Contact> contacts;
public:
//새 연락처 추가
void addContact() {
string name, phone, email, address;
cin.ignore(); // 입력 버퍼 지우기
std::cout << "이름 입력: ";
std::getline(cin, name);
std::cout << "전화번호 입력: ";
std::getline(cin, phone);
std::cout << "이메일 입력: ";
std::getline(cin, email);
std::cout << "주소 입력: ";
std::getline(cin, address);
contacts.emplace_back(name, phone, email, address);
std::cout << "연락처 추가 완료!" << endl;
}
//연락처 검색
void searchContact() {
if (contacts.empty()) {
std::cout << "주소록이 비어있습니다." << endl;
return;
}
string searchName;
std::cout << "검색할 이름 입력: ";
cin.ignore();
getline(cin, searchName);
bool found = false;
for (const auto& contact : contacts) {
if (contact.getName().find(searchName) != string::npos) {
contact.displayContact();
std::cout << "------------------------\n";
found = true;
}
}
if (!found) {
std::cout << "일치하는 연락처를 찾을 수 없습니다." << std::endl;
}
}
// 연락처 수정
void editContact() {
if (contacts.empty()) {
std::cout << "주소록이 비어있습니다." << endl;
return;
}
string searchName;
std::cout << "수정할 연락처 이름 입력: ";
cin.ignore();
getline(cin, searchName);
for (auto& contact : contacts) {
if (contact.getName() == searchName) {
string newPhone, newEmail, newAddress;
std::cout << "새 전화번호 입력 (미변경시 Enter): ";
getline(cin, newPhone);
if (!newPhone.empty()) contact.setPhoneNumber(newPhone);
std::cout << "새 이메일 입력 (미변경시 Enter): ";
getline(cin, newEmail);
if (!newEmail.empty()) contact.setEmail(newEmail);
std::cout << "새 주소 입력 (미변경시 Enter): ";
getline(cin, newAddress);
if (!newAddress.empty()) contact.setAddress(newAddress);
std::cout << "연락처 수정 완료!" << endl;
return;
}
}
std::cout << "일치하는 연락처를 찾을 수 없습니다." << endl;
}
// 연락처 삭제
void deleteContact() {
if (contacts.empty()) {
cout << "주소록이 비어있습니다." << endl;
return;
}
string searchName;
cout << "삭제할 연락처의 이름 입력: ";
cin.ignore();
getline(cin, searchName);
bool found = false;
for (auto it = contacts.begin(); it != contacts.end(); ) {
if (it->getName() == searchName) {
it = contacts.erase(it);
found = true;
} else {
++it;
}
}
if (found) {
cout <<searchName<< "연락처 삭제 완료!" << endl;
} else {
cout << "일치하는 연락처를 찾을 수 없습니다." << endl;
}
}
// 전체 연락처 출력
void displayAllContacts() {
if (contacts.empty()) {
cout << "주소록이 비어있습니다." << endl;
return;
}
cout << "=== 전체 연락처 ===\n";
for (const auto& contact : contacts) {
contact.displayContact();
cout << "------------------------\n";
}
}
// 메뉴
void run() {
while (true) {
cout << "\n=== 주소록 관리 프로그램 ===\n"
<< "1. 연락처 추가\n"
<< "2. 연락처 검색\n"
<< "3. 연락처 수정\n"
<< "4. 연락처 삭제\n"
<< "5. 전체 연락처 보기\n"
<< "6. 종료\n"
<< "선택 > ";
int choice;
cin >> choice;
switch (choice) {
case 1: addContact(); break;
case 2: searchContact(); break;
case 3: editContact(); break;
case 4: deleteContact(); break;
case 5: displayAllContacts(); break;
case 6:
std::cout << "프로그램을 종료합니다." << endl;
return;
default:
cout << "잘못 선택하셨습니다." << endl;
}
}
}
};
int main() {
AddressBook addressBook;
addressBook.run();
return 0;
}