-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseFunctions.c
More file actions
630 lines (539 loc) · 18.9 KB
/
Copy pathdatabaseFunctions.c
File metadata and controls
630 lines (539 loc) · 18.9 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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/**
* Group G
* Isabell Cook
* icook@okstate.edu
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "unistd.h"
#include "database.h"
#include "databaseFunctions.h"
void writeNoInput(int soc_conn, char writeThis[1024]);
BillingTable * tableOfBillings;
CustomerTable * tableOfCustomers;
OrderTable * tableOfOrders;
ProductTable * tableOfProducts;
SellerTable * tableOfSellers;
Seller sellerInfo;
Customer customerInfo;
pthread_mutex_t lockBillingTable;
pthread_mutex_t lockCustomerTable;
pthread_mutex_t lockOrderTable;
pthread_mutex_t lockProductTable;
pthread_mutex_t lockSellerTable;
/**
* Argument: NULL
* Return: void
* Description:
* This function calls all of the functions needed to start up the "database".
* Table structures defined globaly are initialized then the contents stored in the files
* are loaded into the corresponding structures
*/
//Does not need Mutex Locks
void startupStructures(){
//initialize structures that hold all the information
tableOfBillings = initBillings();
tableOfCustomers = initCustomers();
tableOfOrders = initOrders();
tableOfProducts = initProducts();
tableOfSellers = initSellers();
//Load information from each text file
loadBillings(tableOfBillings);
loadCustomers(tableOfCustomers);
loadOrders(tableOfOrders);
loadProducts(tableOfProducts);
loadSellers(tableOfSellers);
}
/**
* Argument: NULL
* Return: void
* Description:
* This function calls the necessary methods in order to save all of the global
* structs that make up the database into their respective text files.
*/
void saveStructuresToFiles(){
saveBillings(*tableOfBillings);
saveCustomers(*tableOfCustomers);
saveOrders(*tableOfOrders);
saveProducts(*tableOfProducts);
saveSellers(*tableOfSellers);
}
/**
* Argument: char []
* Return: int
* Description:
* This function takes in the name of a seller and checks to see if the name
* already exists in the database.
*/
int checkSellerExists(char name[]){
pthread_mutex_lock(&lockSellerTable);
int count = tableOfSellers->count;
//for each entry in the table compare the sellers name and the name given
for (int i = 0; i < count; i++){
if (strcmp(tableOfSellers->entries[i].name, name) == 0){
sellerInfo = tableOfSellers->entries[i];
return 1;
}
}
pthread_mutex_unlock(&lockSellerTable);
return 0;
}
/**
* Argument: char []
* Return: int
* Description:
* This function takes in the name of a customer and checks to see if the name
* already exists in the database.
*/
int checkCustomerExists(char name[]){
pthread_mutex_lock(&lockCustomerTable);
int count = tableOfCustomers->count;
//for each entry in the table compare the customers name and the name given
for (int i = 0; i < count; i++){
if (strcmp(tableOfCustomers->entries[i].name, name) == 0){
customerInfo = tableOfCustomers->entries[i];
return 1;
}
}
pthread_mutex_unlock(&lockCustomerTable);
return 0;
}
/**
* Argument: int, int
* Return: void
* Description:
* This function is for option 2 in showBuyerMenu.
* When a customer makes a purchase, a new order and bill is created,
* and the amount of product bought is removed from the database.
*/
void newOrder(int productID, int quantity){
//create order
Order newOrder;
newOrder.productID = productID;
newOrder.numPurchased = quantity;
strcpy(newOrder.deliveryAddress, customerInfo.address);
//find the product and remove the number bought from the products database
pthread_mutex_lock(&lockProductTable);
Product product = tableOfProducts->entries[productID];
product.numAvailable -= quantity;
tableOfProducts->entries[productID].numAvailable = product.numAvailable;
pthread_mutex_unlock(&lockProductTable);
//find the total price
newOrder.totalPrice = quantity * product.price;
//add order to the database
addOrderToTable(newOrder, tableOfOrders);
//create a new bill
BillingInfo newBill;
newBill.customerID = customerInfo.customerID;
strcpy(newBill.billingAddress, customerInfo.address);
pthread_mutex_lock(&lockOrderTable);
newBill.orderID = tableOfOrders->count - 1;
pthread_mutex_unlock(&lockOrderTable);
newBill.orderPrice = newOrder.totalPrice;
//add order to the bill.
addBillingToTable(newBill, tableOfBillings);
}
/**
* Argument: int
* Return: void
* Description:
* This function is for option 3 in showBuyerMenu.
* When a customer returns a product, the order and bill associated with
* that purchase is removed, and the amount of product being returned is
* added back to the products database.
*/
void returnOrder(int orderID){
//Find order in database.
pthread_mutex_lock(&lockOrderTable);
Order orderToReturn;
int orderCount = tableOfOrders->count;
for (int i = 0; i < orderCount; i++){
if(tableOfOrders->entries[i].orderID == orderID){
orderToReturn = tableOfOrders->entries[i];
break;
}
}
pthread_mutex_unlock(&lockOrderTable);
pthread_mutex_lock(&lockProductTable);
//return products purshased to products available
int productCount = tableOfProducts->count;
for (int i = 0; i < productCount; i++){
if (tableOfProducts->entries[i].productID == orderToReturn.productID){
tableOfProducts->entries[i].numAvailable = tableOfProducts->entries[i].numAvailable + orderToReturn.numPurchased;
break;
}
}
pthread_mutex_unlock(&lockProductTable);
pthread_mutex_lock(&lockOrderTable);
//delete order from orderTable
for (int i = 0; i < orderCount; i++){
if(tableOfOrders->entries[i].orderID == orderToReturn.orderID){
for (int j = i; j < orderCount; j++){
tableOfOrders->entries[i] = tableOfOrders->entries[i+1];
}
break;
}
}
pthread_mutex_unlock(&lockOrderTable);
pthread_mutex_lock(&lockBillingTable);
//delete bill correspondint to order from billingTable
int billingCount = tableOfBillings->count;
for (int i = 0; i < billingCount; i++){
if (tableOfBillings->entries[i].orderID == orderToReturn.orderID){
for (int j = i; j < billingCount; j++){
tableOfBillings->entries[i] = tableOfBillings->entries[i+1];
}
break;
}
}
pthread_mutex_unlock(&lockBillingTable);
}
/**
* Argument: int
* Return: void
* Description:
* Adds new product from sellers into the databse structures
*/
void addProduct(int soc_conn){
Product newProduct;
char msg[1024];
writeNoInput(soc_conn, "Enter a description of the product:\n"); //Using writeNoInput here to prevent buffer issue
//ask for user input from the client
write(soc_conn, "input", sizeof("input"));
bzero(msg, sizeof(msg));
read(soc_conn, msg, sizeof(msg));
strcpy(newProduct.description, msg);
//ask client for number of product available
writeNoInput(soc_conn, "Enter the quantity of the product that is available:\n"); //Using writeNoInput here to prevent buffer issue
//ask for user input from the client
write(soc_conn, "input", sizeof("input"));
bzero(msg, sizeof(msg));
read(soc_conn, msg, sizeof(msg));
//char to int
int numAvailable = atoi(msg);
newProduct.numAvailable = numAvailable;
//enter price
writeNoInput(soc_conn, "Enter the price of the product:\n"); //Using writeNoInput here to prevent buffer issue
//ask user for input from the client
write(soc_conn, "input", sizeof("input"));
bzero(msg, sizeof(msg));
read(soc_conn, msg, sizeof(msg));
double price = atof(msg);
newProduct.price = price;
//assign the sellerID
newProduct.sellerID = sellerInfo.sellerID;
//add product to database
addProductToTable(newProduct, tableOfProducts);
}
/**
* Argument: int, int
* Return: void
* Description:
* Removing a product from the database/from being available to sell
* by the seller. The product is found in the database and is removed
*/
void removeProduct(int productID){
pthread_mutex_lock(&lockProductTable);
int count = tableOfProducts->count;
//find the product entry that matched the product ID given
for(int i = 0; i < count; i++){
if (tableOfProducts->entries[i].productID == productID){
//move all products behind the product being removed up on in the array.
for (int j = i; j < count; j++){
tableOfProducts->entries[i] = tableOfProducts->entries[i+1];
break;
}
}
}
//decrease count of database
tableOfProducts->count--;
pthread_mutex_unlock(&lockProductTable);
}
/**
* Argument: int, int
* Return: void
* Description:
* This is the function that changed the quantity the seller wants to sell of
* a product.
*/
void updateProductQuantity(int productID, int quantity){
pthread_mutex_lock(&lockProductTable);
int count = tableOfProducts->count;
//find product corresponding to given ID
for (int i = 0; i < count; i++){
if (tableOfProducts->entries[i].productID == productID){
//change the quantity
tableOfProducts->entries[i].numAvailable = quantity;
break;
}
}
pthread_mutex_unlock(&lockProductTable);
}
/**
* Argument: int, int
* Return: void
* Description:
* This function changes the price that a seller wants to sell their product at.
*/
void updateProductPrice(int productID, int newPrice){
pthread_mutex_lock(&lockProductTable);
int count = tableOfProducts->count;
//find product matching the given ID
for (int i = 0; i < count; i++){
if(tableOfProducts->entries[i].productID == productID){
//change the price of said product
tableOfProducts->entries[i].price = newPrice;
break;
}
}
pthread_mutex_unlock(&lockProductTable);
}
/**
* Argument: int
* Return: void
* Description:
* This function allows the seller to change either their name, phone number, or address.
* Sellers are not allowed to change their ID because that corresponds to their place as an
* entry in the database
*/
void updateSellerInformation(int soc_conn){
char msg[1024];
strcpy(msg, "What information would you like to update?\n");
strcat(msg, "\t1. Name\n\t2. Address\n\t3. Phone Number\n");
write(soc_conn, msg, sizeof(msg));
bzero(msg, sizeof(msg));
write(soc_conn, "input", 5);
char token[1024];
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
if(strcmp(token, "1") == 0)
{
writeNoInput(soc_conn, "What would you like to change it to?\n");
write(soc_conn, "input", sizeof("input"));
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
bzero(sellerInfo.name, sizeof(sellerInfo.name));
strcpy(sellerInfo.name, token);
writeNoInput(soc_conn, "Information Updated\n");
}
else if(strcmp(token, "2") == 0)
{
writeNoInput(soc_conn, "What would you like to change it to?\n");
write(soc_conn, "input", sizeof("input"));
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
bzero(sellerInfo.address, sizeof(sellerInfo.address));
strcpy(sellerInfo.address, token);
writeNoInput(soc_conn, "Information Updated\n");
}
else if(token[0] == '3')
{
writeNoInput(soc_conn, "What would you like to change it to?\n");
write(soc_conn, "input", sizeof("input"));
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
bzero(sellerInfo.phNumber, sizeof(sellerInfo.phNumber));
strcpy(sellerInfo.phNumber, token);
writeNoInput(soc_conn, "Information Updated\n");
}
else
{
printf("Invalid Input. Try Again.\n");
}
//Send local seller information to database
pthread_mutex_lock(&lockSellerTable);
tableOfSellers->entries[sellerInfo.sellerID] = sellerInfo;
pthread_mutex_unlock(&lockSellerTable);
return;
}
/**
* Argument: int
* Return: void
* Description:
* This function allows the customer to change either their name, phone number, or address.
* Customers are not allowed to change their ID because that corresponds to their place as an
* entry in the database
*/
void updateCustomerInformation(int soc_conn){
char msg[1024];
strcpy(msg, "What information would you like to update?\n");
strcat(msg, "\t1. Name\n\t2. Address\n\t3. Phone Number\n");
write(soc_conn, msg, sizeof(msg));
bzero(msg, sizeof(msg));
write(soc_conn, "input", 5);
char token[1024];
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
if(strcmp(token, "1") == 0)
{
writeNoInput(soc_conn, "What would you like to change it to?\n");
write(soc_conn, "input", sizeof("input"));
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
bzero(customerInfo.name, sizeof(customerInfo.name));
strcpy(customerInfo.name, token);
writeNoInput(soc_conn, "Information Updated\n");
}
else if(strcmp(token, "2") == 0)
{
writeNoInput(soc_conn, "What would you like to change it to?\n");
write(soc_conn, "input", sizeof("input"));
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
bzero(customerInfo.address, sizeof(customerInfo.address));
strcpy(customerInfo.address, token);
writeNoInput(soc_conn, "Information Updated\n");
}
else if(token[0] == '3')
{
writeNoInput(soc_conn, "What would you like to change it to?\n");
write(soc_conn, "input", sizeof("input"));
bzero(token, sizeof(token));
read(soc_conn, token, sizeof(token));
bzero(customerInfo.phNumber, sizeof(customerInfo.phNumber));
strcpy(customerInfo.phNumber, token);
writeNoInput(soc_conn, "Information Updated\n");
}
else
{
printf("Invalid Input. Try Again.\n");
}
//Send local seller information to database
pthread_mutex_lock(&lockCustomerTable);
tableOfCustomers->entries[customerInfo.customerID] = customerInfo;
pthread_mutex_unlock(&lockCustomerTable);
return;
}
/**
* Argument: char [], int
* Return: void
* Description:
* This function allows customers to view all products that they have purchased.
*/
void viewOrdersAsCustomer(char CustomerAddress[], int soc_conn){
pthread_mutex_lock(&lockOrderTable);
int count = tableOfOrders->count;
for (int i; i < count; i++){
if(tableOfOrders->entries[i].deliveryAddress == CustomerAddress){
//print out order
char msg[1024];
bzero(msg, sizeof(msg));
sprintf(msg, "\nOrderID: %d \nProductID: %d \nAmount of Product Purchased: %d \nDeliverAddress: %s \nTotal Price %f\n\n",
tableOfOrders->entries[i].orderID,
tableOfOrders->entries[i].productID,
tableOfOrders->entries[i].numPurchased,
tableOfOrders->entries[i].deliveryAddress,
tableOfOrders->entries[i].totalPrice
);
write(soc_conn, msg, sizeof(msg));
}
}
pthread_mutex_unlock(&lockOrderTable);
}
/**
* Argument: int, int
* Return: void
* Description:
* Option 6 in showBuyerMenu
* This function allows customers to view their billing information
*/
void viewBillingInfo(int customerID, int soc_conn){
pthread_mutex_lock(&lockBillingTable);
int count = tableOfBillings->count;
for (int i; i < count; i++){
if(tableOfBillings->entries[i].customerID == customerID){
//print out order
char msg[1024];
bzero(msg, sizeof(msg));
sprintf(msg, "\nOrderID: %d \nProductID: %d \nBilling Adress: %s \nOrder Price %f\n\n",
tableOfBillings->entries[i].orderID,
tableOfBillings->entries[i].customerID,
tableOfBillings->entries[i].billingAddress,
tableOfBillings->entries[i].orderPrice
);
write(soc_conn, msg, sizeof(msg));
}
}
pthread_mutex_unlock(&lockBillingTable);
}
/**
* Argument: int
* Return: void
* Description:
* This is where all products available for sell are sent to the client's terminal to be printed.
*/
void viewProductsAvailable(int soc_conn){
pthread_mutex_lock(&lockProductTable);
//print out all available products and their quantity and price
for(int i = 0; i < tableOfProducts->count; i++)
{
Product product = tableOfProducts->entries[i];
char msg[1024];
sprintf(msg, "Product ID: %d\nQuantity Available: %d\nPrice: %f\nDescription: %s\n",
product.productID,
product.numAvailable,
product.price,
product.description
);
writeNoInput(soc_conn, msg);
}
pthread_mutex_unlock(&lockProductTable);
return;
}
/**
* Argument: int
* Return: void
* Description:
* This allows sellers to see what products they have up for sell
*/
void viewProductsForSeller(int soc_conn){
int sellerID = sellerInfo.sellerID;
pthread_mutex_lock(&lockProductTable);
//for all products if sellerID matches then print out
for(int i = 0; i < tableOfProducts->count; i++)
{
if(tableOfProducts->entries[i].sellerID == sellerID) // check to see if the seller with selleID listed this product
{
Product product = tableOfProducts->entries[i]; // get product information
char msg[500];
sprintf(msg, "Product ID: %d\nQuantity Available: %d\nPrice: %f\nDescription: %s\n",
product.productID,
product.numAvailable,
product.price,
product.description
);
writeNoInput(soc_conn, msg);
}
}
pthread_mutex_unlock(&lockProductTable);
return;
}
/**
* Argument: int, int
* Return: void
* Description:
* This allows the seller to see how many orders they have for their products.
*/
void viewOrdersForProducts(int productID, int soc_conn)
{
pthread_mutex_lock(&lockOrderTable);
//for all products if sellerID matches then print out
for(int i = 0; i < tableOfOrders->count; i++)
{
if(tableOfOrders->entries[i].productID == productID)
{
Order order = tableOfOrders->entries[i]; // get product with billing information
char msg[500];
sprintf(msg, "Order ID: %d\nQuantity Purchased: %d\nTotal Order Price: %f\nDelivery Address: %s\n",
order.orderID,
order.numPurchased,
order.totalPrice,
order.deliveryAddress
);
writeNoInput(soc_conn, msg);
}
}
pthread_mutex_unlock(&lockOrderTable);
}