티스토리 뷰

하나의 파일을 클래스별로 헤더파일과 소스파일로 분리. 

 

BankingCommon.h 

#ifndef __BANKING_COMMON_H__
#define __BANKING_COMMON_H__

#include <iostream>
#include <cstring>
using namespace std;

const int NAME_LEN = 20;
enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };
enum {NORMAL = 1, HIGH_CREDIT = 2};
enum {A_RATING = 1, B_RATING, C_RATING};


#endif // __BANKING_COMMON_H__

 

Account.h 

#ifndef __ACCOUNT__H__
#define __ACCOUNT__H__

class Account
{
private:
    int accID;
    int balance;
    char * cusName;

public:
    Account(int _accID, int _balance, char * _cusName);
    Account(const Account &copy);

    int Get_accID() const;
    void ShowAccInfo() const;
    virtual void DepositMoney(int amount);
    int WithDrawMoney(int amount);
    ~Account();
};


#endif // __ACCOUNT__H__

 

Account.cpp

#include "BankingCommon.h"
#include "Account.h"

Account::Account(int _accID, int _balance, char * _cusName)
        : accID(_accID), balance(_balance)
{
    cusName = new char[strlen(_cusName)+1];
    strcpy(cusName, _cusName);
}

// 깊은 복사 진행하는 복사 생성자
Account::Account(const Account &copy) : accID(copy.accID), balance(copy.balance)
{
    // 새롭게 동적 할당해서 깊은 복사 진행
    cusName = new char[strlen(copy.cusName) + 1];
    strcpy(cusName, copy.cusName);
}

int Account::Get_accID() const { return accID; }

void Account::ShowAccInfo() const
{
    cout << "계좌ID: " << accID << endl;
    cout << "이 름: " << cusName << endl;
    cout << "잔 액: " << balance << endl << endl;
}

void Account::DepositMoney(int amount) // virtual
{
    balance += amount;
}

int Account::WithDrawMoney(int amount)
{
    if(balance < amount)
    {
        return -1;
    }
    balance -= amount;
    return balance;
}

Account::~Account()
{
    delete cusName;
}

 

NormalAccount.h

#ifndef __NORMAL_ACCOUNT_H__
#define  __NORMAL_ACCOUNT_H__

#include "Account.h"

class NormalAccount : public Account
{
private:
    int interest;
public:
    NormalAccount(int _accID, int _balance, char * _cusName, int _interest)
            : Account(_accID, _balance, _cusName), interest(_interest) {}

    virtual void DepositMoney(int amount)
    {
        Account::DepositMoney(amount + (int)(amount * (interest/100.0)));
    }
};


#endif //  __NORMAL_ACCOUNT_H__

 

HighCreditAccount.h


#ifndef __HIGH_CREDIT_ACCOUNT_H__
#define __HIGH_CREDIT_ACCOUNT_H__

#include "NormalAccount.h"

class HighCreditAccount : public NormalAccount
{
private:
    int rating; // 신용등급
public:
    HighCreditAccount(int _accID, int _balance, char * _cusName, int _interest, int _rating)
            : NormalAccount(_accID, _balance, _cusName, _interest), rating(_rating) {}

    virtual void DepositMoney(int amount)
    {
        int additional_interest;
        switch(this->rating)
        {
            case A_RATING:
                additional_interest = (int)(amount * (7/100.0)); break;
            case B_RATING:
                additional_interest = (int)(amount * (4/100.0)); break;
            case C_RATING:
                additional_interest = (int)(amount * (2/100.0)); break;
        }
        NormalAccount::DepositMoney(amount); // 원금 + 이자
        Account::DepositMoney(additional_interest);
    }
};


#endif // __HIGH_CREDIT_ACCOUNT_H__

 

AccountHandler.h 

#ifndef __ACCOUNT_HANDLER__
#define __ACCOUNT_HANDLER__

#include "Account.h"

class AccountHandler
{
private:
    Account * accounts[100];
    int accNum;

public:
    AccountHandler();
    int PrintMenu();
    void MakeAccount();
    void MakeNormalAccount();
    void MakeHighCreditAccount();
    void Deposit();
    void WithDraw();
    void PrintAllAccountsInfo();
    ~AccountHandler();
};

#endif // __ACCOUNT_HANDLER__

 

AccountHandler.cpp

#include "AccountHandler.h"
#include "BankingCommon.h"
#include "NormalAccount.h"
#include "HighCreditAccount.h"

AccountHandler::AccountHandler() : accNum(0) {}

int AccountHandler::PrintMenu()
{
    cout << "----- MENU -----" << endl;
    cout << "1. 계좌개설" << endl;
    cout << "2. 입 금" << endl;
    cout << "3. 출 금" << endl;
    cout << "4. 계좌정보 전체 출력" << endl;
    cout << "5. 프로그램 종료" << endl;
    cout << "선택: ";
    int chose; cin >> chose;
    cout << endl;
    return chose;
}

void AccountHandler::MakeAccount()
{
    int choice;
    cout << "[계좌종류선택]" << endl;
    cout << "1.보통예금계좌 2.신용신뢰계좌" << endl;
    cout << "선택: "; cin >> choice;

    if(choice == NORMAL) MakeNormalAccount();
    else MakeHighCreditAccount();
}

void AccountHandler::MakeNormalAccount()
{
    int accID, balance, interest;
    char cusName[NAME_LEN];

    cout << "[보통예금계좌 개설]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "이 름: "; cin >> cusName;
    cout << "입금액: "; cin >> balance;
    cout << "이자율: "; cin >> interest;

    // 동적으로 객체 생성
    accounts[accNum++] = new NormalAccount(accID, balance, cusName, interest);
}

void AccountHandler::MakeHighCreditAccount()
{
    int accID, balance, interest, rating;
    char cusName[NAME_LEN];

    cout << "[신용신뢰계좌 개설]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "이 름: "; cin >> cusName;
    cout << "입금액: "; cin >> balance;
    cout << "이자율: "; cin >> interest;
    cout << "신용등급(1toA, 2toB, 3toC): "; cin >> rating;

    accounts[accNum++] = new HighCreditAccount(accID, balance, cusName, interest, rating);
}

void AccountHandler::Deposit()
{
    int accID, balance;
    cout << "[입   금]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "입금액: "; cin >> balance;
    cout << "입금완료" << endl;

    for(int i = 0; i < accNum; i++)
    {
        if(accounts[i]->Get_accID() == accID)
        {
            accounts[i]->DepositMoney(balance);
            return;
        }
    }
    cout << "존재하지 않는 계좌ID 입니다 " << endl;
}

void AccountHandler::WithDraw()
{
    int accID, balance;
    cout << "[출   금]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "출금액: "; cin >> balance;
    cout << "출금완료" << endl;

    for(int i = 0; i < accNum; i++)
    {
        if(accounts[i]->Get_accID() == accID)
        {
            if(accounts[i]->WithDrawMoney(balance) == -1)
                cout << "잔액이 부족합니다" << endl;
            else cout << "출금 완료" << endl;
            return;
        }
    }
    cout << "존재하지 않는 계좌ID 입니다 " << endl;
}

void AccountHandler::PrintAllAccountsInfo()
{
    for(int i = 0; i < accNum; i++)
    {
        accounts[i]->ShowAccInfo();
    }
}

AccountHandler::~AccountHandler()
{
    for(int i = 0; i < accNum; i++) delete accounts[i];
}

 

BankingSystemMain.cpp

#include "AccountHandler.h"
#include "BankingCommon.h"
#include "NormalAccount.h"
#include "HighCreditAccount.h"

AccountHandler::AccountHandler() : accNum(0) {}

int AccountHandler::PrintMenu()
{
    cout << "----- MENU -----" << endl;
    cout << "1. 계좌개설" << endl;
    cout << "2. 입 금" << endl;
    cout << "3. 출 금" << endl;
    cout << "4. 계좌정보 전체 출력" << endl;
    cout << "5. 프로그램 종료" << endl;
    cout << "선택: ";
    int chose; cin >> chose;
    cout << endl;
    return chose;
}

void AccountHandler::MakeAccount()
{
    int choice;
    cout << "[계좌종류선택]" << endl;
    cout << "1.보통예금계좌 2.신용신뢰계좌" << endl;
    cout << "선택: "; cin >> choice;

    if(choice == NORMAL) MakeNormalAccount();
    else MakeHighCreditAccount();
}

void AccountHandler::MakeNormalAccount()
{
    int accID, balance, interest;
    char cusName[NAME_LEN];

    cout << "[보통예금계좌 개설]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "이 름: "; cin >> cusName;
    cout << "입금액: "; cin >> balance;
    cout << "이자율: "; cin >> interest;

    // 동적으로 객체 생성
    accounts[accNum++] = new NormalAccount(accID, balance, cusName, interest);
}

void AccountHandler::MakeHighCreditAccount()
{
    int accID, balance, interest, rating;
    char cusName[NAME_LEN];

    cout << "[신용신뢰계좌 개설]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "이 름: "; cin >> cusName;
    cout << "입금액: "; cin >> balance;
    cout << "이자율: "; cin >> interest;
    cout << "신용등급(1toA, 2toB, 3toC): "; cin >> rating;

    accounts[accNum++] = new HighCreditAccount(accID, balance, cusName, interest, rating);
}

void AccountHandler::Deposit()
{
    int accID, balance;
    cout << "[입   금]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "입금액: "; cin >> balance;
    cout << "입금완료" << endl;

    for(int i = 0; i < accNum; i++)
    {
        if(accounts[i]->Get_accID() == accID)
        {
            accounts[i]->DepositMoney(balance);
            return;
        }
    }
    cout << "존재하지 않는 계좌ID 입니다 " << endl;
}

void AccountHandler::WithDraw()
{
    int accID, balance;
    cout << "[출   금]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "출금액: "; cin >> balance;
    cout << "출금완료" << endl;

    for(int i = 0; i < accNum; i++)
    {
        if(accounts[i]->Get_accID() == accID)
        {
            if(accounts[i]->WithDrawMoney(balance) == -1)
                cout << "잔액이 부족합니다" << endl;
            else cout << "출금 완료" << endl;
            return;
        }
    }
    cout << "존재하지 않는 계좌ID 입니다 " << endl;
}

void AccountHandler::PrintAllAccountsInfo()
{
    for(int i = 0; i < accNum; i++)
    {
        accounts[i]->ShowAccInfo();
    }
}

AccountHandler::~AccountHandler()
{
    for(int i = 0; i < accNum; i++) delete accounts[i];
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함