Несколько классов с отношением has-a - PullRequest
1 голос
/ 28 сентября 2019

Я работаю над классом для управления банковскими реквизитами.У меня есть основной класс «Account», а затем класс «AccountList» (для хранения учетных записей).У меня есть другой класс «Снятие», который я буду использовать для снятия денег со счета.В основном.Я создал "a" объект класса AccountList и объект "w" класса изъятия.Я ожидаю, что закомментированная строка (см. Main) должна вернуть мне номер счета 1-го аккаунта, но на самом деле он возвращает мне ноль.Всем, кому можно объяснить причину и способы ее устранения. Заранее спасибо.: - (

#include<string>
#include<cstdbool>
#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setw;
int i;
const int size=100;
class Account{
public:
    static int count;
    Account() { accNo = 0 ; balance = 0;}
    int returnAcc(){return accNo;}
    int returnBalance(){return balance;}
    setAccNo(int x) { accNo = x ;}
    updateBal(int x) { balance += x; }
    updateBal1(int x) { balance -= x; }
private:
    long long int accNo;
    long int balance;
};
int Account::count = 0;
class AccountList{
    public:
    Account acc[10];
    create_account();  //create during initial
    bool pre_notice();
    search();
    bool search1(int );
    void show_data();
    void addAccount();
};
bool AccountList::search1(int x)
{
    for(i=0;i<Account::count;i++)
    {
        if(acc[i].returnAcc()==x)
        {
            return true;
        }
    }
    return false;
}
void AccountList::addAccount()
{
    long int temp;
    cout<<"Enter account number - ";
    cin>>temp;
    if(search1(temp))
    {
        acc[Account::count-1].setAccNo(temp);
        acc[Account::count-1].updateBal(500);
    }
    else{
        cout<<"Account already exist.\n";
    }

}
bool AccountList::pre_notice()
{
   int choice;
   cout<<"You need to deposit $500 to make account.\n";
   cout<<"You must maintain minimum of $500 in account.\n";
   cout<<"Enter 1 if you want to proceed.\n" ;
   cin>>choice;
   if(choice) {return true;}
   return  false;
}
void AccountList::show_data()
{
    for(i=0;i<Account::count;i++)
    {
        cout<<"Account number - "<<acc[i].returnAcc()<<endl;
        cout<<"Balance - "<<acc[i].returnBalance()<<endl;
    }
    if(Account::count == 0)
    {
        cout<<"No account exists.\n";
    }
}
AccountList::create_account()
{
    int accno,bala=5000;
    for(i=0;i<2;i++)
    {
        cout<<"Costumer "<<(i+1)<<endl;
        if(pre_notice())
        {
            cout<<"Enter account number.\n";
            cin>>accno;
            acc[i].setAccNo(accno);
            acc[i].updateBal(bala);
            Account::count++;
            cout<<"Account created.\n";
        }
        else{
            cout<<"Thanks for visiting . Come back again.\n";
        }
    }

}
AccountList::search()
{
    int x;
    cout<<"Enter Account number - ";
    cin>>x;
    for(i=0;i<Account::count;i++)
    {
        if(acc[i].returnAcc()==x)
        {
            cout<<acc[i].returnAcc();
            cout<<"Account exist.\n";
            cout<<"Balance - "<<acc[i].returnBalance()<<endl;
        }
    }
}
class Withdraw{
public:
    AccountList c;
    withdraw_money( );
    update();
    void deposit(int );
};
Withdraw::withdraw_money()
{
    int x,temp,flag =1;
    cout<<c.acc[0].returnAcc()<<endl<<endl<<endl;
    cout<<"Enter account number - ";
    cin>>temp;
    cout<<Account::count;
    for(i=0;i<Account::count;i++)
    {
        if(c.acc[i].returnAcc()==temp)
        {
            cout<<"Enter amount to withdraw - ";
            cin>>x;
            if(x<c.acc[i].returnBalance()){
                 c.acc[i].updateBal1(x);
                if(c.acc[i].returnBalance()<500)
                {
                    c.acc[i].updateBal(x);
                    cout<<"\nInsufficient money.\n";
                }flag=0;
                cout<<"\nAmount withdrawn.\n";}
            else{
                cout<<"\nInsufficient money.\n";
            }
        }
    }
    if(flag = 1)
    {
        cout<<"Account doesn't exist.\n";
    }
}
int main()
{
    AccountList a;
    Withdraw w;
    a.create_account();
    cout<<w.c.acc[0].returnAcc(); //Issue with this line of code. 
    return 0;

}

...