
Have you ever thought of developing something like a banking system or so? Well, today we are going to venture into the banking system development world.
In today's article we are going to develop a simple banking system that allows users to create an account, deposit money, withdraw money, check their balance, and check loan eligibility. The program utilizes user input to validate account information and manage transactions, ensuring that users can only perform actions if they successfully log in with their account number and PIN.
Let's start by highlighting the main several key functions:
validateAccount : Validates the user's account number and PIN.
countDigits: Counts the number of digits in a given number (essential for user pin and account number).
displayMenu: Displays the main menu options for the banking system.
createAccount: Allows users to create a new account with validation for:
deposit: Allows users to add funds to their account.
withdraw: Allows users to withdraw funds from their account, ensuring sufficient balance is retained necessary for the account's activation.
checkBalance: Displays the user's current account balance.
loanEligibility: Fancy function for checking loan eligibility.
main: The entry point of the program that runs the menu loop.
This program follows this general flow:
First, the program begins by seeding the random number generator for the account number generation - srand(time(NULL)). This is necessarily placed at the start of the main loop so that it could only be referenced once.
The displayMenu function is called in a loop to show user options until the time they choose to exit.
Now, depending on the user's choice:
createAccount: This function collects user information and validating it before generating an account number and PIN.
To be continued, wacha nilale kwanza 💤💤
But here is the code:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
// global vars
int pin = -1; // invalid
int accountNum = -1; // invalid
double amount;
double withdrawAmount;
double balance = 0;
// AC no. and pin entered by user
int enteredAccountNum, enteredPin;
// account validation (User existence, AC no. and pin)
void validateAccount(){
if (accountNum == -1){
printf("User doesn't exist. Please create an account to continue.\n");
return;
}
while (1){
printf("Enter Account number: ");
if (scanf("%d", &enteredAccountNum) != 1){
printf("Account number should be a number.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
if (enteredAccountNum == accountNum){
break;
} else {
printf("Invalid Account number!\n");
}
}
while (1){
printf("Enter pin: ");
if (scanf("%d", &enteredPin) != 1){
printf("Pin should be a number.\n");
while (getchar() != '\n');
continue;
}
if (enteredPin == pin){
break;
} else {
printf("Invalid pin!\n");
}
}
}
// count digits of a number
int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
return count;
}
void displayMenu(){
printf("\n--------- InkCloud Bank ---------\n");
printf("--------- Customer Menu ---------\n");
printf("\t1. Create Account \n");
printf("\t2. Deposit \n");
printf("\t3. Withdraw \n");
printf("\t4. Check Balance \n");
printf("\t5. Check loan eligibility \n");
printf("\t6. Exit \n");
printf("\n");
}
void createAccount(){
int isValid = 1; // for validation
char name[50];
int age;
int idNum;
while (1){
printf("Enter your name: ");
scanf("%s", name);
isValid = 1;
for (int i = 0; name[i] != '\0'; i++) {
if (!isalpha(name[i])) {
isValid = 0;
break;
}
}
if (isValid){
break;
}else {
printf("Name must be comprised of letters only!\n");
}
}
while (1){
printf("Enter your age (18yrs and above): ");
if(scanf("%d", &age) != 1){
printf("Age should be a number!\n");
while (getchar() != '\n');
continue;
}
if(age < 18){
printf("You must 18 and above to create an account.\n");
} else {
break;
}
}
while (1){
printf("Enter ID number(6 digits): ");
if(scanf("%d", &idNum) != 1){
printf("ID number should be a number\n");
while (getchar() != '\n');
continue;
} else if (countDigits(idNum) == 6 && idNum > 0){
break;
} else {
printf("Invalid ID number!\n");
}
}
while (1){
printf("Enter your pin (4 digits): ");
if(scanf("%d", &pin) != 1){
printf("Pin should be a number!\n");
while (getchar() != '\n');
continue;
} else if (countDigits(pin) == 4 && pin > 0){
break;
} else {
printf("Invalid pin!\n");
}
}
accountNum = 10000000 + rand() % 90000000; // 8 digits
printf("\n");
printf("Account created successfully!\n");
printf("Your account number is: %d\n", accountNum);
printf("Your pin is: %d\n", pin);
}
void deposit(){
validateAccount();
if (accountNum == -1) return;
printf("Enter deposit amount: ");
scanf("%lf", &amount);
printf("You have deposited Ksh %.2f\n", amount);
// update balance
balance += amount;
}
void withdraw(){
validateAccount();
if (accountNum == -1) return;
while (1) {
printf("Enter amount to withdraw: ");
if(scanf("%lf", &withdrawAmount) != 1){
printf("Withdrawal amount should be a number!\n");
while (getchar() != '\n');
continue;
}
if (withdrawAmount < 500){
printf("Minimum withdrawal amount is Ksh 500!\n");
} else if( withdrawAmount > balance) {
printf("Balance is lower than the withdrawal amount!\n");
} else {
// update balance after withdrawal
balance -= withdrawAmount;
printf("You have successully withdrawn %.2f\n", withdrawAmount);
printf("Your current balance is Ksh %.2f\n", balance);
break;
}
}
}
void checkBalance(){
validateAccount();
if (accountNum == -1) return;
printf("You balance is Ksh %.2f\n", balance);
}
void loanEligibility(){
printf("eligibiity");
}
int main(){
srand(time(NULL)); // seed for random account number generation
while (1){
displayMenu();
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
// printf("You choice was: %d", choice);
switch (choice){
case 1: {
createAccount();
break;
}
case 2: {
deposit();
break;
}
case 3: {
withdraw();
break;
}
case 4: {
checkBalance();
break;
}
case 5: {
loanEligibility();
break;
}
case 6: {
printf("Thank you for using our banking services");
break;
}
default: {
printf("Invalid choice! Try again\n");
}
}
}
return 0;
}
