프로그램 추가 조건
조건1) class에 const 선언을 추가할 것
조건2) 코드는 은행계좌 관리 프로그램 버전3를 가져와 변경할 것
실행결과
코드
#include <iostream>
#include<cstring>
using namespace std;
const int NAME_LEN = 20;
void ShowMenu(void); //메뉴출력
void MakeAccount(void); //계좌개설을 위한 함수
void DepositMoney(void); //입 금
void WithdrawMoney(void); //출 금
void ShowAllAccInfo(void); //잔액조회
enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };
class Account
{
private:
int accID;
int balance;
char* cusName;
public:
Account(int id, int money, char* name)
:accID(id), balance(money)
{
cusName = new char[strlen(name) + 1];
strcpy(cusName, name);
}
Account(const Account& ref)
:accID(ref.accID), balance(ref.balance)
{
cusName = new char[strlen(ref.cusName) + 1];
strcpy(cusName, ref.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 ShowInfo() const
{
cout << "계좌ID: " << accID << endl;
cout << "이 름: " << cusName << endl;
cout << "잔 액: " << balance << endl;
}
~Account()
{
delete[]cusName;
}
};
Account* accArr[100]; //Account 저장을 위한 배열
int accNum = 0; //저장된 Account 수
int main(void)
{
int choice;
while (1)
{
ShowMenu();
cout << "선택: ";
cin >> choice;
cout << endl;
switch (choice)
{
case MAKE:
MakeAccount();
break;
case DEPOSIT:
DepositMoney();
break;
case WITHDRAW:
WithdrawMoney();
break;
case INQUIRE:
ShowAllAccInfo();
break;
case EXIT:
for (int i = 0; i < accNum; i++)
delete accArr[i];
return 0;
default:
cout << "Illegal selection.." << endl;
}
}
return 0;
}
void ShowMenu(void)
{
cout << endl;
cout << "-----Menu-----" << endl;
cout << "1. 계좌개설" << endl;
cout << "2. 입 금" << endl;
cout << "3. 출 금" << endl;
cout << "4. 계좌정보 전체 출력" << endl;
cout << "5. 프로그램 종료" << endl;
}
void MakeAccount(void)
{
int id, balance;
char name[NAME_LEN];
cout << "[계좌개설]" << endl;
cout << "계좌ID: "; cin >> id;
cout << "이 름: "; cin >> name;
cout << "입금액: "; cin >> balance;
cout << endl;
accArr[accNum++] = new Account(id, balance, name);
}
void DepositMoney(void)
{
int id, money;
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;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl;
}
void WithdrawMoney(void)
{
int id, money;
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;
return;
}
cout << "출금완료" << endl;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl;
}
void ShowAllAccInfo(void)
{
for (int i = 0; i < accNum; i++)
{
accArr[i]->ShowInfo();
cout << endl;
}
}
<참고>
윤성우의 열혈 C++ 프로그래밍
'C++ 언어 > Project' 카테고리의 다른 글
은행계좌 관리 프로그램 버전5 (1) | 2022.11.13 |
---|---|
은행계좌 관리 프로그램 버전3 (1) | 2022.11.03 |
은행계좌 관리 프로그램 버전2 (1) | 2022.11.01 |
은행계좌 관리 프로그램 버전1 (0) | 2022.08.14 |