This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.c
500 lines (460 loc) · 15.5 KB
/
queries.c
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include "definitions.h"
#pragma region GetArtigoIndex
/** @brief Função que permite obter o indíce de um artigo no array
* @param codArtigo - Código do artigo
* @return art - Indíce do artigo
*/
int getArtigoIndex(int codArtigo)
{
for (int art = 0; art <= NA; art++)
{
if (artigo[art].codArtigo == codArtigo)
{
return art;
}
}
return -1;
}
#pragma endregion
#pragma region getArtigoName
/** @brief Função que permite obter o nome do artigo
* @param idx - Indice no array de artigos
* @return artigo - O nome do artigo
*/
char* getArtigoName(int idx)
{
return artigo[idx].artigo;
}
#pragma endregion
#pragma region getTotalEncomenda
/**
* @brief Função que permite obter o total (preço) de encomendas
* @return int - total de encomendas
*/
double getTotalEncomenda()
{
double total = 0;
for (int enc = 0; enc <= NE+1; enc++)
{
if (encomenda[enc].codCliente == runtime->currentClient)
{
if (encomenda[enc].estado)
{
total += encomenda[enc].total;
}
}
}
return total;
}
#pragma endregion
#pragma region GetDateInfo
/** * @brief Obter certo parâmetro de data tal como hora, dia, mês, ano
* @note A enumeração é usada para indicar o parâmetro a obter
*/
int getDateInfo(int info)
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
switch (info)
{
enum InfoQuery {
hourminute = 1,
day = 2,
month = 3,
year = 4,
};
case hourminute:
return tm.tm_hour * 100 + tm.tm_min;
case day:
return tm.tm_mday;
case month:
return tm.tm_mon + 1;
case year:
return tm.tm_year + 1900;
}
return -1;
}
#pragma endregion
#pragma region listArtigos
/** @brief Função que permite listar o(s) artigo(s)
* @param queryType - opção de listagem
* @note - a opção (10) é quando o cliente autenticado pretende listar as encomendas da sessão
*/
void listArtigos(int queryType)
{
// queryType 10&11 = listar todos os artigos disponiveis com valores REDUZIDOS
// queryType 12 = listar todos os artigos disponiveis COM todos os valores
// queryType 13 = listar um artigo especifico COM todos os valories
enum queryType {
reduzidaCompras = 10,
reduzidaAdmin = 11,
completa = 12,
especifica = 13
};
if (NA == -1)
{
red(); printf("\n[!] Não existem artigos registados [!]"); white();
pressEnter(true);
return;
}
int count;
cyan(); printf("\n------------------------ Artigos ------------------------\n"); white();
switch (queryType)
{
case reduzidaCompras:
for (int art = 0; art <= NA; art++)
{
if (artigo[art].estado)
{
printf("\nArtigo: %s | Código: %d", artigo[art].artigo, artigo[art].codArtigo);
printf("\nPreço comum: %.2f€\n", artigo[art].defaultPreco);
printf("|_ Tamanhos disponiveis: %d - %d\n\n", artigo[art].tamanhos[0], artigo[art].tamanhos[1]);
}
}
break;
case reduzidaAdmin:
for (int art = 0; art <= NA; art++)
{
if (artigo[art].estado)
{
printDataArtigo(art, false);
}
}
printf("\nTotal de Artigos - %d\n", NA + 1);
if (queryType == reduzidaAdmin)
{
pressEnter(true);
}
break;
case completa:
for (int art = 0; art <= NA; art++)
{
printDataArtigo(art, true);
}
printf("\nTotal de Artigos - %d\n", NA + 1);
pressEnter(true);
break;
case especifica:
printf("\nPesquisar Artigo por (N)ome ou (C)ódigo? ");
char internalQueryType[5];
scanf(" %s", internalQueryType);
if (strcmp(internalQueryType, "n") == 0 || strcmp(internalQueryType, "N") == 0)
{
printf("\nPesquisar por Nome: ");
char nomePesquisa[SC];
scanf(" %s", nomePesquisa);
count = 0;
for (int art = 0; art <= NA; art++)
{
if (artigo[art].estado && (strcmp(artigo[art].artigo, nomePesquisa) == 0))
{
printDataArtigo(art, true);
count++;
}
}
if (count == 0)
{
red(); printf("\n[!] Não existem artigos com esse nome [!]"); white();
pressEnter(true);
return;
}
}
else if (strcmp(internalQueryType, "c") == 0 || strcmp(internalQueryType, "C") == 0)
{
printf("\nPesquisar por Código: ");
int codigoPesquisa;
scanf("%d", &codigoPesquisa);
count = 0;
for (int art = 0; art <= NA; art++)
{
if (artigo[art].estado && (artigo[art].codArtigo == codigoPesquisa))
{
printDataArtigo(art, true);
count++;
break;
}
}
if (count == 0)
{
red(); printf("\n[!] Não existem artigos com esse código [!]"); white();
pressEnter(true);
return;
}
} else {
red(); printf("\n[!] Opção Inválida [!]"); white();
pressEnter(true);
return;
}
printf("\nTotal de Artigos - %d", NA + 1);
pressEnter(true);
break;
}
}
#pragma endregion
#pragma region listClientes
/** @brief Função que permite listar o(s) cliente(s)
* @param queryType - opção de listagem
*/
void listClientes(int queryType)
{
if (NC == -1)
{
red(); printf("\n[!] Não existem clientes registados [!]"); white();
pressEnter(true);
return;
}
enum queryType {
todos = 51,
especifico = 52,
autoQuery = 53
};
cyan(); printf("\n------------------------ Clientes ------------------------\n"); white();
switch (queryType)
{
case todos:
for (int user = 0; user <= NC; user++)
{
if (cliente[user].estado)
{
printDataCliente(user);
}
}
break;
case especifico:
printf("Pesquisar Cliente por (N)ome ou (C)ódigo? ");
char internalQueryType[SC];
scanf(" %s", internalQueryType);
if (strcmp(internalQueryType, "N") == 0 || strcmp(internalQueryType, "n") == 0)
{
printf("\nNome: ");
char nome[50];
scanf(" %50s", nome);
for (int user = 0; user <= NC; user++)
{
if (cliente[user].estado)
{
if (strcmp(cliente[user].nome, nome) == 0)
{
printDataCliente(user);
}
}
}
}
else if (strcmp(internalQueryType, "C") == 0 || strcmp(internalQueryType, "c") == 0)
{
printf("\nCódigo Cliente: ");
int codCliente;
scanf("%d", &codCliente);
for (int user = 0; user <= NC; user++)
{
if (cliente[user].estado && cliente[user].codCliente == codCliente)
{
printDataCliente(user);
}
}
break;
}
}
pressEnter(true);
}
#pragma endregion
#pragma region EstatisticasEncomendas
/**
* @brief Estatisticas das encomendas
* @note Média do Preço e Quantidade
* @note Melhor e Pior produto (em termos de vendas)
*/
void statisticsEncomendas()
{
if (NE == -1) {
red(); printf("\n[!] Não existem encomendas! [!]"); white();
pressEnter(true);
return;
} else if (NE == 0) {
red(); printf("\n[!] Não existem encomendas sufientes para gerar estatisticas! [!]"); white();
pressEnter(true);
return;
}
/** @note Encontrar as médias (preço e quantidade) das encomendas */
float averagePrice = 0, averageQuantity = 0;
for (int enc = 0; enc <= NE; enc++)
{
if (encomenda[enc].estado)
{
averagePrice += encomenda[enc].total;
averageQuantity += (float) encomenda[enc].quantidade;
}
}
averagePrice = averagePrice / (float) (NE + 1);
averageQuantity = averageQuantity / (float) (NE + 1);
cyan(); printf("\n [ Média Encomendas ]"); white();
printf("\n | Preço: %0.2f€", averagePrice);
printf("\n | Quantidade: %d", (int) averageQuantity);
printf("\n | Total Encomendas: %d\n", NE + 1);
// Best and Worst Seller
int codArtigoHQ, higherQuantity, codArtigoLQ, lowerQuantity, codBestBuyer, bestBuyerQuantity = 0;
/** @note Encontrar o produto mais vendido */
for (int enc = 0; enc <= NE; enc++)
{
if (encomenda[enc].estado)
{
if (encomenda[enc].quantidade > higherQuantity)
{
higherQuantity = encomenda[enc].quantidade;
codArtigoHQ = encomenda[enc].codArtigo;
}
}
}
/** @note Encontrar o produto menos vendido */
lowerQuantity = higherQuantity;
for (int enc = 0; enc <= NE; enc++)
{
if (encomenda[enc].estado)
{
if (encomenda[enc].quantidade < lowerQuantity)
{
lowerQuantity = encomenda[enc].quantidade;
codArtigoLQ = encomenda[enc].codArtigo;
}
}
}
codArtigoHQ = getArtigoIndex(codArtigoHQ); codArtigoLQ = getArtigoIndex(codArtigoLQ);
cyan(); printf("\n [ Produto Mais & Menos Vendido ]"); white();
printf("\n | Artigo:"); green(); printf(" %s com %d unidades vendidas", \
getArtigoName(codArtigoHQ), higherQuantity); white();
printf("\n | Artigo:"); red(); printf(" %s com %d unidades vendidas\n", \
getArtigoName(codArtigoLQ), lowerQuantity); white();
/** @note Encontrar o cliente com mais unidades compradas */
for (int enc = 0; enc <= NE; enc++)
{
if (encomenda[enc].estado)
{
if (encomenda[enc].quantidade > bestBuyerQuantity)
{
bestBuyerQuantity = encomenda[enc].quantidade;
codBestBuyer = encomenda[enc].codCliente;
}
}
}
cyan(); printf("\n [ Cliente que mais comprou ]"); white();
printf("\n | Cliente:"); green(); printf(" %s com %d unidades compradas\n", \
getClientName(codBestBuyer), bestBuyerQuantity); white();
/** @note Encontrar o artigo com maior margem de lucro */
int codArtigoMLH, codArtigoMLL;
float higherMargemLucro;
for (int art = 0; art <= NA; art++)
{
if (artigo[art].estado)
{
if (artigo[art].margemLucro > higherMargemLucro)
{
higherMargemLucro = artigo[art].margemLucro;
codArtigoMLH = art;
}
}
}
/** @note Encontrar o artigo com menor margem de lucro */
float lowerMargemLucro = higherMargemLucro;
for (int art = 0; art <= NA; art++)
{
if (artigo[art].estado)
{
if (artigo[art].margemLucro < lowerMargemLucro)
{
lowerMargemLucro = artigo[art].margemLucro;
codArtigoMLL = art;
}
}
}
cyan(); printf("\n [ Melhor & Pior Margem de Lucro ]"); white();
printf("\n | Artigo:"); green(); printf(" %s com %0.2f€ de margem de lucro", \
getArtigoName(codArtigoMLH), higherMargemLucro); white();
printf("\n | Artigo:"); red(); printf(" %s com %0.2f€ de margem de lucro\n", \
getArtigoName(codArtigoMLL), lowerMargemLucro); white();
/** @note Encontrar a quantidade de encomendas ativas/canceladas */
int stateEncomendas[2] = {0, 0};
for (int enc = 0; enc <= NE; enc++)
{
if (encomenda[enc].estado)
{
stateEncomendas[0] += 1;
}
else
{
stateEncomendas[1] += 1;
}
}
double percentagemEncomendas[2] = {0, 0};
percentagemEncomendas[0] = (double) stateEncomendas[0] / (double) (NE + 1);
percentagemEncomendas[1] = (double) stateEncomendas[1] / (double) (NE + 1);
cyan(); printf("\n [ Encomendas Ativas & Canceladas ]"); white();
printf("\n | Ativas: %d -", stateEncomendas[0]); green();
printf(" %0.2f%% das encomendas", percentagemEncomendas[0] * 100); white();
printf("\n | Canceladas: %d -", stateEncomendas[1]); red();
printf(" %0.2f%% das encomendas", percentagemEncomendas[1] * 100); white();
pressEnter(true);
}
#pragma endregion
#pragma region printDataEncomenda
/**
* @brief Imprime os dados de uma encomenda
* @param enc - Encomenda a imprimir
*/
void printDataEncomenda(int enc)
{
printf("\n[Código Encomenda]: %d ", encomenda[enc].codEncomenda);
printf("\n | Código Cliente: %d (%s)", encomenda[enc].codCliente, getClientName(encomenda[enc].codCliente));
printf("\n | %d pare(s) de %s", encomenda[enc].quantidade, getArtigoName(getArtigoIndex(encomenda[enc].codArtigo)));
printf("\n | Preço Encomenda: %0.2f€", encomenda[enc].total);
printf("\n | Data: %d %d/%d/%d", encomenda[enc].horaminuto, encomenda[enc].dia, encomenda[enc].mes, encomenda[enc].ano);
printf("\n | [Estado]: %s\n", getEstadoEncomenda(encomenda[enc].estado));
}
#pragma endregion
#pragma region printDataArtigo
/**
* @brief Imprime os dados de um artigo
* @param art - Artigo a imprimir
* @param printTamanhas - Define se os tamanhos e os respetivos preços devem ser imprimidos
*/
void printDataArtigo(int art, bool printTamanhos)
{
int looperTamanhos = 0;
printf("\n"
"Artigo: %s (Código: %d)\n"
" | Mão Obra: %0.2f€\n"
" | Custos Fixos: %0.2f€\n"
" | Margem Lucro: %0.2f€\n"
" | Preco por defeito: %0.2f€\n"
" | Incremento (por tamanho): +%0.2f€\n"
" [Estado] %s", artigo[art].artigo, artigo[art].codArtigo, artigo[art].maoObra, \
artigo[art].custosFixos, artigo[art].margemLucro, artigo[art].defaultPreco, artigo[art].incrementoPreco, getEstadoEncomenda(artigo[art].estado));
if (printTamanhos) {
printf("\t| Tamanho(s) e Preço(s):\n");
for (int j = artigo[art].tamanhos[0]; j <= artigo[art].tamanhos[1]; j++)
{
printf("\t|_ Tamanho: %d | Preço: %0.2f€\n", \
(int) artigo[art].tamanhoPreco[looperTamanhos][0], artigo[art].tamanhoPreco[looperTamanhos][1]);
looperTamanhos++;
}
}
}
#pragma endregion
#pragma region printDataCliente
/**
* @brief Imprime os dados de um cliente
* @param cli - Cliente a imprimir
*/
void printDataCliente(int cli)
{
printf("\n[Código Cliente]: %d", cliente[cli].codCliente);
printf("\n | Nome: %s", cliente[cli].nome);
printf("\n | NIF: %d", cliente[cli].nif);
printf("\n | País: %s\n", cliente[cli].pais);
}
#pragma endregion