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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
| #include <iostream>
#include<ctime>
#include<fstream>
class PhoneBook
{
private:
char* firstName;
char* lastName;
char* additionalInformation;
int homePhone;
int workPhone;
static int standartLenght;
public:
PhoneBook(char* firstName = nullptr, char* lastName = nullptr, char* additionalInformation = nullptr, int homePhone = 0, int workPhone = 0) : firstName{ new char[standartLenght] }, lastName{ new char[standartLenght] }, additionalInformation{ new char[standartLenght] }, homePhone{ 0 }, workPhone{ 0 } {
if (firstName != nullptr and lastName != nullptr and additionalInformation != nullptr)
{
this->firstName = new char[strlen(firstName)];
for (int i = 0; firstName[i] != '\0'; i++)
{
this->firstName[i] = firstName[i];
}
this->firstName[strlen(firstName)] = '\0';
this->lastName = new char[strlen(lastName)];
for (int i = 0; lastName[i] != '\0'; i++)
{
this->lastName[i] = lastName[i];
}
this->lastName[strlen(lastName)] = '\0';
this->additionalInformation = new char[strlen(additionalInformation)];
for (int i = 0; additionalInformation[i] != '\0'; i++)
{
this->additionalInformation[i] = additionalInformation[i];
}
this->additionalInformation[strlen(additionalInformation)] = '\0';
this->homePhone = homePhone;
this->workPhone = workPhone;
}
else
{
this->firstName = nullptr;
this->lastName = nullptr;
this->additionalInformation = nullptr;
} }; //Construct
PhoneBook(const PhoneBook& objectPhoneBook) {
PhoneBook(objectPhoneBook.firstName, objectPhoneBook.lastName, objectPhoneBook.additionalInformation, objectPhoneBook.homePhone, objectPhoneBook.workPhone);
}//Construct 2
~PhoneBook() { delete[] firstName; delete[] lastName; delete[] additionalInformation; homePhone = 0; workPhone = 0; };//Destruct
//Set
void setFirstName(char* firstName)
{
if (this->firstName != nullptr) { delete[] this->firstName; }
this->firstName = new char[strlen(firstName)];
for (int i = 0; firstName[i]!='\0'; i++)
{
this->firstName[i] = firstName[i];
}
this->firstName[strlen(firstName)] = '\0';
}
void setLastName(char* lastName)
{
if (this->lastName != nullptr) { delete[] this->lastName; }
this->lastName = new char[strlen(lastName)];
for (int i = 0; lastName[i] != '\0'; i++)
{
this->lastName[i] = lastName[i];
}
this->lastName[strlen(lastName)] = '\0';
}
void setAdditionalInformation(char* additionalInformation)
{
if (this->additionalInformation != nullptr) { delete[] this->additionalInformation; }
this->additionalInformation = new char[strlen(additionalInformation)];
for (int i = 0; additionalInformation[i] != '\0'; i++)
{
this->additionalInformation[i] = additionalInformation[i];
}
this->additionalInformation[strlen(additionalInformation)] = '\0';
}
void setHomePhone(int homePhone)
{
this->homePhone = homePhone;
}
void setWorkPhone(int workPhone)
{
this->workPhone = workPhone;
}
void enterAllContact()
{
char* firstNameOnce = new char[100];
char* lastNameOnce = new char[100];
char* additionalInformationOnce = new char[100];
std::cout << "Enter first name = ";
std::cin >> firstNameOnce;
std::cout << "Enter last name = ";
std::cin >> lastNameOnce;
std::cout << "Enter additional information = ";
std::cin.getline(additionalInformationOnce, 100);
std::cin.ignore(1000, '\n');
std::cout << "Enter home phone = ";
std::cin >> homePhone;
std::cout << "Enter work phone = ";
std::cin >> workPhone;
firstName = firstNameOnce;
lastName = lastNameOnce;
additionalInformation = additionalInformationOnce;
std::cout << "Complete\n";
}
//Get
void showFirstName()
{
if (firstName != nullptr)
{
std::cout <<"Name = "<< firstName << " ";
}
else { std::cout << "Name = " << "NONE" << " "; }
}
void showLastName() {
if (lastName != nullptr)
{
std::cout << lastName << "\n";
}
else { std::cout << "NONE\n"; }
}
void showAdditionaInformation()
{
if (additionalInformation != nullptr)
{
std::cout << "Additional information = "<< additionalInformation << "\n";
}
else { std::cout << "Additional information = " << "NONE\n"; }
}
void showHomePhone()
{
std::cout <<"Home phone = "<< homePhone<<"\n";
}
void showWorkPhone()
{
std::cout <<"Work phone = "<< workPhone<<"\n";
}
//File
void putInFile(int id,std::ofstream* file)
{
if (firstName != nullptr)
{
*file << firstName << "\n";
}
else { *file<< "NONE" << "\n"; }
if (lastName != nullptr)
{
*file<< lastName << "\n";
}
else { *file<< "NONE\n"; }
if (additionalInformation != nullptr)
{
*file<< additionalInformation << "\n";
}
else { *file<< "NONE\n"; }
*file << homePhone << "\n";
*file << workPhone << "\n";
}
//Copy
static void copyTwoObjects(PhoneBook& objectOut, PhoneBook& objectIn, int countOfObject);
};
//Static
int PhoneBook::standartLenght{ 20 };
// Copy obj1 to obj2
void PhoneBook::copyTwoObjects(PhoneBook& objectOut, PhoneBook& objectIn, int countOfObject)
{
objectIn.homePhone = objectOut.homePhone;
objectIn.workPhone = objectOut.workPhone;
if (objectOut.firstName != nullptr) {
int lenghtFirstName = strlen(objectOut.firstName);
objectIn.firstName = new char[lenghtFirstName];
for (int i = 0; objectOut.firstName[i] != '\0'; i++)
{
objectIn.firstName[i] = objectOut.firstName[i];
}
objectIn.firstName[strlen(objectOut.firstName)] = '\0';
}
else
{
objectIn.firstName = nullptr;
}
if (objectOut.lastName != nullptr) {
int lenghtLastName = strlen(objectOut.lastName);
objectIn.lastName = new char[lenghtLastName];
for (int i = 0; objectOut.lastName[i] != '\0'; i++)
{
objectIn.lastName[i] = objectOut.lastName[i];
}
objectIn.lastName[strlen(objectOut.lastName)] = '\0';
}
else
{
objectIn.lastName = nullptr;
}
if (objectOut.additionalInformation != nullptr) {
int lenghtAdditionalInformation = strlen(objectOut.additionalInformation);
objectIn.additionalInformation = new char[lenghtAdditionalInformation];
for (int i = 0; objectOut.additionalInformation[i] != '\0'; i++)
{
objectIn.additionalInformation[i] = objectOut.additionalInformation[i];
}
objectIn.additionalInformation[strlen(objectOut.additionalInformation)] = '\0';
}
else
{
objectIn.additionalInformation = nullptr;
}
}
//Menu of program
void menu()
{
std::cout << "\t\tMenu\n";
std::cout << "1) Create new contact\n2) Edit contact\n3) Show all list\n4) Delete contact\n5) Show full contact\n6) Put in file\n7) Exit\n Choice = ";
}
void main()
{
int choice;
std::cout << "You want to import contacts? 1- Yes, 0- No. Choice = ";
std::cin >> choice;
std::ifstream file("logs.txt");
int countOfMassive = 1;
PhoneBook* phoneBookMassive;
if (choice == 1)
{
char buff[50];
file.getline(buff, 50);
countOfMassive = atoi(buff);
phoneBookMassive = new PhoneBook[countOfMassive];
for (int id = 0; id < countOfMassive; id++)
{
file.getline(buff, 50);
phoneBookMassive[id].setFirstName(buff);
file.getline(buff, 50);
phoneBookMassive[id].setLastName(buff);
file.getline(buff, 50);
phoneBookMassive[id].setAdditionalInformation(buff);
file.getline(buff, 50);
int homePhone = atoi(buff);
phoneBookMassive[id].setHomePhone(homePhone);
file.getline(buff, 50);
int workPhone = atoi(buff);
phoneBookMassive[id].setWorkPhone(workPhone);
}
}
else
{
phoneBookMassive = new PhoneBook[countOfMassive];
}
file.close();
do
{
menu();
std::cin >> choice;
//Create new contact
if (choice == 1)
{
PhoneBook* phoneBookCopy = phoneBookMassive;
countOfMassive++;
phoneBookMassive = new PhoneBook[countOfMassive];
for (int i = 0; i < countOfMassive-1; i++)
{
phoneBookMassive[0].copyTwoObjects(phoneBookCopy[i], phoneBookMassive[i], countOfMassive);
}
int select;
std::cout << "You want to fill contact or no?(1- Yes, 0- No) = ";
std::cin >> select;
if (select == 1)
{
phoneBookMassive[countOfMassive-1].enterAllContact();
}
}
//Edit contact
else if (choice == 2)
{
int choiceEdit, idObject;
std::cout << "1) Set first name\n2) Set last name\n3) Set additional information\n4) Set work phone\n 5) Set home phone\nChoice = ";
std::cin >> choiceEdit;
std::cout << "Enter id of object = ";
std::cin >> idObject;
if (choiceEdit == 1)
{
char* firstNameOnce = new char[100];
std::cout << "Enter first name = ";
std::cin >> firstNameOnce;
phoneBookMassive[idObject].setFirstName(firstNameOnce);
std::cout << "Complete\n";
}
else if (choiceEdit == 2)
{
char* lastNameOnce = new char[100];
std::cout << "Enter last name = ";
std::cin >> lastNameOnce;
phoneBookMassive[idObject].setLastName(lastNameOnce);
std::cout << "Complete\n";
}
else if (choiceEdit == 3)
{
char* addInf = new char[100];
std::cout << "Enter last name = ";
std::cin >> addInf;
phoneBookMassive[idObject].setAdditionalInformation(addInf);
std::cout << "Complete\n";
}
else if (choiceEdit == 4)
{
int workPhone;
std::cout << "Enter work phone = ";
std::cin >> workPhone;
phoneBookMassive[idObject].setWorkPhone(workPhone);
std::cout << "Complete\n";
}
else if (choiceEdit == 5)
{
int homePhone;
std::cout << "Enter home phone = ";
std::cin >> homePhone;
phoneBookMassive[idObject].setWorkPhone(homePhone);
std::cout << "Complete\n";
}
}
//Show all list
else if (choice == 3)
{
for (int i = 0; i < countOfMassive; i++)
{
std::cout << "ID = " << i << ". Name = "; phoneBookMassive[i].showFirstName(); std::cout << " "; phoneBookMassive[i].showLastName(); std::cout << "\n";
}
}
//Delete contact
else if (choice == 4)
{
int idObject;
std::cout << "Enter id object = ";
std::cin >> idObject;
PhoneBook* phoneBookCopy = phoneBookMassive;
countOfMassive--;
phoneBookMassive = new PhoneBook[countOfMassive];
for (int i = 0; i < countOfMassive; i++)
{
phoneBookMassive[0].copyTwoObjects(phoneBookCopy[i<idObject?i+0:i+1], phoneBookMassive[i], countOfMassive);
}
}
//Show full contact
else if (choice == 5)
{
int idObject;
std::cout << "Enter id object = ";
std::cin >> idObject;
phoneBookMassive[idObject].showFirstName();
phoneBookMassive[idObject].showLastName();
phoneBookMassive[idObject].showAdditionaInformation();
phoneBookMassive[idObject].showHomePhone();
phoneBookMassive[idObject].showWorkPhone();
}
//Put in file
else if (choice == 6){
std::ofstream file("logs.txt");
file << countOfMassive << "\n";
for (int i = 0; i < countOfMassive; i++)
{
phoneBookMassive[i].putInFile(i,&file);
}
file.close();
}
//Exit
else if (choice == 7)
{
std::cout << "Bye";
}
} while (choice != 7);
delete[] phoneBookMassive;
} |