-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
285 lines (231 loc) · 6.92 KB
/
menu.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
#include "tp3.h"
#include "menu.h"
void menu(BlockChain bc)
{
int choix = 0;
int idEtu;
while (choix != 8)
{
printf("\n-------------Bienvenue sur EATcoin-------------\n");
printf("\n");
printf("1. Afficher la liste des blocs de la BlockChain\n");
printf("2. Afficher toutes les transactions d'un bloc\n");
printf("3. Afficher toutes les transactions du jour pour un etudiant\n");
printf("4. Afficher l'historique pour un etudiant\n");
printf("5. Crediter un compte\n");
printf("6. Payer un repas\n");
printf("7. Transferer des EATCoins entre 2 etudiants\n");
printf("8. Quitter\n");
printf("9. Exporter toutes les transactions vers un fichier\n\n");
//scanf("%d", &choix);
choix = readInt();
switch(choix)
{
case 1:
afficherBlocs(bc);
break;
case 2:
afficherTrans_bloc(bc);
break;
case 3:
afficherTransEtu_jour(bc);
break;
case 4:
printf("Pour quel etudiant voulez-vous afficher son historique ? \n");
idEtu = readInt();
printf("----------HISTORIQUE DE L'ETUDIANT %d----------\n", idEtu);
consulter(idEtu, bc);
break;
case 5:
bc = credit(bc);
break;
case 6:
paiement(bc);
break;
case 7:
transferer(bc);
break;
case 8:
//on libere la memoire
while (bc != NULL)
{
T_Transaction *liste_transaction = bc->liste;
while (liste_transaction != NULL)
{
T_Transaction *tmp = liste_transaction->next; //on recupere l'adresse de suivant avant de free
free(liste_transaction);
liste_transaction = tmp;
}
BlockChain tmpB = bc->next;
free(bc);
bc = tmpB;
}
bc = NULL;
printf("Vous avez quitte. \n");
break;
case 9:
nettoyerFichier();
export(bc);
break;
case 10:
break;
default : printf("Choix incorrect. \n");
}
}
return;
}
void afficherBlocs(BlockChain bc)
{
if (bc == NULL)
{
printf("\nLa BlockChain est vide.\n");
return;
}
BlockChain ptr = bc;
printf("\n---Affichage de la liste des blocs que contient la BlockChain---\n");
while(ptr != NULL){
printf("\nBlock numero : %d (le %d/%d/%d) \n", ptr->idBlock, ptr->date->tm_mday, ptr->date->tm_mon+1, ptr->date->tm_year+1900);
ptr = ptr->next;
}
ptr = NULL;
}
void afficherTrans_bloc(BlockChain bc){
BlockChain b = bc;
int id;
if (bc == NULL)
{
printf("\nLa BlockChain est vide.\n");
return;
}
printf("Quel bloc voulez vous afficher (saisir id) ? \n");
id = readInt();
while(b != NULL && b->idBlock != id){
b = b->next;
}
if(b == NULL){
printf("\nAucun bloc ne correspond a l'id saisi. \n");
return;
}
else{
printf("\n---Transactions du bloc %d---\n", b->idBlock);
T_Transaction* tmp = b->liste;
while(tmp != NULL){
printf(" idEtu : %d \n", tmp->idEtu);
printf(" Montant : %.2f EATCoin\n", tmp->montant);
printf(" Description : %s \n\n", tmp->descr);
tmp = tmp->next;
}
}
}
void afficherTransEtu_jour(BlockChain bc){
int id;
int trouve = 0 ;
time_t secondes;
struct tm *instant;
if (bc == NULL)
{
printf("\nLa BlockChain est vide.\n");
return;
}
T_Transaction* tmp = bc ->liste;
printf("Veuillez saisir l'identifiant de l'etudiant : ");
id = readInt();
secondes = time(NULL); //met la valeur a renvoyer dans temps equivalent a time(&secondes)
instant = localtime(&secondes);
if(instant->tm_mday != bc->date->tm_mday || instant->tm_mon != bc->date->tm_mon || instant->tm_year != bc->date->tm_year){
printf("Il n'y a pas eu de transaction aujourd'hui \n");
return;
}
printf("\n--Transactions du %d/%d/%d de l'etudiant %d--- \n", bc->date->tm_mday,bc->date->tm_mon+1, bc->date->tm_year+1900,id);
while(tmp != NULL){
if(tmp->idEtu == id){
//printf(" idEtu : %d \n", tmp->idEtu);
printf(" Description : %s \n", tmp->descr);
printf(" Montant : %.2f EATCoin \n\n", tmp->montant);
trouve = 1;
}
tmp = tmp->next;
}
if (trouve == 0)
printf("\nL'etudiant %d n'a pas fait de transaction aujourd'hui.\n", id);
}
//pour remplir la bc
BlockChain credit(BlockChain bc)
{
time_t secondes;
struct tm *instant;
int id;
float montant;
//on recupere la date du jour
secondes = time(NULL); //met la valeur a renvoyer dans temps equivalent a time(&secondes)
instant = localtime(&secondes);
printf("\n---Credit---\n");
printf("\nLe %d/%d/%d a %d:%d:%d\n", instant->tm_mday, instant->tm_mon+1,instant->tm_year+1900, instant->tm_hour, instant->tm_min, instant->tm_sec);
if (bc == NULL || instant->tm_mday != bc->date->tm_mday || instant->tm_mon != bc->date->tm_mon || instant->tm_year != bc->date->tm_year){ //si bc est null OU si la date ne correspond pas a la date du bloc courant
bc = ajouterBlock(bc);
}
printf("Veuillez saisir l'identifiant de l'etudiant : ");
id = readInt();
printf("Veuillez saisir le montant a crediter : ");
montant = readFloat();
//printf("ici");
//printf("id bloc avant crediter : %d", bc->idBlock);
crediter(id, montant, "Credit", bc);
return bc;
}
void paiement(BlockChain bc)
{
int id, result;
float montant;
char *descr = malloc(strlen(descr)*sizeof(char));
printf("\n---Achat repas---\n");
printf("Veuillez saisir l'identifiant de l'etudiant : ");
id = readInt();
printf("Veuillez saisir le montant a payer : ");
montant = readFloat();
printf("Veuillez saisir le repas : ");
scanf("%s", descr);
//printf("ici");
result = payer(id,montant,descr,bc);
if (result == 1)
printf("Le repas a ete paye.\n");
}
int transferer(BlockChain bc)
{
int source, dest;
float montant;
char *descr = malloc(255*sizeof(char));
printf("Veuillez saisir l'id du compte Etu source.\n");
source = readInt();
printf("Veuillez saisir l'id du compte Etu destinataire.\n");
dest = readInt();
printf("Veuillez saisir le montant de la transaction.\n");
montant = readFloat();
printf("Veuillez saisir une description.\n");
scanf("%s", descr);
if(transfert(source,dest,montant,descr,bc) == 1) {
printf("Le virement a un autre etudiant a ete fait.\n");
}
}
int readInt()
{
int a;
while(scanf("%d", &a) != 1) {
printf("Entrée invalide\n");
clean_stdin();
}
return a;
}
float readFloat(){
float a;
while (scanf("%f",&a) != 1){
printf("Entrée invalide");
clean_stdin();
}
return a;
}
int clean_stdin()
{
while (getchar()!='\n');
return 1;
}