열혈강의 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;
}