-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFarmacia.cpp
329 lines (283 loc) · 8.34 KB
/
Farmacia.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "Farmacia.h"
#include <iostream>
using namespace std;
template <> enum tipoSort sorting<Venda>::tipo = DEFAULT;
template <> bool sorting<Venda>::crescente = false;
Farmacia::Farmacia(string nome, string morada) {
this->nome = nome;
this->morada = morada;
this->gerente = NULL;
this->diretorTecnico = NULL;
this->produtosVender.clear();
}
string Farmacia::getNome() const {
return nome;
}
string Farmacia::getMorada() const {
return morada;
}
Funcionario* Farmacia::getGerente() const {
return gerente;
}
Funcionario* Farmacia::getDiretorTecnico() const{
return diretorTecnico;
}
vector<Venda*> Farmacia::getVendas() const {
return vendas;
}
map<Produto, int> Farmacia::getProdutosVender() const {
return produtosVender;
}
unsigned int Farmacia::getNumProdutos() const {
return produtosVender.size();
}
unsigned int Farmacia::getNumVendas() const {
return vendas.size();
}
unsigned int Farmacia::getNumVendasTempo(Data d1, Data d2) const {
unsigned int vendas_count = 0;
for (size_t i = 0; i < vendas.size(); i++) {
Data d = vendas.at(i)->getData();
if ((d<d2 && d1<d) || d1 == d || d2 == d) {
vendas_count++;
}
}
return vendas_count;
}
vector<Venda*> Farmacia::getVendasTempo(Data d1, Data d2) const {
vector<Venda*> vendas_tempo;
for (size_t i = 0; i < vendas.size(); i++) {
Data d = vendas.at(i)->getData();
if ((d<d2 && d1<d) || d1 == d || d2 == d) {
vendas_tempo.push_back(vendas.at(i));
}
}
return vendas_tempo;
}
unsigned int Farmacia::getNumVendasDia(Data d) const {
unsigned int vendas_count = 0;
for (size_t i = 0; i < vendas.size(); i++) {
Data d1 = vendas.at(i)->getData();
if (d == d1)
vendas_count++;
}
return vendas_count;
}
vector <Venda*> Farmacia::getVendasDia(Data d) const {
vector<Venda*> vendas_dia;
for (size_t i = 0; i < vendas.size(); i++) {
Data d1 = vendas.at(i)->getData();
if (d1 == d) {
vendas_dia.push_back(vendas.at(i));
}
}
return vendas_dia;
}
float Farmacia::getPrecoProduto(string nomeProd) const {
map<Produto, int>::const_iterator it;
for (it = produtosVender.begin(); it != produtosVender.end(); it++) {
if (it->first.getNome() == nomeProd)
return it->first.getPreco();
}
return -1;
}
int Farmacia::getQuantProduto(std::string nomeProd) const{
map<Produto, int>::const_iterator it;
for (it = produtosVender.begin(); it != produtosVender.end(); it++) {
if (it->first.getNome() == nomeProd)
return it->second;
}
return -1;
}
void Farmacia::setGerente(Funcionario* gerente) {
this->gerente=gerente;
if(gerente != NULL)
gerente->setCargo("GERENTE");
}
void Farmacia::setDiretorTecnico(Funcionario* diretor){
this->diretorTecnico=diretor;
if(diretor != NULL)
diretor->setCargo("DIRETOR TECNICO");
}
void Farmacia::addProdutosVender(vector<Produto*> produtosVender_new) {
Produto* p = NULL;
for (size_t i = 0; i < produtosVender_new.size(); i++) {
p = produtosVender_new.at(i);
if (!existeProduto(p->getNome()))
produtosVender[*p] = 0;
}
}
const Produto* Farmacia::removeProduto(std::string nomeP){
map<Produto, int>::iterator it = produtosVender.begin();
for(; it != produtosVender.end(); it++){
if((*it).first.getNome() == nomeP){
produtosVender.erase(it);
const Produto* p = &(it->first);
return p;
}
}
throw ProdutoInexistente(nomeP);
}
bool Farmacia::addVenda(Venda* venda) {
std::map<Produto, std::vector<float>> prodVenda;
prodVenda = venda->getProdutosVendidos();
map<Produto, vector<float>>::const_iterator it = prodVenda.begin();
string nomeProd;
int quant;
for (; it != prodVenda.end(); it++){
nomeProd = it->first.getNome();
quant = static_cast<int>(it->second.at(QUANTIDADE));
if (!existeProdutoQuant(nomeProd, quant)){
return false;
}
}
vendas.push_back(venda);
for (; it != prodVenda.end(); it++){
nomeProd = it->first.getNome();
quant = getQuantProduto(nomeProd) - static_cast<int>(it->second.at(QUANTIDADE));
setQuantidade(nomeProd, quant);
}
Cliente *c = venda->getCliente();
if(c != NULL)
c->addCompra(venda);
return true;
}
bool Farmacia::existeProduto(string nomeProduto) const {
map<Produto, int>::const_iterator it = produtosVender.begin();
for (; it != produtosVender.end(); it++) {
if (it->first.getNome() == toupperstring(nomeProduto))
return true;
}
return false;
}
bool Farmacia::existeProdutoQuant(string nomeProduto, int quant) const{
map<Produto, int>::const_iterator it = produtosVender.begin();
for (; it != produtosVender.end(); it++) {
if (it->first.getNome() == toupperstring(nomeProduto)){
if (it->second>=quant)
return true;
else return false;
}
}
return false;
}
bool Farmacia::addProdutoVender(Produto* produtoVender) {
if (!existeProduto(produtoVender->getNome())) {
produtosVender[*produtoVender]= 0;
return true;
}
return false;
}
void Farmacia::sortVendas(enum tipoSort tipo, bool crescente){
sorting<Venda> s;
sorting<Venda>::tipo = tipo;
sorting<Venda>::crescente = crescente;
s.sort_p(vendas);
}
bool Farmacia::setQuantidade(std::string nomeProd, int quant) {
map<Produto, int>::iterator it;
for (it = produtosVender.begin(); it != produtosVender.end(); it++) {
if (nomeProd == it->first.getNome()) {
it->second = quant;
return true;
}
}
return false;
}
bool Farmacia::operator< (const Farmacia &f1) const{
if (nome < f1.getNome())
return true;
if (nome == f1.getNome() && produtosVender.size() > f1.getNumProdutos())
return true;
if (nome == f1.getNome() && produtosVender.size() == f1.getNumProdutos() && vendas.size() < f1.getNumVendas())
return true;
return false;
}
void Farmacia::imprimeFatura(Venda* v) const{
cout << "Farmacia " << nome << endl;
cout << morada << endl;
if(gerente != NULL)
cout << endl << "Gerente: " << gerente->getNome() << endl;
if(gerente != NULL)
cout << "Diretor Tecnico: " << diretorTecnico->getNome() << endl;
v->imprimeFatura();
cout << "Obrigado pela sua visita! Volte sempre." << endl << endl;
}
void Farmacia::imprimeDados() const {
cout << "Nome da farmacia: " << nome << endl;
cout << "Morada: " << morada << endl;
if (gerente != NULL)
cout << "Gerente: " << gerente->getNome() << " " << gerente->getNoContribuinte() << endl;
if (diretorTecnico != NULL)
cout << "Diretor Tecnico: " << diretorTecnico->getNome() << " " << diretorTecnico->getNoContribuinte() << endl;
}
bool Farmacia::menorQue(const Farmacia &f1, enum tipoSort tipo, bool crescente) const{
switch(tipo){
case NOME:
if (crescente)
return nome < f1.getNome();
else
return nome > f1.getNome();
break;
case NUM_PROD:
if (crescente)
return produtosVender.size() < f1.getNumProdutos();
else
return produtosVender.size() > f1.getNumProdutos();
break;
case NUM_VENDA:
if (crescente)
return vendas.size() < f1.getNumVendas();
else
return vendas.size() > f1.getNumVendas();
break;
default:
return (*this)<f1;
break;
}
}
std::ostream& operator<<(std::ostream &output, const Farmacia &f){
output << f.nome << endl;
output << f.morada << endl;
if(f.gerente!=NULL){
output << f.gerente->getNoContribuinte()<< endl;
}
else
output << "NULL" << endl;
if(f.diretorTecnico != NULL)
output << f.diretorTecnico->getNoContribuinte() << endl;
else
output << "NULL" << endl;
output << f.produtosVender.size() << endl;
map<Produto, int>::const_iterator it;
for(it = f.produtosVender.begin(); it != f.produtosVender.end(); it++){
output << it->second;
output << endl;
output << it->first << endl;
output << endl;
}
output << f.vendas.size() << endl;
for(size_t i = 0; i < f.vendas.size(); i++){
output << (*f.vendas[i]) << endl;
}
return output;
}
Produto Farmacia::getProduto(unsigned long codigo){
map<Produto, int>::iterator it = produtosVender.begin();
for(; it != produtosVender.end(); it++){
if((*it).first.getCodigo() == codigo){
return (*it).first;
}
}
return Produto(0, "", 0, "", 0, 0, 0);
}
Venda* Farmacia::getVenda(unsigned long codigo) const{
for(vector<Venda*>::const_iterator it = vendas.begin(); it != vendas.end(); it++){
if((*it)->getCodigo() == codigo)
return (*it);
}
return NULL;
}
void Farmacia::setVendas(vector<Venda*> v){
this->vendas = v;
}