-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.c
More file actions
215 lines (179 loc) · 6.37 KB
/
Copy pathitems.c
File metadata and controls
215 lines (179 loc) · 6.37 KB
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
// Name: Adam Korsunski, ID: 207803933
// Name: Nadav Rogers, ID: 326301520
#include "items.h"
#include "employees.h"
#include "log.h"
#define ITEM_FILE "items.bin"
// הפונקציה מוסיפה פריט לרשימה מקושרת בסדר עולה לפי המספר הסידורי
Item* insertItemSorted(Item* head, Item* newItem) {
if (!head || atoi(newItem->id) < atoi(head->id)) {
newItem->next = head;
return newItem;
}
Item* current = head;
while (current->next && atoi(current->next->id) < atoi(newItem->id)) {
current = current->next;
}
newItem->next = current->next;
current->next = newItem;
return head;
}
// טוען פריטים מקובץ בינארי
Item* loadItems(int* count) {
FILE* file = fopen(ITEM_FILE, "rb");
if (!file) {
*count = 0;
printf("No items file found, starting empty.\n");
return NULL;
}
if (fread(count, sizeof(int), 1, file) != 1) {
fclose(file);
*count = 0;
printf("Error reading item count from file.\n");
return NULL;
}
Item* head = NULL;
for (int i = 0; i < *count; i++) {
Item* newItem = (Item*)malloc(sizeof(Item));
if (!newItem) {
fclose(file);
freeItemList(head);
*count = 0;
printf("Memory allocation failed while loading items.\n");
return NULL;
}
memset(newItem, 0, sizeof(Item)); // מבטיח שכל השדות מאותחלים
if (fread(newItem, sizeof(Item), 1, file) != 1) {
free(newItem);
fclose(file);
freeItemList(head);
*count = 0;
printf("Error reading item from file.\n");
return NULL;
}
newItem->next = NULL;
head = insertItemSorted(head, newItem);
}
fclose(file);
return head;
}
// שומר את רשימת הפריטים לקובץ בינארי
void saveItems(Item* items, int count) {
FILE* file = fopen(ITEM_FILE, "wb");
if (!file) return;
fwrite(&count, sizeof(int), 1, file);
Item* current = items;
while (current) {
fwrite(current, sizeof(Item), 1, file);
current = current->next;
}
fclose(file);
}
// מחפש פריט לפי מזהה או שם
Item* searchItem(Item* items, const char* username, const char* searchTerm) {
Item* current = items;
while (current) {
if (strcmp(current->id, searchTerm) == 0 || strcmp(current->name, searchTerm) == 0) {
logItemAction(username, current->id, "Searched for an item - Found");
return current;
}
current = current->next;
}
logItemAction(username, searchTerm, "Searched for an item - Not Found");
return NULL;
}
// מוסיף פריט חדש למערך ומעדכן את הקובץ
Item* addItem(Item* items, int* count, const char* username, const char* id, const char* name, const char* category, int IsKosher, float price, int quantity) {
Item* newItem = (Item*)malloc(sizeof(Item));
if (!newItem) {
logItemAction(username, id, "Failed to add item - Memory allocation failed");
return items;
}
strcpy(newItem->id, id);
strcpy(newItem->name, name);
strcpy(newItem->category, category);
newItem->IsKosher = IsKosher;
getCurrentDate(newItem->EntryDate);
newItem->price = price;
newItem->quantity = quantity;
newItem->next = NULL;
// הכנסת הפריט החדש לרשימה בצורה ממיונת לפי id
items = insertItemSorted(items, newItem);
(*count)++;
// שמירת הפריטים המעודכנים לקובץ
saveItems(items, *count);
// רישום פעולה בלוג
logItemAction(username, id, "Added a new item");
printf("Item added successfully! (Entry Date: %s)\n", newItem->EntryDate);
return items;
}
// מסיר פריט מהמערך ומעדכן את הקובץ
Item* removeItem(Item* items, int* count, const char* username, const char* searchTerm) {
if (!items) {
logItemAction(username, searchTerm, "Tried to remove an item - List is empty");
return NULL;
}
Item* current = items;
Item* prev = NULL;
// חיפוש הפריט להסרה
while (current) {
if (strcmp(current->id, searchTerm) == 0 || strcmp(current->name, searchTerm) == 0) {
// אם הפריט בראש הרשימה
if (!prev) {
items = current->next;
}
else {
prev->next = current->next;
}
logItemAction(username, current->id, "Removed an item");
free(current);
(*count)--;
// שמירת הרשימה המעודכנת
saveItems(items, *count);
return items;
}
prev = current;
current = current->next;
}
logItemAction(username, searchTerm, "Tried to remove an item - Not Found");
return items;
}
// מעדכן את מאפייני הפריט (מחיר וכמות) ומעדכן בקובץ
void updateItemAttributes(Item* items, int count, const char* username, const char* id, float newPrice, int newQuantity) {
Item* item = searchItem(items, username, id);
if (item) {
item->price = newPrice;
item->quantity = newQuantity;
saveItems(items, count);
logItemAction(username, id, "Updated item attributes");
}
else {
logItemAction(username, id, "Tried to update an item - Not Found");
printf("Error: Item not found!\n");
}
}
// משחרר את הזיכרון של רשימת הפריטים
void freeItemList(Item* items) {
Item* current = items;
while (current) {
Item* temp = current;
current = current->next;
free(temp);
}
}
// מציג את רשימת הפריטים
void displayItems(Item* items) {
printf("\n=== Items List ===\n");
if (!items) { // אם הרשימה ריקה, נציג הודעה ולא נעבור על NULL
printf("No items available.\n");
return;
}
Item* current = items;
while (current) {
printf("ID: %s | Name: %s | Category: %s | Kosher: %s | Entry Date: %s | Price: %.2f | Quantity: %d\n",
current->id, current->name, current->category,
current->IsKosher ? "Yes" : "No", current->EntryDate,
current->price, current->quantity);
current = current->next;
}
}