-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.cpp
111 lines (89 loc) · 2.25 KB
/
repository.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "repository.h"
template <typename T>
Repository<T>::Repository() {
/* Initializes repository
*/
// this->validator = new OfferValidator();
offerList.resize(0);
}
template <typename T>
Repository<T>::Repository(EntityValidator<T> *validator) {
offerList.resize(0);
this->validator = validator;
}
template <typename T>
Repository<T>::~Repository() {
// Destroy repository
//delete this;
}
template <typename T>
vector<T> Repository<T>::getAll() {
/* Get all the elemts
returns: a vector with all the elements
*/
return this->offerList;
}
template <typename T>
void Repository<T>::save(T p) throw (MyException) {
/* Save a element
param: p - add a element of type T to the repository
*/
// validator->validate(p);
offerList.push_back(p);
}
template <typename T>
void Repository<T>::insertAtPosition(int id, T p) throw(MyException) {
/* Insert a new element
param: id - position of the element
param: p - element to be saved
*/
if((unsigned) id >= offerList.size()){
throw MyException("Index error");
} else {
validator->validate(p);
offerList.insert(offerList.begin() + id, p);
}
}
template <typename T>
void Repository<T>::update(int id, T p) throw(MyException) {
/* Update the repository
param: id - position to be updated
param: p - element to replace
*/
if((unsigned)id >= offerList.size()){
throw MyException("Index error");
} else {
validator->validate(p);
offerList[id] = p;
}
}
template <typename T>
void Repository<T>::remove(int id) throw(MyException) {
/* Remove element at position
param: id - position from where to remove an element
*/
if((unsigned) id >= offerList.size()){
throw MyException("Index error");
} else {
offerList.erase(offerList.begin() + id);
}
}
template <typename T>
const T Repository<T>::findById(int id) throw (MyException) {
/* Find element at position
param: id - position of the element
returns: element at the position id
*/
if((unsigned) id >= offerList.size()){
throw MyException("Index error");
} else {
return offerList[id];
}
}
template <class T>
int Repository<T>::size() {
/* The size of the Repository
returns: the size of the repository
*/
return (int)offerList.size();
}