본문 바로가기

Programming/C++ 3

[C++ 프로젝트] 은행계좌 관리 프로그램 Part. 05

반응형

클래스의 분류= 컨트롤클래스와 Entity 클래스

 

클래스의 기능을 관리, 제어하는 컨트롤 클래스의 적용

 

컨트롤 클래스의 특징

  • 프로그램 전체의 기능을 담당한다.
  • 기능적 성격이 강한 클래스
  • 컨트롤 클래스만 봐도 프로그램의 전체 기능과 흐름을 파악할 수 있다.

 

Entity 클래스의 특징

  • 데이터적 성격이 강한 클래스
  • 파일 및 데이터 베이스에 저장되는 데이터를 소유한다.
  • 프로그램의 기능을 파악하는데는 관련이 없다
  • 프로그램상에서 관리되는 데이터의 종류를 파악하는데 목적

은행계좌 관리시스템의 주요기능은 전역함수를 통해 구현되어 있다.

 

C++에서는 전역함수와 전역변수의 선언을 허용하지만, 객체지향에는 전역이라는 개념은 존재하지 않는다.

 

때문에 컨트롤 클래스를 이용해 전역함수를 이용하지 않도록  하나의 컨트롤 클래스로 묶는다.

 

  • AccountHandler라는 컨트롤 클래스를 정의하고, 전역함수들을 이 클래스의 멤버함수에 포함
  • Account 객체의 저장을 위해 선언한 배열과 변수도 이 클래스의 멤버에 포함
  • AccountHandler 클래스를 기반으로 프로그램이 실행되도록 main 함수를 변경

 


#include <iostream>
#include <cstring>
#include <string>

using namespace std;
const int NAME_LEN = 20;

enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };

class Account
{
private:
	int accID;		//계좌번호
	int balance;	//잔 액
	char* cusName;	//고객이름

public:
	Account(int ID, int money, char* name);
	Account(const Account& ref);

	int GetAccID() const;
	void Deposit(int money);
	int Withdraw(int money);
	void ShowAccInfo()const;
	~Account();

};
Account::Account(int ID, int money, char* name)
		:accID(ID), balance(money)
	{
		cusName = new char[strlen(name) + 1];
		strcpy_s(cusName, strlen(name) + 1, name);
	}

Account::Account(const Account& ref)
		:accID(ref.accID), balance(ref.balance)
	{
		cusName = new char[strlen(ref.cusName) + 1];
		strcpy_s(cusName, strlen(ref.cusName) + 1, ref.cusName);
	}
int Account::GetAccID() const { return accID; }

void Account:: Deposit(int money)
{
	balance += money;
}

int Account::Withdraw(int money) //출금액 반환, 부족 시 0 반환
{
	if (balance < money)
	{
		return 0;
	}
	balance -= money;
	return money;
}

void Account::ShowAccInfo() const
{
	cout << "계좌ID: " << accID << endl;
	cout << "이  름: " << cusName << endl;
	cout << "잔  액: " << balance << endl;
}

Account::~Account()
{
	delete[]cusName;
}

class AccountHandler
{
private:
	Account* accArr[100];
	int accNum;
public:
	AccountHandler();
	void ShowMenu(void) const;
	void MakeAccount(void);
	void DepositMoney(void);
	void WithdrawMoney(void);
	void ShowAllAccInfo(void) const;
	~AccountHandler();
};


void AccountHandler:: ShowMenu(void) const
{
	cout << "-----Menu-----" << endl;
	cout << "1. 계좌개설" << endl;
	cout << "2. 입 금" << endl;
	cout << "3. 출 금" << endl;
	cout << "4. 계좌정보 전체 출력" << endl;
	cout << "5. 프로그램 종료" << endl;
}

void AccountHandler::MakeAccount(void) {
	int id;
	char name[NAME_LEN];
	int balance;

	cout << "[계좌개설]" << endl;
	cout << "계좌ID: "; cin >> id;
	cout << "이  름: "; cin >> name;
	cout << "입금액: "; cin >> balance;
	cout << endl;

	accArr[accNum++] = new Account(id, balance, name);
}
void AccountHandler::DepositMoney(void) {
	int money;
	int id;

	cout << "[입 금]" << endl;
	cout << "계좌ID: "; cin >> id;
	cout << "입금액 : "; cin >> money;

	for (int i = 0; i < accNum; i++)
	{
		if (accArr[i]->GetAccID() == id)
		{
			accArr[i]->Deposit(money);
			cout << "입금완료" << endl << endl;
			return;
		}
	}
	cout << "유효하지 않은 ID입니다." << endl << endl;
}

void AccountHandler::WithdrawMoney(void) {
	int money;
	int id;
	cout << "[출 금]" << endl;
	cout << "계좌ID: "; cin >> id;
	cout << "출금액: "; cin >> money;

	for (int i = 0; i < accNum; i++)
	{
		if (accArr[i]->GetAccID() == id)
		{
			if (accArr[i]->Withdraw(money) == 0)
			{
				cout << "잔액부족" << endl << endl;
				return;
			}

			cout << "출금완료" << endl << endl;
			return;
		}
	}
	cout << "유효하지 않은 ID 입니다." << endl << endl;
}
AccountHandler::AccountHandler():accNum(0)
{ }

void AccountHandler::ShowAllAccInfo(void) const {
	for (int i = 0; i < accNum; i++)
	{
		accArr[i]->ShowAccInfo();
		cout << endl;
	}
}

AccountHandler::~AccountHandler()
{
	for (int i = 0; i < accNum; i++)
		delete accArr[i];
}

int main(void)
{
	AccountHandler manager;
	int choice;

	while (1)
	{
		manager.ShowMenu();
		cout << "선택 : ";
		cin >> choice;
		cout << endl;

		switch (choice)
		{
		case MAKE:
			manager.MakeAccount();
			break;
		case DEPOSIT:
			manager.DepositMoney();
			break;
		case WITHDRAW:
			manager.WithdrawMoney();
			break;
		case INQUIRE:
			manager.ShowAllAccInfo();
			break;
		case EXIT:
			
			return 0;
		default:
			cout << "Illegal selection.." << endl;
		}
	}
	return 0;
}

반응형