/*
* Banking System Ver 0.4
* author : YEJIN LEE
* contents : const로 안정성 높이기
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
enum { DEPOSIT = 1, WITHDRAW, CREATE, INQUIRY, EXIT };
const int NAME_LEN = 10;
///////////////////// 계좌 /////////////////////////
class Account
{
public:
Account(int id, char* name, int balance);
~Account();
private:
int m_Id; // 계좌번호
char* m_cpName; // 이름
int m_Balance; // 잔고
public:
int getID() const;
int getBalance() const;
char* getName() const;
void changeBalance(int money);
};
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()
{
delete[]m_cpName;
}
int Account::getID() const {
return m_Id;
}
int Account::getBalance() const {
return m_Balance;
}
char* Account::getName() const {
return m_cpName;
}
void Account::changeBalance(int money) {
m_Balance = money;
}
//////////////////// 은행 //////////////////////
class BankSystem
{
public:
BankSystem();
~BankSystem();
private:
int m_NumOfAcc; // 계좌 개수
Account* m_pAccs[50]; // 계좌 배열
public:
void AnnounceSys(); // 시스템 안내
void CreateAccount(); // 계좌 개설
void DepositMoney(); // 입금
void WithdrawMoney(); // 출금
void InquiryAcc() const; // 잔액조회
};
BankSystem::BankSystem() : m_NumOfAcc(0)
{
}
BankSystem::~BankSystem()
{
delete[]m_pAccs;
}
void BankSystem::AnnounceSys() {
cout << "----- Menu -----" << endl;
cout << "1. 입 금" << endl;
cout << "2. 출 금" << endl;
cout << "3. 개 설" << endl;
cout << "4. 계좌정보" << endl;
cout << "5. 종 료" << endl;
}
void BankSystem::CreateAccount() {
int id;
char name[NAME_LEN];
int money;
cout << "계좌번호 : " << endl; cin >> id;
cout << "이 름 : " << endl; cin >> name;
cout << "잔 액 : " << endl; cin >> money;
m_pAccs[m_NumOfAcc++] = new Account(id, name, money);
}
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]->getBalance();
retMoney += money;
m_pAccs[i]->changeBalance(retMoney);
cout << "잔액 : " << m_pAccs[i]->getBalance() << endl << endl;
return;
}
}
cout << "해당 계좌가 존재하지 않습니다" << endl << endl;
return;
}
void BankSystem::WithdrawMoney() {
int id;
int money;
cout << "[출금]" << endl;
cout << "계좌 ID : "; cin >> id;
cout << "출금액 : "; cin >> money;
for (int i = 0; i < m_NumOfAcc; i++) {
if (m_pAccs[i]->getID() == id) {
int retMoney = m_pAccs[i]->getBalance();
retMoney -= money;
if (m_pAccs[i]->getBalance() < 0) {
cout << "잔액이 부족합니다" << endl << endl;
retMoney = m_pAccs[i]->getBalance();
retMoney += money;
m_pAccs[i]->changeBalance(retMoney);
return;
}
m_pAccs[i]->changeBalance(retMoney);
cout << "잔액 : " << m_pAccs[i]->getBalance() << 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;
cout << "계좌번호 : " << id << endl;
cout << "이름 : " << m_pAccs[i]->getName() << endl;
cout << "잔고 : " << m_pAccs[i]->getBalance() << endl << endl;
return;
}
}
cout << "해당 계좌가 존재하지 않습니다" << endl << endl;
}
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;
}