-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.c
More file actions
250 lines (228 loc) · 6.54 KB
/
2.c
File metadata and controls
250 lines (228 loc) · 6.54 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
#include <stdio.h>
#include <string.h>
#include <time.h>
using namespace std;
class Account
{
public:
Account()
{
}
char account[15];
char pin[7];
char accName[10];
double balances;
char moneyType[6];
};
bool checkValid(char s[])
{
for (int i = 0; i < strlen(s); i++)
{
if (s[i] < 48 || s[i] > 57)
return false;
}
return true;
}
int registerID()
{
//char title[4][20] = {"Account", "Name", "Balances", "MoneyType"};
//char title1[2][20] = {"Account", "Pin"};
FILE *registerAcc;
FILE *accInfo;
registerAcc = fopen("account.txt", "a");
accInfo = fopen("accInfo.txt", "a");
//fprintf(accInfo, "%-20s%-15s%-22s%-11s\n",title[0],title[1],title[2],title[3]);
//fprintf(registerAcc,"%-20s%-7s\n", title1[0], title1[1]);
if (!registerAcc)
return 1;
if (!accInfo)
return 1;
Account mrname;
printf("Enter your first name: ");
scanf("%s", &mrname.accName);
printf("name: %s\n", mrname.accName);
printf("Enter your Account code(14 numbers): ");
scanf("%s", &mrname.account);
printf("Enter your Account pin(6 numbers): ");
scanf("%s", &mrname.pin);
printf("Enter your Account Money type (USD or VND): ");
scanf("%s", &mrname.moneyType);
printf("Enter your Account balances: ");
char balances[22];
scanf("%s", &balances);
sscanf(balances, "%lf", &mrname.balances);
fprintf(registerAcc, "%-20s%-7s\n", mrname.account, mrname.pin);
fprintf(accInfo, "%-20s%-15s%-22.3lf%-11s\n", mrname.account, mrname.accName, mrname.balances, mrname.moneyType);
fclose(registerAcc);
fclose(accInfo);
}
int countUser()
{
int length = 0;
FILE *count;
char buf[1000];
count = fopen("account.txt", "r");
while (fgets(buf, 1000, count) != NULL)
{
length += 1;
}
fclose(count);
return length;
}
int getUser(char logCode[])
{
char logPin[7];
printf("Account Code: ");
scanf("%s", &logCode);
printf("PIN: ");
scanf("%s", &logPin);
char tempCode[15];
char tempPin[7];
FILE *login;
login = fopen("account.txt", "r");
if (!login)
return 0;
while (fscanf(login, "%s%s", &tempCode, &tempPin) == 2)
{
if (strcmp(logCode, tempCode) == 0 && strcmp(logPin, tempPin) == 0)
{
fclose(login);
return 1;
}
}
fclose(login);
return 0;
}
void currTime(char s[])
{
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strcpy(s, asctime(timeinfo));
}
void update(char logCode[], double balances)
{
char tempName[15];
char tempAccount[20];
double tempBalances;
char tempMoneyType[11];
char title[4][20] = {"Account", "Name", "Balances", "MoneyType"};
FILE *copy;
FILE *paste;
copy = fopen("accInfo.txt", "r");
paste = fopen("newInfo.txt", "w");
fprintf(paste, "%-20s%-15s%-22s%-11s\n", title[0], title[1], title[2], title[3]);
while (fscanf(copy, "&s&s&f&s", &tempAccount, &tempName, &tempBalances, &tempMoneyType) == 4)
{
if (strcpy(logCode, tempAccount) != 0)
fprintf(paste, "%-20s%-15s%-22.3lf%-11s\n", tempAccount, tempName, tempBalances, tempMoneyType);
else
fprintf(paste, "%-20s%-15s%-22.3lf%-11s\n", tempAccount, tempName, balances, tempMoneyType);
}
fclose(copy);
fclose(paste);
remove("accInfo.txt");
rename("newInfo", "accInfo.txt");
}
void withdrawTrans(char logCode[], double amount)
{
char tempName[15];
char tempAccount[20];
double tempBalances;
char tempMoneyType[11];
char title[5][20] = {"Account", "Name", "Amount", "MoneyType", "Time"};
char time[28];
currTime(time);
FILE *saveWithdraw;
FILE *readTrans;
saveWithdraw = fopen("withdraw.txt", "a");
readTrans = fopen("accInfo.txt", "r");
fprintf(saveWithdraw, "%-15s%-22lf%-11s%-28s\n", title[0], title[1], title[2], title[3]);
while (fscanf(readTrans, "&s&s&lf&s", &tempAccount, &tempName, &tempBalances, &tempMoneyType) == 4)
{
if (strcpy(logCode, tempAccount) == 0)
{
if (amount > tempBalances)
{
fclose(saveWithdraw);
fclose(readTrans);
printf("Not enough money to withdraw!\n");
}
else
fprintf(saveWithdraw, "%-20s%-15s%-22lf%-11s%-29s\n", tempAccount, tempName, amount, tempMoneyType, time);
}
}
fclose(saveWithdraw);
fclose(readTrans);
update(logCode, tempBalances - amount);
}
void transferTrans(double amount, char dest[], char logCode[])
{
char tempName[15];
char tempAccount[20];
double tempBalances;
char tempMoneyType[11];
char time[28];
char title[8][20] = {"AccountSrc", "NameSrc", "AccountDest", "NameDest", "Amount", "MoneyType", "Time"};
currTime(time);
FILE *transfer;
FILE *readTrans;
transfer = fopen("transfer.txt", "a");
readTrans = fopen("accInfo.txt", "r");
fprintf(transfer, "%-20s%-15%-20s%-15s%-22lf%-11s%-28s\n", title[0], title[1], title[2], title[3], title[4], title[5], title[6]);
while (fscanf(readTrans, "%s%s%lf%s", &tempName, &tempAccount, &tempBalances, &tempMoneyType) == 4)
{
}
}
int main()
{
int tc;
int n;
do
{
printf("1. Login\n");
printf("2. Register new ATM card\n");
printf("3. Exit\n");
printf("Choose (1-3): ");
scanf("%d%c", &tc, &n);
if (tc == 1)
{
char logCode[20];
if (getUser(logCode))
{
printf("ACCESS GRANTED!!\n");
int tc1;
do
{
int n1;
printf("1. Withdaw\n");
printf("2. Transfer\n");
printf("3. Exit\n");
printf("Choose (1-3): ");
scanf("%d%c", &tc1, &n1);
if (tc1 == 1)
{
}
if (tc1 == 2)
{
}
} while (tc1 != 3);
}
else
printf("ACCESS DENIED!!\n");
}
else if (tc == 2)
{
char u[] = "user";
char str[3];
int th = countUser();
//sprintf(str,"%d",th);
//strcpy(user[th], strcat(u,str));
//Account user[th];
registerID();
}
} while (tc != 3);
}