5 minute read

🔭프로그램 설명

  • 단계별 프로젝트를 더 이상 진행하기에 앞서 하나의 파일을 여러 개로 나눠서 내용을 정리할 필요가 있다. 하나의 파일 안에 모든 것이 다 들어있어서 내용파악이 쉽지 않기 때문이다. 그래서 이번 단계에서는 파일을 분할하고자 한다.

  • 각각의 클래스마다 선언은 .h파일에, 정의는 .cpp 파일에 저장하는 것이 좋다. 이렇게 되면 프로그램을 관리하기도 좋고, 또클래스의 구성이 한눈에 들어오기 때문에 프로그램의 내용을 파악하기도 수월해진다. 짧은 프로그램이라 할지라도 하나의 파일에 필요한 모든 요소를 담는 것은 바람직하지 못하다.

  • 비록 조금 늦었지만, 지금이라도 프로그램을 여러 개의 파일에 나눠서 관리를 하자. 예를 들어서 Simple이라는 이름의 클래스가 있다면, Simple.h와 Simple.cpp 파일을 만들자, 그리고 Simple.h에는 클래스의 선언을, Simple.cpp는 클래스의 정의(멤버함수의 정의)를 담자. 그리하여 다음의 구조로 파일을 분할하자.

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

🖥️구현

  • 헤더

    • Account.h
      #pragma once
    
      class Account
      {
      public:
          Account(int id, char* name, int balance);
          Account(const Account& ref);
          ~Account();
    
      private:
          int m_Id;			// 계좌번호
          char* m_cpName;		// 이름
          int m_Balance;		// 잔고
    
      public:
          int getID() const;
          virtual int Deposit(int monyey);
          int Withdraw(int money);
          void ShowAccInfo() const;
      };
    
    • AccountType.h
      #pragma once
      #include"Stage_7_Account.h"
    
      class NormalAccount : public Account {
      public:
          NormalAccount(int id, char* name, int balance, int interestRate);
          ~NormalAccount();
    
      private:
          int m_iInterRate;
      public:
          int Deposit(int money);
    
      };
    
      class HighCreditAccount : public Account {
      public:
          HighCreditAccount(int id, char* name, int balance, int interestRate, int creditlevel);
          ~HighCreditAccount();
      private:
          int m_iInterRate;
          int m_iCreditLevel;
      public:
          int Deposit(int money);
    
      };
    
      //////////// 보통예금계좌 //////////////////
      NormalAccount::NormalAccount(int id, char* name, int balance, int interestRate)
          : Account(id, name, balance), m_iInterRate(interestRate)
      {
    
      }
      NormalAccount::~NormalAccount() {
    
      }
    
      int NormalAccount::Deposit(int money) {
          money = (1 + 0.01 * m_iInterRate) * money;		//이자와 원금을 합친 돈으로, 최종적으로 입금될 금액
          return Account::Deposit(money);
      }
    
      //////////// 신용신뢰계좌 //////////////////
      HighCreditAccount::HighCreditAccount(int id, char* name, int balance, int interestRate, int creditlevel)
          : Account(id, name, balance), m_iInterRate(interestRate), m_iCreditLevel(creditlevel)
      {
    
      }
    
      HighCreditAccount::~HighCreditAccount() {
    
      }
      int HighCreditAccount::Deposit(int money) {
          int inter = m_iInterRate + m_iCreditLevel;
          money = (1 + 0.01 * inter) * money;
          return Account::Deposit(money);
      }
    
    • BankDec.h
      #pragma once
    
      enum { DEPOSIT = 1, WITHDRAW, CREATE, INQUIRY, EXIT };
      enum { LEVEL_A = 7, LEVEL_B = 4, LEVEL_C = 2 };
      enum { NormalAcc = 1, HighCreditAcc };
    
      const int NAME_LEN = 10;
    
    • BankSystem.h
      #pragma once
      #include"Stage_7_Account.h"
    
      class BankSystem
      {
      public:
          BankSystem();
          ~BankSystem();
    
      private:
          int m_NumOfAcc;				// 계좌 개수
          Account* m_pAccs[50];		// 계좌 배열
    
      public:
          void AnnounceSys() const;	// 시스템 안내
          void CreateAccount();		// 계좌 개설
          void DepositMoney();		// 입금
          void WithdrawMoney();		// 출금
          void InquiryAcc() const;	// 잔액조회
    
      protected:
          void MakeNormalAcc();
          void MakeHighCreditAcc();
      };
    
  • cpp 소스파일

    • Account.cpp
      #define _CRT_SECURE_NO_WARNINGS
      #include<iostream>
      #include<cstring>
      #include "Stage_7_Account.h"
    
      using namespace std;
    
      ///////////////// 계좌 /////////////////////////
    
      Account::Account(int id, char* name, int balance) : m_Id(id), m_Balance(balance)
      {
          m_cpName = new char[strlen(name) + 1];
          strcpy(m_cpName, name);
      }
    
      Account::Account(const Account& ref)
          : m_Id(ref.m_Id), m_Balance(ref.m_Balance) {
          m_cpName = new char[strlen(ref.m_cpName) + 1];
          strcpy(m_cpName, ref.m_cpName);
      }
    
      Account::~Account()
      {
          delete[]m_cpName;
      }
    
      int Account::getID() const {
          return m_Id;
      }
    
      int Account::Deposit(int money) {
          m_Balance += money;
          return m_Balance;			// 잔액 반환
      }
    
      int Account::Withdraw(int money) {
          if (m_Balance - money < 0) {
              return -1;
          }
    
          m_Balance -= money;
          return m_Balance;
      }
    
      void Account::ShowAccInfo() const {
          cout << "계좌번호 : " << m_Id << endl;
          cout << "이    름 : " << m_cpName << endl;
          cout << "잔    액 : " << m_Balance << endl;
      }
    
    
    • BankSystem.cpp
      #define _CRT_SECURE_NO_WARNINGS
      #include<iostream>
      #include<cstring>
    
      #include "Stage_7_BankSystem.h"
      #include"Stage_7_AccountType.h"
      #include"Stage_7_BankDec.h"
    
    
      using namespace std;
    
      //////////////// 은행 //////////////////////
    
      BankSystem::BankSystem() : m_NumOfAcc(0)
      {
      }
    
      BankSystem::~BankSystem()
      {
          delete m_pAccs;
      }
    
      void BankSystem::AnnounceSys() const {
    
          cout << "----- Menu -----" << endl;
          cout << "1. 입    금" << endl;
          cout << "2. 출    금" << endl;
          cout << "3. 개    설" << endl;
          cout << "4. 계좌정보" << endl;
          cout << "5. 종    료" << endl;
    
      }
    
      void BankSystem::CreateAccount() {
          int acc;
    
          cout << "[계좌선택]" << endl;
          cout << "1. 보통예금계좌" << endl;
          cout << "2. 신용신뢰계좌" << endl;
          cout << "선택 : "; cin >> acc;
    
          if (acc == NormalAcc) {
              MakeNormalAcc();
          }
          else
              MakeHighCreditAcc();
    
      }
    
      void BankSystem::MakeNormalAcc() {
          int id;
          char name[NAME_LEN];
          int money;
          int interestRate;
    
          cout << "계좌번호 : "; cin >> id;
          cout << "이    름 : "; cin >> name;
          cout << "잔    액 : "; cin >> money;
          cout << "이    자 : "; cin >> interestRate;
    
          m_pAccs[m_NumOfAcc++] = new NormalAccount(id, name, money, interestRate);
      }
    
      void BankSystem::MakeHighCreditAcc() {
          int id;
          char name[NAME_LEN];
          int money;
          int interestRate;
          int level;
    
          cout << "계좌번호 : "; cin >> id;
          cout << "이    름 : "; cin >> name;
          cout << "잔    액 : "; cin >> money;
          cout << "이    자 : "; cin >> interestRate;
          cout << "신용등급(1.A, 2.B, 3.C) : "; cin >> level;
    
          switch (level)
          {
          case 1:
              m_pAccs[m_NumOfAcc++] = new HighCreditAccount(id, name, money, interestRate, LEVEL_A);
              break;
    
          case 2:
              m_pAccs[m_NumOfAcc++] = new HighCreditAccount(id, name, money, interestRate, LEVEL_B);
              break;
    
          case 3:
              m_pAccs[m_NumOfAcc++] = new HighCreditAccount(id, name, money, interestRate, LEVEL_C);
              break;
    
          default:
              cout << "잘못된 신용등급입니다" << endl;
              break;
          }
    
    
      }
    
      void BankSystem::DepositMoney() {
    
          int id;
          int money;
    
          cout << "[입금]" << endl;
          cout << "계좌번호 : "; cin >> id;
          cout << "입금액 : "; cin >> money;
    
          for (int i = 0; i < m_NumOfAcc; i++) {
              if (m_pAccs[i]->getID() == id) {
                  int retMoney = m_pAccs[i]->Deposit(money);
    
                  cout << "잔액 : " << retMoney << endl << endl;
                  return;
              }
          }
    
          cout << "해당 계좌가 존재하지 않습니다" << endl << endl;
          return;
      }
    
      void BankSystem::WithdrawMoney() {
          int id;
          int money;
    
          cout << "[출금]" << endl;
          cout << "계좌번호 : "; cin >> id;
          cout << "출금액 : "; cin >> money;
    
          for (int i = 0; i < m_NumOfAcc; i++) {
              if (m_pAccs[i]->getID() == id) {
                  int retMoney = m_pAccs[i]->Withdraw(money);
    
                  if (retMoney < 0) {
                      cout << "잔액이 부족합니다" << endl << endl;
                      m_pAccs[i]->ShowAccInfo();
                      return;
                  }
    
                  cout << "잔액 : "  << retMoney << endl << endl;
                  return;
              }
          }
          cout << "해당 계좌가 존재하지 않습니다" << endl << endl;
          return;
      }
    
      void BankSystem::InquiryAcc() const {
          int id;
    
          cout << "[계좌조회]" << endl;
          cout << "계좌번호 : "; cin >> id;
          cout << endl << endl;
    
          for (int i = 0; i < m_NumOfAcc; i++) {
              if (m_pAccs[i]->getID() == id) {
                  cout << "--------------------" << endl;
                  m_pAccs[i]->ShowAccInfo();
                  return;
              }
          }
    
          cout << "해당 계좌가 존재하지 않습니다" << endl << endl;
    
      }
    
    • Main.cpp
      #define _CRT_SECURE_NO_WARNINGS
      #include<iostream>
      #include<cstring>
    
      #include "Stage_7_BankSystem.h"
      #include "Stage_7_BankDec.h"
    
      using namespace std;
    
      int main(void) {
          int option;
          BankSystem shinhan;
    
          while (true)
          {
              shinhan.AnnounceSys();
              cout << "선택 : ";
              cin >> option;
              cout << endl;
    
              switch (option)
              {
              case DEPOSIT:
                  shinhan.DepositMoney();
                  break;
    
              case WITHDRAW:
                  shinhan.WithdrawMoney();
                  break;
    
              case CREATE:
                  shinhan.CreateAccount();
                  break;
    
              case INQUIRY:
                  shinhan.InquiryAcc();
                  break;
    
              case EXIT:
                  return 0;
    
              default:
                  cout << "Illegal selection" << endl;
    
              }
          }
          return 0;
      }
    

🐦

  • 미리 하나의 헤더와 소스파일로 나눠서 진행해왔지만 이번 기회에 더 파일을 나누어 볼 수 있었다. 확실히 하나의 파일에 담겨있을 땐, 코드가 길어지니까 찾기도 힘들고 가독성도 떨어졌지만, 정리해놓아 더 명확하게 프로그램을 볼 수 있었던 것 같다. 마치 책장정리하는 것 같았다.

Tags: ,

Categories:

Updated: