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

OOP 단계별 프로젝트 7

꾸준함. 2017. 6. 8. 15:40

비록 조금 늦었지만, 지금이라도 프로그램을 여러 개의 파일에 나눠서 관리를 하자.

예를 들어서 Simple이라는 이름의 클래스 있다면, Simple.h와 Simple.cpp 파일을 만들자.

그리고 Simple.h에는 클래스의 선언을, Simple.cpp는 클래스의 정의(멤버함수의 정의)를 담자.

그리하여 다음의 구조로 파일을 분할하자.

Account.h, Account.cpp //Account 클래스의 선언과 정의

NormalAccount.h //NormalAccount 클래스의 선언과 정의

HighCreditAccount.h //HighCreditAccount 클래스의 선언과 정의

AccountHandler.h, AccountHandler.cpp //AccountHandler 클래스의 선언과 정의

BankingCommonDecl.h //공통헤더 및 상수선언들

BankingSystemMain.cpp //main 함수의 정의


여기서 NormalAccount 클래스와 HighCreditAccount 클래스는 크기가 작은 관계로 멤버함수의 정의를 별도의 파일에 분리하지 않고 헤더파일에 모두 삽입한다


[Account.h]

#ifndef __ACCOUNT_H__

#define __ACCOUNT_H__

 

//Entity 클래스

class BankAccount

{

private:

        int AccountNumber; //계좌 번호

        char *Name; //고객 이름

        int Money; //고객의 잔액

public:

        BankAccount(int num, char *name, int money);//생성자

        BankAccount(const BankAccount &copy);//추가된 복사생성자(나름 간단), const 추가

        int GetNumber() const;//const 추가

        virtual void Deposit(int money);

        int Withdraw(int money);

        void ShowAccountInfo() const; //const 추가

        ~BankAccount(); //소멸자

};

 

#endif


[Account.cpp]

#include "BankingCommonDecl.h"

#include "Account.h"

 

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

{

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

        strcpy(Name, name);

}

 

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

{

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

        strcpy(Name, copy.Name);

}

 

int BankAccount::GetNumber() const //const 추가

{

        return AccountNumber;

}

 

void BankAccount::Deposit(int money)

{

        //printf("%d원이 입금되었습니다\n", money); //제대로 동작하나 확인

        Money += money;

}

 

int BankAccount::Withdraw(int money)

{

        if (Money < money)

               return 0;

 

        Money -= money;

        return money;

}

 

void BankAccount::ShowAccountInfo() const //const 추가

{

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

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

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

}

 

BankAccount::~BankAccount() //소멸자

{

        delete[]Name;

}


[NormalAccount.h]

#ifndef __NORMAL_ACCOUNT_H__

#define __NORMAL_ACCOUNT_H__

 

#include "Account.h"

 

//노말

class NormalAccount :public BankAccount

{

private:

        int ratio;

public:

        NormalAccount(int num, char *name, int money, int profits) :BankAccount(num, name, money), ratio(profits)

        {

        }

        virtual void Deposit(int money)

        {

               BankAccount::Deposit(money); //원금

               BankAccount::Deposit(money*(ratio / 100.0)); //이자

        }

};

 

#endif


[HighCreditAccount.h]

#ifndef __HIGHCREDIT_ACCOUNT_H__

#define __HIGHCREDIT_ACCOUNT_H__

 

#include "NormalAccount.h"

 

//신용

class HighCreditAccount :public NormalAccount

{

private:

        int credit;

public:

        HighCreditAccount(int num, char *name, int money, int profits, int credibility) :NormalAccount(num, name, money, profits), credit(credibility)

        {

        }

        virtual void Deposit(int money)

        {

               NormalAccount::Deposit(money); //원금 이자

               BankAccount::Deposit(money*(credit / 100.0)); //추가이자

        }

};

 

#endif


[AccountHandler.h]

#ifndef __ACCOUNT_HANDLER_H__

#define __ACCOUNT_HANDLER_H__

 

#include "Account.h"

 

//컨트롤 클래스

class AccountHandler

{

private:

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

        int AccountNum; //계좌 개수

public:

        AccountHandler();

        ~AccountHandler();

        int Menu(void); //메뉴

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

        void Deposit(int money); //입금

        void Withdraw(int money); //출금

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

private: // 부분은 책을 참고, protected 외부에서 보면 private, 상속관계에서 보면 public!

        void MakeNormalAccount(void);

        void MakeCreditAccount(void);

};

 

#endif


[AccountHandler.cpp]

#include "BankingCommonDecl.h"

#include "AccountHandler.h"

#include "Account.h"

#include "NormalAccount.h"

#include "HighCreditAccount.h"

 

AccountHandler::AccountHandler() :AccountNum(0)

{

}

 

AccountHandler::~AccountHandler()

{

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

               delete Arr[i];

}

 

int AccountHandler::Menu(void)

{

        int sel;

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

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

        cout << "선택";

        cin >> sel;

        return sel;

}

 

void AccountHandler::MakeBankAccount()

{

        int sel;

        cout << "계좌 종류를 선택하시오" << endl;

        cout << "1.보통계좌\t2.신용계좌:";

        cin >> sel;

 

        if (sel == 1)

               MakeNormalAccount();

        else if (sel == 2)

               MakeCreditAccount();

        else

        {

               while (sel == 1 || sel == 2)

               {

                       cout << "번호를 잘못 선택하셨습니다 다시 선택하세요";

                       cin >> sel;

                       if (sel == 1)

                              MakeNormalAccount();

                       else if (sel == 2)

                              MakeCreditAccount();

               }

        }

}

 

void AccountHandler::MakeNormalAccount(void)

{

        int accountNumber;

        char name[30];

        int money;

        int ratio;

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

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

        cin >> accountNumber;

        cout << "이름 입력:";

        cin >> name;

        cout << "잔액 입력:";

        cin >> money;

        cout << "이율 입력:";

        cin >> ratio;

        cout << endl;

 

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

}

 

void AccountHandler::MakeCreditAccount(void)

{

        int accountNumber;

        char name[30];

        int money;

        int ratio;

        int rank;

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

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

        cin >> accountNumber;

        cout << "이름 입력:";

        cin >> name;

        cout << "잔액 입력:";

        cin >> money;

        cout << "이율 입력:";

        cin >> ratio;

        cout << "신용등급(1.RankA, 2.RankB, 3.RankC) 입력:";

        cin >> rank;

        cout << endl;

 

        switch (rank)

        {

        case 1:

               Arr[AccountNum++] = new HighCreditAccount(accountNumber, name, money, ratio, RankA);

               break;

        case 2:

               Arr[AccountNum++] = new HighCreditAccount(accountNumber, name, money, ratio, RankB);

               break;

        case 3:

               Arr[AccountNum++] = new HighCreditAccount(accountNumber, name, money, ratio, RankC);

               break;

        }

}

 

void AccountHandler::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 AccountHandler::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 AccountHandler::ShowAccountInfo(void)

{

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

        {

               Arr[i]->ShowAccountInfo();

               cout << endl;

        }

}


[BankingCommonDecl.h]

#ifndef __BANKING_COMMON_DECL_H__

#define __BANKING_COMMON_DECL_H__

 

#include <iostream>

#include <cstring>

using namespace std;

 

enum//신용등급

{

        RankA = 7,

        RankB = 4,

        RankC = 2

};

 

#endif


[BankingSystemMain.cpp]

#include "BankingCommonDecl.h"

#include "AccountHandler.h"

 

int main(void)

{

        AccountHandler handler;

        int money;

        while (1)

        {

               int sel = handler.Menu();

               switch (sel)

               {

                case 1:

                       handler.MakeBankAccount();

                       break;

               case 2:

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

                       cin >> money;

                       handler.Deposit(money);

                       break;

               case 3:

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

                       cin >> money;

                       handler.Withdraw(money);

                       break;

               case 4:

                       handler.ShowAccountInfo();

                       break;

               case 5:

                       return 0;

               default:

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

               }

        }

        return 0;

}



개발 환경:Visual Studio 2017


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


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



반응형