This repository has been archived by the owner on Oct 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory.c
242 lines (213 loc) · 6.05 KB
/
category.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "structures.h"
#include "categ.h"
#include "product.h"
categoryList * fillTmpCategList();
categoryCell * createCategoryCell()
{
categoryCell * c = (categoryCell*)malloc(sizeof(categoryCell));
c->next = NULL;
return c;
}
categoryList * createCategoryList()
{
categoryList * l = (categoryList*)malloc(sizeof(categoryList));
l->head = NULL;
return l;
}
void addToHeadCateg(categoryList * l, char name[])
{
categoryCell * c = createCategoryCell();
c->next = l->head;
strcpy(c->name, name);
l->head = c;
}
void addToTailCateg(categoryList * l, char name[])
{
if(l->head == NULL)
{
addToHeadCateg(l, name);
return;
}
categoryCell * tmp = l->head;
while(tmp->next != NULL)
tmp = tmp->next;
categoryCell * c = createCategoryCell();
strcpy(c->name, name);
tmp->next = c;
}
void deleteHeadCateg(categoryList * l)
{
if(l->head == NULL)
return;
categoryCell * tmp = l->head;
l->head = l->head->next;
free(tmp);
}
void emptyTheListCateg(categoryList * l)
{
if(l->head == NULL)
return;
categoryCell * tmp = l->head;
l->head = l->head->next;
free(tmp);
emptyTheListCateg(l);
}
void deleteAllOccurenceCateg(productList * prodList, char name)
{
if(prodList->head == NULL)
return;
while(prodList->head != NULL && strcasecmp(prodList->head->data.productCategory, name) == 0)
{
printf("hello\n");
productCell * tmp = prodList->head;
prodList->head = prodList->head->next;
free(tmp);
}
if(prodList->head == NULL)
return;
productCell * tmp2 = prodList->head;
while(tmp2->next != NULL)
{
if(strcasecmp(tmp2->next->data.productCategory, name) == 0)
{
productCell * tmp3 = tmp2->next;
tmp2->next = tmp2->next->next;
free(tmp3);
}
else
tmp2 = tmp2->next;
}
}
/** Returns 1 if yes, 0 if no **/
int belongsProdCategToCategList(categoryList l, char prodCateg[])
{
for(categoryCell * tmp = l.head; tmp != NULL; tmp = tmp->next)
if(strcmp(tmp->name, prodCateg) == 0)
return 1;
return 0;
}
/** Returns 1 if yes, 0 if no **/
int belongsNameToCategList(categoryList l, char name[])
{
for(categoryCell * tmp = l.head; tmp != NULL; tmp = tmp->next)
if(strcmp(tmp->name, name) == 0)
return 1;
return 0;
}
/** Used ONLY for fillCategoryList() method **/
void addToCategList(categoryList * l, Product prod)
{
if(l->head == NULL)
{
categoryCell * c = createCategoryCell(prod.productName);
strcpy(c->name, prod.productCategory);
l->head = c;
c->data = createProductList();
addToTailProd(c->data, prod);
return;
}
if(belongsProdCategToCategList(*l, prod.productCategory) == 0)
{
categoryCell * tmp = l->head;
while(tmp->next != NULL)
tmp = tmp->next;
categoryCell * c = createCategoryCell(prod.productName);
strcpy(c->name, prod.productCategory);
tmp->next = c;
c->data = createProductList();
addToTailProd(c->data, prod);
return;
}
categoryCell * tmp = l->head;
while(tmp->next != NULL && (strcmp(tmp->name, prod.productCategory) != 0))
tmp = tmp->next;
addToTailProd(tmp->data, prod);
}
/** Gets data from files then adds it to the list **/
void fillCategoryList(categoryList * l)
{
Product prod;
FILE * pf = fopen("product.txt", "r");
while(!feof(pf))
{
fscanf(pf, "%d %s %f %d %s", &prod.productId, prod.productName, &prod.productPrice, &prod.productStock, prod.productCategory);
addToCategList(l, prod);
}
fclose(pf);
}
void printAvailableCateg(categoryList l)
{
printf("\t\t\t\t\t\t\t\t\tAvailable categories:\n");
printf("\t\t\t\t\t\t\t\t\t---------------------\n\n");
for(categoryCell * tmp = l.head; tmp != NULL; tmp = tmp->next)
printf("\t\t\t\t\t\t\t\t\t> > > > %s\n\n", tmp->name);
printf("\t\t\t\t\t\t\t\t\t---------------------");
}
/// NEW
categoryList * fillTmpCategList()
{
char name[20];
categoryList * tmpCategList = createCategoryList();
FILE * pf = fopen("category.txt", "r");
while(!feof(pf))
{
fscanf(pf, "%s", name);
addToTailCateg(tmpCategList, name);
}
fclose(pf);
return tmpCategList;
}
///
/** Updates the data from the category list to the file **/
void updateDataOfFileCateg(char name[])
{
FILE * pf = fopen("category.txt", "a");
fprintf(pf, "\n%s", name);
fclose(pf);
}
/** Saves the data from the category list to the file **/
void saveDataToFileCateg(categoryList l)
{
FILE * pf = fopen("category.txt", "w");
categoryCell * tmp = l.head;
while(tmp != NULL)
{
fprintf(pf, "\n%s", tmp->name);
tmp = tmp->next;
}
fclose(pf);
}
void addNewCategory(categoryList * categList)
{
categoryList * tmpCategList = fillTmpCategList();
char newName[20];
printf("\n\t\t\t\t\t\t\t\t\tEnter the new name: ");
scanf("%s", newName);
printf("\t\t\t\t\t\t\t\t\t-----------------------\n");
while(belongsNameToCategList(*tmpCategList, newName) == 1)
{
printf("\n\t\t\t\t\t\t\t\t\tName Already Exists!\n");
printf("\n\t\t\t\t\t\t\t\t\tEnter the new name: ");
scanf("%s", newName);
printf("\t\t\t\t\t\t\t\t\t-----------------------\n");
}
addToTailCateg(tmpCategList, newName);
saveDataToFileCateg(*tmpCategList);
emptyTheListCateg(categList);
fillCategoryList(categList);
/// free tmpCategList
free(tmpCategList);
FILE * pf = fopen("activityLog.txt", "a");
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
fprintf(pf, "\nNew Category [%s] Added at %s ------------------------", newName, asctime(timeinfo));
fclose(pf);
printf("\n\t\t\t\t\t\t\t\t\tCategory Added Successfully!\n");
printf("\t\t\t\t\t\t\t\t\t----------------------------\n");
}