-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject3.c
334 lines (314 loc) · 7.85 KB
/
project3.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
struct single_node
{
float D_W;
float current;
struct single_node *next;
};
struct node
{
char ATM_number[30];
int pin;
struct node *prv;
struct node *next;
struct single_node *transactionlog;
};
struct node *head = NULL;
struct node *top = NULL;
void balance_node(struct node **account_address, float D_W)
{
struct single_node **head_bal = &((*account_address)->transactionlog);
struct single_node *new_node;
new_node = (struct single_node *)malloc(sizeof(struct single_node));
if (new_node == NULL)
{
printf("Memory allocation fails.\n");
return;
}
else if ((*head_bal) == NULL)
{
new_node->D_W = D_W;
new_node->current = D_W;
new_node->next = NULL;
(*head_bal) = new_node;
}
else
{
new_node->D_W = D_W;
new_node->current = ((*head_bal)->current) + D_W;
new_node->next = (*head_bal);
(*head_bal) = new_node;
}
}
//
struct node *isalreadyexit(char *ATM_number)
{
struct node(*ptr) = head;
if (ptr == NULL)
{
return NULL;
}
while (ptr != NULL)
{
if(strcmp((ptr->ATM_number),ATM_number) == 0)
{
return ptr;
}
ptr=ptr->next;
}
return NULL;
}
//
struct node *newuser_details(char *ATM_number, int pin)
{
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("Memory allocation fails.\n");
return NULL;
}
else if (head == NULL)
{
new_node->prv = NULL;
new_node->next = NULL;
strcpy(new_node->ATM_number, ATM_number);
// new_node->ATM_number=ATM_number;
new_node->pin = pin;
new_node->transactionlog = NULL;
top = head = new_node;
}
else
{
new_node->prv = top;
new_node->next = NULL;
strcpy(new_node->ATM_number, ATM_number);
// new_node->ATM_number=ATM_number;
new_node->pin = pin;
new_node->transactionlog = NULL;
top->next = new_node;
top = new_node;
}
return top;
}
//
//
float Balance(struct node **acc_address, int pin)
{
if ((*acc_address) == NULL || ((*acc_address)->transactionlog) == NULL)
{
return;
}
if ((*acc_address)->pin != pin)
{
printf("Invalid pin\n\n");
return;
}
return ((*acc_address)->transactionlog)->current;
}
int Deposit(struct node **acc_address, int pin, float deposit)
{
if (((*acc_address)->pin) != pin)
{
printf("Invalid pin\n\n");
return 0;
}
balance_node(acc_address, deposit);
return 1;
}
//
int Withdraw(struct node **acc_address, int pin, float Withdraw)
{
if ((*acc_address)->pin != pin)
{
printf("Invalid pin");
return 0;
}
if((((*acc_address)->transactionlog)->current) < Withdraw)
{
printf("\n\t\t\tYour balance is %f.\n so the Withdrawal of amount %f not possible.\n\n",((*acc_address)->transactionlog)->current,Withdraw);
return 0;
}
balance_node(acc_address, (-Withdraw));
return 1;
}
//
void Last_Transactions(struct node **acc_address, int pin)
{
if ((*acc_address) == NULL || ((*acc_address)->transactionlog) == NULL)
{
return;
}
struct single_node *tamp = (*acc_address)->transactionlog;
printf("\n\t\t### ([D_W] \t\t [Balance])\n");
while (tamp != NULL)
{
printf("\t\t### ([%f] \t [%f])\n", tamp->D_W, tamp->current);
tamp = tamp->next;
}
}
void w_on_file(struct node **acc_address, int pin, char *f_name)
{
if ((*acc_address) == NULL || ((*acc_address)->transactionlog) == NULL)
{
return;
}
struct single_node *tamp = (*acc_address)->transactionlog;
FILE *fptr;
fptr = fopen(f_name, "w");
if (fptr == NULL)
{
printf("Error opening file!\n");
return;
}
char str[30];
strcpy(str,((*acc_address)->ATM_number));
fprintf(fptr, "\n\t\t###\t\t\t\t %s newuser_details\n\n", str);
strcat(str, ".txt");
fprintf(fptr, "\n\t\t### ([D_W] \t\t \t\t [Balance])\n");
while (tamp != NULL)
{
fprintf(fptr, "\t\t### ([%.2f] \t \t\t [%.2f])\n", tamp->D_W, tamp->current);
tamp = tamp->next;
}
fclose(fptr);
}
int is_valid_atm_no(char *ATM_number)
{
if (ATM_number!=NULL && strlen(ATM_number)== 12)
{
int i=0;
for(i=0;i<12;i++)
{
if(!isdigit(ATM_number[i]))
{
printf("\nAll characters must be digits\n");
return 1;
}
}
return 0;
}
else
{
printf("\nSize of ATM_number must be 12 digits\n");
return 1;
}
}
int main()
{
char str[6];
do
{
char ATM_number[30];
printf("\t\t\t\tSTART\n\n\n");
//label :
printf("Enter your ATM_number[12 digit] : ");
scanf("%s",ATM_number);
if (is_valid_atm_no(ATM_number))
{
printf("Invalid - ATM_number\n");
// goto label;
continue;
}
struct node *exist;
exist = isalreadyexit(ATM_number);
if (exist == NULL)
{
printf("You need to add your card first to continue.\n");
int pin;
printf("Enter your pin : ");
scanf("%d", &pin);
exist = newuser_details(ATM_number, pin);
printf("Your account added susscefully.\n");
}
int option = 1;
while (option)
{
printf("\n\t\tWELCOME SCREEN\n");
printf("\t# 1.Balance(Enter-1).\n");
printf("\t# 2.Deposit(Enter-2).\n");
printf("\t# 3.Withdraw(Enter-3).\n");
printf("\t# 4.Last Transactions(Enter-4).\n");
printf("\t# 5.Make Last Transactions txt file (Enter-5).\n");
printf("\t# 0.Exit(Enter-0).\n");
printf("Enter the option : ");
scanf("%d", &option);
switch (option)
{
case 1:
{
int pin;
printf("\t# Enter the pin(4-digit).\n");
scanf("%d", &pin);
printf("Your Balance is : %f", Balance(&exist, pin));
break;
}
case 2:
{
int pin;
float deposit;
printf("\t# Enter the pin(4-digit).\n");
scanf("%d", &pin);
printf("\t# Enter the amount to diposit.\n");
scanf("%f", &deposit);
int susscefully_transection = Deposit(&exist, pin, deposit);
if (susscefully_transection != 0)
{
printf("Your Deposit of rupess %f is susscefully\n", deposit);
printf("Your Balance is now : %f.\n", Balance(&exist, pin));
}
break;
}
case 3:
{
int pin;
float Withdrawal;
printf("\t# Enter the pin(4-digit).\n");
scanf("%d", &pin);
printf("\t# Enter the Withdrawal amount.\n");
scanf("%f", &Withdrawal);
int susscefully_transection = Withdraw(&exist, pin, Withdrawal);
if (susscefully_transection != 0)
{
printf("Your Withdrawal of rupess %f is susscefully\n", Withdrawal);
printf("Your Balance is : %f", Balance(&exist, pin));
}
break;
}
case 4:
{
int pin;
printf("\t# Enter the pin(4-digit).\n");
scanf("%d", &pin);
Last_Transactions(&exist, pin);
break;
}
case 5:
{
int pin;
printf("\t# Enter the pin(4-digit).\n");
scanf("%d", &pin);
char str[30];
strcpy(str,ATM_number);
// sprintf(str, "%ls", ATM_number); // Convert integer to string
strcat(str, ".txt");
w_on_file(&exist, pin, str);
printf("\n\t\t\t Your file created successfully having file name %s.\n",str);
break;
}
default:
{
printf("Exit the program.\n");
break;
}
}
}
printf("Enter `close` to close the machine or `new` to continue: ");
scanf("%s", str);
} while (strcmp(str, "close") != 0);
return 0;
}