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

cuối môn word

The source code defines functions for creating, reading, updating, and finding bank accounts from a data file. It includes functions to randomly generate account numbers, create new accounts by writing them to a file, read accounts from the file into an array, update account balances and PINs, and find accounts by their number. The main function initializes existing accounts from the file, allows a user to log in, and provides a menu for checking balances, withdrawing funds, transferring between accounts, changing PINs, and creating new accounts by writing updated data back to the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

cuối môn word

The source code defines functions for creating, reading, updating, and finding bank accounts from a data file. It includes functions to randomly generate account numbers, create new accounts by writing them to a file, read accounts from the file into an array, update account balances and PINs, and find accounts by their number. The main function initializes existing accounts from the file, allows a user to log in, and provides a menu for checking balances, withdrawing funds, transferring between accounts, changing PINs, and creating new accounts by writing updated data back to the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Phạm hoàng thịnh ID: 134010123043

Source code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define MAX_ACCOUNTS 5

typedef struct
{
int pin;
long long accountNumber;
long long balance;
char accountName[100];
} ACC;
// tao so the bat ki
long long randomSoThe(long long soThe)
{
srand(time(NULL));
soThe = rand() % (9999999999999999 -
1020000000000000 + 1) + 1020000000000000;
return soThe;
}
// tao the ATM
void createATMAccount(long long soThe, int
password, long long main_balance, char
main_name[100])
{
FILE *f = fopen("account-number.dat", "a"); //
Mở file để append thông tin tài khoản mới
if (f != NULL)
{
ACC newAccount;
newAccount.pin = password;
newAccount.balance = main_balance;
newAccount.accountNumber = soThe;
strcpy(newAccount.accountName, main_name);

// Ghi thông tin tài khoản mới vào file


fprintf(f, "pin:%d\nbalance:%lld\
ncardNumber:%lld\naccountName:%s\n\n",
newAccount.pin, newAccount.balance,
newAccount.accountNumber, newAccount.accountName);

fclose(f);
}
}

// doc file theo tung dong


void readAccountsFromFile(ACC accounts[])
{
FILE *f = fopen("main_account.txt", "r");
if (f == NULL)
{
printf("Không thể mở file!\n");
return;
}

int accountIndex = 0;
while (1)
{
int result = fscanf(f, "pin:%d\nbalance:
%lld\naccountNumber:%lld\naccountName:%99[^\n]\n\
n",

&accounts[accountIndex].pin,

&accounts[accountIndex].balance,

&accounts[accountIndex].accountNumber,

accounts[accountIndex].accountName);
if (result == EOF || accountIndex >=
MAX_ACCOUNTS)
{
break;
}
accountIndex++;
}

fclose(f);
}
// ghi vao file dung format
void writeAccountsToFile(ACC accounts[], int count)
{
FILE *f = fopen("main_account.txt", "w");
if (f != NULL)
{
for (int i = 0; i < count; i++)
{
fprintf(f, "pin:%d\nbalance:%lld\
naccountNumber:%lld\naccountName:%s\n\n",
accounts[i].pin,
accounts[i].balance,
accounts[i].accountNumber,
accounts[i].accountName);
}
fclose(f);
}
}
// tim con tro tai khoan
ACC *findAccountByNumber(ACC accounts[], int count,
long long targetAccountNumber)
{
for (int i = 0; i < count; i++)
{
if (accounts[i].accountNumber ==
targetAccountNumber)
{
return &accounts[i];
}
}
return NULL; // Trả về NULL nếu không tìm thấy
tài khoản
}
// cập nhập số dư
void updateAccountBalance(ACC accounts[], int
count, long long targetAccountNumber, long long
newBalance)
{
ACC *accountToUpdate =
findAccountByNumber(accounts, count,
targetAccountNumber);
if (accountToUpdate != NULL)
{
accountToUpdate->balance = newBalance;
}
}

// cap nhat PIN


void updateAccountPin(ACC accounts[], int count,
long long targetAccountNumber, long long newPin)
{
ACC *accountToUpdate =
findAccountByNumber(accounts, count,
targetAccountNumber);
if (accountToUpdate != NULL)
{
accountToUpdate->pin = newPin;
}
}
int main()
{
// dien thong tin và xác nhận thông tin tài
khoản chính đang sử dụng
ACC accounts[MAX_ACCOUNTS];
readAccountsFromFile(accounts);
int password;
long long main_stk;
char choice;
long long main_balance;
char main_name[100];
int flag2 = 0;
nhap:
system("cls");
if (flag2 > 0)
{
printf("Du lieu khong hop le, vui long nhap
lai.\n");
flag2 = 0;
}
printf("Ban co muon dang nhap khong (Y/N): ");

scanf("%c", &choice);

if (choice == 'Y' || choice == 'y')


{
printf("dang nhap tai khoan:\n");
printf("STK: ");
scanf("%lld", &main_stk);
printf("PIN: ");
scanf("%d", &password);

ACC *targetAccount =
findAccountByNumber(accounts, MAX_ACCOUNTS,
main_stk);
if (targetAccount != NULL && main_stk ==
targetAccount->accountNumber && password ==
targetAccount->pin)
{
main_stk = targetAccount-
>accountNumber;
password = targetAccount->pin;
main_balance = targetAccount->balance;
strcpy(main_name, targetAccount-
>accountName); // Sử dụng strcpy để gán chuỗi vào
main_name
// printf("%d\n", password);
// printf("%s", main_name);
}
else
{
flag2++;
goto nhap;
}
}
else if (choice == 'N' || choice == 'n')
{
return 0;
}
else
{
flag2++;
goto nhap;
}

// thực hiện giao dịch qua menu


int decision;
int n;
int menu = 0;
system("cls");
printf("==============================\n");
printf(" VTC Academy\n");
printf("==============================\n");
printf(" ATM Machine\n");
printf("------------------------------\n");
printf(" Account No: %lld \n", main_stk);
printf(" PIN CODE:****** \n");
printf("------------------------------\n");
printf(" Account Name: %s\n", main_name);
printf("------------------------------\n");
printf(" 1. Checking account balance\n");
printf(" 2. Withdrawl\n");
printf(" 3. Transfer\n");
printf(" 4. Change PIN\n");
printf(" 5. Create ATM card\n");
printf(" 6. End of transacation\n");
printf("------------------------------\n");
console:
if (menu > 0)
{
printf("khong ton tai chuong trinh do, vui
long nhap lai:\n");
menu = 0;
}
while (1)
{
printf("\n Your choice: ");
ACC *targetAccount =
findAccountByNumber(accounts, MAX_ACCOUNTS,
main_stk);
scanf("%d", &n);
if (n == 1)
{
readAccountsFromFile(accounts);
printf("------------------------------\
n");
printf(" Your current balane: %lld VND\
n", targetAccount->balance);
printf("------------------------------\
n");
}
else if (n == 2)
{
printf("==============================\
n");
printf(" VTC Academy\n");
printf("==============================\
n");
printf(" ATM Machine - Withdraw\n");
printf("------------------------------\
n");
printf("1. 100.000 VND\n");
printf("2. 200.000 VND\n");
printf("3. 500.000 VND\n");
printf("4. 1.000.000 VND\n");
printf("5. 2.000.000 VND\n");
printf("6. Other number\n");
printf("------------------------------\
n");
long long withDrawl;
long long money = targetAccount-
>balance;
int pick;
pick:
printf(" Your choice: ");
scanf("%d", &pick);
switch (pick)
{
case 1:
withDrawl = 100000;
break;

case 2:
withDrawl = 200000;
break;

case 3:
withDrawl = 500000;
break;

case 4:
withDrawl = 1000000;
break;

case 5:
withDrawl = 2000000;
break;
default:
printf(" Nhap so tien can rut(VND):
");
scanf("%lld", &withDrawl);
break;
}
if (withDrawl > money)
{
printf("\n So du tai khoan khong
du:\n");

printf("------------------------------\n");
goto pick;
}
printf("------------------------------\
n");
printf(" Rut tien thanh cong!\n");
printf("------------------------------\
n");
long long newBalance = money -
withDrawl;
updateAccountBalance(accounts,
MAX_ACCOUNTS, targetAccount->accountNumber,
newBalance);
writeAccountsToFile(accounts,
MAX_ACCOUNTS);
}
else if (n == 3)
{
long long transfer;
long long current_balance =
targetAccount->balance;
long long transfer_account;
long long new_balance;
long long new_transfered_balance;
//
chuyen1:
printf("------------------------------\
n");
printf(" Transfer to account: ");
scanf("%lld", &transfer_account);
ACC *check =
findAccountByNumber(accounts, MAX_ACCOUNTS,
transfer_account);
if (check == NULL)
{
printf("\n Khong ton tai tai
khoan:\n");
goto chuyen1;
}
printf("------------------------------\
n");
chuyen2:
printf(" Money to transfer(VND): ");
scanf("%lld", &transfer);
if (transfer > current_balance)
{
printf("\n So du tai khoan khong
du:\n");
printf("------------------------------\n");
goto chuyen2;
}
printf("------------------------------\
n");
printf(" Transfer complete!\n");
printf("------------------------------\
n");
//
ACC *targetAccount2 =
findAccountByNumber(accounts, MAX_ACCOUNTS,
transfer_account);
new_balance = current_balance -
transfer;
new_transfered_balance =
targetAccount2->balance + transfer;
updateAccountBalance(accounts,
MAX_ACCOUNTS, targetAccount->accountNumber,
new_balance);
updateAccountBalance(accounts,
MAX_ACCOUNTS, targetAccount2->accountNumber,
new_transfered_balance);
writeAccountsToFile(accounts,
MAX_ACCOUNTS);
}
else if (n == 4)
{
int newPin;
checkpin:
printf("------------------------------\
n");
printf(" Ma pin moi(6 number): ");
scanf("%d", &newPin);
if (newPin < 100000 || newPin > 999999)
{
printf(" Ma pin khong hop le:\n");
goto checkpin;
}
printf("------------------------------\
n");
printf("doi ma pin thanh cong!\n");
updateAccountPin(accounts,
MAX_ACCOUNTS, targetAccount->accountNumber,
newPin);
writeAccountsToFile(accounts,
MAX_ACCOUNTS);
}
else if (n == 5)
{
// khai bao gia tri ATM moi
long long soThe;
int new_password;
long long new_balance;
char x[100];
int space = 0;
// nhap gia tri cua ATM
printf("==============================\
n");
printf(" VTC Academy\n");
printf("==============================\
n");
printf(" Create ATM Card\n");
printf("------------------------------\
n");
x:
printf(" Input Account Name : ");
getchar();
fgets(x, 100, stdin);
//chuan hoa xau ki tu
for (int i = 0; i < strlen(x); i++)
{
if (x[i] == ' ')
{
space++;
}
else if (islower(x[i]))
{
space = 0;
printf("ten tai khoan khong hop
le\n");
goto x;
}
else if(x[i]==' '&&x[i+1]==' '){
space = 0;
printf("ten tai khoan khong hop
le\n");
goto x;
}
else if(x[0]==' '){
space = 0;
printf("ten tai khoan khong hop
le\n");
goto x;
}
}
if (space <2||space>3)
{
printf("ten tai khoan khong hop le\
n");
space=0;
goto x;
}
soThe:
printf(" Input accountNumber : ");
scanf("%lld", &soThe);
if (soThe < 1000000000000 || soThe >
9999999999999)
{
printf("so the khong hop le:\n");
goto soThe;
}
pincode:
printf(" Input Pin code : ");
scanf("%d", &new_password);
if (new_password < 100000 ||
new_password > 999999)
{
printf("ma pin khong hop le\n");
goto pincode;
}
printf(" Input balance : ");
scanf("%lld", &new_balance);
createATMAccount(soThe, new_password,
new_balance, x);
printf("Create complete!\n");
}
else if (n == 6)
{
printf("ket thuc chuong trinh");
return 0;
}
else
{
menu++;
goto console;
}
}
return 0;
}
FILE Lưu TRỮ main_account.txt:

Giao diện đăng nhập:


Giao diện menu:

Menu 1:
Menu 2:

Menu 3:

Menu 4:
Menu 5:

Menu 6

You might also like