4 minute read

🔭프로그램 설명

  • 앞서 정의한 Account 클래스는 생성자에서 문자열을 동적 할당하기 때문에, 소멸자와 깊은 복사를 위한 복사 생성자, 대입연산자가 정의되어 있다. 그런데 이번에 적용할 String 클래스는 메모리 공안을 동적할당하고 깊은 복사를 진행하는 형태로 복사 생성자와 대입연산자가 정의되어 있기 때문에, 이를 이용하면 Account 클래스의 구현이 한결 간단해진다.

🖥️구현

  • Account.h

      #pragma once
      #include"String.h"
    
      class Account
      {
      public:
          Account(int id, String name, int balance);
          //Account(const Account& ref);
          //~Account();
    
      private:
          int m_Id;			// 계좌번호
          String m_SName;		//char* m_cpName;		// 이름
          int m_Balance;		// 잔고
    
      public:
          //Account& operator=(const Account& ref);
          int getID() const;
          virtual int Deposit(int monyey);
          int Withdraw(int money);
          void ShowAccInfo() const;
      };
    
    
    
  • Account.cpp

      #define _CRT_SECURE_NO_WARNINGS
    
      #include "Stage_7_Account.h"
      #include"Stage_7_BankDec.h"
    
    
      ///////////////// 계좌 /////////////////////////
    
      Account::Account(int id, String name, int balance) : m_Id(id), m_Balance(balance)
      {
          m_SName = 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;
      //}
    
      //Account& Account::operator=(const Account& ref)
      //{
      //	m_Id = ref.m_Id;
      //	m_Balance = ref.m_Balance;
      //
      //	delete[]m_cpName;		//대입연산자는 두 객체가 모두 초기화 된 상태이므로, 먼저 이름을 지워준다
      //	m_cpName = new char[strlen(ref.m_cpName)+1];
      //	strcpy(m_cpName, ref.m_cpName);
      //	return *this;
      //}
    
      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_SName << endl;
          cout << "잔    액 : " << m_Balance << endl;
      }
    
  • AccountType.h

      #pragma once
    
      #include"Stage_7_Account.h"
      #include"String.h"
    
      class NormalAccount : public Account {
      public:
          NormalAccount(int id, String name, int balance, int interestRate);
          ~NormalAccount();
    
      private:
          int m_iInterRate;
      public:
          int Deposit(int money);
    
      };
    
      class HighCreditAccount : public Account {
      public:
          HighCreditAccount(int id, String name, int balance, int interestRate, int creditlevel);
          ~HighCreditAccount();
      private:
          int m_iInterRate;
          int m_iCreditLevel;
      public:
          int Deposit(int money);
    
      };
    
      //////////// 보통예금계좌 //////////////////
      NormalAccount::NormalAccount(int id, String name, int balance, int interestRate)
          : Account(id, name, balance), m_iInterRate(interestRate)
      {
    
      }
      NormalAccount::~NormalAccount() {
    
      }
    
      int NormalAccount::Deposit(int money) {
          money = (int)((1 + 0.01 * m_iInterRate) * money);		//이자와 원금을 합친 돈으로, 최종적으로 입금될 금액
          return Account::Deposit(money);
      }
    
      //////////// 신용신뢰계좌 //////////////////
      HighCreditAccount::HighCreditAccount(int id, String 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 = (int)((1 + 0.01 * inter) * money);
          return Account::Deposit(money);
      }
    
  • BankSystem.cpp

      #define _CRT_SECURE_NO_WARNINGS
    
      #include"Stage_7_BankSystem.h"
      #include"Stage_7_Account.h"
      #include"Stage_7_AccountType.h"
      #include"Stage_7_BankDec.h"
      #include"String.h"
    
      //////////////// 은행 //////////////////////
    
      BankSystem::BankSystem() : m_NumOfAcc(0)
      {
      }
    
      BankSystem::~BankSystem()
      {
          for (int i = 0; i < m_NumOfAcc; i++) {
              delete m_pAccs[i];
          }
      }
    
      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;
          String name;
          int money;
          int interestRate;
    
          cout << "[보통예금계좌 개설]" << endl;
          cout << "계좌번호 : "; cin >> id;
          cout << "이    름 : "; cin >> name;
          cout << "잔    액 : "; cin >> money;
          cout << "이    자 : "; cin >> interestRate;
          cout << endl;
    
          m_pAccs[m_NumOfAcc++] = new NormalAccount(id, name, money, interestRate);
      }
    
      void BankSystem::MakeHighCreditAcc() {
          int id;
          String name;
          int money;
          int interestRate;
          int level;
    
          cout << "[신용신뢰계좌 개설]" << endl;
          cout << "계좌번호 : "; cin >> id;
          cout << "이    름 : "; cin >> name;
          cout << "잔    액 : "; cin >> money;
          cout << "이    자 : "; cin >> interestRate;
          cout << "신용등급(1.A, 2.B, 3.C) : "; cin >> level;
          cout << endl;
    
          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;
    
      }
    

Tags: ,

Categories:

Updated: