-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontectManager.cpp
More file actions
182 lines (161 loc) · 4.46 KB
/
contectManager.cpp
File metadata and controls
182 lines (161 loc) · 4.46 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
ofstream dataFileOut;
ifstream dataFileIn;
vector<vector<string>> data;
void collectData(){
string line;
string str;
int dataIndex = 0;
vector<string> vecTemp;
dataFileIn.open("contactManager.txt");
if(dataFileIn.is_open()){
cout<<"Open file successfully!"<<endl;
string str;
while(getline(dataFileIn, line)){
stringstream input(line);
data.push_back(vecTemp);
while(input>>str){
data[dataIndex].push_back(str);
}
dataIndex++;
}
} else {
cout<<"Failed to open file!"<<endl;
}
dataFileIn.close();
}
void createContactManagerTxt(){
vector<string> vecTemp;
vecTemp.push_back("Name");
vecTemp.push_back("PhoneNo.");
vecTemp.push_back("Email");
vecTemp.push_back("favorite");
data.push_back(vecTemp);
}
void listAll(){
for(int i = 0; i < data.size(); i++){
cout<<left<<setw(10)<<data[i][0]<<left<<setw(15)<<data[i][1]<<left<<setw(25)<<data[i][2]<<left<<setw(10)<<data[i][3]<<endl;
}
}
void addMember(){
string memberName;
string memberPhoneNo;
string memberEmail;
string favorite;
vector<string> vecTemp;
cout<<"Please input Name : ";
cin>>memberName;
cout<<"Please input Phone No. : ";
cin>>memberPhoneNo;
cout<<"Please input Email : ";
cin>>memberEmail;
cout<<"Please input favorite (yes/no): ";
cin>>favorite;
vecTemp.push_back(memberName);
vecTemp.push_back(memberPhoneNo);
vecTemp.push_back(memberEmail);
vecTemp.push_back(favorite);
data.push_back(vecTemp);
}
void deleteMember(){
string memberName;
int index;
cout<<"Which member do you want to delete (Please enter the name) : ";
cin>>memberName;
for(int i = 0; i < data.size(); i++){
if(data[i][0] == memberName) {
index = i;
break;
}
}
data.erase(data.begin()+index);
}
void EditMember(){
string memberName;
string modified;
int item;
int index;
cout<<"Which member do you want to edit (Please enter the name) : ";
cin>>memberName;
for(int i = 0; i < data.size(); i++){
if(data[i][0] == memberName) {
index = i;
break;
}
}
cout<<"Which item needs to be modified ?"<<endl;
cout<<"Press[1] for Phone No."<<endl;
cout<<"Press[2] for Email"<<endl;
cout<<"Press[3] for favorite"<<endl;
cin>>item;
cout<<"Modified "<<data[index][item]<<" into : ";
cin>>modified;
data[index][item] = modified;
}
void searchMember(){
string memberName;
cout<<"Which member do you want to search : ";
cin>>memberName;
for(int i = 0; i < data.size(); i++){
if(data[i][0] == memberName) {
cout<<left<<setw(10)<<data[0][0]<<left<<setw(15)<<data[0][1]<<left<<setw(25)<<data[0][2]<<left<<setw(10)<<data[0][3]<<endl;
cout<<left<<setw(10)<<data[i][0]<<left<<setw(15)<<data[i][1]<<left<<setw(25)<<data[i][2]<<left<<setw(10)<<data[i][3]<<endl;
break;
} else if(i == data.size()-1) {
cout<<"Not found"<<endl;
}
}
}
void selectFavorite(){
bool flag = false;
cout<<left<<setw(10)<<data[0][0]<<left<<setw(15)<<data[0][1]<<left<<setw(25)<<data[0][2]<<left<<setw(10)<<data[0][3]<<endl;
for(int i = 0; i < data.size(); i ++){
if(data[i][3] == "yes"){
flag = true;
cout<<left<<setw(10)<<data[i][0]<<left<<setw(15)<<data[i][1]<<left<<setw(25)<<data[i][2]<<left<<setw(10)<<data[i][3]<<endl;
}
}
if(flag == false) {
cout<<"Not found"<<endl;
}
}
void storeInTxt(){
dataFileOut.open("contactManager.txt");
for(int i = 0; i < data.size(); i++){
dataFileOut<<left<<setw(10)<<data[i][0]<<left<<setw(15)<<data[i][1]<<left<<setw(25)<<data[i][2]<<left<<setw(10)<<data[i][3]<<endl;
}
dataFileOut.close();
}
int main(){
int input = 1;
collectData();
if(data.empty()) createContactManagerTxt();
cout<<"Press[0] to save & exit"<<endl;
cout<<"Press[1] to execute listAll"<<endl;
cout<<"Press[2] to execute addMember"<<endl;
cout<<"Press[3] to execute deleteMember"<<endl;
cout<<"Press[4] to execute EditMember"<<endl;
cout<<"Press[5] to execute searchMember"<<endl;
cout<<"Press[6] to execute selectFavorite"<<endl;
while(input){
cout<<endl;
cout<<"Please press button : ";
cin>>input;
cout<<endl;
if(input == 0) break;
if(input == 1) listAll();
if(input == 2) addMember();
if(input == 3) deleteMember();
if(input == 4) EditMember();
if(input == 5) searchMember();
if(input == 6) selectFavorite();
}
storeInTxt();
return 0;
}