Skip to content

Instantly share code, notes, and snippets.

@ken-kuro
Created April 24, 2019 15:52
Show Gist options
  • Select an option

  • Save ken-kuro/148e8529ad4677cac9d13f5518e88727 to your computer and use it in GitHub Desktop.

Select an option

Save ken-kuro/148e8529ad4677cac9d13f5518e88727 to your computer and use it in GitHub Desktop.
/*
To-do list :
(type)(expression) ( Ep kieu )
expression1?expression2:expression3
printf %[flags][width][.precision][length]specifier
scanf %[*][width][length]specifier
name of array = &array[0]
fflush(stdin)
*/
//MY FIRST PROJECT : ATM SIMULATION SYSTEM IN C
#include <unistd.h> // For sleep
#include <windows.h> // For sleep ???
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h> // For string
#include "main.h" // File header that contains prototypes
typedef struct AccountData
{
char ID[IDsize],PIN[PINsize]; // ID's size is bigger than usual ???
unsigned long balance;
} Data;
Data *account;
int accountCount = 0, loginAccount = 0, receiveAccount = 0, loginStatus = 0,session = 0;
unsigned long newbalancelogin, newbalancereceive;
int languageChoice;
void GetData()
{
char ch;
int i = 0;
FILE *f;
f = fopen(FILENAME,"r");
if (f == NULL)
{
printf("Can not get data");
perror("Error");
}
while (!feof(f))
{
ch=fgetc(f);
if (ch=='\n' && session == 0) accountCount++; /* i++!=++i
Supposing i=1
if j=i++ then j=1, i=2
if j=++i then j=2, i=2
*/
}
session = 1;
// printf("Line count: %d\n",accountCount+1); //For testing
rewind(f); //fseek(f,0,SEEK_SET);
account = malloc((accountCount+1)*sizeof(Data));
while (i <= accountCount)
{
fscanf(f,"%s %s %lu",account[i].ID,account[i].PIN,&account[i].balance);// Use fgets when need " " in string
i++;
}
// printf("%s,%s,%lu\n",account[0].ID,account[0].PIN,account[0].balance); //For testing
// printf("%s,%s,%lu\n",account[1].ID,account[1].PIN,account[1].balance);
// printf("%s,%s,%lu",account[2].ID,account[2].PIN,account[2].balance);
fclose(f);
}
void ShowLogo() // Created using TexttoASCIIArt
{
printf("\n\n\a");
printf("\t\t ::::::::: ::: :::: ::: \n");
printf("\t\t :+: :+: :+: :+: :+:+: :+: \n");
printf("\t\t +:+ +:+ +:+ +:+ :+:+:+ +:+ \n");
printf("\t\t +#+ +:+ +#++:++#++: +#+ +:+ +#+ \n");
printf("\t\t +#+ +#+ +#+ +#+ +#+ +#+#+# \n");
printf("\t\t #+# #+# #+# #+# #+# #+#+# \n");
printf("\t\t ######### ### ### ### #### \n");
printf("\n\n");
printf("\t\t ATM Simulation System \n");
printf("\n\n");
printf("\t\t Date : %s",__DATE__);
Sleep(1000);
}
void FirstSetup()
{
char ID[IDsize];
int i;
ShowLogo();
printf("\n\n\n\a");
printf("\t\t xxxxxxxxxx WELCOME xxxxxxxxxx \n");
printf("\n\t\t xxxxxxxxxx CHAO MUNG QUY KHACH xxxxxxxxxx \n");
INPUT:;// label for goto
printf("\n\t\t Please insert your card (By enter your ID)\n");
printf("\n\t\tVui long cho the cua ban vao khe (Bang cach nhap ID cua ban)\n");
printf("\t\t\t\t\t");
scanf("%s",ID);
printf("\n\t\t\tChecking");
printf("\n\t\t\tDang kiem tra");
Sleep(500);
for (i=0;i<=accountCount;i++)
{
if (strcmp(ID,account[i].ID) == 0)
{
if (strcmp(account[i].PIN,"locked") == 0)
{
printf("\n\t\t\t\aPIN tries exceeded");
printf("\n\t\t\tYour ATM card is temporarily blocked");
printf("\n\t\t\tPlease contact Customer Service");
printf("\n");
printf("\n\t\t\t\aDa vuot qua so lan thu ma PIN");
printf("\n\t\t\tThe ATM cua ban da tam thoi bi chan");
printf("\n\t\t\tVui long lien he dich vu cham soc khach hang");
Sleep(3000);
exit(0);
}
else
{ loginAccount = i;
getchar(); // = getc(stdin)
system("cls"); // clrscr;
printf("\n\n\a");
printf("\t\t ==================LANGUAGE================ \n");
printf("\t\t ========================================== \n");
printf("\t\t | Select your language |\n");
printf("\t\t | Chon ngon ngu cua ban |\n");
printf("\t\t | [1] English [2] Tieng Viet |\n");
printf("\t\t | |\n");
printf("\t\t ========================================== \n");
printf("\t\t\t\t\t");
scanf("%d",&languageChoice);
break;
}
}
else continue;
}
if (i>accountCount)
{
printf("\n\n\t\t\tPlease try again");
printf("\n\t\t\tVui long thu lai");
goto INPUT;
}
}
void Overwriteblocked ()
{
int i = 0;
FILE *f;
strcpy(account[loginAccount].PIN,"locked");
f = fopen(NEWFILENAME,"w");
while (i < accountCount)
{
fprintf(f,"%s %s %lu\n",account[i].ID,account[i].PIN,account[i].balance);
i++;
}
fprintf(f,"%s %s %lu",account[i].ID,account[i].PIN,account[i].balance);
fclose(f);
remove(FILENAME);
rename(NEWFILENAME,FILENAME);
}
void Login()
{
char password[PINsize];
int i = 0, limit = 0;
system("cls");
printf("\n\n\a");
if (languageChoice == 1)
{
printf("\t\t ========================================== \n");
printf("\t\t | Please type personal |\n");
printf("\t\t | identification number (6-digit) |\n");
printf("\t\t ========================================== \n");
}
else
{
printf("\t\t ========================================== \n");
printf("\t\t | Vui long nhap ma so |\n");
printf("\t\t | nhan dang ca nhan (6 chu so) |\n");
printf("\t\t ========================================== \n");
}
INPUT:;
fflush(stdin);
printf("\t\t\t\t PIN:");
for(i=0;i<(PINsize-1);i++) printf("_");
for(i=0;i<(PINsize-1);i++) printf("\b");
i = 0;
while (i<PINsize)
{
password[i]=getch(); // like getche(), enter is not required. Can be used as system("pause"). Do not display content on sdout
if (password[i] == enter) break;
else if (password[i] == bksp)
{
printf("\b_\b");
i--;
password[i]='\0';
}
else
{
printf("*");
i++;
}
}
password[i]='\0';
i = 0; // Prevent the loop when go to INPUT
//printf("%s",password); For testing
if (languageChoice == 1) printf("\n\t\t\tChecking");
else printf("\n\t\t\tDang kiem tra");
Sleep(500);
if (limit >= 3)
{
if (languageChoice == 1)
{
printf("\n\t\t\t\aPIN tries exceeded");
printf("\n\t\t\tYour ATM card is temporarily blocked");
printf("\n\t\t\tPlease contact Customer Service");
}
else
{
printf("\n\t\t\t\aDa vuot qua so lan thu ma PIN");
printf("\n\t\t\tThe ATM cua ban da tam thoi bi chan");
printf("\n\t\t\tVui long lien he dich vu cham soc khach hang");
}
Overwriteblocked();
Sleep(3000);
exit(0);
}
while (limit < 3)
{
if (strcmp(password,account[loginAccount].PIN) == 0)
{
loginStatus = 1;
break;
}
else
{
if (languageChoice == 1)
{
printf("\n\t\t\t\aYour PIN is invalid");
printf("\n\t\t\tPlease try again");
printf("\n\t\t\t%d tries left\n",3-limit);
}
else
{
printf("\n\t\t\t\aMa PIN khong hop le");
printf("\n\t\t\tVui long thu lai");
printf("\n\t\t\tCon %d lan nhap\n",3-limit);
}
limit++;
goto INPUT;
}
}
}
void Overwritelogin()
{
int i = 0;
FILE *f;
account[loginAccount].balance=newbalancelogin;
f = fopen(NEWFILENAME,"w");
while (i < accountCount)
{
fprintf(f,"%s %s %lu\n",account[i].ID,account[i].PIN,account[i].balance);
i++;
}
fprintf(f,"%s %s %lu",account[i].ID,account[i].PIN,account[i].balance);
fclose(f);
remove(FILENAME);
rename(NEWFILENAME,FILENAME);
}
void Overwritereceive()
{
int i = 0;
FILE *f;
account[receiveAccount].balance=newbalancereceive;
f = fopen(NEWFILENAME,"w");
while (i < accountCount)
{
fprintf(f,"%s %s %lu\n",account[i].ID,account[i].PIN,account[i].balance);
i++;
}
fprintf(f,"%s %s %lu",account[i].ID,account[i].PIN,account[i].balance);
fclose(f);
remove(FILENAME);
rename(NEWFILENAME,FILENAME);
}
void Confirm()
{
char ch;
if (languageChoice == 1)
{
printf("\n\t\t\t\aWould you like to make another transaction ? Y/N");
}
else
{
printf("\n\t\t\t\aBan co muon thuc hien mot giao dich khac ? C/K");
}
printf("\n\t\t\t\t\t");
scanf("%s",&ch);
if (ch == y || ch == Y || ch == c || ch == C)
{
ShowMenu();
}
else if (ch == n || ch == N || ch == k || ch == K)
{
exit(0);
}
else exit(0);
}
void Withdrawal()
{
int withdrawamount,withdrawChoice;
if (account[loginAccount].balance <= 50000)
{
if (languageChoice == 1)
{
printf("\n\t\t\tYour balance is not enough to make transaction");
}
else
{
printf("\n\t\t\tTai khoan cua quy khach khong du de thuc hien giao dich");
}
Sleep(3000);
exit(0);
}
Login();
if (loginStatus == 1)
{
system("cls");
if (languageChoice ==1)
{
printf("\n\n\a");
printf("\t\t ==========Withdrawal Transaction========== \n");
printf("\t\t ========================================== \n");
printf("\t\t | Select an amount |\n");
printf("\t\t | [1] 100000d [2] 500000d |\n");
printf("\t\t | [3] 1000000d [4] 3000000d |\n");
printf("\t\t | [5] 5000000d [6] Other amount |\n");
printf("\t\t ========================================== \n");
printf("\n\t\t\tEnter your choice : ");
}
else
{
printf("\n\n\a");
printf("\t\t ============Giao dich rut tien============ \n");
printf("\t\t ========================================== \n");
printf("\t\t | Vui long chon so tien |\n");
printf("\t\t | [1] 100000d [2] 500000d |\n");
printf("\t\t | [3] 1000000d [4] 3000000d |\n");
printf("\t\t | [5] 5000000d [6] So tien khac |\n");
printf("\t\t ========================================== \n");
printf("\n\t\t\tNhap so de chon : ");
}
scanf("%d",&withdrawChoice);
switch (withdrawChoice)
{
case 1:
{
withdrawamount=100000;
break;
}
case 2:
{
withdrawamount=500000;
break;
}
case 3:
{
withdrawamount=1000000;
break;
}
case 4:
{
withdrawamount=3000000;
break;
}
case 5:
{
withdrawamount=5000000;
break;
}
case 6:
{
if (languageChoice == 1)
{
printf("\n\t\t\t\aPlease enter amount : ");
}
else
{
printf("\n\t\t\t\aVui long nhap so tien : ");
}
scanf("%d",&withdrawamount);
break;
}
}
newbalancelogin=account[loginAccount].balance-withdrawamount;
if (newbalancelogin <= 50000)
{
if (languageChoice == 1)
{
printf("\n\t\t\tYour balance is not enough to make transaction");
}
else
{
printf("\n\t\t\tTai khoan cua quy khach khong du de thuc hien giao dich");
}
Sleep(3000);
exit(0);
}
Overwritelogin();
if (languageChoice == 1)
{
printf("\n\t\t\t\aTransaction completed successfully !");
}
else
{
printf("\n\t\t\t\aGiao dich da thuc hien thanh cong !");
}
if (languageChoice == 1)
{
printf("\n\t\t\t\aYour available balance : %lu",newbalancelogin);
}
else
{
printf("\n\t\t\t\aTai khoan kha dung cua ban : %lu",newbalancelogin);
}
Confirm();
}
}
void Balance()
{
if (languageChoice == 1)
{
printf("\n\t\t\t\aYour balance : %lu",account[loginAccount].balance);
printf("\n\t\t\tYour available balance : %lu\n",account[loginAccount].balance-50000);
}
else
{
printf("\n\t\t\t\aTai khoan cua ban : %lu",account[loginAccount].balance);
printf("\n\t\t\tTai khoan kha dung cua ban : %lu\n",account[loginAccount].balance-50000);
}
Confirm();
}
void Transfer()
{
char ID[IDsize];
int i,transferamount,transferChoice;
if (account[loginAccount].balance <= 50000)
{
if (languageChoice == 1)
{
printf("\n\t\t\tYour balance is not enough to make transaction");
}
else
{
printf("\n\t\t\tTai khoan cua quy khach khong du de thuc hien giao dich");
}
Sleep(3000);
exit(0);
}
if (languageChoice == 1)
{
printf("\n\t\t\t\aPlease enter receiver's ID : ");
}
else
{
printf("\n\t\t\t\aVui long nhap ID nguoi nhan : ");
}
scanf("%s",ID);
Login();
if (loginStatus == 1)
{
system("cls");
if (languageChoice ==1)
{
printf("\n\n\a");
printf("\t\t ===========Transfer Transaction=========== \n");
printf("\t\t ========================================== \n");
printf("\t\t | Select an amount |\n");
printf("\t\t | [1] 100000d [2] 500000d |\n");
printf("\t\t | [3] 1000000d [4] 3000000d |\n");
printf("\t\t | [5] 5000000d [6] Other amount |\n");
printf("\t\t ========================================== \n");
printf("\n\t\t\tEnter your choice : ");
}
else
{
printf("\n\n\a");
printf("\t\t ============Giao dich gui tien============ \n");
printf("\t\t ========================================== \n");
printf("\t\t | Vui long chon so tien |\n");
printf("\t\t | [1] 100000d [2] 500000d |\n");
printf("\t\t | [3] 1000000d [4] 3000000d |\n");
printf("\t\t | [5] 5000000d [6] So tien khac |\n");
printf("\t\t ========================================== \n");
printf("\n\t\t\tNhap so de chon : ");
}
scanf("%d",&transferChoice);
switch (transferChoice)
{
case 1:
{
transferamount=100000;
break;
}
case 2:
{
transferamount=500000;
break;
}
case 3:
{
transferamount=1000000;
break;
}
case 4:
{
transferamount=3000000;
break;
}
case 5:
{
transferamount=5000000;
break;
}
case 6:
{
if (languageChoice == 1)
{
printf("\n\t\t\t\aPlease enter amount : ");
}
else
{
printf("\n\t\t\t\aVui long nhap so tien : ");
}
scanf("%d",&transferamount);
break;
}
}
newbalancelogin=account[loginAccount].balance-transferamount;
if (newbalancelogin <= 50000)
{
if (languageChoice == 1)
{
printf("\n\t\t\tYour balance is not enough to make transaction");
}
else
{
printf("\n\t\t\tTai khoan cua quy khach khong du de thuc hien giao dich");
}
Sleep(3000);
exit(0);
}
Overwritelogin();
if (languageChoice == 1)
{
printf("\n\t\t\t\aTransaction completed successfully !");
printf("\n\t\t\t\aMoney transfered to %s",ID);
}
else
{
printf("\n\t\t\t\aGiao dich da thuc hien thanh cong !");
printf("\n\t\t\t\aTien da duoc chuyen toi %s",ID);
}
if (languageChoice == 1)
{
printf("\n\t\t\t\aYour available balance : %lu",newbalancelogin);
}
else
{
printf("\n\t\t\t\aTai khoan kha dung cua ban : %lu",newbalancelogin);
}
for (i=0;i<=accountCount;i++)
{
if (strcmp(ID,account[i].ID) == 0)
{
receiveAccount = i;
newbalancereceive = account[receiveAccount].balance+ transferamount;
Overwritereceive();
break;
}
else break;
}
Confirm();
}
}
void ChangePIN()
{
int i = 0;
char password[PINsize],confirm[PINsize];
INPUT:;
if (languageChoice == 1)
{
printf("\n\t\t\t\a Enter your new PIN : ");
}
else
{
printf("\n\t\t\t\a Nhap lai ma PIN : ");
}
for(i=0;i<(PINsize-1);i++) printf("_");
for(i=0;i<(PINsize-1);i++) printf("\b");
i=0;
while (i<PINsize)
{
password[i]=getch();
if (password[i] == enter) break;
else if (password[i] == bksp)
{
printf("\b_\b");
i--;
password[i]='\0';
}
else
{
printf("*");
i++;
}
}
password[i]='\0';
i = 0;
if (languageChoice == 1)
{
printf("\n\t\t\t\a Enter your new PIN again : ");
}
else
{
printf("\n\t\t\t\a Nhap lai ma PIN moi : ");
}
for(i=0;i<(PINsize-1);i++) printf("_");
for(i=0;i<(PINsize-1);i++) printf("\b");
i = 0;
while (i<PINsize)
{
confirm[i]=getch();
if (confirm[i] == enter) break;
else if (confirm[i] == bksp)
{
printf("\b_\b");
i--;
confirm[i]='\0';
}
else
{
printf("*");
i++;
}
}
confirm[i]='\0';
i = 0;
if (strcmp(password,confirm) == 0)
{
loginStatus = 0;
if (languageChoice == 1)
{
printf("\n\t\t\t\a Enter your PIN to continue : ");
}
else
{
printf("\n\t\t\t\a Nhap ma PIN cua ban de tiep tuc : ");
}
Login();
if (loginStatus == 1)
{
FILE *f;
strcpy(account[loginAccount].PIN,confirm);
f = fopen(NEWFILENAME,"w");
while (i < accountCount)
{
fprintf(f,"%s %s %lu\n",account[i].ID,account[i].PIN,account[i].balance);
i++;
}
fprintf(f,"%s %s %lu",account[i].ID,account[i].PIN,account[i].balance);
fclose(f);
remove(FILENAME);
rename(NEWFILENAME,FILENAME);
if (languageChoice == 1)
{
printf("\n\t\t\t\aPIN changed successfully, please login again to continue ");
}
else
{
printf("\n\t\t\t\aPIN da duoc thay doi, vui long dang nhap lai ");
}
Sleep(3000);
exit(0);
}
}
else
{
if (languageChoice == 1)
{
printf("\n\t\t\t\a Not matched, please try again ! ");
}
else
{
printf("\n\t\t\t\a Khong khop, vui long thu lai ! ");
}
goto INPUT;
}
}
void ShowMenu()
{
int mainmenuChoice;
GetData();
if (loginStatus == 1)
{
if (languageChoice == 1)
{
printf("\n\t\t\tHello %s",account[loginAccount].ID);
printf("\n\t\t\t\aMoving to main menu");
Sleep(1500);
}
else
{
printf("\n\t\t\tXinchao %s",account[loginAccount].ID);
printf("\n\t\t\t\aDang chuyen toi menu chinh");
Sleep(1500);
}
system("cls");
if (languageChoice == 1)
{
printf("\n\n\a");
printf("\t\t =================MAIN MENU================ \n");
printf("\t\t ========================================== \n");
printf("\t\t | Select a transaction |\n");
printf("\t\t | [1] Withdrawal [2] Balance Inquiry |\n");
printf("\t\t | [3] Transfer [4] Change PIN |\n");
printf("\t\t | [5] Quit |\n");
printf("\t\t ========================================== \n");
printf("\n\t\t\tEnter your choice : ");
}
else
{
printf("\n\n\a");
printf("\t\t ================MENU CHINH================ \n");
printf("\t\t ========================================== \n");
printf("\t\t | Vui long chon giao dich |\n");
printf("\t\t | [1] Rut tien [2] So du tai khoan |\n");
printf("\t\t | [3] Chuyen tien [4] Doi ma PIN |\n");
printf("\t\t | [5] Thoat |\n");
printf("\t\t ========================================== \n");
printf("\n\t\t\tNhap so de chon : ");
}
scanf("%d",&mainmenuChoice);
switch(mainmenuChoice)
{
case 1:
{
Withdrawal();
break;
}
case 2:
{
Balance();
break;
}
case 3:
{
Transfer();
break;
}
case 4:
{
ChangePIN();
break;
}
case 5:
exit(0);
//default:fullswitch
}
}
loginStatus=0;
}
int main(int argc,char *argv[])
{
GetData(); //For first run
FirstSetup();
Login(); //For first run
ShowMenu();
return 0;
free(account);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment