-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonList.cpp
More file actions
130 lines (111 loc) · 3.29 KB
/
PersonList.cpp
File metadata and controls
130 lines (111 loc) · 3.29 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
#include "PersonList.h"
#include <cstring>
void deletePersonArray(Person** personList, const int noe);
PersonList::PersonList(){
this->noe = 0;
this->personList = nullptr;
}
PersonList::PersonList(const std::initializer_list<const Person> &personList) {
this->noe = 0;
for(int i = 0; i < (int) personList.size(); i++){
this->copyMemory(1);
this->personList[i] = new Person(*(personList.begin() + i));
this->noe += 1;
}
}
PersonList::~PersonList(){
deletePersonArray(this->personList, this->noe);
}
PersonList::PersonList(const PersonList &p){
this->noe = p.noe;
this->personList = new Person*[p.noe];
for(int i = 0; i < p.noe; i++){
this->personList[i] = new Person(*(p.personList[i]));
}
}
void PersonList::add(const Person &p){
this->copyMemory(1);
this->personList[this->noe] = new Person(p);
this->noe += 1;
}
void PersonList::removeAt(int index){
if(!inRange(index)) return;
if(this->noe == 1){
delete this->personList[this->noe - 1];
delete [] this->personList;
this->noe = 0;
return;
}
for(int i = index; i < this->noe - 1; i++){
delete this->personList[i];
this->personList[i] = new Person(*(this->personList[i + 1]));
}
delete this->personList[this->noe - 1];
this->noe -= 1;
this->copyMemory();
}
Person* PersonList::getPersonbyIndex(int index) const{
if(!this->inRange(index)) return NULL;
return new Person(*(this->personList[index]));
}
PersonList PersonList::getPersonListFromWithZIP(const char* fromStr) const{
PersonList temp;
for(int i = 0; i < this->noe; i++){
if(strcmp(this->personList[i]->getAddress().getZip(), fromStr) >= 0){
temp.add(*(this->personList[i]));
}
}
return temp;
}
void PersonList::show() const {
std::cout << "PersonList: [\n";
for(int i = 0; i < this->noe; i++){
std::cout << i << " ";
this->personList[i]->show();
if(i != this->noe - 1) std::cout << ",";
std::cout << "\n";
}
std::cout << "]";
}
void PersonList::copyMemory(int addValue){
if(!this->noe){
this->personList = new Person*[1];
return;
}
Person* *temp = new Person*[this->noe + addValue];
for(int i = 0; i < this->noe; i++){
temp[i] = new Person(*(this->personList[i]));
}
deletePersonArray(this->personList, this->noe);
this->personList = temp;
}
PersonList& PersonList::operator=(const PersonList &rhs){
if(this == &rhs) return *this;
deletePersonArray(this->personList, this->noe);
this->noe = rhs.noe;
this->personList = new Person*[rhs.noe];
for(int i = 0; i < rhs.noe; i++){
this->personList[i] = new Person(*(rhs.personList[i]));
}
return *this;
}
std::ostream& operator<<(std::ostream& os, const PersonList &rhs){
os << "PersonList: [\n";
for(int i = 0; i < rhs.noe; i++){
os << i << " ";
rhs.personList[i]->show();
if(i != rhs.noe - 1) os << ",";
os << "\n";
}
os << "]";
return os;
}
void deletePersonArray(Person** personList, const int noe) {
for(int i = 0; i < noe; i++){
delete personList[i];
}
delete [] personList;
}
bool PersonList::inRange(int index) const{
return !(index < 0 || index > this->noe - 1);
}