-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.h
150 lines (122 loc) · 3.02 KB
/
repository.h
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
#ifndef __Contest_CPP__Repository__
#define __Contest_CPP__Repository__
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include "domain.h"
#include "EntityValidator.h"
#include "MyException.h"
#include "AbstractRepository.h"
#include "validator.h"
#include <memory>
using namespace std;
template <typename T>
class Repository : public AbstractRepository<T> {
vector<T> offerList;
EntityValidator<T>* validator;
public:
virtual ~Repository();
Repository();
Repository(EntityValidator<T>* validator);
vector<T> getAll();
virtual void save(T p) throw (MyException);
virtual void insertAtPosition(int id, T p) throw (MyException);
virtual void update(int id, T p) throw (MyException);
virtual void remove(int id) throw (MyException);
const T findById(int id) throw (MyException);
int size();
};
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();
}
#endif /* defined(__Contest_CPP__Repository__) */