티스토리 뷰
- 입출금 시 0 미만의 값 입력시 예외 발생, 처리
- 출금 시 잔액보다 큰 금액 출금 요구 시 예외 발생, 처리
ExceptionClass.h
#ifndef __EXCEPTION_CLASS_H__
#define __EXCEPTION_CLASS_H__
// 입출금 진행 시 사용자로 부터 0보다 작은 값이 입력되는 예외 상황
class NotAllowedInputException
{
private:
int money;
public:
NotAllowedInputException(int _money) : money(_money) {}
void ShowExceptionInfo()
{
cout << "입출금 금액 " << money << "는 유효하지 않습니다" << endl;
}
};
// 출금 시 예금된 금액보다 더 많은 금액의 출금을 요구하는 예외 상황
class NotEnoughBalanceException
{
private:
int balance;
int money;
public:
NotEnoughBalanceException(int _balance, int _money) : balance(_balance), money(_money) {}
void ShowExceptionInfo()
{
cout << "현재 잔액은 " << balance << "입니다" << endl;
cout << "요청하신 출금 금액은 " << money << "입니다" << endl;
}
};
#endif // __EXCEPTION_CLASS_H__
Account.cpp
/*
* 예외 발생
*/
#include "BankingCommon.h"
#include "Account.h"
#include "ExceptionClass.h"
Account::Account(int _accID, int _balance, String _cusName)
: accID(_accID), balance(_balance)
{
cusName = _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
{
// 0 미만 값 입력시 예외 발생
if(amount < 0) throw NotAllowedInputException(amount);
balance += amount;
}
int Account::WithDrawMoney(int amount)
{
if(amount < 0) throw NotAllowedInputException(amount);
// 입금 금액보다 큰 금액 출금 시도 시 예외 발생
if(balance < amount) throw NotEnoughBalanceException(balance, amount);
balance -= amount;
return balance;
}
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, String _cusName, int _interest)
: Account(_accID, _balance, _cusName), interest(_interest) {}
virtual void DepositMoney(int amount)
{
if(amount < 0) throw NotAllowedInputException(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, String _cusName, int _interest, int _rating)
: NormalAccount(_accID, _balance, _cusName, _interest), rating(_rating) {}
virtual void DepositMoney(int amount)
{
if(amount < 0) throw NotAllowedInputException(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.cpp
/*
* 예외 발생
*/
#include "BankingCommon.h"
#include "Account.h"
#include "ExceptionClass.h"
Account::Account(int _accID, int _balance, String _cusName)
: accID(_accID), balance(_balance)
{
cusName = _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
{
// 0 미만 값 입력시 예외 발생
if(amount < 0) throw NotAllowedInputException(amount);
balance += amount;
}
int Account::WithDrawMoney(int amount)
{
if(amount < 0) throw NotAllowedInputException(amount);
// 입금 금액보다 큰 금액 출금 시도 시 예외 발생
if(balance < amount) throw NotEnoughBalanceException(balance, amount);
balance -= amount;
return balance;
}
'윤성우의 열헐 C++' 카테고리의 다른 글
윤성우의 열혈 c++ ) 끝. 그리고 코드 올려놓은 github (0) | 2022.03.28 |
---|---|
윤성우의 열혈 c++) Chapter 16. c++의 형 변환 연산자 (0) | 2022.03.28 |
윤성우의 열혈 c++) Chapter 15. 예외처리 (0) | 2022.03.28 |
윤성우의 열혈 c++) Chapter 14. 템플릿 (Template) 2 (0) | 2022.03.26 |
윤성우의 열혈 c++) OOP 단계별 프로젝트 10단계 (0) | 2022.03.26 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Unity
- 이분탐색
- floyd warshall
- priority queue
- permutation
- two pointer
- Dijkstra
- DP
- 조합
- dfs
- C++
- graph
- back tracking
- Tree
- 자료구조
- Python
- BFS
- db
- 재귀
- Kruskal
- greedy
- MVC
- Spring
- Brute Force
- Implementation
- C
- CSS
- recursion
- Stack
- binary search
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함