0

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);
}
James
  • 1
  • 1
  • 1
    you are looking for [`std::find_if`](https://en.cppreference.com/w/cpp/algorithm/find) – 463035818_is_not_an_ai Aug 12 '21 at 10:03
  • sorry i meant cout the info + i will surely change the name! it makes more sense – James Aug 12 '21 at 10:08
  • 1
    OT, but relevant: [How can I declare a member vector of the same class?](https://stackoverflow.com/q/41913578/580083) – Daniel Langr Aug 12 '21 at 10:19
  • Unrelated: **Never** do `using namespace std;` in the global namespace in a header file. This will taint the namespace in every translation unit that includes that header - and most people do not want that. Just add `std::` infront of the standard types you are using. – Ted Lyngmo Aug 12 '21 at 10:49
  • 1
    when you always look up the account via its account number you can consider to store the accounts in a `std::unordered_map`. However, currently your `account` is a single account and a vector of accounts. This is confusing and will lead to more confusion later. You should rather have a `Account` for a single account and `ManyAccounts` (choose a better name) for the container of accounts – 463035818_is_not_an_ai Aug 12 '21 at 10:52
  • 1
    Ted Lyngmo: thanks for the info i used std:: instead as you suggested – James Aug 12 '21 at 11:45
  • 463035818_is_not_a_number: Thank you for your help. am not familiar with std::unordered_map but will do my homework and use it. could you please elaborate more on: "You should rather have a Account for a single account and ManyAccounts (choose a better name) for the container of accounts" ? did not get it. many thanks!. – James Aug 12 '21 at 11:48

0 Answers0