-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
168 lines (151 loc) · 3.79 KB
/
helper.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
#include<stdio.h>
#include<stdlib.h>
struct User * openDatabase (FILE * dbFile)
{
size_t length = 0;
char * input;
char * part;
int largest = 0 ;
struct User *newUser;
struct Node *tail;
struct Node *temp;
struct Node *it;
struct User *userList;
char * username;
char * uid;
char * credits;
if (dbFile == NULL)
{
return NULL;
}
/*username | userID | credits | address | state | zip*/
while(getline(&input,&length,dbFile) != -1)
{
strcpy(username,strtok(input,"|"));
strcpy(uid,strtok(NULL,"|"));
strcpy(credits,strtok(NULL,"|"));
newUser = createUser(username,uid,atof(credits));
if (atoi(newUser->uid) > largest)
{
largest = newUser->uid;
}
if (tail != NULL)
{
temp = calloc(1,sizeof(struct Node));
temp->data = newUser;
temp->next = tail->next;
tail->next = temp;
tail = temp;
}
else
{
/*Create a new node and add to the head */
tail = calloc(1,sizeof(struct Node));
tail->data = newUser;
tail->next = tail;
}
}
userList = calloc(largest+1,sizeof(struct User));
it = tail->next;
while(it != tail)
{
userList[it->data->uid] = it->data;
temp = it;
it = it->next;
free(temp);
}
userList[it->data->uid] = it->data;
free(it);
return userList;
}
/*producer*/
void openOrders (FILE * bookOrderFile,struct User *users)
{
/*Book title | cost | userID | category */
size_t length = 0;
char * input;
char * bookTitle;
char * cost;
char * uid;
struct Node * temp;
struct User * currentUser;
if(bookOrderFile == NULL || users == NULL)
{
return;
}
while(getline(&input,&length,bookOrderFile) != -1)
{
/*start consumer thread, feed it a struct containing the input line which contains a single order and the pointer to the user database*/
/*set input to NULL so getline will allocate new memory for every order, the consumer threads are responsible for freeing their input strings*/
input=NULL;
/*
* This part needs to go in the consumer threads:
*
* strcpy(bookTitle,strtok(input,"|"));
* strcpy(cost,strtok(NULL,"|"));
* strcpy(uid,strtok(NULL,"|"));
*
* currentUser = (struct User)users[atoi(uid)];
* purchase(currentUser->success, currentUser->fail,currentUser,cost,bookTitle);
*/
}
fclose(bookOrderFile);
}
void printToFile (Node* userArray)
{
int i = 0;
int length;
if (userArray == NULL)
{
return;
}
else
{
length = sizeof(userArray)/sizeof(struct Node);
while(i<length)
{
printf("%s",(char *)userArray[i]->data->username);
printf("=== BEGIN CUSTOMER INFO ===\n");
printf("### BALANCE ###\n");
printf("Customer name: %f",userArray[i]->data->username);
printf("Customer ID number: %f",userArray[i]->data->uid);
printf("Remaining credit balance after all purchases (a dollar amount): %d",userArray[i]->data->remainingCredits);
i++;
}
}
}
void purchase(struct Node * success, struct Node * fail,struct User * currentUser,double cost,char *bookTitle)
{
struct Node *temp;
struct order *orderTemp;
if (currentUser->remainingCredits - cost < 0)
{
/*Add to the fail node list */
temp = calloc(1,sizeof(struct Node));
order = calloc(1,sizeof(struct order));
order->bookTitle = bookTitle;
order->cost = cost;
order->currentCredits = currentUser->remainingCredits;
temp->data = order;
fail->next = temp;
}
else
{
/* Add to the success node list */
temp = calloc(1,sizeof(struct Node));
order = calloc(1,sizeof(struct order));
order->bookTitle = bookTitle;
order->cost = cost;
order->currentCredits = currentUser->remainingCredits-cost;
temp->data = order;
success->next = temp;
}
}
struct User createUser(char *username,int uid,double initialCredits)
{
struct User newUser = calloc(1,sizeof(struct User));
newUser->username = username;
newUser->uid = uid;
newUser->initialCredits = initialCredits;
pthread_mutex_init(newUser->userMutex,NULL);
}