728x90
반응형
Account 클래스의 멤버함수에 const 선언을 추가해서 코드의 안전성을 높인다.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int NAME_LEN = 20;
void ShowMenu(void); //메뉴출력
void MakeAccount(void); //계좌개설을 위한 함수
void DespositMoney(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_s(cusName, strlen(name) + 1, name);
}
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 GetAccID() const { return accID; }
void Deposit(int money)
{
balance += money;
}
int Withdraw(int money) //출금액 반환, 부족 시 0 반환
{
if (balance < money)
{
return 0;
}
balance -= money;
return money;
}
void ShowAccInfo()
{
cout << "계좌ID: " << accID << endl;
cout << "이 름: " << cusName << endl;
cout << "잔 액: " << balance << endl;
}
~Account()
{
delete[]cusName;
}
};
Account* accArr[100]; //Account 저장을 위한 배열
int accNum = 0;
int main(void)
{
int choice;
while (1)
{
ShowMenu();
cout << "선택 : ";
cin >> choice;
cout << endl;
switch (choice)
{
case MAKE:
MakeAccount();
break;
case DEPOSIT:
DespositMoney();
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 << "-----Menu-----" << endl;
cout << "1. 계좌개설" << endl;
cout << "2. 입 금" << endl;
cout << "3. 출 금" << endl;
cout << "4. 계좌정보 전체 출력" << endl;
cout << "5. 프로그램 종료" << endl;
}
void 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 DespositMoney(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 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;
}
void ShowAllAccInfo(void) {
for (int i = 0; i < accNum; i++)
{
accArr[i]->ShowAccInfo();
cout << endl;
}
}
728x90
반응형
'Programming > C++ 3' 카테고리의 다른 글
[C++] 07-01. 상속에 대한 이해와 접근 [급여관리시스템 1단계] (0) | 2021.05.28 |
---|---|
[C++ 프로젝트] 은행계좌 관리 프로그램 Part. 05 (0) | 2021.05.18 |
[C++ 프로젝트] 은행계좌 관리 프로그램 Part. 03 (0) | 2021.05.17 |
[C++ 프로젝트] 은행계좌 관리 프로그램 Part. 02 (0) | 2021.05.17 |
[C++ 프로젝트] 은행계좌 관리 프로그램 Part. 01 (0) | 2021.05.17 |