-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock.c
418 lines (386 loc) · 13.5 KB
/
stock.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define false 0 //false boolean
#define true 1 // for true the boolean
struct product
{
char id[10]; // product code/no.
char name[20]; // product name
int quantity; // remaining quantity of product. Subtract from the original quantity the quantity purchased
int numSold; // initially zero, when no purchase yet.
float price; // price of one piece of product
int discount; // discount for this product
float sales; // accumulated sales, total sales for this product
float value;
};
struct product prod[30]; // the maximum array elements.
int count = 0; // number of unique products present in stock
FILE *f; // for the file pointer
int writeFile();
int readFile();
void disZeroQuant();
void dispHsale();
void purchaseprod();
void deleteprod();
void addProd();
int IDChecker(int i, int j);
void editProd();
int checkID(char id[]);
void displayprod();
void worth();
int main()
{
int choice;
count = readFile(); // read file in structure from the text file
if (count < 0) // there is no file located.
printf("Cannot locate file\n");
do
{
printf("\n");
printf("\t\t\t ================================\n");
printf("\t\t\t Stock Management PROGRAM\n");
printf("\t\t\t ================================");
printf("\n\nPress:");
printf("\n 1.) Input new product record.");
printf("\n 2.) Edit a Product.");
printf("\n 3.) Delete a Product");
printf("\n 4.) Display all existing product.");
printf("\n 5.) Make a purchase.");
printf("\n 6.) Display the product record with highest sale.");
printf("\n 7.) Display all product with zero quantity");
printf("\n 8.) Display the total value of stock");
printf("\n 9.) Exit the program.");
printf("\nChoice--> ");
scanf("%d", &choice);
switch (choice)
{
case 1: // add product
addProd();
break;
case 2: // edit data product
editProd();
break;
case 3: // delete a product
deleteprod();
break;
case 4: // display the products
displayprod();
break;
case 5: // make a purchased.
purchaseprod();
break;
case 6:
dispHsale(); // to display highest sale.
break;
case 7:
disZeroQuant(); // display lowest sale.
break;
case 8:
worth(); // display total value of stock.
break;
case 9:
exit(1);
break;
default:
printf("Your choice is wrong please try again");
break;
}
} while (choice != 9); // infinite loop until the user will choose number8 .
printf("Thankyou for using this program");
}
int writeFile() // write file function
{
int i;
f = fopen("stock.dat", "w"); // ayaw i append; change from f = fopen("stock.dat", "a");
if (f == NULL)
writeFile();
fprintf(f, "%d\n", count);
for (i = 0; i < count; ++i) // writing all the details from all the function to the text file.
{
// Changed
fputs(prod[i].id, f);
fprintf(f, "\n");
fputs(prod[i].name, f);
fprintf(f, "\n");
fprintf(f, "%d\n", prod[i].quantity);
fprintf(f, "%d\n", prod[i].numSold);
fprintf(f, "%f\n", prod[i].price);
fprintf(f, "%d\n", prod[i].discount);
fprintf(f, "%f\n", prod[i].sales);
fprintf(f, "%f\n", prod[i].quantity * prod[i].price);
fprintf(f, "\n");
}
fclose(f);
return 0;
}
int readFile() // read file function
{
int n = 0;
int i;
f = fopen("stock.dat", "r");
if (f == NULL)
writeFile();
fscanf(f, "%d\n", &n);
for (i = 0; i < n; ++i)
{
fgets(prod[i].id, 10, f);
prod[i].id[strlen(prod[i].id) - 1] = 0; // remove new lines
fgets(prod[i].name, 20, f);
prod[i].name[strlen(prod[i].name) - 1] = 0; // remove new lines
fscanf(f, "%d", &prod[i].quantity);
fscanf(f, "%d", &prod[i].numSold);
fscanf(f, "%f", &prod[i].price);
fscanf(f, "%d", &prod[i].discount);
fscanf(f, "%f", &prod[i].sales);
fscanf(f, "%f\n\n", &prod[i].value);
}
fclose(f);
return n;
}
void disZeroQuant()
{ // for the switch number 7= calling all the product id with zero quantity.
int i;
count = readFile(); // call the read function
printf("\nProducts with zero Quantity: ");
for (i = 0; i < count; i++)
{
if (prod[i].quantity == 0)
{ // printing the highest product.
printf("\nName of the product: %s \nProduct Id: %s \nQuantity left: %d \nNumber of product sold: %d \nPrice of the product: %.2f \nDiscount of the product: %d %% \nTotal Sales: %.2lf\nTotal value: %f \n", prod[i].name, prod[i].id, prod[i].quantity, prod[i].numSold, prod[i].price, prod[i].discount, prod[i].sales,prod[i].value);
}
}
writeFile();
}
void dispHsale()
{ // to display the highest sale function
int high, i;
high = prod[0].numSold; // getting the first element of the array that has been sold
for (i = 0; i < count; i++) // loop for the num item sold.
{
if (prod[i].numSold > high) // if the element containts the highest sold product.
high = prod[i].numSold; // it will pass on the high variable.
}
printf("\nThe Highest Product Sale is: \n");
for (i = 0; i < count; i++) // loop to search the highest sold product.
{
if (prod[i].numSold == high) // printing the highest product.
printf("Name of the product: %s \nProduct Id: %s \nQuantity left: %d \nNumber of product sold: %d \nPrice of the product: %.2f \nDiscount of the product: %d %% \nTotal Sales: %.2lf\nTotal value: %f\n", prod[i].name, prod[i].id, prod[i].quantity, prod[i].numSold, prod[i].price, prod[i].discount, prod[i].sales,prod[i].value);
}
}
void purchaseprod()
{ // function for purchasing a product
int quant, i;
char id[10];
int z = true;
count = readFile();
printf("Sell an Item ");
printf("\nProduct ID: ");
fflush(stdin);
gets(id);
for (i = 0; i < count; i++)
{
if (strcmp(id, prod[i].id) == 0) // if the id that the user want to find and the data id that has been saved at file is matched.
{
z = false;
printf("\nItem found! Containing: \n"); //...then display the match
printf("\nProduct name: %s", prod[i].name);
printf("\nPrice: %.2lfphp\n\n", prod[i].price);
printf("Enter the quantity you want to buy : ");
fflush(stdin);
scanf("%d", &quant);
if (quant > prod[i].quantity)
{ // if the quantity is lessthan the users quant
puts("\nInsufficient Quantity\nPlease Restock.\n ");
break; // break and back to the choices.
}
float tempSales = prod[i].sales; // will be executed if the quantity is greater than the users selected quantity.
prod[i].numSold += quant;
prod[i].quantity -= quant;
prod[i].sales = quant * (prod[i].price * (prod[i].discount / 100.0));
prod[i].sales += tempSales;
}
}
if (z == true)
{ // if the product id is not available.
printf("Cant find the product id: %s.", id);
}
writeFile();
}
void deleteprod()
{ // function for the delete product.
count = readFile();
char id[10];
int i, j;
int z = true;
printf("Enter the id that you want to be delete : "); // user's input for deleting.
fflush(stdin);
gets(id);
for (i = 0; i < count; i++)
{ // loop to finding the user's input
z = false;
if (strcmp(prod[i].id, id) == 0)
{ // if the user's input matched the data
for (j = i; j < (count - 1); j++) // it will erase the selected data.
{
prod[j] = prod[j + 1];
}
count--;
}
}
if (z == true)
{ // will be executed if the product id is not available.
printf("Cant find product id: %s .", id);
}
writeFile();
}
void addProd()
{ // function to add products to the file
printf("ENTER NEW PRODUCTS\n");
readFile(); // reading the files .
if (count > 0)
{
count = readFile();
IDChecker(0, count); // to check if the id is already used.
}
else
{
printf("\nProduct ID Number: ");
fflush(stdin);
gets(prod[count].id);
}
printf("Product Name: ");
gets(prod[count].name);
printf("Quantity of the product: ");
scanf("%d", &prod[count].quantity);
printf("Price of the product: ");
scanf("%f", &prod[count].price);
printf("Item Discount: ");
scanf("%d", &prod[count].discount);
++count; // increment count for the product positions and how many are they in the array.
writeFile(); // putting/saving this to the file.
}
int IDChecker(int i, int j) // checking the input id
{
count = readFile();
printf("Product ID: ");
fflush(stdin);
gets(prod[count].id);
if (strcmp(prod[i].id, prod[j].id) == 0)
{
printf("ID number is already taken!");
return IDChecker(i++, j--);
}
}
void editProd()
{ // Editing the product function
char id[10];
int test;
int i;
int choice;
printf("EDIT A PRODUCT!");
printf("\nEnter the id of the product that you want to edit: "); // users input for what data will be change
fflush(stdin);
gets(id);
test = checkID(id);
if (test == 0)
{
printf("The id num %s is not found.", id); // if the data is empty
}
else
{
readFile();
{
for (i = 0; i < count; i++)
{
if (strcmp(id, prod[i].id) != 0) // if the data is not empty
writeFile();
else
{
printf("\n1. Update product ID Number?");
printf("\n2. Update Name of the product? ");
printf("\n3. Update Quantitiy of the product?");
printf("\n4. Update Price of the product?");
printf("\n5. Update Discount of the product?");
printf("\nEnter your choice:");
fflush(stdin);
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter new ID: ");
fflush(stdin);
gets(prod[i].id);
break;
case 2:
printf("Enter new Name: ");
fflush(stdin);
gets(prod[i].name);
break;
case 3:
printf("Enter Quantity: ");
scanf("%d", &prod[i].quantity);
break;
case 4:
printf("Enter the new price: ");
scanf("%f", &prod[i].price);
break;
case 5:
printf("Enter the new discount of the product: ");
scanf("%d", &prod[i].discount);
default:
printf("Invalid Selection");
break;
}
writeFile();
}
}
}
fclose(f);
f = fopen("stock.dat", "r");
readFile();
{
writeFile();
}
fclose(f);
printf("RECORD UPDATED");
}
}
int checkID(char id[])
{ // checking the id if available
int i;
count = readFile();
readFile();
for (i = 0; i < count; i++)
{
if (strcmp(id, prod[i].id) != 0)
{ // if the id and data id doesnt match.
fclose(f);
}
return 1; // returning an error.
}
fclose(f);
return 0; // return 0 if no error.
}
void displayprod()
{ writeFile();
int i;
count = readFile(); // the output is how many products inside the file.
if (count < 0)
puts("cannot open file");
printf(" \t\t\t\t\t ***** STOCK *****\n");
printf("----------------------------------------------------------------------------------------------------\n");
printf("S.N.| NAME | PROD ID | QUANTITY | PROD SOLD | PRICE | DISCOUNT | SALES | VALUE |\n");
printf("----------------------------------------------------------------------------------------------------\n");
for (i = 0; i < count; i++)
{ // getting the details on each product updates.
printf("%d %-10s %-8s %-5d %-3d %-6.2f %-5d%% P%.2lf %.2f\n", i + 1, prod[i].name, prod[i].id, prod[i].quantity, prod[i].numSold, prod[i].price, prod[i].discount, prod[i].sales,prod[i].value);
}
}
void worth(){
float sum = 0;
for(int i=0;i<30;i++){
sum = sum + prod[i].value;
}
printf("The total value of the current stock is: %.2f\n",sum);
}