프로젝트 기능 제공의 핵심이 되는 컨트롤 클래스 정의
AccountHandler 클래스를 정의하고 앞에서 정의한 전역함수들을 클래스에 멤버함수로 포함
#include <iostream>
#include <cstring>
using namespace std;
// 02 class추가, 객체 포인터 배열 추가
// 03 복사 생성자 추가, 소멸자 추가
// 04 Account 클래스내의 const선언이 가능한 멤버함수에 const선언
// 05 전반적인 기능을 담당하는 Handler 컨트롤 클래스 추가
enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };
class Account
{
private:
int accId;
int balance;
char* cusName;
public:
Account()
:accId(0), balance(0)
{}
Account(const int id, int money,const char* name)
:accId(id), balance(money)
{
cusName = new char[strlen(name) + 1];
strcpy_s(cusName, strlen(name) + 1, name);
}
Account(const Account& ac)
:accId(ac.accId), balance(ac.balance)
{
cusName = new char[strlen(ac.cusName) + 1];
strcpy_s(cusName, strlen(ac.cusName) + 1, ac.cusName);
}
int GetAccId() const
{
return accId;
}
void Deposit(int money)
{
balance += money;
}
int WithDraw(int money)
{
if (balance < money)
{
return 0;
}
balance -= money;
return money;
}
void ShowAccount() const
{
cout << "계좌번호: " << accId << endl;
cout << "고객이름: " << cusName << endl;
cout << "잔 액: " << balance << endl<<endl;
}
~Account()
{
delete[]cusName;
}
};
//handler 클래스 추가
class AccountHandler
{
private:
Account* userList[100];
int userIndex;
public:
AccountHandler()
:userIndex(0)
{ }
void ShowMenu() {
cout << "----- Menu -----" << endl;
cout << "1. 계좌개설" << endl;
cout << "2. 입 금" << endl;
cout << "3. 출 금" << endl;
cout << "4. 계좌정보 전체 출력" << endl;
cout << "5. 프로그램 종료" << endl;
}
void MakeAccount() {
int id;
int money = 0;
char userName[30];
cout << "[계좌개설]" << endl;
cout << "계좌번호: ";
cin >> id;
cout << "고객이름: ";
cin >> userName;
userList[userIndex++] = new Account(id, money, userName);
cout << endl;
}
void DepositMoney() {
int tempId;
int tempBalance;
cout << "[입 금]" << endl;
cout << "입금할 계좌번호: ";
cin >> tempId;
for (int i = 0; i < userIndex; i++)
{
if (tempId == userList[i]->GetAccId())
{
cout << "입금할 금액: ";
cin >> tempBalance;
userList[i]->Deposit(tempBalance);
cout << endl;
return;
}
cout << userList[0]->GetAccId() << endl;
cout << tempId << endl;
}
cout << "유효하지 않은 ID 입니다." << endl << endl;
}
void WithdrawMoney() {
int tempId;
int tempBalance;
cout << "[출 금]" << endl;
cout << "출금할 계좌번호: ";
cin >> tempId;
for (int i = 0; i < userIndex; i++)
{
if (tempId == userList[i]->GetAccId())
{
cout << "출금할 금액: ";
cin >> tempBalance;
if (userList[i]->WithDraw(tempBalance) == 0)
{
cout << "잔액 부족" << endl << endl;
return;
}
cout << "출금완료" << endl;
cout << endl;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl << endl;
}
void ShowAllAccount() {
for (int i = 0; i < userIndex; i++)
{
userList[i]->ShowAccount();
}
}
~AccountHandler()
{
for (int i = 0; i < userIndex; i++)
delete userList[i];
}
};
int main(void)
{
AccountHandler controller;
int choice;
while (1)
{
controller.ShowMenu();
cout << "선택: ";
cin >> choice;
cout << endl;
switch(choice)
{
case MAKE:
controller.MakeAccount();
break;
case DEPOSIT:
controller.DepositMoney();
break;
case WITHDRAW:
controller.WithdrawMoney();
break;
case INQUIRE:
controller.ShowAllAccount();
break;
case EXIT:
return 0;
}
}
return 0;
}
'C++' 카테고리의 다른 글
열혈 C++ 프로그래밍 OOP 프로젝트 07 (2) | 2024.03.23 |
---|---|
열혈 C++ 프로그래밍 OOP 프로젝트 06 (1) | 2024.03.22 |
열혈 C++ 프로그래밍 OOP 프로젝트 04 (0) | 2024.03.20 |
열혈 C++ 프로그래밍 OOP 프로젝트 03 (0) | 2024.03.20 |
열혈 C++ 프로그래밍 OOP 프로젝트 01 (0) | 2024.03.20 |