2 minute read

🔭프로그램 설명

  • 프로그램의 목적은 이전 포스팅과 같다.
  • 콘솔에 익숙해지기 보다는 MFC 윈도우와 친해지는 것이 더 도움이 될 듯 하여, 원래의 프로젝트를 이에 맞게 수정하여 동일한 기능이 되는 은행 시스템을 만들어보고자 한다.

💻코드 설명

  • static 컨트롤과 edit 컨트롤을 사용해 입력받고 출력값이 나타나게 했다.
  • 버튼으로 입금/출금/개설/조회 등을 할 수 있도록 했다.

  • 실행 결과

    fail to bring

//Dlg.cpp

BankSystem  bs;     //은행

void CChap5Dlg::OnBnClickedButton1()
{
	UpdateData(TRUE);

	int iId = atol(m_Input_1);
	CString sName = m_Input_2;
	int iMoney = atol(m_Input_3);

	bs.CreateAccount(iId, sName, iMoney);

	m_strOut1.Format("계좌번호 : %d", iId);
	m_strOut2.Format("이    름 : %s", sName);
	m_strOut3.Format("잔    액 : %d", iMoney);

	UpdateData(FALSE);
}

void CChap5Dlg::OnBnClickedButton2()		//입금
{
	UpdateData(TRUE);
	
	int iId = atol(m_Input_1);
	int iMoney = atol(m_Input_3);

	bs.DepositMoney(iId, iMoney);

	m_strOut1.Format("계좌번호 : %d", iId);
	m_strOut2.Format("이    름 : %s", bs.getName(iId));
	m_strOut3.Format("잔    액 : %d", bs.getBalance(iId));

	UpdateData(FALSE);

}

void CChap5Dlg::OnBnClickedButton4()		//출금
{
	UpdateData(TRUE);

	int iId = atol(m_Input_1);
	int iMoney = atol(m_Input_3);

	bs.WithdrawMoney(iId, iMoney);

	m_strOut1.Format("계좌번호 : %d", iId);
	m_strOut2.Format("이    름 : %s", bs.getName(iId));
	m_strOut3.Format("잔    액 : %d", bs.getBalance(iId));

	UpdateData(FALSE);
}

void CChap5Dlg::OnBnClickedButton5()		//잔액조회
{

	UpdateData(TRUE);

	int iId = atol(m_Input_1);

	m_strOut1.Format("계좌번호 : %d", iId);
	m_strOut2.Format("이    름 : %s", bs.getName(iId));
	m_strOut3.Format("잔    액 : %d", bs.getBalance(iId));

	UpdateData(FALSE);
}

//Project2.cpp
#include "pch.h"
#include "framework.h"
#include "Proj2.h"


Account::Account(int id, const char* name, int balance) : m_Id(id), m_Balance(balance)
{
	int len = strlen(name) + 1;
	m_cpName = new char[len];
	strcpy_s(m_cpName,len, name);
}

Account::~Account()
{
}

int Account::getID() {
	return m_Id;
}

int Account::getBalance() {
	return m_Balance;
}

char* Account::getName() {
	return m_cpName;
}

void Account::changeBalance(int money) {
	m_Balance = money;
}


BankSystem::BankSystem() : m_NumOfAcc(0)
{
}

BankSystem::~BankSystem()
{
	delete[]m_pAccs;
}



void BankSystem::CreateAccount(int id, CString name, int money) {

	m_pAccs[m_NumOfAcc++] = new Account(id, LPCTSTR(name), money);
	return;

}

void BankSystem::DepositMoney(int id, int 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);
			return;
		}
	}

	return;
}

void BankSystem::WithdrawMoney(int id, int 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) {
				
				retMoney = m_pAccs[i]->getBalance();
				retMoney += money;
				m_pAccs[i]->changeBalance(retMoney);
				return;
			}

			m_pAccs[i]->changeBalance(retMoney);
		
			return;
		}
	}

	return;
}

CString BankSystem::getName(int id) const {
	for (int i = 0; i < m_NumOfAcc; i++) {
		if (m_pAccs[i]->getID() == id) {
			return m_pAccs[i]->getName();
		}
	}
	CString error = "계좌정보가 없습니다";
	return error;
}

int BankSystem::getBalance(int id) const{
	for (int i = 0; i < m_NumOfAcc; i++) {
		if (m_pAccs[i]->getID() == id) {
			return m_pAccs[i]->getBalance();
		}
	}
	return -1;
}
  • 선언은 헤더파일에 담았고, 정의는 cpp에 담았다.
  • 만들면서 코드가 점점 꼬이는 듯한 느낌이었는데, 정보은닉 한답시고 비효율적인 액세스함수를 많이 만든 것 같다.
    코드가 중복되는 부분도 많다. 더 깔끔하게 어떻게 수정해야 할까.