0% found this document useful (0 votes)
2 views

code

The document is a C program that implements a banking system allowing users to create accounts, deposit, withdraw, transfer money, and manage their PINs. It includes functions for file handling to save and load account data, as well as transaction history. The program uses a console interface for user interaction and employs structures to manage account details.

Uploaded by

chihaivshieu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code

The document is a C program that implements a banking system allowing users to create accounts, deposit, withdraw, transfer money, and manage their PINs. It includes functions for file handling to save and load account data, as well as transaction history. The program uses a console interface for user interaction and employs structures to manage account details.

Uploaded by

chihaivshieu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

#include<stdio.

h>
#include<conio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
struct TaiKhoan {
char Ten[50];
int SoThe;
int MaPIN;
int SoDu;
int nap, rut, chuyen, nhan;
int tiennap[50], tienrut[50], tienchuyen[50], tiennhan[50];
};

typedef struct TaiKhoan TK;

void XoaXuongDong(char x[]) {


int n = strlen(x);
if (x[n - 1] == '\n')
x[n - 1] = '\0';
}
void gotoxy(int x, int y)
{
static HANDLE h=NULL;
if(!h)
h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD c={x,y};
SetConsoleCursorPosition(h,c);
}
void LuuTep(TK danhsachTK[], int soluong) {
FILE *f = fopen("kh.txt", "wb");
int i;
if (f == NULL) {
printf("Khong the mo file de luu du lieu.\n");
return;
}
fscanf(f,"%d\n", soluong);
for( i = 0; i < soluong; i++)
{
fprintf(f,"%s %d %d \n",
danhsachTK[i].Ten,danhsachTK[i].SoThe,danhsachTK[i].MaPIN);
fread(danhsachTK,sizeof(int),1,f);
}
fclose(f);
}

void TaiTep(TK danhsachTK[], int *soluong) {


FILE *f = fopen("kh.txt", "rb");
int i;
if (f == NULL) {
printf("File khong ton tai, bat dau voi danh sach tai khoan rong.\n");
*soluong = 0;
return;
}
fprintf(f, "%d", soluong);
for( i = 0; i < *soluong; i++)
{
fscanf(f,"%s %d %d \n",
danhsachTK[i].Ten,danhsachTK[i].SoThe,danhsachTK[i].MaPIN);
fprintf(f,"%s %d %d \n",
danhsachTK[i].Ten,danhsachTK[i].SoThe,danhsachTK[i].MaPIN);
}
fclose(f);
}

void ThemTaiKhoan(TK danhsachTK[], int *soluong) {


system("cls");
int i;
printf("\nNhap so the: ");
int soTheMoi;
scanf("%d", &soTheMoi);
for (i = 0; i < *soluong; i++) {
if (soTheMoi == danhsachTK[i].SoThe) {
printf("So the da duoc su dung!\n");
return;
}
}
danhsachTK[*soluong].SoThe = soTheMoi;
printf("Nhap ten nguoi dung: ");
fflush(stdin);
fgets(danhsachTK[*soluong].Ten, sizeof(danhsachTK[*soluong].Ten),
stdin);
XoaXuongDong(danhsachTK[*soluong].Ten);
printf("Nhap ma PIN: ");
char c;
danhsachTK[*soluong].MaPIN=0;
danhsachTK[*soluong].SoDu = 0;
while ((c = getch()) != '\r')
{
if (c == '\b')
{
if (danhsachTK[*soluong].MaPIN > 0)
{
danhsachTK[*soluong].MaPIN /= 10;
printf("\b \b");
}
} else if (c >= '0' && c <= '9') {
danhsachTK[*soluong].MaPIN = danhsachTK[*soluong].MaPIN* 10
+ (c - '0');
printf("*");
}
}

char s[20];

/* do
{
gotoxy(13+i,3);s[i] = getch();
gotoxy(13+i,3);printf("*");
i++;
} while(s[i-1]!=13);*/

danhsachTK[*soluong].nap = danhsachTK[*soluong].rut =
danhsachTK[*soluong].chuyen = danhsachTK[*soluong].nhan = 0;
(*soluong)++;
LuuTep(danhsachTK, *soluong);
printf("\nDa tao tai khoan thanh cong!\n");
}

void Xoatk(TK danhsachTK[], int *soluong) {


system("cls");
printf("Nhap so the: ");
int x;
scanf("%d", &x);
for (int i = 0; i < *soluong; i++) {
if (x == danhsachTK[i].SoThe) {
int y;
printf("Xin chao %s! Nhap ma PIN: ",danhsachTK[i].Ten);
y=0;

char c;
while ((c = getch()) != '\r')
{
if (c == '\b')
{
if (y > 0)
{
y /= 10;
printf("\b \b");
}
} else if (c >= '0' && c <= '9') {
y = y* 10 + (c - '0');
printf("*");
}
}
if (y == danhsachTK[i].MaPIN) {
for (int j = i; j < *soluong - 1; j++)
danhsachTK[j] = danhsachTK[j + 1];
(*soluong)--;
LuuTep(danhsachTK, *soluong);
printf("\nTai khoan da bi xoa!\n");
return;
} else {
printf("\nSai ma PIN!\n");
return;
}
}
}
printf("Khong tim thay so the!\n");
}

int DangNhap(TK danhsachTK[], int soluong, int *chiso) {


system("cls");
printf("Nhap so the: ");
int x, y;
scanf("%d", &x);
for (int i = 0; i < soluong; i++)
{
if (x == danhsachTK[i].SoThe) {
printf("Nhap ma PIN: ");
char c;
y=0;

while ((c = getch()) != '\r')


{
if (c == '\b')
{
if (y > 0)
{
y /= 10;
printf("\b \b");
}
} else if (c >= '0' && c <= '9') {
y = y* 10 + (c - '0');
printf("*");
}
}

if (y == danhsachTK[i].MaPIN) {
*chiso = i;
printf("Dang nhap thanh cong!\n");
return 1;
} else {
printf("Sai ma PIN!\n");
return 0;
}
}
}
printf("Khong tim thay so the!\n");
return 0;
}

char lich_su_GD[100][50];
int so_luong_GD = 0;

void ghi_giao_dich(const char loai_gd[100], int tien) {


if (so_luong_GD < 100) {
SYSTEMTIME time;
GetLocalTime(&time);
snprintf(lich_su_GD[so_luong_GD], 50,
"%02d:%02d:%02d %02d/%02d/%04d %s: %d VND",
time.wHour,time.wMinute,time.wSecond,time.wDay,time.wMonth,time.wY
ear
, loai_gd,tien);
so_luong_GD++;
}
}
void in(){
FILE*f;
f=fopen("lich_su.txt","a");
if(f==NULL){
printf("file loi\n");
return;
}
fprintf(f,"---------------------------------------------\n");
fprintf(f," LỊCH SỬ CÁC GIAO DỊCH GẦN ĐÂY \n");

for (int i=0;i<so_luong_GD;i++){


fprintf(f, "Giao dich thu %d: %s\n",i+1,lich_su_GD[i]);
}fclose(f);
printf("Da in lich su giao dich!\n");
}

void NapTien(TK danhsachTK[], int soluong, int chiso) {


int soTien;
printf("\nNhap so tien muon nap: ");
scanf("%d", &soTien);

if (soTien <= 0) {
printf("So tien khong hop le!!\n");
return;
}

danhsachTK[chiso].SoDu += soTien;
ghi_giao_dich("Da nap tien",
danhsachTK[chiso].tiennap[danhsachTK[chiso].nap] = soTien);
danhsachTK[chiso].nap++;

printf("Nap tien thanh cong: %d\n", danhsachTK[chiso].SoDu);

LuuTep(danhsachTK, soluong);
}
void rut_tien(TK danhsachTK[], int soluong, int chiso) {
int so_tien;
printf("Nhap so tien muon rut: ");
scanf("%d", &so_tien);
if (so_tien > danhsachTK[chiso].SoDu) {
printf("So du khong du!\n");
} else {
ghi_giao_dich("Da rut tien",danhsachTK[chiso].SoDu -= so_tien);
LuuTep(danhsachTK, soluong);

printf("Rut thanh cong %d.\nSo du moi: %d .\n", so_tien,


danhsachTK[chiso].SoDu);
}
}
void DoiMaPIN(TK danhsachTK[], int chisonguoidung, int soluong) {
int NhapMAPIN, MAPINMOI, XACNHANPIN;
printf("Nhap ma PIN hien tai: ");
NhapMAPIN = 0;
char c;
while ((c = getch()) != '\r') {
if (c == '\b') {
if (NhapMAPIN > 0) {
NhapMAPIN /= 10;
printf("\b \b");
}
} else if (c >= '0' && c <= '9') {
NhapMAPIN = NhapMAPIN * 10 + (c - '0');
printf("*");
}
}
printf("\n");

if (NhapMAPIN != danhsachTK[chisonguoidung].MaPIN) {
printf("Sai ma PIN. Khong the doi.\n");
return;
}

printf("Nhap ma PIN moi: ");


MAPINMOI = 0;
while ((c = getch()) != '\r') {
if (c == '\b') {
if (MAPINMOI > 0) {
MAPINMOI /= 10;
printf("\b \b");
}
} else if (c >= '0' && c <= '9') {
MAPINMOI = MAPINMOI * 10 + (c - '0');
printf("*");
}
}
printf("\n");

printf("Xac nhan ma PIN moi: ");


XACNHANPIN = 0;
while ((c = getch()) != '\r') {
if (c == '\b') {
if (XACNHANPIN > 0) {
XACNHANPIN /= 10;
printf("\b \b");
}
} else if (c >= '0' && c <= '9') {
XACNHANPIN = XACNHANPIN * 10 + (c - '0');
printf("*");
}
}
printf("\n");

if (MAPINMOI != XACNHANPIN) {
printf("Xac nhan ma PIN khong khop.\n");
return;
}

danhsachTK[chisonguoidung].MaPIN = MAPINMOI;
LuuTep(danhsachTK, soluong);
FILE *f = fopen("D:\\pin_change.txt", "a");
if (f == NULL) {
printf(" luu thay doi ma PIN.\n");
return;
}
fprintf(f, "So the: %d, Ma PIN moi: %d\n",
danhsachTK[chisonguoidung].SoThe,
danhsachTK[chisonguoidung].MaPIN);
fclose(f);

printf("Da doi ma PIN thanh cong!\n");


}
void XemCT() {
printf("Nhap phim bat ky de tiep tuc: \n");
getch();
system("cls");
}
void ChuyenTien(TK danhsachTK[], int soluong, int chiso) {
int soTheNhan, soTien, indexNhan = -1;

printf("\nNhap so the nguoi nhan: ");


scanf("%d", &soTheNhan);

for (int i = 0; i < soluong; i++) {


if (danhsachTK[i].SoThe == soTheNhan) {
indexNhan = i;
break;
}
}

if (indexNhan == -1) {
printf("Khong tim thay tai khoan nhan!\n");
return;
}

printf("Nhap so tien muon chuyen: ");


scanf("%d", &soTien);

if (soTien <= 0) {
printf("So tien khong hop le!\n");
return;
}
if (soTien > danhsachTK[chiso].SoDu) {
printf("So du khong du!\n");
return;
}

danhsachTK[chiso].SoDu -= soTien;
danhsachTK[indexNhan].SoDu += soTien;

danhsachTK[chiso].tienchuyen[danhsachTK[chiso].chuyen] = soTien;
danhsachTK[chiso].chuyen++;
danhsachTK[indexNhan].tiennhan[danhsachTK[indexNhan].nhan] =
soTien;
danhsachTK[indexNhan].nhan++;

LuuTep(danhsachTK, soluong);

printf("Chuyen tien thanh cong!\nSo du moi: %d .\n",


danhsachTK[chiso].SoDu);
}
int main() {
TK danhsachTK[20];
int soluong = 0;
int dangnhap = 0, chisonguoidung = -1;
TaiTep(danhsachTK, &soluong);
while (!dangnhap) {
printf("========== CHAO MUNG DEN CT-BANK
==========\n");
printf("| 1. Dang nhap |\n");
printf("| 2. Tao tai khoan moi |\n");
printf("| 3. Xoa tai khoan |\n");

printf("========================================
===\n");
int luachon;
printf("Nhap lua chon: ");
scanf("%d", &luachon);
switch (luachon) {
case 1:
dangnhap = DangNhap(danhsachTK, soluong,
&chisonguoidung);
system("cls");
printf("\t\t\t\tDANG NHAP TAI KHOAN CUA BAN\n\
n");
printf("Dang kiem tra thong tin...\n");
Sleep(1000);
printf("\nDANG NHAP THANH CONG!\a\a\n\n");
Sleep(2000);
system("cls");
break;
case 2:

ThemTaiKhoan(danhsachTK, &soluong);
break;
system("cls");
case 3:
system("cls");
Xoatk(danhsachTK, &soluong);
break;
default:
printf("Lua chon khong hop le.\n");
}
}

int b = 1;
while (b) {
printf("=================== MENU
===================\n");
printf("| 1. Xem tai khoan |\n");
printf("| 2. Nap tien |\n");
printf("| 3. Rut tien |\n");
printf("| 4. Chuyen tien |\n");
printf("| 5. Xem lich su giao dich |\n");
printf("| 6. Doi ma pin |\n");
printf("| 0. Thoat |\n");

printf("========================================
===\n");

int luachon;
printf("Nhap lua chon: ");
scanf("%d", &luachon);

switch (luachon) {
case 1:
system("cls");
printf("\nThong tin tai khoan cua ban:");
printf("\nTen: %s, So The: %d, So Du: %d\n",
danhsachTK[chisonguoidung].Ten, danhsachTK[chisonguoidung].SoThe,
danhsachTK[chisonguoidung].SoDu);
break;
case 2:
system("cls");
NapTien(danhsachTK, soluong, chisonguoidung);
break;
case 3:
system("cls");
rut_tien(danhsachTK, soluong, chisonguoidung);
break;
case 4:
system("cls");
ChuyenTien(danhsachTK, soluong, chisonguoidung);
break;
case 5:
system("cls");
in();
break;
case 6:
system("cls");
DoiMaPIN( danhsachTK, chisonguoidung, soluong);
break;
case 0:
system("cls");
b = 0;
printf("Thoat chuong trinh.\n");
break;
default:
printf("Lua chon khong hop le.\n");
}
XemCT();
}
return 0;
}

You might also like