-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATMSimulator.c
231 lines (175 loc) · 5.7 KB
/
ATMSimulator.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct node {
char statement[50];
struct node* link;
} node;
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);
}
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;
}
int checkPin(void) {
FILE *fp;
char pin[8];
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);
}
while (fgets(pin, sizeof(pin), fp) != NULL) {
if (strstr(pin, keyPin)) {
pinValid = 1;
}
}
fclose(fp);
return pinValid;
}
void pinGeneration(void) {
FILE *fp;
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);
}
fprintf(fp, "%d\n", generatedPin);
fclose(fp);
}
void showBalance(int *balance) {
printf("\nYour current balance is Rs.%d\n", *balance);
}
void depositMoney(node **head, int *balance) {
int depositAmount;
char depositStmt[50];
printf("\nEnter amount to deposit: ");
scanf("%d", &depositAmount);
if (depositAmount > 0) {
*balance += depositAmount;
printf("\nRs.%d deposited\n", depositAmount);
snprintf(depositStmt, sizeof(depositStmt), "Rs.%d deposited\n", depositAmount);
saveHistory(head, depositStmt);
} else {
printf("\nInvalid amount entered\n.");
}
}
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);
snprintf(withdrawStmt, sizeof(withdrawStmt), "Rs.%d withdrawn\n", withdrawAmount);
saveHistory(head, withdrawStmt);
}
} else {
printf("\nInvalid amount entered\n.");
}
}
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++;
}
}
void removeHistory(node **head) {
node *temp;
temp = *head;
*head = (*head)->link;
temp->link = NULL;
free(temp);
}
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;
}
}
}