Banking record system

The following code is named “Banking record system”. Actually this project is very simple. It adds a new information of accounts, show their content , search for them by account numbers, update information and delete it.

Although this project is quite simple, I think there are some interesting and useful notes to discuss.

  1. Verify input values
  2. Convert string to number and vice versa
  3. String and char datatype
  4. Dump a csv file into vector and vice versa

I will dig the above problems in the next entries.

/**
  *Project description:
  *Name:   Banking Record System
  *@author: Dang Van Truong
  *This project records basic terms of a bank account into a file. I set it in "csv" type (accountnumber,firstname,lastname,balance).
  #paramenters
  *@infor_record.csv stores information of all accounts
  *@list_record.txt stores all account numbers
  *@accountNumber saves account number
  *@firstName saves the first name of an account's owner
  *@secondName saves the second name of an account's owner
  *@balance saves the amount of money in an account
  *@s_prefix indicates a value for setting
  *@n_prefix indicates a new value - to replace old value
  *@c_prefix indicates a value for checking
  #functions
  *@readData() records information of a new account.
  *@showData() displays information of existing accounts.
  *@searchRecord() find information of an existing account via its account number
  *@editRecord() updates information of an existing account.
  *@deleteRecord() deletes all information of an existing account.
  */
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <vector>
#define INFOR_FILE "infor_record.csv"
#define LIST_FILE "list_record.txt"
#include <sstream>
using namespace std;
const vector<char>  CONFIRMATION {'Y','N'};
const vector<int> G_OPTION {1,2,3,4,5,6};
const vector<int> C_OPTION {0,1,2,3,4};
// functions declaration
void readData();
void showData();
void searchRecord();
void editRecord();
void deleteRecord();
int stoi(string s);
int checkExistenceAccountNumber(unsigned c_accountNumber);
template <class T>
T confirm(vector<T> source);
// functions definition
// defenition function @readData
void readData(){
	char option;
	do{
		fstream fout(INFOR_FILE,ios::app);
		if (!fout.is_open()){
			cout<<"The recorded file cannot be opened!"<<endl;
		}else{
			unsigned s_accountNumber;
			string s_firstName,s_lastName;
			float s_balance;
			cout<<"Enter information of a new account. Please be careful to keep exactness of all fields"<<endl;
			cout<<"Account number: \t"; 			cin>>s_accountNumber;cin.ignore();
			if (checkExistenceAccountNumber(s_accountNumber)==-1){
				cout<<"The program has encountered an error in checking existence of the input account number"<<endl;
				fout.close();
			}
			if (checkExistenceAccountNumber(s_accountNumber)==1){
				cout<<"The input account number has existed in the system. Please recheck!"<<endl;
				fout.close();
			}
			if (checkExistenceAccountNumber(s_accountNumber)==0){
				cout<<"First name: \t";
				getline(cin,s_firstName);
				cout<<"Last name: \t";
				getline(cin,s_lastName);
				cout<<"Balance: \t"; 				cin>>s_balance;
				fstream foutL(LIST_FILE,ios::app);
				if(!fout.is_open()) {
					cout<<"Fatal error. Please redo!"<<endl;
				}else{
					fout<<s_accountNumber<<","<<s_firstName<<","<<s_lastName<<","<<s_balance<<endl;
					foutL<<s_accountNumber<<endl;
					fout.close();
					foutL.close();
					cout<<"Record information is successful"<<endl;
				}
			}
		}
		cout<<"Do you want to continue or back to the main menu (Y for continue):\t";
		option=confirm(CONFIRMATION);
	}while(option=='Y');
}
// finish defining
// definition function @showData
void showData(){
	char option;
	do{
		fstream fin(INFOR_FILE,ios::in);
		if (!fin.is_open()){
			cout<<"The recorded file cannot be opened!"<<endl;
		}else{
			cout<<"Information of all existing accounts"<<endl;
			string tempT;
			while (getline(fin,tempT)){
				vector<string> _t;
				while (!tempT.empty()){
					if (tempT.find(",")!=std::string::npos){
						_t.push_back(tempT.substr(0,tempT.find(",")));
						tempT=tempT.substr(tempT.find(",")+1);
					}else {
						_t.push_back(tempT);
						tempT.clear();
					}
				}
				if (!_t.empty()){
                    cout<<"------------------------------------------------------------"<<endl;
                    cout<<"Information of account number "<<_t[0]<<" : "<<endl;
                    cout<<"First Name:"<<"\t\t"<<_t[1]<<endl;
                    cout<<"Last Name:"<<"\t\t"<<_t[2]<<endl;
                    cout<<"Current balance:"<<"\t"<<_t[3]<<endl;
                    cout<<"------------------------------------------------------------"<<endl;
				}
			}
			fin.close();
        }
		cout<<"Do you want to continue or back to the main menu (Y for continue):\t";
		option=confirm(CONFIRMATION);
	}while(option=='Y');
}
// finish defining
// definition function @searchRecord
void searchRecord(){
	char option;
	do{
		fstream fin(INFOR_FILE,ios::in);
		if (!fin.is_open()){
			cout<<"The recorded file cannot be opened!"<<endl;
		}else{
			unsigned c_accountNumber;
			vector<vector<string> > vContent;
			string tempT;
			while (getline(fin,tempT)){
				vector<string> _t;
				while (!tempT.empty()){
					if (tempT.find(",")!=std::string::npos){
						_t.push_back(tempT.substr(0,tempT.find(",")));
						tempT=tempT.substr(tempT.find(",")+1);
					}else {
						_t.push_back(tempT);
						tempT.clear();
					}
				}
				if(!_t.empty()) {
					vContent.push_back(_t);
				}
			}
			fin.close();
			cout<<"There are "<<vContent.size()<<" records in the file"<<endl;
			cout<<"Enter an account number you want to look for:\t"; 			cin>>c_accountNumber;
			cout<<"------------------------------------------------------------"<<endl;
			bool flagT(false);
			for(unsigned ii=0;ii<vContent.size();++ii){
				if (stoi(vContent[ii][0])==c_accountNumber){
					flagT=true;
					cout<<"Information of account number "<<vContent[ii][0]<<" : "<<endl;
					cout<<"First Name:"<<"\t\t"<<vContent[ii][1]<<endl;
					cout<<"Last Name:"<<"\t\t"<<vContent[ii][2]<<endl;
					cout<<"Current balance:"<<"\t"<<vContent[ii][3]<<endl;
				}
			}
			if (!flagT) {
				cout<<"The account number is not found. Please recheck!"<<endl;
			}
			cout<<"------------------------------------------------------------"<<endl;
		}
		cout<<"Do you want to continue or back to the main menu (Y for continue):\t";
		option=confirm(CONFIRMATION);
	}while(option=='Y');
}
// finish defining
// definition function @editRecord
void editRecord(){
	char option;
	do {
		fstream fin(INFOR_FILE,ios::in);
		fstream finL(LIST_FILE,ios::in);
		unsigned accountNumber,n_accountNumber,foundIndex;
		string n_firstName,n_lastName;
		float n_balance;
		bool flag(false);
		vector<vector<string> > vContent;
		vector<string> vContentL;
		if (!fin.is_open()||!finL.is_open()){
			cout<<"The recorded file cannot be opened!"<<endl;
		}else{
			// write all data into a temporary vector
			string tempT;
			while (getline(fin,tempT)){
				vector<string> _t;
				while (!tempT.empty()){
					if (tempT.find(",")!=std::string::npos){
						_t.push_back(tempT.substr(0,tempT.find(",")));
						tempT=tempT.substr(tempT.find(",")+1);
					}else {
						_t.push_back(tempT);
						tempT.clear();
					}
				}
				if(!_t.empty()) {
					vContent.push_back(_t);
				}
			}
			fin.close();
			while(getline(finL,tempT)){
				if(!tempT.empty()){
					vContentL.push_back(tempT);
				}
			}
			finL.close();
		}
		cout<<"Enter the account number you want to update information:\t"; 		cin>>accountNumber;cin.ignore();
		for(unsigned ii=0;ii<vContent.size();++ii){
			if (stoi(vContent[ii][0])==accountNumber){
				flag=true;
				foundIndex=ii;
				break;
			}
		}
		if(!flag){
			cout<<"The account number is not found. Please recheck!"<<endl;
		}else{
			cout<<"-----------------------------------------------------"<<endl;
			cout<<"Current information of account number "<<accountNumber<<endl;
			cout<<"First name: \t"<<vContent[foundIndex][1]<<endl;
			cout<<"First name: \t"<<vContent[foundIndex][2]<<endl;
			cout<<"First name: \t"<<vContent[foundIndex][3]<<endl;
			cout<<"-----------------------------------------------------"<<endl;
			cout<<"Enter information you want to modify (leave a field empty if you do not want to change it)."<<endl;
			string temp;
			cout<<"Enter new account number:\t";
            getline(cin,temp);
			if (temp.empty()){
				n_accountNumber=accountNumber;
			}else{
				n_accountNumber=stoi(temp);

			}
			bool flagT(false);
            for(unsigned ii=0;ii<vContent.size();++ii){
                if(stoi(vContent[ii][0])==n_accountNumber && n_accountNumber!=accountNumber){
                    flagT=true;
                    break;
                }
            }
            if (flagT){
                cout<<"You can not update information of an another existing account. It is prohibited!"<<endl;
            }else{
				cout<<"Enter new first name:\t";
				getline(cin,n_firstName);
				if (n_firstName.empty()){
					n_firstName=vContent[foundIndex][1];
				}
				cout<<"Enter new last name:\t";
				getline(cin,n_lastName);
				if (n_lastName.empty()){
					n_lastName=vContent[foundIndex][2];
				}
				cout<<"Enter new balance:\t";
				getline(cin,temp);
				if (temp.empty()){
					n_balance=atof(vContent[foundIndex][3].c_str());
				}else{
					n_balance=atof(temp.c_str());
				}
				cout<<"The new information is going to be updated. Please wait for confirmation!"<<endl;
				fstream fout(INFOR_FILE,ios::out);
				fstream foutL(LIST_FILE,ios::out);
				if (!fout.is_open()||!foutL.is_open()){
					cout<<"Error!. The new information can not be updated. Please redo!"<<endl;
				}else{
					vContent[foundIndex][0]=static_cast<ostringstream*>(&(ostringstream()<<n_accountNumber))->str();
					vContent[foundIndex][1]=n_firstName;
					vContent[foundIndex][2]=n_lastName;
					vContent[foundIndex][3]=static_cast<ostringstream*>(&(ostringstream()<<n_balance))->str();
					vContentL[foundIndex]=static_cast<ostringstream*>(&(ostringstream()<<n_accountNumber))->str();
					for(unsigned ii=0;ii<vContent.size();++ii){
						fout<<vContent[ii][0]<<","<<vContent[ii][1]<<","<<vContent[ii][2]<<","<<vContent[ii][3]<<endl;
					}
					fout.close();
					for(unsigned ii=0;ii<vContentL.size();++ii){
						foutL<<vContentL[ii]<<endl;
					}
					foutL.close();
					cout<<"The information has been just updated successfully!"<<endl;
				}
			}
        }
		cout<<"Do you want to continue or back to the main menu (Y for continue):\t";
		option=confirm(CONFIRMATION);
	}while(option=='Y');
}
// finish defining
// definition function @deleteRecord
void deleteRecord(){
	char option;
	do {
		fstream fin(INFOR_FILE,ios::in);
		fstream finL(LIST_FILE,ios::in);
		unsigned accountNumber,foundIndex;
		bool flag(false);
		vector<vector<string> > vContent;
		vector<string> vContentL;
		if (!fin.is_open()||!finL.is_open()){
			cout<<"The recorded file cannot be opened!"<<endl;
		}else{
        // write all data into a temporary vector
			string tempT;
			while (getline(fin,tempT)){
				vector<string> _t;
				while (!tempT.empty()){
					if (tempT.find(",")!=std::string::npos){
						_t.push_back(tempT.substr(0,tempT.find(",")));
						tempT=tempT.substr(tempT.find(",")+1);
					}else {
						_t.push_back(tempT);
						tempT.clear();
					}
				}
				if(!_t.empty()) {
					vContent.push_back(_t);
				}
			}
			fin.close();
			while(getline(finL,tempT)){
				if(!tempT.empty()){
					vContentL.push_back(tempT);
				}
			}
			finL.close();
		}
		cout<<"Enter the account number you want to delete information:\t"; 		cin>>accountNumber;
		for(unsigned ii=0;ii<vContent.size();++ii){
			if (stoi(vContent[ii][0])==accountNumber){
				flag=true;
				foundIndex=ii;
				break;
			}
		}
		if(!flag){
			cout<<"The account number is not found. Please recheck!"<<endl;
		}else{
			char confirmT;
			cout<<"-----------------------------------------------------"<<endl;
			cout<<"Current information of account number "<<accountNumber<<endl;
			cout<<"First name: \t"<<vContent[foundIndex][1]<<endl;
			cout<<"First name: \t"<<vContent[foundIndex][2]<<endl;
			cout<<"First name: \t"<<vContent[foundIndex][3]<<endl;
			cout<<"-----------------------------------------------------"<<endl;
			cout<<"Do you want to delete it? (Y/N)\t";
			confirmT=confirm(CONFIRMATION);
			if (confirmT=='Y'){
				cout<<"The information is going to be deleted. Please wait for confirmation!"<<endl;
				fstream fout(INFOR_FILE,ios::out);
				fstream foutL(LIST_FILE,ios::out);
				if (!fout.is_open()||!foutL.is_open()){
					cout<<"Error!. The information can not be deleted. Please redo!"<<endl;
				}else{
					for(unsigned ii=0;ii<vContent.size();++ii){
						if (ii!=foundIndex){
							fout<<vContent[ii][0]<<","<<vContent[ii][1]<<","<<vContent[ii][2]<<","<<vContent[ii][3]<<endl;
						}
					}
					fout.close();
					for(unsigned ii=0;ii<vContentL.size();++ii){
						if (ii!=foundIndex){
							foutL<<vContentL[ii]<<endl;
						}
					}
					foutL.close();
					cout<<"The information has been just deleted successfully!"<<endl;
				}
			}
		}
		cout<<"Do you want to continue or back to the main menu (Y for continue):\t";
		option=confirm(CONFIRMATION);
	}while(option=='Y');
}
// finish defining
// definition function @stoi
int stoi(string s){
    return atoi(s.c_str());
}
// finish defining
// definition function @checkExistenceAccountNumber
int checkExistenceAccountNumber(unsigned c_accountNumber){
	ifstream fin(LIST_FILE);
	if (!fin.is_open()){
		return -1;
	}
	string temp;
	bool flag(false);
	while(getline(fin,temp)){
		if(stoi(temp)==c_accountNumber){
			flag=true;
			break;
		}
	}
	fin.close();
	if (flag) return 1;
	else return 0;
}
// finish defining
// definition function @confirm
template<class T>
T confirm(vector<T> source){
    T inputT;
    bool flag(false);
    do{
        cin>>inputT;
        if (isalpha(inputT)) {inputT=toupper(inputT);}
        for(unsigned ii=0;ii<source.size();++ii){
            if (inputT==source[ii]){
                flag=true;
                break;
            }
        }
        if (!flag){
            cout<<"Please enter a valid option!"<<endl;
            cout<<"Enter a new option:\t";
        }
    }while(!flag);
    return inputT;
}
// finish defining
int main()
{
    unsigned answer;
    do{
        system("cls");
        cout<<"*****************************************************************************"<<endl;
        cout<<"********************************Banking Record System************************"<<endl;
        cout<<"***Select one option:                                                       *"<<endl;
        cout<<"*                 1->Add a new record                                       *"<<endl;
        cout<<"*                 2->Show all records                                       *"<<endl;
        cout<<"*                 3->Search for a record                                    *"<<endl;
        cout<<"*                 4->Edit information of a record                           *"<<endl;
        cout<<"*                 5->Delete a record                                        *"<<endl;
        cout<<"*                 6->Quit                                                   *"<<endl;
        cout<<"*****************************************************************************"<<endl;
        cout<<"  Enter your option:\t";
        answer=confirm(G_OPTION);
        switch (answer){
            case 1:
                readData();
                break;
            case 2:
                showData();
                break;
            case 3:
                searchRecord();
                break;
            case 4:
                editRecord();
                break;
            case 5:
                deleteRecord();
                break;
            case 6:
                return 0;
                break;
        }
    }while(true);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s