Bank management system
Code:
#include<iostream>
#include<fstream>
using namespace std;
class account
{
private:
char account_number[20];
char firstName[10];
char lastName[10];
float total_Balance;
public:
void deposit();
void read_data();
void show_data();
void write_rec();
void read_rec();
void search_rec();
void edit_rec();
};
void account::read_data()
{
cout<<"\nPlease Enter Account Number: ";
cin>>account_number;
cout<<"Enter First Name: ";
cin>>firstName;
cout<<"Enter Last Name: ";
cin>>lastName;
cout<<"Enter Balance: ";
cin>>total_Balance;
cout<<endl;
}
void account::show_data()
{
cout<<"Account Number: "<<account_number<<endl;
cout<<"First Name: "<<firstName<<endl;
cout<<"Last Name: "<<lastName<<endl;
cout<<"Current Balance: Rs. "<<total_Balance<<endl;
cout<<"-------------------------------"<<endl;
}
void account::write_rec()
{
ofstream outfile;
outfile.open("record.dat", ios::binary|ios::app);
read_data();
outfile.write(reinterpret_cast<char *>(this), sizeof(*this));
outfile.close();
}
void account::read_rec()
{
ifstream infile;
infile.open("record.dat", ios::binary);
if(!infile)
{
cout<<"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::search_rec()
{
int n;
ifstream infile;
infile.open("record.dat", 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 Search: ";
cin>>n;
infile.seekg((n-1)*sizeof(*this));
infile.read(reinterpret_cast<char*>(this), sizeof(*this));
show_data();
}
void account::edit_rec()
{
int n;
fstream iofile;
iofile.open("record.dat", 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.dat", ios::out|ios::in|ios::binary);
iofile.seekp((n-1)*sizeof(*this));
cout<<"\n Modify Data "<<endl;
deposit();
iofile.write(reinterpret_cast<char*>(this), sizeof(*this));
}
void account::deposit()
{ int x;
cout<<"Enter amount that you want to add";
cin>>x;
total_Balance+=x;
}
int main()
{
account A;
int choice;
cout<<"Acount Record 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-->Quit";
cout<<"\nEnter 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:
exit(0);
break;
default:
cout<<"\nEnter corret choice";
exit(0);
}
}
system("pause");
return 0;
}
0 comments:
Post a Comment