-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathContacts.cpp
More file actions
193 lines (179 loc) · 6.83 KB
/
Contacts.cpp
File metadata and controls
193 lines (179 loc) · 6.83 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
#include "Contacts.h"
Date::Date(uint day, uint month, uint year) : day(day), month(month), year(year) {}
Date::Date(const Date& other) : day(other.day), month(other.month), year(other.year) {}
Date::~Date() {}
void Date::SetDay(uint day) { this->day = day; }
void Date::SetMonth(uint month) { this->month = month; }
void Date::SetYear(uint year) { this->year = year; }
uint Date::GetDay() const { return day; }
uint Date::GetMonth() const { return month; }
uint Date::GetYear() const { return year; }
Date& Date::operator=(const Date& other)
{
this->day = other.day;
this->month = other.month;
this->year = other.year;
return *this;
}
std::istream& operator>>(std::istream& in, Date& date)
{
char c;
in >> date.day >> c >> date.month >> c >> date.year;
return in;
}
std::ostream& operator<<(std::ostream& out, const Date& date)
{
out << date.day << "." << date.month << "." << date.year;
return out;
}
//--------------------------------------------------------------------------------
Human::Human(const std::string& first_name,
const std::string& second_name,
const std::string& last_name,
const Date& brth) : first_name(first_name), second_name(second_name), last_name(last_name), brth(brth) {}
Human::Human(const Human& other) : first_name(other.first_name), second_name(other.second_name), last_name(other.last_name), brth(other.brth) {}
Human::~Human() {}
void Human::SetFName(const std::string& first_name) { this->first_name = first_name; }
void Human::SetSName(const std::string& second_name) { this->second_name = second_name; }
void Human::SetLName(const std::string& last_name) { this->last_name = last_name; }
void Human::SetBrth(const Date& brth) { this->brth = brth; }
std::string Human::GetFName() const { return first_name; }
std::string Human::GetSName() const { return second_name; }
std::string Human::GetLName() const { return last_name; }
Date Human::GetBrth() const { return brth; }
Human& Human::operator=(const Human& other)
{
this->first_name = other.first_name;
this->second_name = other.second_name;
this->last_name = other.last_name;
this->brth = other.brth;
return *this;
}
std::istream& operator>>(std::istream& in, Human& human)
{
in >> human.first_name >> human.second_name >> human.last_name >> human.brth;
return in;
}
std::ostream& operator<<(std::ostream& out, const Human& human)
{
out << human.first_name << " " << human.second_name << " " << human.last_name << " " << human.brth;
return out;
}
//----------------------------------------------------------------------------------------------------
Contact::Contact(const std::string& first_name,
const std::string& second_name,
const std::string& last_name,
const Date& brth,
const ll& phone) : Human(first_name, second_name, last_name, brth), phone(phone) {}
Contact::Contact(const Contact& other) : Human(other.first_name, other.second_name, other.last_name, other.brth), phone(other.phone) {}
Contact::~Contact() {}
void Contact::SetPhone(const ll& phone) { this->phone = phone; }
ll Contact::GetPhone() const { return phone; }
Contact& Contact::operator=(const Contact& other)
{
this->first_name = other.first_name;
this->second_name = other.second_name;
this->last_name = other.last_name;
this->brth = other.brth;
this->phone = other.phone;
return *this;
}
std::istream& operator>>(std::istream& in, Contact& contact)
{
in >> contact.first_name >> contact.second_name >> contact.last_name >> contact.brth >> contact.phone;
return in;
}
std::ostream& operator<<(std::ostream& out, const Contact& contact)
{
out << contact.first_name << " " << contact.second_name << " " << contact.last_name << " " << contact.brth << " " << contact.phone;
return out;
}
//---------------------------------------------------------------------------------------------------------
Contacts::Contacts() {}
Contacts::Contacts(const Contacts& other)
{
for (auto contact : other.contacts)
this->contacts.push_back(contact);
for (auto contact : other.favorites)
this->favorites.push_back(contact);
}
Contacts::~Contacts() {}
void Contacts::Save(const std::string& path)
{
std::fstream file;
file.open(path, std::fstream::out);
if (file.is_open())
for (auto contact : contacts)
file << contact << std::endl;
file.close();
}
void Contacts::Read(const std::string& path)
{
std::fstream file;
file.open(path, std::fstream::in);
if (file.is_open())
{
contacts.clear(); favorites.clear();
while (!file.eof())
{
Contact contact;
file >> contact;
contacts.push_back(contact);
}
contacts.pop_back();
}
file.close();
}
void Contacts::Append(const Contact& contact) { contacts.push_back(contact); }
Contact Contacts::Find(const std::string& first_name, const std::string& second_name, const std::string& last_name)
{
for (auto contact : contacts)
if (contact.GetFName() == first_name && contact.GetSName() == second_name && contact.GetLName() == last_name)
return contact;
throw std::exception("Êîíòàêòà ñ òàêèì ÔÈÎ â ñïèñêå íåò");
}
void Contacts::Change(const std::string& first_name, const std::string& second_name, const std::string& last_name, const Contact& new_contact)
{
for (std::vector<Contact>::iterator i = contacts.begin(); i < contacts.end(); i++)
if (i->GetFName() == first_name && i->GetSName() == second_name && i->GetLName() == last_name)
{
*i = new_contact;
return;
}
throw std::exception("Êîíòàêòà ñ òàêèì ÔÈÎ â ñïèñêå íåò");
}
Contact Contacts::Find(const ll& phone)
{
for (auto contact : contacts)
if (contact.GetPhone() == phone)
return contact;
throw std::exception("Êîíòàêòà ñ òàêèì ÔÈÎ â ñïèñêå íåò");
}
std::vector<Contact> Contacts::FirstSymbol(char c)
{
std::vector<Contact> res;
for (auto contact : contacts)
if (contact.GetLName()[0] == c)
res.push_back(contact);
return res;
}
size_t Contacts::Size() { return contacts.size(); }
void Contacts::AppendToFavorites(const std::string& first_name, const std::string& second_name, const std::string& last_name)
{
for (std::vector<Contact>::iterator i = contacts.begin(); i < contacts.end(); i++)
if (i->GetFName() == first_name && i->GetSName() == second_name && i->GetLName() == last_name)
favorites.push_back(&(*i));
}
void Contacts::DeleteFromFavorites(const std::string& first_name, const std::string& second_name, const std::string& last_name)
{
for (std::vector<Contact*>::iterator i = favorites.begin(); i < favorites.end(); i++)
if ((*i)->GetFName() == first_name && (*i)->GetSName() == second_name && (*i)->GetLName() == last_name)
favorites.erase(i);
}
std::vector<Contact*> Contacts::GetFavorites() { return favorites; }
void Contacts::Delete(const ll& phone)
{
for (std::vector<Contact>::iterator i = contacts.begin(); i < contacts.end(); i++)
if (i->GetPhone() == phone)
contacts.erase(i);
}