Introduction
Welcome to the world of C programming! In this blog post, we will embark on an exciting journey to build an Automatic Teller Machine (ATM) prototype using the C language. This project will allow us to simulate an ATM, enabling users to perform various banking transactions such as withdrawals, balance inquiries, bill payments, fund transfers, and deposits.
Table of Content
Project Setup
Before we dive into the code, let's set up our development environment. We will be using the Code::Blocks IDE for this project. Follow these steps to get started:
- Download and install Code::Blocks on your preferred operating system (Windows, macOS, or Linux) from the codeblocks.org website
- Create a new folder on your desktop and rename it ATM (This is because the receipt generated by the ATM prototype will be saved in this folder
- Launch Code::Blocks. To create a new C project, click on the new file icon on the menu bar and click on empty file.
- Save the file as atmPrototype.c to the ATM folder you created earlier
- All the images below show step 2 down to step 4
step 2: create a folder |
step 3: create a new file |
step 4: save the new file |
Short Note
Note that this program has the current information of 10 customers. The information cannot be altered automatically, only manually. What ever change carried out during any transaction will not be reflected when the program is re-run except the change and new information has been manually updated.
State Variables
The first step of our ATM C project is to state the variables to be used by the programs and also create the files that will be used by the program to generate receipt for each transaction carried out. The code is shown below. The the following code in your new file that is your atmPrototype.c file.
#include <stdio.h>
#include <stdlib.h>
FILE * fco;
FILE * fct;
FILE * fcth;
FILE * fcfr;
FILE * fcfi;
main()
{
int ctr; //Loop counter
int pinSearch; // to look for customers using the pin
int found = 0; // will become 1(true) if a customer is found
/* Defines the 10 customers in the two parallel array. first array
to hold the pin and the second array to hold the account balance */
int pin[10] = {4121, 3231, 5162, 7181, 3321, 8979, 9323, 2023, 2152,
6564};
float custBal[10] = {126772.67, 51123.61, 3321.82, 116371.31,
1821.37, 11756.70, 2521.32, 907.21,
37121.91, 5381.87};
int pick1, pick2; //accept choices from users
int account; //allow users to input 9 digit account number when
//transferring funds
float money; //allow users to input the amount of money they want
char words[20]; //allow users to input string characters when required
int year; //variable to hold year when needed
Verifying the User:
The second step in our ATM project is to verify the user's identity using a 4-digit PIN. We have predefined information for ten customers, including their PINs and account balances. Here's how the verification process works:
- The user is prompted to enter their 4-digit PIN.
- The program searches for the entered PIN in the customer database.
- If the PIN is correct, the user is allowed to proceed with the ATM functionalities; otherwise, the program exits.
The following code shows the verification step. type the code below immediately after the previous code. Remember everything goes into the main function
// The following code perform verification for the user
printf("\tWELCOME TO NIMARK BANK");
printf("\nPlease verify yourself\n");
printf("please input your 4 digit pin\n\n");
printf("\t");
scanf(" %d", &pinSearch);
//Search to see if the customer exist in the array
for (ctr = 0; ctr < 10; ctr++)
{
if (pinSearch == pin[ctr])
{
found = 1;
break;
}
}
if (found)
{
printf("\nYou have been verified");
printf("\nYou may now continue to your transaction");
}
else
{
printf("\nYour pin is incorrect!\n\n")
exit(1);
}
Main Menu and Transaction Selection:
Once the user is verified, the main menu will be displayed, presenting various transaction options. The user will be asked to pick a number corresponding to the desired transaction. The program will repeatedly prompt the user until a valid choice is made. This prompt is done using a do--while loop and the various transaction is done using a switch--case. The following code shows the Main Menu for the ATM C project, opens the do--while loop and opens the switch--case loop. Type the following code below after the previous verification code.
//The following transactions can be performed by the user
printf("\n\n\tNIMARK BANK");
printf("\nWhat transaction will you like to perform?\n");
printf("1. Withdraw Cash \t2. Check Balance\n");
printf("3. Pay Bills \t\t4. Transfer Funds\n");
printf("5. Make Deposit \t6. Cancel\n");
do
{
printf("Pick a Number: ");
scanf(" %d", &pick1);
switch(pick1)
{
/*The following codes will use the case to pick out each
transaction to perform using a series of if and else conditions
and receipt generated when required*/
Withdraw Cash (Transaction 1):
In this section, the user can withdraw cash from their account. They have the option to enter the withdrawal amount or cancel the transaction. Here's how it works:
- The user selects the "Withdraw Cash" option from the main menu.
- The program prompts the user to enter the withdrawal amount or cancel the transaction.
- The program checks if the withdrawal amount is within the customer's balance and withdrawal limit.
- If the transaction is successful, the customer's balance is updated, and a receipt is generated.
The withdraw cash is shown in case (1). The following code shows how the withdraw cash transaction explained above is been performed. Type the code below immediately after the previous main menu code. Do leave a line before you start typing.
case (1): //This will perform transaction 1 (withdraw cash)
{
printf("\n\nHow much cash do you want to withdraw?");
printf("\nThe limit for withdrawer is #50000.00");
printf("\n1. Enter Amount \t2. Cancel");
printf("\nPick a number: ");
scanf(" %d", &pick2);
if (pick2 == 1)
{
printf("\nEnter Amount (XX.XX): \t");
scanf(" %f", &money);
if ((money < custBal[ctr]) && (money < 50000.00))
{
printf("\n\n\nYour transaction is being processed.");
custBal[ctr] -= money;
printf("\n\tYour receipt has been generated.");
printf("\n\tIt contains your balance.");
printf("\nThanks for banking with us\n\n\n\n");
//Code to generate receipt
fco = fopen("C:\\users\\FAVOUR\\Desktop\\ATM\\TransReceipt.txt", "w");
// if the receipt generation fails
if (fco == 0)
{
printf("Error---Receipt could not be generated.\n");
exit(1);
}
// code to print receipt
fprintf(fco, "\n\nNIMARK BANK\n");
fprintf(fco, "Your transaction was successful\n\n");
fprintf(fco, "Debited: %.2f\n", money);
fprintf(fco, "Balance: %.2f\n", custBal[ctr]);
fprintf(fco, "\n\n");
fclose(fco);
break;
} else
{
printf("\nThis transaction cannot be processed");
printf("\nDue to insufficient balance or withdrawer limit exceeded");
printf("\nThanks for banking with us\n\n");
break;
}
} else if (pick2 == 2)
{
exit(1);
} else
{
printf("\nYou have not entered a valid choice!\n");
break;
}
}
NOTE: You should replace the file directory where you want your receipt to be saved in. My file directory is "C:\\users\\FAVOUR\\Desktop\\ATM" replace this. To know your file directory, right click on your ATM project C file that is atmPrototype.c file and then clicking the properties, check the location as shown in the image below
checking the directory of your C project file |
Check Balance (Transaction 2):
The "Check Balance" option allows users to view their account balance. Here's how it works:
- The user selects the "Check Balance" option from the main menu.
- The program displays the customer's account balance.
- A receipt with the account balance is generated.
The check balance is shown in case (2). The following code shows how the check balance transaction explained above is been performed. Type the code below immediately after the previous withdraw cash code leaving at least a line before you start typing.
case (2): //This case will perform transaction 2: Check Balance
{
printf("\n\nYour Balance is: %.2f", custBal[ctr]);
printf("\nYour Receipt has been generated");
printf("\nThanks for Banking with us!!!\n\n");
//code to generate receipt
fct = fopen("C:\\Users\\FAVOUR\\Desktop\\ATM\\AcctBalance.txt", "w");
// if the receipt generation fails
if (fct == 0)
{
printf("Error---Receipt could not be generated\n");
}
// code to print receipt
fprintf(fct, "\n\nNIMARK BANK\n");
fprintf(fct, "Your current Account Balance: %.2f\n", custBal[ctr]);
fprintf(fct, "\n\n");
fclose(fct);
break;
}
Remember to replace my directory with yours. Else no receipt will be generated
Pay Bills (Transaction 3):
With the "Pay Bills" option, users can pay electricity or water bills. They have the option to cancel the transaction. Here's how it works:
- The user selects the "Pay Bills" option from the main menu.
- The program prompts the user to choose between paying electricity or water bills or cancel the transaction.
- The user enters relevant details such as the month, year, amount, account number, and bank name.
- The program verifies if there are sufficient funds to pay the bill and updates the balance accordingly.
- A receipt with the transaction details is generated.
The Pay bills transaction is shown in case (3). The following code shows how the pay bills transaction explained above is been performed. Type the code below immediately after the previous check balance code leaving at least a line before you start typing.
case (3): // This case will perform transaction 3: Pay Bills
{
printf("\n\nPay Bills");
printf("1. Electricity bill \t2. Water bill");
printf("\n3. Cancel");
printf("Pick a number: ");
scanf(" %d", &pick2);
if (pick2 == 1)
{
printf("\nWhich Month are you paying for: ");
scanf(" %s", words);
printf("Which year are you paying for: ");
scanf(" %d", &year);
printf("Enter amount for electricity bill (xx.xx): ");
scanf(" %f", &money);
printf("Enter account number (9 digit): ");
scanf(" %d", &account);
if (money < custBal[ctr])
{
printf("\nYour Receipt");
printf("\nELECTRICITY HOLDING COMPANY (EHC)");
printf("\nDestination Account number: %d", account);
printf("\nPayment Month: %s\tYear: %d", words, year);
printf("\nAmount Paid: %.2f", money);
printf("\n\nYour Transaction was successful!");
printf("\nYour receipt has been generated!");
printf("\nThank you for banking with us");
custBal[ctr] -= money;
printf("\n\nYour balance is: %.2f\n\n", custBal[ctr]);
//code to generate receipt
fcth = fopen("C:\\users\\Favour\\Desktop\\ATM\\billReceipt_1.txt", "w");
// if the receipt generation fails
if (fcth == 0)
{
printf("Error---Receipt could not be generated!\n");
exit(1);
}
//code to print receipt
fprintf(fcth, "\n\nNIMARK BANK\n");
fprintf(fcth, "\nYour Receipt for Electricity Bill");
fprintf(fcth, "\n\nYour transaction was successful");
fprintf(fcth, "\n\nELECTRICITY HOLDING COMPANY (EHC)");
fprintf(fcth, "\nDestination Account Number: %d", account);
fprintf(fcth, "\nPayment Month: %s\tYear: %d", words, year);
fprintf(fcth, "\nAmount paid: %.2f", money);
fprintf(fcth, "\nCurrent Balance: %.2f\n", custBal[ctr]);
fprintf(fcth, "\n\n");
fclose(fcth);
break;
} else
{
printf("\nThis transaction cannot be processed");
printf("\nDue to insufficient balance");
printf("\nThanks for banking with us!!\n\n");
break;
}
} else if (pick2 == 2)
{
printf("\nWhich Month are you paying for: ");
scanf(" %s", words);
printf("Which year are you paying for: ");
scanf(" %d", &year);
printf("Enter amount for water bill (xx.xx): ");
scanf(" %f", &money);
printf("Enter account number (10 digit): ");
scanf(" %d", &account);
if (money < custBal[ctr])
{
printf("\nYour Receipt");
printf("\nWATER BOARD COMPANY (WBC)");
printf("\nDestination Account number: %d", account);
printf("\nPayment Month: %s\tYear: %d", words, year);
printf("\nAmount Paid: %.2f", money);
printf("\n\nYour Transaction was successful!");
printf("\nYour receipt has been generated!");
printf("\nThank you for banking with us");
custBal[ctr] -= money;
printf("\n\nYour balance is: %.2f\n\n", custBal[ctr]);
//code to generate receipt
fcth = fopen("C:\\users\\Favour\\Desktop\\ATM\\billReceipt_2.txt", "w");
// if the receipt generation fails
if (fcth == 0)
{
printf("Error---Receipt could not be generated!\n");
exit(1);
}
//code to print receipt
fprintf(fcth, "\n\nNIMARK BANK\n");
fprintf(fcth, "\nYour Receipt for Electricity Bill");
fprintf(fcth, "\n\nYour transaction was successful");
fprintf(fcth, "\n\nWATER BOARD COMPANY (WBC)");
fprintf(fcth, "\nDestination Account Number: %d", account);
fprintf(fcth, "\nPayment Month: %s\tYear: %d", words, year);
fprintf(fcth, "\nAmount paid: %.2f", money);
fprintf(fcth, "\nCurrent Balance: %.2f\n", custBal[ctr]);
fprintf(fcth, "\n\n");
fclose(fcth);
break;
} else
{
printf("\nThis transaction cannot be processed");
printf("\nDue to insufficient balance");
printf("\nThanks for banking with us!!\n\n");
break;
}
} else if (pick2 == 3)
{
exit (1);
} else
{
printf("\nYou have not entered a valid choice!\n");
break;
}
}
Remember to replace my directory with yours. Else no receipt will be generated
Transfer Funds (Transaction 4):
The "Transfer Funds" option allows users to transfer money to another account. Here's how it works:
- The user selects the "Transfer Funds" option from the main menu.
- The program prompts the user to enter the recipient's account number, bank name, and transfer amount.
- The program checks if the transfer amount is within the customer's balance.
- Both the sender's and recipient's balances are updated, and a receipt is generated.
The transfer funds transaction is shown in case (4). The following code shows how the Transfer transaction explained above is been performed. Type the code below immediately after the previous pay bills code leaving at least a line before you start typing.
case (4): //This case will perform transaction 4: transfer funds
{
printf("\n\nEnter account number (9 digit): ");
scanf(" %d", &account);
printf("Enter bank name (in one word): ");
scanf(" %s", words);
printf("Enter amount (format: xx.xx): ");
scanf(" %f", &money);
if (money < custBal[ctr])
{
custBal[ctr] -= money;
printf("\nYour transaction was successful");
printf("\nYour new balance is: %.2f", custBal[ctr]);
printf("\nYour receipt has been generated!");
printf("\nThanks for banking with us\n\n");
//code to generate receipt
fcfr = fopen("C:\\users\\Favour\\Desktop\\ATM\\fundTransfer.txt", "w");
// if the receipt generation fails
if (fcfr == 0)
{
printf("Error---Receipt could not be generated!\n");
exit(1);
}
//code to print receipt
fprintf(fcfr, "\n\nNIMARK BANK\n");
fprintf(fcfr, "\nYour Receipt for Funds Transfer");
fprintf(fcfr, "\n\nYour transaction was successful");
fprintf(fcfr, "\nDestination Account Number: %d", account);
fprintf(fcfr, "\nBank Name: %s", words);
fprintf(fcfr, "\nAmount Transfered: %.2f", money);
fprintf(fcfr, "\nCurrent Balance: %.2f\n", custBal[ctr]);
fprintf(fcfr, "\n\n");
fclose(fcfr);
break;
} else
{
printf("\nYour transaction cannot be processed");
printf("\nDue to insufficient balance");
printf("\nThanks for banking with us\n\n");
break;
}
}
Remember to replace my directory with yours. Else no receipt will be generated
Make Deposit (Transaction 5):
The "Make Deposit" option enables users to deposit money into their accounts. Here's how it works:
- The user selects the "Make Deposit" option from the main menu.
- The user enters the amount to deposit.
- The customer's balance is updated, and a receipt is generated.
The Make Deposit transaction is shown in case (5). The following code shows how the make deposit transaction explained above is been performed. Type the code below immediately after the previous transfer funds code leaving at least a line before you start typing.
case (5): // This case will perform transaction 5: Make deposit
{
printf("\n\nEnter the amount to deposit (format: xx.xx): ");
scanf(" %f", &money);
custBal[ctr] += money;
printf("\nYour transaction was successful!");
printf("\nYour new balance is: %.2f", custBal[ctr]);
printf("\nYour receipt has been generated!");
printf("\nThanks for banking with us\n\n");
//code to generate receipt
fcfi = fopen("C:\\users\\Favour\\Desktop\\ATM\\deposit.txt", "w");
// if the receipt generation fails
if (fcfi == 0)
{
printf("Error---Receipt could not be generated!\n");
exit(1);
}
//code to print receipt
fprintf(fcfi, "\n\nNIMARK BANK\n");
fprintf(fcfi, "\nYour Receipt for Funds Deposit");
fprintf(fcfi, "\n\nYour transaction was successful");
fprintf(fcfi, "\nAmount Deposited: %.2f", money);
fprintf(fcfi, "\nCurrent Balance: %.2f\n", custBal[ctr]);
fprintf(fcfi, "\n\n");
fclose(fcfi);
break;
}
Remember to replace my directory with yours. Else no receipt will be generated
Cancel and Exit (Transaction 6):
Lastly, users have the option to cancel their transaction or exit the program after completing a transaction.
The Cancel and Exit is shown in case (6). The following code shows how the exit code, the default message if the user fails to pick any valid number from the main main, and the close to the switch--close function and do--while function. Type the code below immediately after the previous make deposit code leaving at least a line before you start typing.
case (6): //This case will perform transaction 6: to cancel or print default message
exit (1);
default: printf("\n\n%d is not a valid choice.\n", pick1);
printf("Try again\n");
break;
}
} while ((pick1 < 1) || (pick1 > 6));
printf("Take your card: \nTo start another transaction input your card\n\n");
return (0);
}
Now we are done with writing Codes. You can test your program by clicking on the yellow wrench icon with a green play button as shown in the image below:
the run button |
Conclusion:
Congratulations! You have successfully built an ATM prototype in C. This project showcases the power of C programming and its application in real-world scenarios. Feel free to explore further and enhance the project, adding more features or customizations. The video below will show you how the ATM C project works.
Now that you have completed this project, you are well on your way to becoming a proficient C programmer. The knowledge and experience gained from building this ATM prototype will serve as a solid foundation for your future programming endeavors. Happy coding and keep exploring the fascinating world of C!