-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainStrategy.cpp
More file actions
184 lines (166 loc) · 4.76 KB
/
MainStrategy.cpp
File metadata and controls
184 lines (166 loc) · 4.76 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
// MainStrategy.cpp: implementation of the MainStrategy class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MainStrategy.h"
void MainStrategy::print_prsn(const Person& prt_Person, const string info, bool refresh) const{
system("cls");
cout<<endl<<info;
if(refresh) Sleep(20);
cout<<"\n\tName: "<<prt_Person.name;
if(refresh) Sleep(20);
cout<<"\n\tTel: "<<prt_Person.tel;
if(refresh) Sleep(20);
cout<<"\n\tAddress: "<<prt_Person.addr;
if(refresh) Sleep(20);
cout<<"\n\tGendle: "<<prt_Person.sex ;
if(refresh) Sleep(20);
cout<<"\n\tZip: "<<prt_Person.zip;
if(refresh) Sleep(20);
cout<<"\n\tMail: "<<prt_Person.mail;
if(refresh) Sleep(20);
cout<<"\n\tQQ: "<<prt_Person.qq;
if(refresh) Sleep(20);
cout<<"\n\tCategory: "<<prt_Person.category;
if(refresh) Sleep(20);
}
void MainStrategy::printAll() const{
int num = contact_item.size();
for (int i = 0; i < num; i++){
cout<<"("<<i+1<<"): "<<&*contact_item[i]->name<<" TEL: "<<&*contact_item[i]->tel<<" ADDR: "<<&*contact_item[i]->addr<<endl;
Sleep(20);
}
}
bool MainStrategy::delete_prsn(Person& del_Person) const{
char y_n;
string addr = ".\\contact\\";
cout<<"\n\n\t\tReally want to delete?[y/n]: ";
cin.clear();
cin.sync();
y_n = getch();
if ((y_n == 'y') || (y_n == 'Y')){
addr += del_Person.name;
addr += ".ctt";
if (remove(addr.c_str())){
cout<<"\n\t\tError removing data!\n";
return false;
}
cout<<"\n\t\tDelete Successful!\n";
return true;
}
else
cout<<"\n\t\tCanceled!\n";
return false;
}
bool MainStrategy::modify_prsn(Person& m_Person) const{
char t_c;
char y_n;
string addr = ".\\contact\\";
string t_str = "";
addr += m_Person.name;
addr += ".ctt";
Person t_info = m_Person;
system("cls");
cout<<endl<<"=====Modify Contact====================(Enter a new value at the cursor.ENTER to SKIP.)\n";
cout<<"\n When you entering Tel and QQ information, you can input '+' at the beginning to add the data to the end of the existing data.\n I will remind you later.(*)\n";
cout<<"\n\tName: "<<m_Person.name;
cout<<"\n\t*Tel: "<<m_Person.tel<<"\t:(eg:+12345) ";
cin.clear();cin.sync();
t_str = "";
while((t_c = getchar()) != '\n')
{t_str += t_c;}
if (t_str[0] == '+'){
t_str = t_str.substr(1, t_str.length());
string _t_str = t_info.tel;
_t_str += ",";
_t_str += t_str;
t_str = _t_str;
strcpy(t_info.tel,t_str.c_str());
}
else{
if (t_str != "") strcpy(t_info.tel,t_str.c_str());
if (t_str == " ") strcpy(t_info.tel,"");
}
cout<<"\tAddress: "<<m_Person.addr<<"\t: ";
cin.clear();cin.sync();
t_str = "";
while((t_c = getchar()) != '\n')
{t_str += t_c;}
if (t_str != "") strcpy(t_info.addr,t_str.c_str());
if (t_str == " ") strcpy(t_info.addr,"");
cout<<"\tGendle: "<<m_Person.sex <<"\t: ";
cin.clear();cin.sync();
t_c= getchar();
if (t_c != '\n')
t_info.sex = t_c;
if (t_c == ' ')
t_info.sex = '\0';
cout<<"\tZip: "<<m_Person.zip<<"\t: ";
cin.clear();cin.sync();
t_str = "";
while((t_c = getchar()) != '\n')
{t_str += t_c;}
if (t_str != "") strcpy(t_info.zip,t_str.c_str());
if (t_str == " ") strcpy(t_info.zip,"");
cout<<"\tMail: "<<m_Person.mail<<"\t: ";
cin.clear();cin.sync();
t_str = "";
while((t_c = getchar()) != '\n')
{t_str += t_c;}
if (t_str != "") strcpy(t_info.mail,t_str.c_str());
if (t_str == " ") strcpy(t_info.mail,"");
cout<<"\t*QQ: "<<m_Person.qq<<"\t:(eg:+12345) ";
cin.clear();cin.sync();
t_str = "";
while((t_c = getchar()) != '\n')
{t_str += t_c;}
if (t_str[0] == '+'){
t_str = t_str.substr(1, t_str.length());
string _t_str = t_info.qq;
_t_str += ",";
_t_str += t_str;
t_str = _t_str;
strcpy(t_info.qq,t_str.c_str());
}
else{
if (t_str != "") strcpy(t_info.qq,t_str.c_str());
if (t_str == " ") strcpy(t_info.qq,"");
}
cout<<"\tCategory: "<<m_Person.category<<"\t: ";
cin.clear();cin.sync();
t_str = "";
while((t_c = getchar()) != '\n')
{t_str += t_c;}
if (t_str != "") strcpy(t_info.category,t_str.c_str());
if (t_str == " ") strcpy(t_info.category,"");
this->print_prsn(t_info,"=====Modify Contact====================\n",true);
cout<<"\n\n\t\tReally want to modify?[y/n]: ";
cin.clear();
cin.sync();
y_n = getch();
if ((y_n == 'y') || (y_n == 'Y')){
if(!check(t_info, false) ){
cout<<"\n\t\tInfomation Error!";
return false;
}
else{
FILE *fp = fopen(addr.c_str(),"wb+");
if (fp == NULL ){
cout<<"\n\t\tError opening file!\n";
return false;
}
else
if (fwrite(&t_info,sizeof(Person), 1, fp) != 1){
cout<<"\n\t\tError writing to file!\n";
return false;
}
else{
cout<<"\n\t\tModify succeed!\n";
fclose(fp);
}
}
}
else
cout<<"\n\t\tCanceled!\n";
return true;
}