0

Could you tell me what problem this code has?
This is a simple ATM code. (Menu: openAccount, deposit, withdraw, displayAccount <-display All Account).
There is no noticed error(red line). Code was running.
But I can't store information on Object.. (It maybe my fault thought)

class Account{

private:
    string accNum;
    string pin;
    int balance;
    string name;    
public:
    Account();
    Account(string i_accNum, string i_pin, string i_name,int i_balance){
    accNum=i_accNum;
    pin=i_pin;
    balance=i_balance;
    name=i_name; }

    string getAccNum(){return accNum; }
    string getPin(){return pin;}
    int getBalance(){return balance;}
    string getName(){return name;}
    virtual void deposit(int money);

    void withdraw(int money)
    {
        if(money>balance)
        {
            cout<<"cannot withdraw"<<endl;
            cout<<"Balance is"<<balance<<endl;
        }
        else if(money<=0)
        {
            cout<<"wrong fiture"<<endl;
            return ;
        }
        else
        {
        balance-=money;
        cout<<"Balance is"<<balance<<endl;
        }
    }

};

class AccountList
{
private:
    Account acc_list[MAX];
    int count;

public:
    AccountList(){count=0;}
    Account getAccount(int index){return acc_list[count];}
    int getCount(){return count;}

    void withdraw(int,int);
    void deposit(int,int);
    int findAccount(string);
    int checkPin(string);
    void insertAccount(Account a);
};

class taskManager
{
public:
    void openAccount(AccountList& a_list);
    int deposit(AccountList& al); 
    int withdraw(AccountList& al); 
    void displayAccount(AccountList al);
};



class ioHandler
{
public:
    void putAccount(Account);
    Account getAccount();
    int getMenu();
};

In openAccount(from class taskManager), I use these methods: insertAccount, findAccount(from AccountList)

void AccountList::insertAccount(Account a)
{
    if (count > MAX) {
        cout << "you can't open new account";
        return;
    }
    Account acc_list[count](a);  
    /* error is "Use constant instead invariable count"
    before modification=> Account acc_list[count]=a; */
    count++;
}




int AccountList::findAccount(string acc)
{
    for (int i=0; i<count; i++) {
        if (acc_list[i].getAccNum() == acc) {
            return i;
        }
    }
    return NOT_FOUND;
}


void taskManager::openAccount(AccountList& a_list)
{
    ioHandler ioh;
    Account a  = ioh.getAccount();

    int index = a_list.findAccount(a.getAccNum());
    if (index != NOT_FOUND) {
        cout<<"Account Overlap"<<endl;
        return;
    }

     a_list.insertAccount(a);  

}




Account ioHandler::getAccount()
{
    string accNum;
    string pin;
    string name;
    int balance;

    cout<<"Account->";
    cin>>accNum;
    cout<<"Pin->";
    cin>>pin;
    cout<<"Name->";
    cin>>name;
    cout<<"Balance->";
    cin>>balance;

    Account a(accNum,pin,name, balance);
    return a;
}

Account acc_listcount; <= It was wrong; I think i will use 'Copy Constructor'.

Ah.. Why my code cannot store.. X(

Edited by Jimin: sorry

2
Contributors
1
Reply
24
Views
2 Years
Discussion Span
Last Post by tinstaafl
0

One thing I noticed:
ioHandler::getAccount doesn't validate the user input.

Also since acc_list is an array of type Account you don't need to declare an element to assign a value to it:
Instead of:

Account acc_list[count](a);

Try:

acc_list[count] = a;

In:

void AccountList::insertAccount(Account a)
{
    if (count > MAX) {
        cout << "you can't open new account";
        return;
    }
    acc_list[count] = a;
    count++;
}

It looks to me that MAX is the maximum number of accounts, and count is the index of the next account. Therefore the condition checking if an account can be added should be if (count > MAX - 1):

void AccountList::insertAccount(Account a)
{
    if (count > MAX - 1) {
        cout << "you can't open new account";
        return;
    }
    acc_list[count] = a;
    count++;
}

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.