-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoneBook.cpp
88 lines (75 loc) · 1.85 KB
/
phoneBook.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
#include "phoneBook.hpp"
#include <stdexcept>
void PhoneBook::showRecord(PhoneBook::iterator it, std::ostream &out) const
{
if (isEmpty())
{
throw std::logic_error("PhoneBook is empty");
}
out << it->number_ << " " << it->name_ << "\n";
}
void PhoneBook::insertBefore(std::list<PhoneBook::record_t>::iterator it, const PhoneBook::record_t &record)
{
if (record.name_.empty() || record.number_.empty())
{
throw std::invalid_argument("Name or Number is Empty");
}
phoneBook_.insert(it, record);
}
void PhoneBook::insertAfter(PhoneBook::iterator it, const PhoneBook::record_t &record)
{
if (record.name_.empty() || record.number_.empty())
{
throw std::invalid_argument("Name or Number is Empty");
}
it++;
phoneBook_.insert(it, record);
}
void PhoneBook::replaceRecord(PhoneBook::iterator it, const PhoneBook::record_t &record)
{
if (record.name_.empty() || record.number_.empty())
{
throw std::invalid_argument("Name or Number is Empty");
}
if (isEmpty())
{
throw std::logic_error("PhoneBook is empty");
}
phoneBook_.insert(phoneBook_.erase(it), record);
}
void PhoneBook::add(const PhoneBook::record_t &record)
{
if (record.name_.empty() || record.number_.empty())
{
throw std::invalid_argument("Name or Number is Empty");
}
phoneBook_.push_back(record);
}
bool PhoneBook::isEmpty() const
{
return phoneBook_.empty();
}
PhoneBook::iterator PhoneBook::erase(PhoneBook::iterator it)
{
if (!isEmpty())
{
return phoneBook_.erase(it);
}
throw std::logic_error("Deleting from an empty phoneBook");
}
PhoneBook::iterator PhoneBook::begin()
{
return phoneBook_.begin();
}
PhoneBook::iterator PhoneBook::end()
{
return phoneBook_.end();
}
PhoneBook::const_iterator PhoneBook::begin() const
{
return phoneBook_.cbegin();
}
PhoneBook::const_iterator PhoneBook::end() const
{
return phoneBook_.cend();
}