검색결과 리스트
글
c++ oop프로젝트 8장
#include <iostream>
#include <cstring>
using namespace std;
const int NAME_LEN = 20;
enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };
class Account
{
int id; // 계좌번호
int balance; // 잔 액
char *name; // 고객이름
public:
Account(int id, char *name, int balance);
Account(const Account& _Account);
~Account();
int getID() const; //계좌조회
virtual void AddMoney(int money); //입금
void MinMoney(int money); //출금
int getBalance() const; //잔액조회
char* getName() const; //이름
virtual void ShowAllData();
};
Account::Account(int id, char *name, int balance) {
this->id = id;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->balance = balance;
}
Account::Account(const Account& _Account)
{
this->id = _Account.id;
this->name = new char[strlen(_Account.name) + 1];
strcpy(this->name, _Account.name);
this->balance = _Account.balance;
}
Account::~Account()
{
delete name;
}
int Account::getID() const
{
return id;
}
void Account:: AddMoney(int money)
{
this->balance += money;
}
int Account:: getBalance() const
{
return balance;
}
void Account:: MinMoney(int money)
{
balance -= money;
}
char* Account:: getName() const
{
return name;
}
void Account::ShowAllData()
{
cout << "ID : " << id << endl;
cout << "이름 : " <<name << endl;
cout << "잔액 : " << balance << endl;
}
//신용계좌 입금시 1프로 추가
class FaithAccount : public Account
{
public:
FaithAccount(int id, char *name, int balance) : Account(id, name, balance + balance * 0.01)
{
}
void AddMoney(int money)
{
Account::AddMoney(money + money * 0.01);
}
};
//기부계좌 입금액 1프로 기부 기부총액 저장
class ContriAccount : public Account
{
int contri_sum; //기부금 합
public:
ContriAccount(int id, char *name, int balance) : Account(id, name, balance - balance * 0.01)
{
contri_sum += balance * 0.01;
}
void AddMoney(int money)
{
Account::AddMoney(money - money * 0.01);
contri_sum += money * 0.01;
}
void ShowAllData()
{
Account::ShowAllData();
cout << "총기부액 : " << contri_sum << endl;
}
};
class AccManger
{
Account* pArr[100]; // Account 저장을 위한 배열
int index; // 저장된 Account 수
public:
AccManger() :index(0)
{};
void ShowMenu(void); // 메뉴출력
void MakeAccount(void); // 계좌개설을 위한 함수
void DepositMoney(void); // 입 금
void WithdrawMoney(void); // 출 금
void Inquire(void); // 잔액조회
};
int main(void)
{
int choice;
AccManger ac;
while (1)
{
ac.ShowMenu();
cout << "선택: ";
cin >> choice;
cout << endl;
switch (choice)
{
case MAKE:
ac.MakeAccount();
break;
case DEPOSIT:
ac.DepositMoney();
break;
case WITHDRAW:
ac.WithdrawMoney();
break;
case INQUIRE:
ac.Inquire();
break;
case EXIT:
return 0;
default:
cout << "Illegal selection.." << endl;
}
}
return 0;
}
void AccManger::ShowMenu(void)
{
cout << "-----Menu------" << endl;
cout << "1. 계좌개설" << endl;
cout << "2. 입 금" << endl;
cout << "3. 출 금" << endl;
cout << "4. 계좌정보 전체 출력" << endl;
cout << "5. 프로그램 종료" << endl;
}
void AccManger::MakeAccount(void)
{
int id;
char name[NAME_LEN];
int balance;
int sel;
cout << "1. 일반계좌 개설" << endl;
cout << "2. 신용계좌 개설" << endl;
cout << "3. 기부계좌 개설" << endl;
cout << ">>";
cin >> sel;
cout << "[계좌개설]" << endl;
cout << "계좌ID: "; cin >> id;
cout << "이 름: "; cin >> name;
cout << "입금액: "; cin >> balance;
cout << endl;
if (sel == 1)
pArr[index++] = new Account(id, name, balance);
else if (sel == 2)
pArr[index++] = new FaithAccount(id, name, balance);
else if (sel == 3)
pArr[index++] = new ContriAccount(id, name, balance);
else
cout << "오류"<<endl;
}
void AccManger::DepositMoney(void)
{
int money;
int id;
cout << "[입 금]" << endl;
cout << "계좌ID: "; cin >> id;
cout << "입금액: "; cin >> money;
for (int i = 0; i<index; i++)
{
if (pArr[i]->getID() == id)
{
pArr[i]->AddMoney(money);
cout << "입금완료" << endl << endl;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl << endl;
}
void AccManger::WithdrawMoney(void)
{
int money;
int id;
cout << "[출 금]" << endl;
cout << "계좌ID: "; cin >> id;
cout << "출금액: "; cin >> money;
for (int i = 0; i<index; i++)
{
if (pArr[i]->getID() == id)
{
if (pArr[i]->getBalance()<money)
{
cout << "잔액부족" << endl << endl;
return;
}
pArr[i]->MinMoney(money);
cout << "출금완료" << endl << endl;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl << endl;
}
void AccManger::Inquire(void)
{
for (int i = 0; i<index; i++)
{
pArr[i]->ShowAllData();
}
}
'♪C++ > 소스' 카테고리의 다른 글
c++ stack (0) | 2018.09.19 |
---|---|
열혈강의 3장 연습문제 3-1 3-2 (0) | 2017.11.10 |
성우 1장 OOP 단계별 프로젝트 (0) | 2017.10.09 |