C++ 练习(-)小型银行数据记录系统

 
添加记录:
对于此功能,void read_data()函数已用于将银行记录添加到文件中。它要求输入诸如帐号,名字,姓氏和余额等信息。
显示/列表数据:
通过添加记录提供的信息,C ++中的银行记录系统项目中的void show_data()函数显示与特定账号,名字和姓氏相对应的记录。显示帐户持有者的当前余额。
搜索记录:
当此功能的功能首次执行时,它会显示文件中的全部记录,然后用户可以按记录编号进行搜索。如果未找到搜索到的记录,C ++中的银行记录系统项目会显示消息 - Error in opening! File Not Found!!
编辑记录:
这与搜索功能类似。编辑记录功能首次执行时,会显示文件中的全部记录,用户可以通过提供记录编号来编辑信息。然后,C ++项目显示该记录中的所有数据,并且用户可以输入任何要修改的数据。如果没有找到要编辑的记录,则会显示消息 - Error in opening! File Not Found!!
删除记录:
首先,执行该功能的功能时,会显示文件中的所有记录,用户可以输入要删除的记录编号。如果没有找到该记录,则该C ++中的银行记录系统项目会显示消息 - Error in opening! File Not Found!!
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

class account_query
{
private:
	char account_number[20];
	char firstName[10];
	char lastName[10];
	float total_Balance;

public:
	void read_data();
	void show_data();
	void write_rec();
	void read_rec();
	void search_rec();
	void edit_rec();
	void delete_rec();
};

void account_query::read_data()
{
	cout<<"\nEnter AccountNumber:";
	cin>>account_number;
	cout<<"Enter FirstName:";
	cin>>firstName;
	cout<<"Enter LastName:";
	cin>>lastName;
	cout<<"Enter Balance:";
	cin>>total_Balance;
	cout<<endl;
}

void account_query::show_data()
{
	cout<<"Account Number:"<<account_number<<endl;
	cout<<"FirstName:"<<firstName<<endl;
	cout<<"LastName:"<<lastName<<endl;
	cout<<"CurrentBalance:Rs."<<total_Balance<<endl;
	cout<<"____________________________"<<endl;
}

void account_query::write_rec()
{
	ofstream outfile;
	outfile.open("record.bank",ios::binary|ios::app);
	read_data();
	outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
	outfile.close();
}

void account_query::read_rec()
{
	ifstream infile;
	infile.open("record.bank",ios::binary);
	if(!infile)
	{
		cout<<"Error in Opening!File Not Found!!"<<endl;
		return;
	}
	cout<<"\n*****Data from file**********"<<endl;
	while(!infile.eof())
	{
		if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0)
		{
			show_data();
		}
	}
	infile.close();
}

void account_query::search_rec()
{
	int n;
	ifstream infile;
	infile.open("record.bank",ios::binary);
	if(!infile)
	{
		cout<<"\nError in Opening! File Not Found!!"<<endl;
		return;
	}
	infile.seekg(0,ios::end);
	int count= infile.tellg()/sizeof(*this);
	cout<<"\n There are"<<cout<< "record in the file";
	cout<<"\nEnter  Record Number to Search:";
	cin>>n;
	infile.seekg((n-1)*sizeof(*this));
	infile.read(reinterpret_cast<char*>(this),sizeof(*this));
	show_data();
}

void account_query::edit_rec()
{
	int n;
	fstream iofile;
	iofile.open("record.bank",ios::in|ios::binary);
	if(!iofile)
	{
		cout<<"\nError in opening! File Not Found!!"<<endl;
		return;
	}
	iofile.seekg(0,ios::end);
	int count=iofile.tellg()/sizeof(*this);
	cout<<"\n There are"<<count<<"record in the file";
	cout<<"\n Enter Record Number to edit:";
	cin>>n;
	iofile.seekg((n-1)*sizeof(*this));
	iofile.read(reinterpret_cast<char*>(this),sizeof(*this));
	cout<<"Record"<<n<<"has following data"<<endl;
	show_data();
	iofile.close();
	iofile.open("record.bank",ios::out|ios::binary);
	iofile.seekg((n-1)*sizeof(*this));
	cout<<"\n Enter data to Modify"<<endl;
	read_data();
	iofile.write(reinterpret_cast<char*>(this),sizeof(*this));
}

void account_query::delete_rec()
{
	int n;
	ifstream infile;
	infile.open("record.bank",ios::binary);
	if(!infile)
	{
		cout<<"\nError in Opening! File Not Found!!"<<endl;
		return;
	}
	infile.seekg(0,ios::end);
	int count=infile.tellg()/sizeof(*this);
	cout<<"\n There are"<<count<<"record in the file";
	cout<<"\n Enter Record Number to Delete:";
	cin>>n;
	fstream tmpfile;
	tmpfile.open("tmpfile.bank",ios::out|ios::binary);
	infile.seekg(0);
	for(int i=0;i<count;i++)
	{
		infile.read(reinterpret_cast<char*>(this),sizeof(*this));
		if(i==(n-1))
			continue;
		tmpfile.write(reinterpret_cast<char*>(this),sizeof(*this));
	}
	infile.close();
	tmpfile.close();
	remove("record.bank");
	rename("tmpfile.bank","record.bank");

}

int main()
{
	account_query A;
	int choice;
	cout<<"***Acount Information System***"<<endl;
	while(true)
	{
		cout<<"Select one option below";
		cout<<"\n\t1-->Add record to file";
		cout<<"\n\t2-->Show record from file";
		cout<<"\n\t3-->Search Record from file";
		cout<<"\n\t4-->Update Record";
		cout<<"\n\t5-->Delete Record";
		cout<<"\n\t6-->Quit"<<endl;
		cout<<"enter your choice:";
		cin>>choice;
		switch(choice)
		{
			case 1:
				A.write_rec();
				break;

			case 2:
				A.read_rec();
				break;

			case 3:
				A.search_rec();
				break;

			case 4:
				A.edit_rec();
				break;
				
			case 5:
				A.delete_rec();
				break;

			case 6:
				exit(0);
				break;

			default:
				cout<<"\nEnter corret choice";
				exit(0);


		}
	}
	system("pause");
		return 0;
}
主菜单
数据输入:
 
显示系统储存的数据:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非愚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值