-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathatm.c
More file actions
271 lines (233 loc) · 6.95 KB
/
Copy pathatm.c
File metadata and controls
271 lines (233 loc) · 6.95 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
/*
node structure for implementing a queue to store transaction history
*/
typedef struct node {
char statement[50];
struct node* link;
} node;
/*
ATM function prototypes
*/
void pinGeneration(void);
int checkPin(void);
void showBalance(int *);
void depositMoney(node **, int *);
void withdrawMoney(node **, int *);
void saveHistory(node **, char *);
void removeHistory(node **);
void showHistory(node **);
int main(void) {
int choice1, choice2;
int pinValid = 0, balance = 0;
node *head = NULL;
while (1) {
printf("\n\nATM System\n======================\n");
printf("1. Generate PIN\n2. Use ATM\n3. Exit\n");
printf("\nYour choice: ");
scanf("%d", &choice1);
switch (choice1) {
case 1: pinGeneration();
exit(EXIT_SUCCESS);
case 2: pinValid = checkPin();
if (pinValid) {
printf("\nValid PIN\n");
} else {
printf("\nInvalid PIN. Please generate a PIN if you don't have one.\n");
exit(EXIT_FAILURE);
}
/*
On valid PIN entry by user, the ATM Menu is presented to the user
*/
while(pinValid) {
printf("\nATM System Menu\n===============\n\n");
printf("1. Check Balance\n2. Deposit\n3. Withdraw\n4. View transaction history\n5. Quit\n\n");
printf("Enter choice: ");
scanf("%d", &choice2);
switch(choice2) {
case 1: showBalance(&balance);
break;
case 2: depositMoney(&head, &balance);
break;
case 3: withdrawMoney(&head, &balance);
break;
case 4: showHistory(&head);
break;
case 5: printf("\nThank you for using the ATM\n");
exit(EXIT_SUCCESS);
default: printf("\nInvalid option entered!\n");
break;
}
}
case 3: exit(EXIT_SUCCESS);
default: printf("\nInvalid choice...Try again...\n");
break;
}
}
return 0;
}
/*
This function will search for the PIN entered by the user
in the file where the PIN numbers are stored
If PIN is found return 1
Otherwise, return 0
*/
int checkPin(void) {
FILE *fp;
// buffer to read PIN and store from file
char pin[8];
// buffer to read PIN and store from user
char keyPin[8];
int pinValid = 0;
printf("\n\nEnter the PIN: \n");
scanf("%s", keyPin);
fp = fopen("pin.txt", "r");
if (NULL == fp) {
printf("\nFile cannot be opened\n");
exit(EXIT_FAILURE);
}
/*
Search for the PIN entered by user in file pin.txt
*/
while (fgets(pin, sizeof(pin), fp) != NULL) {
if (strstr(pin, keyPin)) {
pinValid = 1;
}
}
fclose(fp);
return pinValid;
}
/*
This function will generate a 4-digit random number that
is considered as PIN
*/
void pinGeneration(void) {
FILE *fp;
/*
Generate a random 4 digit number
*/
srand(time(NULL));
int generatedPin = 1000+rand()%9000;
printf("\nPIN generated successfully\n");
printf("\nYour generated PIN: %d\n", generatedPin);
printf("\nRe-run the program and use ATM with this PIN\n\n");
fp = fopen("pin.txt", "a");
if (NULL == fp) {
printf("\nCannot open file!");
exit(EXIT_FAILURE);
}
/*
Write PIN to the file
*/
fprintf(fp, "%d\n", generatedPin);
fclose(fp);
}
/*
This function will display the current balance amount
*/
void showBalance(int *balance) {
printf("\nYour current balance is Rs.%d\n", *balance);
}
/*
This function will add the money deposited to the balance
*/
void depositMoney(node **head, int *balance) {
int depositAmount;
/*
buffer to store
*/
char depositStmt[50];
printf("\nEnter amount to deposit: ");
scanf("%d", &depositAmount);
if (depositAmount > 0) {
*balance += depositAmount;
printf("\nRs.%d deposited\n", depositAmount);
/*
saving formatted string in depositStmt character array
*/
snprintf(depositStmt, sizeof(depositStmt), "Rs.%d deposited\n", depositAmount);
saveHistory(head, depositStmt);
} else {
printf("\nInvalid amount entered\n.");
}
}
/*
This function will deduct the money withdrawn from the balance
*/
void withdrawMoney(node **head, int *balance) {
int withdrawAmount;
char withdrawStmt[50];
printf("\nEnter amount to withdraw: ");
scanf("%d", &withdrawAmount);
if (withdrawAmount > 0) {
if (withdrawAmount > *balance) {
printf("\nCannot withdraw. Balance Rs.%d\n", *balance);
} else {
*balance -= withdrawAmount;
printf("\nRs.%d withdrawn\n", withdrawAmount);
/*
saving formatted string in withdrawStmt character array
*/
snprintf(withdrawStmt, sizeof(withdrawStmt), "Rs.%d withdrawn\n", withdrawAmount);
saveHistory(head, withdrawStmt);
}
} else {
printf("\nInvalid amount entered\n.");
}
}
/*
This function will save a transaction statement
*/
void saveHistory(node **head, char *str) {
static int count = 0;
node *temp;
temp = (node *)malloc(sizeof(node));
strcpy(temp->statement, str);
temp->link = NULL;
if (NULL == *head) {
*head = temp;
count++;
} else {
if (10 == count) {
removeHistory(head);
count--;
}
node *p;
p = *head;
while (NULL != p->link) {
p = p->link;
}
p->link = temp;
count++;
}
}
/*
This function is used to remove the oldest transaction when
10 transactions are made
*/
void removeHistory(node **head) {
node *temp;
temp = *head;
*head = (*head)->link;
temp->link = NULL;
free(temp);
}
/*
This function will display the transaction history
*/
void showHistory(node **head) {
node *temp;
temp = *head;
if (NULL == temp) {
printf("\nNo transaction history...\n");
} else {
printf("\nTransaction History\n-------------------\n\n");
while (NULL != temp) {
printf("%s\n", temp->statement);
temp = temp->link;
}
}
}