-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.c
More file actions
305 lines (207 loc) · 6.73 KB
/
account.c
File metadata and controls
305 lines (207 loc) · 6.73 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
/*
==================================================
Assignment #2 Milestone #2
==================================================
Name : Shayan Chabook
ID : 159844208
Email : Schabook@myseneca.ca
Section: ZAA
*/
#define _CRT_SECURE_NO_WARNINGS
#include "account.h"
#include "accountTicketingUI.h"
#include "commonHelpers.h"
#include <stdio.h>
#include <string.h>
static int CurrentID = 50600;
void getAccount(struct Account *account)
{
printf("New Account Data (Account#:%d)\n", CurrentID);
printf("----------------------------------------\n");
account->id = CurrentID;
CurrentID++;
printf("Enter the account type (A=Agent | C=Customer): ");
account->type = getCharOption("AC");
printf("\n");
getPerson(&(account->person));
if (account->type == 'A')
getUserLogin(&(account->login));
else
{
account->login.username[0] = '\0';
account->login.password[0] = '\0';
}
printf("*** New account added! ***\n\n");
}
void getPerson(struct Person *person)
{
printf("Person Data Input\n");
printf("----------------------------------------\n");
printf("Enter the person's full name (30 chars max): ");
getCString(person->fullname, 1, MAX_NAME_LEN);
printf("Enter birth year (current age must be between 18 and 110): ");
int Current_Year = thisYear();
person->birth_year = getIntFromRange(Current_Year - 110, Current_Year - 18);
printf("Enter the household Income: $");
person->household_income = getPositiveDouble();
printf("Enter the country (30 chars max.): ");
getCString(person->country, 1, MAX_COUNTRY_LEN);
makeUpper(person->country);
printf("\n");
}
void getUserLogin(struct UserLogin *login)
{
printf("User Login Data Input\n");
printf("----------------------------------------\n");
char TMP_UN[MAX_USERNAME_LEN + 1];
do
{
printf("Enter user login (10 chars max): ");
getCString(TMP_UN, 1, MAX_USERNAME_LEN);
}
while (!checkUser(TMP_UN));
strcpy(login->username, TMP_UN);
char TMP_PSS[MAX_PASSWORD_LEN + 1] = {0};
do
{
printf("Enter the password (must be 8 chars in length): ");
getCString(TMP_PSS, MAX_PASSWORD_LEN, MAX_PASSWORD_LEN);
}
while (!checkPass(TMP_PSS));
strcpy(login->password, TMP_PSS);
printf("\n");
}
void updateAccount(struct Account *account)
{
while (1)
{
printf("Update Account: %d (%s)\n", account->id, account->person.fullname);
printf("----------------------------------------\n");
printf("1) Update account type (current value: %c)\n", account->type);
printf("2) Person\n");
printf("3) Login\n");
printf("0) Done\n");
printf("Selection: ");
int Option = getIntFromRange(0, 3);
printf("\n");
if (Option == 1)
{
printf("Enter the account type (A=Agent | C=Customer): ");
account->type = getCharOption("AC");
printf("\n");
if (account->type == 'A')
{
printf("Agent type accounts require a user login. Please enter this information now:\n\n");
getUserLogin(&(account->login));
}
else
{
account->login.username[0] = '\0';
account->login.password[0] = '\0';
}
}
else if (Option == 2)
updatePerson(&(account->person));
else if (Option == 3)
{
if (account->type == 'C')
printf("ERROR: Customer account types don't have user logins!\n\n");
else
updateUserLogin(&(account->login));
}
else
return;
}
}
void updatePerson(struct Person *person)
{
while (1)
{
printf("Person Update Options\n");
printf("----------------------------------------\n");
printf("1) Full name (current value: %s)\n", person->fullname);
printf("2) Household Income (current value: $%.2lf)\n", person->household_income);
printf("3) Country (current value: %s)\n", person->country);
printf("0) Done\n");
printf("Selection: ");
int Option = getIntFromRange(0, 3);
printf("\n");
if (Option == 1)
{
printf("Enter the person's full name (30 chars max): ");
getCString(person->fullname, 1, MAX_NAME_LEN);
printf("\n");
}
else if (Option == 2)
{
printf("Enter the household Income: $");
person->household_income = getPositiveDouble();
printf("\n");
}
else if (Option == 3)
{
printf("Enter the country (30 chars max.): ");
getCString(person->country, 1, MAX_COUNTRY_LEN);
makeUpper(person->country);
printf("\n");
}
else
return;
}
}
void updateUserLogin(struct UserLogin *login)
{
while (1)
{
printf("User Login: %s - Update Options\n", login->username);
printf("----------------------------------------\n");
printf("1) Password\n");
printf("0) Done\n");
printf("Selection: ");
int Option = getIntFromRange(0, 1);
printf("\n");
if (Option == 1)
{
char TMP_PSS[MAX_PASSWORD_LEN + 1] = {0};
do
{
printf("Enter the password (must be 8 chars in length): ");
getCString(TMP_PSS, MAX_PASSWORD_LEN, MAX_PASSWORD_LEN);
}
while (!checkPass(TMP_PSS));
strcpy(login->password, TMP_PSS);
printf("\n");
}
else
return;
}
}
int authenticate(const struct Account accounts[], int max_accounts)
{
char UserName[25];
char Password[25];
int Remaining_Attemps = 3;
while (Remaining_Attemps > 0)
{
int IDX = findAccountIndexByAcctNum(-1, accounts, max_accounts, 1);
printf("User Login : ");
scanf("%100[^\n]", UserName);
clearStandardInputBuffer();
printf("Password : ");
scanf("%100[^\n]", Password);
clearStandardInputBuffer();
int Is_Autherized = (IDX != -1 &&
strcmp(accounts[IDX].login.username, UserName) == 0 &&
strcmp(accounts[IDX].login.password, Password) == 0)
? 1
: 0;
if (!Is_Autherized)
{
Remaining_Attemps--;
printf("INVALID user login/password combination! [attempts remaining:%d]\n\n", Remaining_Attemps);
}
else
return IDX;
}
return 0;
}