티스토리 툴바


열혈강의 C++ OOP프로젝트 7단계입니다.

7단계에서는 6단계까지의 단일 File 관리에서 각 Class 파일과 Head File 로 구분하여 관리하도록 함. 물론 main.cpp또 따로 관리 한다.

. class .. type redefinition error를 막기위해 각 Header File마다 아래의 구조를 따르도록 했다.

#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
...
Account Class Header 내용
...
#endif

이글은 읽는 Developer 께서는 혹시 더 좋은 안이나 Bug 발견시,
조언 부탁 드립니다.
감사합니다.

Posted by KiwiJay
열혈강의 C++ OOP프로젝트 6단계입니다.

6단계에서는 Account를 상속하는 두개의 Class 를 추가합니다.
 - 기부계좌(DonationClass) : 입금액의 1%를 무조건 기부
 - 신용계좌(CreditClass) : 입급액의 1%를 무조건 이자 지급

구현상의 문제점 및 해결안
. 기부 총액 Display 문제 발생함. 개념적으로 arrAccount 배열에 저장된
  각각의 개체가 어떤 Class 인지를 알 수 있는 방법은 없는지?
. 상기의 문제를 해결 못해,
  Account Class 에  virtual double getDonation() const; 를 선언하여
  문제 해결...

이글은 읽는 Developer 께서는 혹시 더 좋은 안이 있으시면, 조언 부탁 드립니다.

6단계 부터는 Source code를 첨부로 할 계획입니다.
Posted by KiwiJay

열혈강의 C++ OOP프로젝트 5단계입니다.
OOP프로젝트 4단계에서 커다란 변화가 있습니다.
전역변수로 사용되었던 index, arrAccount[]를 하나의 class로 묶어서 control class를 만들었습니다. 이제 좀 객체지향 code가 되었네요!!!


아래 Source 참조 하세요.

/**********************************************************/
/* NAME   : OOP Project                                   */
/* DATE   : 03/04/2008                                    */
/* AUTHOR : JAY                                           */
/**********************************************************/
/* VERSION : Project 3.0 - 12/04/2008 ~ 13/04/2008        */
/**********************************************************/

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Account
{
private:
 int accountId;
 char *customerName;
 int balance;
public:
 Account(){};
 Account(int _accountId, char *_customerName, int _balance);
 Account(const Account& a);
 ~Account();
 void addBalance(int _money);
 void minusBalance(int _money);
 int getAccountId() const;
 const char* getCustomerName() const;
 int getBalance() const;

};

Account::Account(int _accountId, char *_customerName, int _balance)
{
 customerName = new char[strlen(_customerName) + 1];
 strcpy(customerName, _customerName);
 accountId = _accountId;
 balance = _balance;
}

Account::Account(const Account& a)
{
 customerName = new char[strlen(a.customerName) + 1];
 accountId = a.accountId;
 balance = a.balance;
}

Account::~Account()
{
 if(customerName != NULL)
 {
  delete []customerName;
  customerName = NULL;
 }
}

void Account::addBalance(int _money)
{
 balance += _money;
}

void Account::minusBalance(int _money)
{
 balance -= _money;
}


int Account::getAccountId() const
{
 return accountId;
}

const char* Account::getCustomerName() const
{
 return customerName;
}

int Account::getBalance() const
{
 return balance;
}

class AccManager {
private:
 Account *arrAccount[100];
 int index;
public:
 AccManager():index(0){};
 ~AccManager(){};
 void menuDisplay() const;
 void openAccount();
 void deposit();
 void withdraw();
 void checkAllCustomerBalance() const;
};

void AccManager::menuDisplay() const
{
 cout << "+---------------------------------+" << endl;
 cout << "|      MANAGE BANK ACCOUNT        |" << endl;
 cout << "+---------------------------------+" << endl;
 cout << "| 1. OPEN ACCOUNT                 |" << endl;
 cout << "| 2. DEPOSIT                      |" << endl;
 cout << "| 3. WITHDRAW                     |" << endl;
 cout << "| 4. CHECK ALL CUSTOMER'S BALANCE |" << endl;
 cout << "| 5. QUIT                         |" << endl;
 cout << "+---------------------------------+" << endl;
 cout << "Select number ? ";
}

void AccManager::openAccount()
{
 int id;
 char name[30];
 int deposit;

 cout << endl << "WELCOME TO MY BANK !!" << endl;
 cout << "< OPEN ACCOUNT >" << endl;

 cout << "MAY I HAVE YOUR CREATE ACCOUNT NUMBER ? (4 digit, eg.1111) ";
 cin >> id;
 cout << "MAY I HAVE YOUR NAME ? ";
 cin >> name;
 cout << "HOW MUCH DO YOU WANT TO DEPOSIT ? ";
 cin >> deposit;

 arrAccount[index] = new Account(id, name, deposit);

 cout << endl;
 cout << ". YOUR ACCOUNT : " << arrAccount[index]->getAccountId() << endl;
 cout << ". YOUR NAME    : " << arrAccount[index]->getCustomerName() << endl;
 cout << ". YOUR BALANCE : " << arrAccount[index]->getBalance() << endl << endl;

 index++;
 
}

void AccManager::deposit()
{
 int n;
 int flag = 0;
 int number;
 int depositMoney;

 cout << endl << "WELCOME TO MY BANK!!" << endl;
 cout << "< DEPOSIT >" << endl;

 cout << "WHAT IS YOUR ACCOUNT NUMBER ? ";
 cin >> number;
 for(int i = 0; i < index ; i++)
 {
  if(arrAccount[i]->getAccountId() == number)
  {
   cout << ". YOUR ACCOUNT : " << arrAccount[i]->getAccountId() << endl;
   cout << ". YOUR NAME    : " << arrAccount[i]->getCustomerName() << endl;
   cout << "IS TAHT RIGHT (Yes-1 / No-2) ? ";
   cin >> n;
   if(n==1)
    flag = 1;
  }

  if(flag == 1)
  {
   cout << "HOW MUCH DO YOU WANT TO DEPOSIT ? ";
   cin >> depositMoney;
 
   arrAccount[i]->addBalance(depositMoney);

   cout << ". YOUR ACCOUNT : " << arrAccount[i]->getAccountId() << endl;
   cout << ". YOUR NAME    : " << arrAccount[i]->getCustomerName() << endl;
   cout << ". YOUR BALANCE : " << arrAccount[i]->getBalance() << endl;
   return;
  }
 }
 cout << "YOUR ACCOUNT NUMBER IS WRONG !!" << endl;
}

void AccManager::withdraw()
{
 int n;
 int flag = 0;
 int number;
 int withdrawMoney;

 cout << endl << "WELCOME TO MY BANK!!" << endl;
 cout << "< WITHDRAW >" << endl;

 cout << "WHAT IS YOUR ACCOUNT NUMBER ? ";
 cin >> number;
 for(int i = 0; i < index ; i++)
 {
  if(arrAccount[i]->getAccountId() == number)
  {
   cout << ". YOUR ACCOUNT : " << arrAccount[i]->getAccountId() << endl;
   cout << ". YOUR NAME    : " << arrAccount[i]->getCustomerName() << endl;
   cout << "IS TAHT RIGHT (Yes-1 / No-2) ? ";
   cin >> n;
   if(n==1)
    flag = 1;
  }

  if(flag == 1)
  {
   cout << "HOW MUCH DO YOU WANT TO WITHDRAW ? ";
   cin >> withdrawMoney;
 
   arrAccount[i]->minusBalance(withdrawMoney);

   cout << ". YOUR ACCOUNT : " << arrAccount[i]->getAccountId() << endl;
   cout << ". YOUR NAME    : " << arrAccount[i]->getCustomerName() << endl;
   cout << ". YOUR BALANCE : " << arrAccount[i]->getBalance() << endl;
   return;
  }
 }
 cout << "YOUR ACCOUNT NUMBER IS WRONG !!" << endl;
}

void AccManager::checkAllCustomerBalance() const
{
 cout << endl << "WELCOME TO MY BANK !!" << endl;
 cout << "< CHECK ALL CUSTOMER'S BALANCE >" << endl;
 for(int i = 0; i < index; i++)
 {
  cout << ". ACCOUNT  : " << arrAccount[i]->getAccountId()<< endl;
  cout << ". CUSTOMER : " << arrAccount[i]->getCustomerName() << endl;
  cout << ". BALANCE  : " << arrAccount[i]->getBalance() << endl;
  cout << "------------------------------" << endl;
 }
}

int main()
{
 AccManager accmgr;
 int choice;

 while(1)
 {

  accmgr.menuDisplay();
  cin >> choice;

  switch(choice)
  {
  case 1:
   accmgr.openAccount();
   break;
  case 2:
   accmgr.deposit();
   break;
  case 3:
   accmgr.withdraw();
   break;
  case 4:
   accmgr.checkAllCustomerBalance();
   break;
  case 5:
   return 0;
  default:
   cout << "Your selected number is wrong!!" << endl;
   break;
  }
 }

 return 0;
}

Posted by KiwiJay