as a part of practicing, i am trying to create a financial chart of accounts (coa)
and i am using vector as data structure since i am familiar with it more or less.
my account is based upon 7 segments: parent.name.description.type.currency.balance
so as a first attempt, i have created a vector and used push_back to insert each account (of 7 segments). NOW i want to get the account information by passing the account number to the function "void getAccountInfo(string &account_number){ ...}" and the function should print the info for that specific account number. how can i accomplish that? i have no idea. also not sure if this is the right way to use the vector to accomplish my task. e.g in a database, i would issue an SQL statement to return the information. i need to do the same thing here. Thanks for any help.
#ifndef CPP_COA_H
#define CPP_COA_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class account{
public:
account(){
this -> parent_level_3 = "00000";
this -> account_balance = 0.0;
this -> account_name = "default";
this -> account_type = "default";
this -> account_number ="00000.0";
this -> account_ccy = "CAD";
}
// create new account
account(string &parent_3,
string &number,
string &name,
string &type,
double &bal,
string &ccy){
this -> parent_level_3 = parent_3;
this -> account_name = name;
this -> account_number = number;
this -> account_type = type;
this -> account_ccy = ccy;
this -> account_balance = bal;
}
// get functions
string getAccParent3(){ return this -> parent_level_3; }
string getAccNum(){ return this -> account_number; }
string getAccName(){ return this -> account_name; }
string getAccDesc(){ return this -> account_description; }
string getAccType(){ return this -> account_type; }
string getAccCcy(){ return this -> account_ccy; }
double getAccBal(){ return this -> account_balance; }
void getAccountInfo(string &account_number){
/*
* in this part how can i ask the vector to give me
* the information for that specific account?
* in a database, for instance, i would say: SELECT info from table WHERE account = &account_number;
* so how can ask the vector for the same favor?
*/
string all = getAccName() + " "
+ getAccDesc() + " "
+ getAccType() + " "
+ getAccCcy() + " "
+ to_string(getAccBal());
cout << all << endl;
}
// set account's balance
void setBalance(double bal){ this -> account_balance = bal; }
// set account's level 3 parent
void setParent3(string parent3){ this -> parent_level_3 = parent3; }
// set account's Number
void setAccNum(string acc_num){ this -> account_number = acc_num; }
// set account's Name
void setAccName(string acc_name){ this -> account_name = acc_name; }
// set account's Description
void setAccDesc(string acc_desc){ this -> account_description = acc_desc; }
// set account's Type
void setAccType(string acc_type){ this -> account_type = acc_type; }
// set account's Currency
void setAccCurr(string acc_ccy){ this -> account_ccy = acc_ccy; }
// set account's Balance
void setAccBal(double acc_bal){ this -> account_balance = acc_bal; }
void setupAccount(account acc){ this-> coa.push_back(acc); }
void printAccountInfo(){
//to traverse and print the vector from start to finish
for (unsigned int i = 0; i < this->coa.size(); i++)
std::cout << this->coa.at(i) << " "; //cout doesn't work !!
}
private:
string parent_level_3;
string account_number;
string account_name;
string account_description;
string account_type;
string account_ccy;
double account_balance = 0.0;
vector <account> coa; // chart of accounts
/* sample of an account entry:
Account Type Account Parent Account Number Account Name Account Description CCY Balance
------------ -------------- -------------- ------------- ------------------- ------ ---------
Asset 10000 11110.0 HSBC CHECKING Main assets Account CAD 12,500.23
*/
};
#endif CPP_COA_H
int main(){
string parent3 ="10000"; // a parent account
account newAcc; // default constructor; will use the parametrized constructor
// when things workout instead
string str = "10000.1"; // a child account
newAcc.setupAccount(newAcc); // add the account details to vector
newAcc.getAccountInfo(str);
}