C++/열혈 C++ 프로그래밍(윤성우 저)

OOP 단계별 프로젝트 3

꾸준함. 2017. 5. 31. 15:56

/*

BankAccount 클래스에 깊은 복사를 진행하는 복사 생성자를 정의하면서, Banking System 버전을

0.2에서 0.3으로 업그레이드 시켜보겠다.

비록, 복사생성자가 호출되지는 않지만, 깊은 복사를 원칙으로 정하고 이를 위해서 복사 생성자를 추가하기로 하겠다.

복사생성자 추가 외에는 프로젝트 2와 똑같다.

*/

#include <iostream>

#include <cstring>

 

using namespace::std;

 

int Menu(void); //메뉴

void MakeBankAccount(void); //계좌 개설

void Deposit(int money); //입금

void Withdraw(int money); //출금

void ShowAccountInfo(void); //전체고객 잔액조회

 

class BankAccount

{

private:

        int AccountNumber; //계좌 번호

        char *Name; //고객 이름

        int Money; //고객의 잔액

public:

        BankAccount(int num, char *name, int money) :AccountNumber(num), Money(money) //생성자

        {

               Name = new char[strlen(name) + 1];

               strcpy(Name, name);

        }

        BankAccount(BankAccount &copy) :AccountNumber(copy.AccountNumber), Money(copy.Money) //이번에 추가된 복사생성자(나름 간단)

        {

               Name = new char[strlen(copy.Name) + 1];

               strcpy(Name, copy.Name);

        }

        int GetNumber()

        {

               return AccountNumber;

        }

        void Deposit(int money)

        {

               Money += money;

        }

        int Withdraw(int money)

        {

               if (Money < money)

                       return 0;

 

               Money -= money;

               return money;

        }

        void ShowAccountInfo()

        {

               cout << "이름:" << Name << endl;

               cout << "계좌번호:" << AccountNumber << endl;

               cout << "잔고:" << Money << endl;

        }

        ~BankAccount() //소멸자

        {

               delete[]Name;

        }

};

 

BankAccount *Arr[10]; //계좌 포인터 배열

int AccountNum = 0; //계좌 개수

 

int main(void)

{

        int money;

        while (1)

        {

               int sel = Menu();

               switch (sel)

               {

               case 1:

                       MakeBankAccount();

                       break;

               case 2:

                       cout << "입금할 금액:";

                       cin >> money;

                       Deposit(money);

                       break;

               case 3:

                       cout << "출금할 금액:";

                       cin >> money;

                       Withdraw(money);

                       break;

               case 4:

                       ShowAccountInfo();

                       break;

               case 5:

                       for (int i = 0; i < AccountNum; i++) //포인터배열을 동적할당했으므로

                              delete Arr[i];

                       return 0;

               default:

                       cout << "잘못된 번호를 입력하셨습니다" << endl;

               }

        }

        return 0;

}

 

int Menu(void)

{

        int sel;

        cout << "-----Menu-----" << endl;

        cout << "1.계좌개설" << endl << "2.입금" << endl << "3.출금" << endl << "4.계좌정보 전체 출력" << endl << "5.프로그램 종료" << endl;

        cout << "선택";

        cin >> sel;

        return sel;

}

 

void MakeBankAccount()

{

        int accountNumber;

        char name[30];

        int money;

        cout << AccountNum + 1 << "사람 정보 입력" << endl;

        cout << "계좌번호 입력:";

        cin >> accountNumber;

        cout << "이름 입력:";

        cin >> name;

        cout << "잔액 입력:";

        cin >> money;

        cout << endl;

 

        Arr[AccountNum++] = new BankAccount(accountNumber, name, money); //계좌개설 완료 다음 사람으로

}

 

void Deposit(int money)

{

        int number;

        cout << "고객님의 계좌번호를 입력해주세요:";

        cin >> number;

        for (int i = 0; i <= AccountNum; i++)

        {

               if (Arr[i]->GetNumber() == number) //계좌번호가 같으면

               {

                       Arr[i]->Deposit(money);

                       cout << "입금이 완료되었습니다" << endl;

                       return;

               }

        }

        cout << "없는 계좌번호입니다" << endl;

}

 

void Withdraw(int money)

{

        int number;

        cout << "고객님의 계좌번호를 입력해주세요:";

        cin >> number;

        for (int i = 0; i <= AccountNum; i++)

        {

               if (Arr[i]->GetNumber() == number) //계좌번호가 같으면

               {

                       if (Arr[i]->Withdraw(money) == 0)

                       {

                              cout << "잔액이 부족합니다" << endl;

                              return;

                       }

                       cout << "출금이 완료되었습니다" << endl;

                       return;

               }

        }

        cout << "없는 계좌번호입니다" << endl;

}

 

void ShowAccountInfo(void)

{

        for (int i = 0; i < AccountNum; i++)

        {

               Arr[i]->ShowAccountInfo();

               cout << endl;

        }

}



개발 환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 열혈 C++ 프로그래밍 윤성우 저

반응형