Created
November 23, 2023 20:41
-
-
Save upsilun/eedb8f83a76c58c9bb83937efcbfe0f8 to your computer and use it in GitHub Desktop.
An Bank application made for my project in C Programming in KKU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Fully Devolped by Yazeed Alahmad and Mohmammad Alasmari | |
*/ | |
// These are the libraries that used in this project | |
#include <stdlib.h> | |
#include <string.h> | |
// These libraries are only for some text formatting purposes | |
#include <stdio.h> | |
#include <windows.h> | |
// Global Variables Section # | |
// This variable is for selecting the loggedin user | |
int selectedUsername = -1; | |
// The example database arrays | |
// 10 -> max users ~~ 30 -> max letters per string | |
char usernames[10][30] = { | |
"Ahmad", | |
"Yazeed", | |
"Mohammad" | |
}; | |
char passwords[10][30] = { | |
"1234", | |
"0000", | |
"1122" | |
}; | |
double balances[10] = { | |
0.0, | |
100.0, | |
122.0 | |
}; | |
// These arrays are for storing history and saves the actions | |
char historyActionTypes[10][100][20]; // History array for action name | |
double historyAmounts[10][100]; // History array for action amount | |
int historyCount[10] = {0}; // Number of actions for each user | |
// This functions is to login and validate the username and password | |
void loginPage() { | |
char username[30]; | |
char password[30]; | |
system("cls"); | |
printf("\nEnter your -USERNAME- : "); | |
gets(username); | |
printf("\nEnter your -PASSWORD- : "); | |
gets(password); | |
int logged = 0; | |
for(int i = 0; i < 10; i++) { | |
if (strcmp(username, usernames[i]) == 0) { | |
if (strcmp(password, passwords[i]) == 0) { | |
selectedUsername = i; | |
logged = 1; | |
} | |
} | |
} | |
if(logged == 0) { | |
loginPage(); // recursion | |
} | |
} | |
void logoutPage() { | |
selectedUsername = -1; | |
system("cls"); | |
printf("You have been logged out.\n"); | |
Sleep(2000); | |
} | |
// Micro functions # | |
// This function is to add an action to the history array | |
void addToHistory(const char *actionType, double amount) { | |
// Check if he is loggedin and history count is not too much over 100 action | |
if (selectedUsername != -1 && historyCount[selectedUsername] < 100) { | |
// Copy the action type to the array | |
strcpy(historyActionTypes[selectedUsername][historyCount[selectedUsername]], actionType); | |
// Copy the action amount to the array | |
historyAmounts[selectedUsername][historyCount[selectedUsername]] = amount; | |
// Adding 1 to the history to make sure he is still in the range of 100 | |
historyCount[selectedUsername]++; | |
} | |
} | |
// This function is to change the username by getting a pointer parameter | |
void changeUsername(char *newUsername) { | |
// Check if the new username already exists | |
for (int i = 0; i < 10; i++) { | |
if (i != selectedUsername && strcmp(newUsername, usernames[i]) == 0) { | |
printf("Username already exists! Please choose a different username\n"); | |
Sleep(2000); | |
return; | |
} | |
} | |
// Update the username using pointers | |
strcpy(usernames[selectedUsername], newUsername); | |
printf("Username changed successfully\n"); | |
addToHistory("Username changed", -1); | |
Sleep(2000); | |
} | |
// This function is to change the password by getting a pointer parameter | |
void changePassword(char *newPassword) { | |
// Update the password using pointers | |
strcpy(passwords[selectedUsername], newPassword); | |
printf("Password changed successfully\n"); | |
addToHistory("Password changed", -1); | |
Sleep(2000); | |
} | |
// Actions Functions # | |
// This function is for deposting ( adding ) amounts to the account balance | |
void depositePage() { | |
system("cls"); | |
printf("- Deposite Balance\n\nPlease enter the amount in $"); | |
int amount; | |
scanf("%d", &amount); | |
balances[selectedUsername] = balances[selectedUsername] + amount; | |
addToHistory("Deposit", amount); | |
} | |
// This function is for withdrawl ( removing ) amounts from the account balance | |
void withdrawPage() { | |
system("cls"); | |
printf("- Withdraw Balance\n\nPlease enter the amount in $"); | |
int amount; | |
scanf("%d", &amount); | |
balances[selectedUsername] = balances[selectedUsername] - amount; | |
addToHistory("Withdrawal", amount); | |
} | |
// This function is for transfering amounts between the saved acccounts by subtracting the amount from the logged user and adding it to the desired user | |
void transferPage() { | |
system("cls"); | |
printf("- Transfer Balance\n\nPlease enter the Recipient username: "); | |
char recipientName[30]; | |
int recipientID = -1; | |
scanf("%s", recipientName); | |
// Find the recipient ID | |
for (int i = 0; i < 10; i++) { | |
if (strcmp(recipientName, usernames[i]) == 0) { | |
recipientID = i; | |
break; | |
} | |
} | |
if (recipientID != -1) { | |
printf("\nPlease enter the amount in $"); | |
double amount; | |
if (scanf("%lf", &amount) != 1 || amount <= 0) { | |
// Invalid amount | |
printf("\nInvalid amount.\n"); | |
Sleep(2000); | |
return; | |
} | |
if (amount <= balances[selectedUsername]) { | |
balances[selectedUsername] -= amount; // withdrawing the amount from the logged user | |
balances[recipientID] += amount; // depositing the amount to the desired user | |
printf("\n\nTransfer successful\n"); | |
addToHistory("Transfer", amount); | |
Sleep(2000); | |
} else { | |
printf("\nYour balance is not enough to transfer\n"); | |
Sleep(2000); | |
} | |
} else { | |
printf("\nRecipient not found\n"); | |
Sleep(2000); | |
} | |
} | |
// This function is to show multiple actions to do and call these functions alone | |
void accountSettingsPage() { | |
system("cls"); | |
printf("Account Settings\n"); | |
printf("=======================\n"); | |
printf("1) Change Username\n2) Change Password\n3) Back to Main Menu\n"); | |
printf("=======================\n"); | |
int choice; | |
scanf("%d", &choice); | |
char newInput[30]; | |
switch (choice) { | |
case 1: | |
printf("Enter your new username: "); | |
scanf("%s", newInput); | |
changeUsername(newInput); | |
accountSettingsPage(); | |
break; | |
case 2: | |
printf("Enter your new password: "); | |
scanf("%s", newInput); | |
changePassword(newInput); | |
accountSettingsPage(); | |
break; | |
case 3: | |
// Back to the main menu | |
break; | |
default: | |
printf("Invalid choice! Please try again\n"); | |
Sleep(2000); | |
accountSettingsPage(); | |
} | |
} | |
// This function is to show the account logging details and balance with a brief history of the actions he did | |
void bankstatementPage() { | |
system("cls"); | |
printf("- Bank Details & History\n"); | |
printf("=======================\n"); | |
printf("Your USERNAME is : %s\n", usernames[selectedUsername]); | |
printf("Your PASSWORD is : %s\n", passwords[selectedUsername]); | |
printf("=======================\n"); | |
printf("Your Available Balance is : $%.2lf\n", balances[selectedUsername]); | |
printf("=======================\n"); | |
printf("Transaction History:\n"); | |
for (int i = 0; i < historyCount[selectedUsername]; i++) { | |
if(historyAmounts[selectedUsername][i] == -1){ | |
printf("%s\n", historyActionTypes[selectedUsername][i]); | |
}else { | |
printf("%s: $%.2lf\n", historyActionTypes[selectedUsername][i], historyAmounts[selectedUsername][i]); | |
} | |
} | |
printf("\n=======================\n"); | |
printf("Press ( 1 ) To back to main menu\n"); | |
int i; | |
scanf("%d", &i); | |
} | |
void main() { | |
// System("cls") is used to make the app look as a multi page actions this command helped us alot to make it looks good! | |
system("cls"); | |
// to make sure the user name is logged in which : | |
// -1 means that he is not logged in with any user | |
if(selectedUsername != -1) { | |
system("cls"); | |
// Welcome headline and balance | |
printf("Welcome %s to KKU Bank!\n", usernames[selectedUsername]); | |
printf("$%.2lf\n\n", balances[selectedUsername]); | |
// Main menu options | |
printf("Please Enter your choice number\n"); | |
printf("=======================\n"); | |
printf("1 -) Deposit Balance\n2 -) Withdraw Balance\n3 -) Transfer Balance\n4 -) Bank Details & History\n5 -) Account Settings\n6 -) Logout\n"); | |
printf("=======================\n"); | |
// Enter his choice and read it to check it by switch statement | |
int choice; | |
scanf("%d", &choice); | |
switch (choice) | |
{ | |
case 1: | |
depositePage(); | |
main(); | |
break; | |
case 2: | |
withdrawPage(); | |
main(); | |
break; | |
case 3: | |
transferPage(); | |
main(); | |
break; | |
case 4: | |
bankstatementPage(); | |
main(); | |
break; | |
case 5: | |
accountSettingsPage(); | |
main(); | |
break; | |
case 6: | |
logoutPage(); | |
main(); | |
default: | |
main(); | |
break; | |
} | |
} | |
else { | |
// if the user is not logged in then bring him back to login page | |
loginPage(); | |
main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment