In: Computer Science
Please code this in C
In this project, we shall simulate the operations of an ATM machine.
Suppose you’re in charge of this simulation and here is a scenario of what is required to do:
The customer will be assigned a random number for his/her balance.
First, the customer is prompted to enter his personal identification number pin (for this case study, we test only if this pin is formed by 4 digits! otherwise, a message like “Invalid PIN, try again . . .” will be displayed) and the user is re-prompted to enter the pin. The customer is given three chances to enter his pin. If he/she fails during the three trials you display a message like “Sorry you can’t continue, contact your bank for assistance!”
If the pin is correct (formed by 4 digits), then the system will ask the customer for the receipt ( 1 for YES and 2 for NO ) and a menu will be displayed containing five possible options to choose from: Fast Cash, Deposit, Withdraw, Balance and Get Card Back.
Here is the explanation of each of the 5 options:
Get Card Back: Display the message “Goodbye! “and exit the program.
Fast Cash: Let the customer choosing the amount of cash from a menu similar to the following:
Press:
1 --> $20.00 $40.00 <-- 2
3 --> $80.00 $100.00 <-- 4
Withdraw: Prompt the user for the amount of money he/she would like to withdraw and make the sure that he/she has enough money for that!
Deposit: Prompt the customer for the amount of deposit.
Balance: Just display the amount of money the customer has.
Don’t forget to print the receipt if the customer wants one.
Sample execution: bolded text represents the user entry
Virtual Bank at West
WELCOME
Enter Pin: 245
Invalid PIN, Re-enter Pin: 5487
(clear screen )
Receipt y or Y -> Yes No <- n or N
Enter choice: N
(Clear screen)
CHOOSE FROM THE FOLLOWING
1 -> Fast Cash Withdraw <- 2
3 -> Deposit Check Balance <- 4
5 -> Get Card Back
Enter your choice: 4
(Clear screen)
Your Balance is : $124.3
1 -> Another Transaction Get Card Back <- 2
Enter your choice: 1
(Clear screen)
CHOOSE FROM THE FOLLOWING
1 -> Fast Cash Withdraw <- 2
3 -> Deposit Check Balance <- 4
5 -> Get Card Back
Enter your choice: 2
(Clear screen )
Enter amount (enter 0 to cancel): 300.00
Sorry not enough balance
Enter amount (enter 0 to cancel): 30.00
Take your cash…
(Clear screen)
Your Balance is: $124.32
1 -> Another Transaction Get Card Back <- 2
Enter your choice: 1
(Clear screen)
CHOOSE FROM THE FOLLOWING
1 -> Fast Cash Withdraw <- 2
3 -> Deposit Check Balance <- 4
5 -> Get Card Back
Enter your choice: 8
Invalid Entry
(Clear screen)
CHOOSE FROM THE FOLLOWING
1 -> Fast Cash Withdraw <- 2
3 -> Deposit Check Balance <- 4
5 -> Get Card Back
Enter your choice: 5
(Clear screen)
THANK FOR USING OUR VIRTUAL BANK SYSTEM
GOODBYE. . .
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
int main() {
int userChoice, cardPin, pinTries = 0, ch;
char rt;
double balance;
srand(time(NULL));
balance = rand() % 900 + 100;
printf("Virtual Bank at West\n\t\tWELCOME ");
do {
if (pinTries == 0)
printf("\nEnter PIN: ");
else
printf("INVALID PIN, Re-enter PIN: ");
scanf("%d", & cardPin);
pinTries++;
if (cardPin >= 1000 && cardPin < 10000) {
system("cls");
printf("Receipt y or Y --> YES\t\tn or N --> No\nEnter Choice: ");
fflush(stdin);
scanf("%c", & rt);
system("cls");
break;
}
} while (pinTries < 3);
if (pinTries < 3) {
do {
printf("\nCHOOSE FROM THE FOLLOWING \n1.Fast Cash\n2.Withdraw\n3.Deposit\n4.Check Balance\n5.Get Card Back \nEnter your choice:");
scanf("%d", & userChoice);
switch (userChoice) {
case 1:
printf("\nFast Cash:\n1-->$20.00 \t2-->$40.00\t 3-->$80.00\t4-->$100.00 ");
scanf("%d", & userChoice);
if (balance > userChoice) {
switch (userChoice) {
case 1:
balance = balance - 20;
break;
case 2:
balance = balance - 40;
break;
case 3:
balance = balance - 80;
break;
case 4:
balance = balance - 100;
break;
default:
printf("\nInvalid Choice\n");
}
} else {
system("cls");
printf("\nSorry not enough balance");
}
printf("\nYour Balance is: %f\n", balance);
break;
case 2:
printf("\nEnter amount (0 to Cancel) : ");
scanf("%d", & userChoice);
if(userChoice == 0)
break;
else if (balance > userChoice) {
balance = balance - userChoice;
system("cls");
printf("\nTake your cash… ");
} else {
system("cls");
printf("\nSorry not enough balance");
}
printf("\nYour Balance is: %f", balance);
printf("\n1.Another Transaction\n 2.Get Card Back: ");
scanf("%d",&userChoice);
if(userChoice == 2)
userChoice = 5;
break;
break;
case 3:
printf("\nEnter amount to deposit: ");
scanf("%d", & userChoice);
system("cls");
balance = balance + userChoice;
printf("\nYour final Balance is: %f", balance);
break;
case 4:
printf("\nYour Balance is: %f", balance);
break;
case 5:
printf("\nTake yoyr card.....Goodbye! ");
break;
default:
printf("\nInvalid Choice\n");
}
} while (userChoice != 5);
system("cls");
printf("\nTHANK FOR USING OUR VIRTUAL BANK SYSTEM GOODBYE");
} else
printf("\nSorry you can't continue, contact your bank for assistance!");
return 0;
}
Output: