Simple Bank Management System Project Using C++
Introduction
Hello, coders. Welcome to codewithrandom. Today we will learn how to make a simple bank management system consisting of operations to add, show, search, delete and update records of the banking system.
We will be performing the CRUD operations in C++ with this project. The data will be in a file after adding at least one record. The uniqueness of this project is that we are using a different technique than the other articles that use file input and output.
Code by | N/A |
Project Download | Link Available Below |
Language used | C++ |
External link / Dependencies | NO |
Responsive | YES |
The project might be a little advanced to beginners. Nonetheless, I will explain it in the easiest way possible. Let us start by seeing the options available for us.
Overview of the UI
As we can see from the User Interface of the app in the main picture at the start of our article, the available options are:
- Add a record to file
- Show record(s) from the file
- Search Record from file
- Update Record
- Delete Record
- Quit
Now let’s see the code part of the project.
Code
1: #include<iostream> 2: #include<fstream> 3: #include<cstdlib> 4: using std::cout; 5: using std::cin; 6: using std::endl; 7: using std::fstream; 8: using std::ofstream; 9: using std::ifstream; 10: using std::ios; 11: class account_query 12: { 13: private: 14: char account_number[20]; 15: char firstName[10]; 16: char lastName[10]; 17: float total_Balance; 18: public: 19: void read_data(); 20: void show_data(); 21: void write_rec(); 22: void read_rec(); 23: void search_rec(); 24: void edit_rec(); 25: void delete_rec(); 26: }; 27: void account_query::read_data() 28: { 29: cout<<"nEnter Account Number: "; 30: cin>>account_number; 31: cout<<"Enter First Name: "; 32: cin>>firstName; 33: cout<<"Enter Last Name: "; 34: cin>>lastName; 35: cout<<"Enter Balance: "; 36: cin>>total_Balance; 37: cout<<endl; 38: } 39: void account_query::show_data() 40: { 41: cout<<"Account Number: "<<account_number<<endl; 42: cout<<"First Name: "<<firstName<<endl; 43: cout<<"Last Name: "<<lastName<<endl; 44: cout<<"Current Balance: Rs. "<<total_Balance<<endl; 45: cout<<"-------------------------------"<<endl; 46: } 47: void account_query::write_rec() 48: { 49: ofstream outfile; 50: outfile.open("record.txt", ios::binary|ios::app); 51: read_data(); 52: outfile.write(reinterpret_cast<char *>(this), sizeof(*this)); 53: outfile.close(); 54: } 55: void account_query::read_rec() 56: { 57: ifstream infile; 58: infile.open("record.txt", ios::binary); 59: if(!infile) 60: { 61: cout<<"Error in Opening! File Not Found!!"<<endl; 62: return; 63: } 64: cout<<"n****Data from file****"<<endl; 65: while(!infile.eof()) 66: { 67: if(infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0) 68: { 69: show_data(); 70: } 71: } 72: infile.close(); 73: } 74: void account_query::search_rec() 75: { 76: int n; 77: ifstream infile; 78: infile.open("record.txt", ios::binary); 79: if(!infile) 80: { 81: cout<<"nError in opening! File Not Found!!"<<endl; 82: return; 83: } 84: infile.seekg(0,ios::end); 85: int count = infile.tellg()/sizeof(*this); 86: cout<<"n There are "<<count<<" record in the file"; 87: cout<<"n Enter Record Number to Search: "; 88: cin>>n; 89: infile.seekg((n-1)*sizeof(*this)); 90: infile.read(reinterpret_cast<char*>(this), sizeof(*this)); 91: show_data(); 92: } 93: void account_query::edit_rec() 94: { 95: int n; 96: fstream iofile; 97: iofile.open("record.txt", ios::in|ios::binary); 98: if(!iofile) 99: { 100: cout<<"nError in opening! File Not Found!!"<<endl; 101: return; 102: } 103: iofile.seekg(0, ios::end); 104: int count = iofile.tellg()/sizeof(*this); 105: cout<<"n There are "<<count<<" record in the file"; 106: cout<<"n Enter Record Number to edit: "; 107: cin>>n; 108: iofile.seekg((n-1)*sizeof(*this)); 109: iofile.read(reinterpret_cast<char*>(this), sizeof(*this)); 110: cout<<"Record "<<n<<" has following data"<<endl; 111: show_data(); 112: iofile.close(); 113: iofile.open("record.txt", ios::out|ios::in|ios::binary); 114: iofile.seekp((n-1)*sizeof(*this)); 115: cout<<"nEnter data to Modify "<<endl; 116: read_data(); 117: iofile.write(reinterpret_cast<char*>(this), sizeof(*this)); 118: } 119: void account_query::delete_rec() 120: { 121: int n; 122: ifstream infile; 123: infile.open("record.bank", ios::binary); 124: if(!infile) 125: { 126: cout<<"nError in opening! File Not Found!!"<<endl; 127: return; 128: } 129: infile.seekg(0,ios::end); 130: int count = infile.tellg()/sizeof(*this); 131: cout<<"n There are "<<count<<" record in the file"; 132: cout<<"n Enter Record Number to Delete: "; 133: cin>>n; 134: fstream tmpfile; 135: tmpfile.open("tmpfile.txt", ios::out|ios::binary); 136: infile.seekg(0); 137: for(int i=0; i<count; i++) 138: { 139: infile.read(reinterpret_cast<char*>(this),sizeof(*this)); 140: if(i==(n-1)) 141: continue; 142: tmpfile.write(reinterpret_cast<char*>(this), sizeof(*this)); 143: } 144: infile.close(); 145: tmpfile.close(); 146: remove("record.bank"); 147: rename("tmpfile.bank", "record.bank"); 148: } 149: int main() 150: { 151: account_query A; 152: int choice; 153: cout<<"***Acount Information System***"<<endl; 154: while(true) 155: { 156: cout<<"Select one option below "; 157: cout<<"nt1-->Add record to file"; 158: cout<<"nt2-->Show record from file"; 159: cout<<"nt3-->Search Record from file"; 160: cout<<"nt4-->Update Record"; 161: cout<<"nt5-->Delete Record"; 162: cout<<"nt6-->Quit"; 163: cout<<"nEnter your choice: "; 164: cin>>choice; 165: switch(choice) 166: { 167: case 1: 168: A.write_rec(); 169: break; 170: case 2: 171: A.read_rec(); 172: break; 173: case 3: 174: A.search_rec(); 175: break; 176: case 4: 177: A.edit_rec(); 178: break; 179: case 5: 180: A.delete_rec(); 181: break; 182: case 6: 183: exit(0); 184: break; 185: default: 186: cout<<"nEnter corret choice"; 187: exit(0); 188: } 189: } 190: system("pause"); 191: return 0; 192: }
Code Explanation
After the headers of the program, we can see that we have declared the account_number, firstName, lastName, and total_Balance as private because we don’t want to modify values within the function.
In general, it is a good practice to declare such variables private.
Let’s now evaluate the main parts of the code.
100+ JavaScript Projects With Source Code ( Beginners to Advanced)
The main() function
It consists of a switch-case statement with the respective functions.
The read_data() function
The function aims to take the data whenever necessary. It is more like a helper function to the main functions.
The show_data() function
It is another helper function, just like the read_data(). But this function prints the values when we need them.
The read_rec() function
It is the main function used to add data to the file. Here we can see a new technique known to people experienced in C++. The infile.read() method reads the structure data directly from a file into the memory occupied by it.
ADVERTISEMENT
We keep this in the while loop to display all the data present.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
The write_rec() function
It is used to take the data from the user. Similar to read_rec() function we use outfile.write() here which performs the same thing except writes instead of reading it.
The search_rec() function
If the file is not there or the file is empty, we check and print the needed message.
If it exists, we get the total records using the infile.tellg()/sizeof(*this) method.
the tellg() method is used to return the “get” position of the pointer in the stream. It reports a token value that can later be used to seek the same place, instead of the size.
Then we use infile.seekg() to point to the position and get the data necessary.
50+ HTML, CSS & JavaScript Projects With Source Code
The edit_rec() function
So to edit the data, we first need to know the total number of records, which record to update.
Once we get the record we need to update, we show the record data using the show_data() function.
Then open the file to append that record and call read_data() to take the new data for that record.
The delete_rec() function
In the function, we create a temp file to store the details and then delete the record we want to delete. We use the remove function as a helper function to remove the file and rename the temp file that already has the same data without the deleted data as the deleted file name.
That is the working of the code. It might seem a bit hard but understanding this gives us knowledge of core concepts in C++.
Conclusion
We have reached the end of the article but we have a lot more projects in C++ coming. We started with awesome and fun projects. Now we will be moving to projects for learning C++.
If you have enjoyed the article and learned something new today, let us know in the comments.
Thank you.
Happy Reading 🙂