-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddressbook.cpp
101 lines (87 loc) · 2.06 KB
/
addressbook.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
#include <vector>
#include <fstream>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
struct Employee {
std::string name;
std::string phone;
std::string email;
};
class Addressbook {
private:
std::vector<Employee> list;
public:
Addressbook() = default ;
void insert();
void show();
void save();
void read();
void clear();
};
int main() {
char op = 0;
Addressbook myAddressbook;
while (op != 'q') {
// cout << endl;
cin >> op;
switch (op) {
case 'i':
myAddressbook.insert();
break;
case 'l':
myAddressbook.show();
break;
case 's':
myAddressbook.save();
break;
case 'o':
myAddressbook.read();
break;
case 'c':
myAddressbook.clear();
break;
}
}
return 0;
}
void Addressbook::insert() {
Employee peopleToInsert;
cout << "姓名: ";
cin >> peopleToInsert.name;
cin >> peopleToInsert.phone;
cin >> peopleToInsert.email;
list.push_back(peopleToInsert);
}
void Addressbook::show() {
for (Employee & employee : list) {
cout << "姓名: " << employee.name << endl;
cout << "電話: " << employee.phone << endl;
cout << "Email: " << employee.email << endl;
}
}
void Addressbook::save() {
std::string savefile;
std::cin >> savefile;
std::fstream in{savefile, std::ios::out};
for (Employee & employee : list) {
in << employee.name << "\n"
<< employee.phone << "\n"
<< employee.email << "\n";
}
}
void Addressbook::read() {
std::string restorefile;
cin >> restorefile;
std::fstream in{restorefile, std::ios::in};
list.clear();
Employee new_employee;
while (in >> new_employee.name >> new_employee.phone >> new_employee.email) {
list.push_back(new_employee);
}
}
void Addressbook::clear() {
list.clear();
}