21xrx.com
2024-12-22 21:02:10 Sunday
登录
文章检索 我的文章 写文章
C++银行账户管理系统代码
2023-07-10 05:55:54 深夜i     --     --
C++ 银行账户 管理系统 代码 数据库

C++银行账户管理系统是一个功能丰富的应用程序,可以帮助用户管理其银行账户。本文将介绍该程序的代码以及其主要特点。

主要特点:

- 可以添加,删除,更新和查看账户信息

- 支持多个账户

- 采用文件存储数据,在程序关闭时保存数据

该程序的主要代码如下:


#include<iostream>

#include<fstream>

#include<cstring>

#include<iomanip>

using namespace std;

class Account

{

private:

  int accountNumber;

  char firstName[10];

  char lastName[10];

  float balance;

public:

  void createAccount();

  void showAccount();

  void modify();

  void deposit(float);

  void withdraw(float);

  void report();

  int getAccountNumber();

  float getBalance();

};

void Account::createAccount()

{

  cout<<"\nEnter the account number: ";

  cin>>accountNumber;

  cout<<"\nEnter the first name: ";

  cin>>firstName;

  cout<<"\nEnter the last name: ";

  cin>>lastName;

  cout<<"\nEnter the balance: ";

  cin>>balance;

  cout<<"\nAccount created...\n";

}

void Account::showAccount()

{

  cout<<"\nAccount Number: "<<accountNumber;

  cout<<"\nFirst Name: "<<firstName;

  cout<<"\nLast Name: "<<lastName;

  cout<<"\nBalance: "<<balance;

}

void Account::modify()

{

  cout<<"\nAccount Number: "<<accountNumber;

  cout<<"\nModify First Name: ";

  cin>>firstName;

  cout<<"\nModify Last Name: ";

  cin>>lastName;

  cout<<"\nModify Balance: ";

  cin>>balance;

  cout<<"\nAccount updated...";

}

void Account::deposit(float amount)

{

  balance+=amount;

}

void Account::withdraw(float amount)

{

  if(balance<amount)

    cout<<"\nInsufficient balance...";

  else

    balance-=amount;

}

void Account::report()

{

  cout<<accountNumber<<setw(10)<<" "<<firstName<<setw(10)<<" "<<lastName<<setw(10)<<balance<<endl;

}

int Account::getAccountNumber()

  return accountNumber;

float Account::getBalance()

  return balance;

void writeAccount();

void displayAll();

void displaySp(int);

void modifyAccount(int);

void deleteAccount(int);

void depositWithdraw(int, int);

int main()

{

  char welcome;

  cout<<"\n\n\n\t\t\t\t===========================================================";

  cout<<"\n\t\t\t\t\t Welcome to the Bank Account Management System";

  cout<<"\n\t\t\t\t===========================================================";

  cout<<"\n\n\n\t\t\t\tEnter any key to continue";

  cin>>welcome;

  int choice;

  int accountNumber;

  do

  {

    cout<<"\n\n\n\t\t\t\t===========================================================";

    cout<<"\n\t\t\t\t\tMain Menu";

    cout<<"\n\t\t\t\t===========================================================";

    cout<<"\n\n\t\t\t\t1. New Account";

    cout<<"\n\t\t\t\t2. Deposit Amount";

    cout<<"\n\t\t\t\t3. Withdraw Amount";

    cout<<"\n\t\t\t\t4. Balance Enquiry";

    cout<<"\n\t\t\t\t5. Show All Accounts";

    cout<<"\n\t\t\t\t6. Close An Account";

    cout<<"\n\t\t\t\t7. Modify An Account";

    cout<<"\n\t\t\t\t8. Exit";

    cout<<"\n\n\t\t\t\tEnter your choice: ";

    cin>>choice;

    switch(choice)

    {

    case 1:

      writeAccount();

      break;

    case 2:

      cout<<"\n\n\tEnter the account number: ";

      cin>>accountNumber;

      depositWithdraw(accountNumber, 1);

      break;

    case 3:

      cout<<"\n\n\tEnter the account number: ";

      cin>>accountNumber;

      depositWithdraw(accountNumber, 2);

      break;

    case 4:

      cout<<"\n\n\tEnter the account number: ";

      cin>>accountNumber;

      displaySp(accountNumber);

      break;

    case 5:

      displayAll();

      break;

    case 6:

      cout<<"\n\n\tEnter the account number: ";

      cin>>accountNumber;

      deleteAccount(accountNumber);

      break;

    case 7:

      cout<<"\n\n\tEnter the account number: ";

      cin>>accountNumber;

      modifyAccount(accountNumber);

      break;

    case 8:

      cout<<"\n\n\tThanks for using bank management system!!";

      break;

    default:cout<<"\n\n\tInvalid choice!!";

    }

    cin.ignore();

    cin.get();

  }while(choice!=8);

  return 0;

}

void writeAccount()

{

  Account ac;

  ofstream outFile;

  outFile.open("account.txt",ios::binary|ios::app);

  ac.createAccount();

  outFile.write(reinterpret_cast<char *> (&ac), sizeof(Account));

  outFile.close();

}

void displayAll()

{

  Account ac;

  ifstream inFile;

  inFile.open("account.txt",ios::binary);

  if(!inFile)

  

    cout<<"File could not be open !! Press any Key...";

    return;

  

  cout<<"\n\n\t\tDISPLAYING ALL ACCOUNTS\n\n";

  cout<<"====================================================\n";

  cout<<"A/c no.   NAME      Type  Balance\n";

  cout<<"====================================================\n";

  while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(Account)))

  {

    ac.report();

  }

  inFile.close();

}

void displaySp(int n)

{

  Account ac;

  bool flag=false;

  ifstream inFile;

  inFile.open("account.txt",ios::binary);

  if(!inFile)

  

    cout<<"File could not be open !! Press any Key...";

    return;

  

  cout<<"\nBALANCE DETAILS\n";

  while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(Account)))

  {

    if(ac.getAccountNumber()==n)

    {

      ac.showAccount();

      flag=true;

    }

  }

  inFile.close();

  if(flag==false)

    cout<<"\n\nAccount number does not exist";

}

void modifyAccount(int n)

{

  bool found=false;

  Account ac;

  ifstream inFile;

  ofstream outFile;

  inFile.open("account.txt",ios::binary);

  if(!inFile)

  

    cout<<"File could not be open !! Press any Key...";

    return;

  

  outFile.open("Temp.txt",ios::binary);

  while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(Account)))

  {

    if(ac.getAccountNumber()==n)

    {

      ac.modify();

      found=true;

    }

    outFile.write(reinterpret_cast<char *> (&ac), sizeof(Account));

  }

  inFile.close();

  outFile.close();

  if(found==false)

    cout<<"\n\nAccount number does not exist";

  else

  {

    remove("account.txt");

    rename("Temp.txt","account.txt");

    cout<<"\n\n\tRecord Updated!";

  }

}

void deleteAccount(int n)

{

  Account ac;

  ifstream inFile;

  ofstream outFile;

  inFile.open("account.txt",ios::binary);

  if(!inFile)

  

    cout<<"File could not be open !! Press any Key...";

    return;

  

  outFile.open("Temp.txt",ios::binary);

  while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(Account)))

  {

    if(ac.getAccountNumber()!=n)

    {

      outFile.write(reinterpret_cast<char *> (&ac), sizeof(Account));

    }

  }

  inFile.close();

  outFile.close();

  remove("account.txt");

  rename("Temp.txt","account.txt");

  cout<<"\n\n\tRecord Deleted..";

}

void depositWithdraw(int n, int option)

{

  float amount;

  bool found=false;

  Account ac;

  fstream File;

  File.open("account.txt", ios::binary|ios::in|ios::out);

  if(!File)

  

    cout<<"File could not be open !! Press any Key...";

    return;

  

  while(File.read(reinterpret_cast<char *> (&ac), sizeof(Account)) && found==false)

  {

    if(ac.getAccountNumber()==n)

    {

      ac.showAccount();

      if(option==1)

      {

        cout<<"\n\n\tTO DEPOSIT AMOUNT ";

        cout<<"\n\nEnter the amount to be deposited: ";

        cin>>amount;

        ac.deposit(amount);

      }

      if(option==2)

      {

        cout<<"\n\n\tTO WITHDRAW AMOUNT ";

        cout<<"\n\nEnter the amount to be withdrawn: ";

        cin>>amount;

        float bal=ac.getBalance()-amount;

        if((bal<500 && ac.getAccountNumber()<285) || (bal<1000 && ac.getAccountNumber()>284))

          cout<<"Insufficient balance";

        else

          ac.withdraw(amount);

      }

      int pos=(-1)*static_cast<int>(sizeof(Account));

      File.seekp(pos,ios::cur);

      File.write(reinterpret_cast<char *> (&ac), sizeof(Account));

      cout<<"\n\n\tRecord Updated!";

      found=true;

    }

  }

  File.close();

  if(found==false)

    cout<<"\n\nRecord Not Found!!";

}

该程序包含一个类 Account,其中定义了所有银行账户的属性和方法。方法包括 createAccount(),showAccount(),modify(),deposit(),withdraw(),report(),getAccountNumber()和getBalance()。main()函数根据用户的选择调用相应的方法。

该程序采用文件来存储数据并在程序关闭时自动保存。在每次修改数据时,程序会读取整个文件,将修改后的数据写回文件。该程序的优点是可以很方便地管理多个账户,同时保护用户的隐私和数据安全。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复