OOP프로젝트의 마지막 11에서는 예외처리 적용예정
허용되는 숫자보다 프로그램사용자로부터 더 작거나 큰 값을 입력받았을 경우
try catch문으로 예외처리
그 과정에서 AccountException.h 파일을 추가하고
숫자가 입력되거나 계산되는 부분에서 if문으로 먼저 확인하고 정상적이지 않은 값일 경우
throw로 Exception 클래스로 전달함
추가한 파일
1. AccountException.h
#ifndef __ACCOUNT_EXCEPTION_H__
#define __ACCOUNT_EXCEPTION_H__
class MinusException
{
private:
int exval;
public:
MinusException(int val) : exval(val) {}
void ShowExceptionInfo(void) const
{
cout << "입(출)금액 " << exval << "은 유효하지 않습니다!" << endl;
}
};
class InsuffException
{
private:
int balance;
int reqval;
public:
InsuffException(int val, int req) : balance(val), reqval(req)
{}
void ShowExceptionInfo() const
{
cout << "잔액에서 " << reqval - balance << "가(이) 부족합니다!" << endl;
}
};
#endif
exval, balance, reqval 등 변수는 예외처리과정에서 전달받는 값들
변경된 파일들
1. NormalAccount.h
#ifndef __NOMAL_H__
#define __NOMAL_H__
#include "Account.h"
#include "AccountException.h"
class NomalAccount : public Account
{
private:
int interestInfo;
public:
NomalAccount(const int id, int money, const String name, int interInfo)
:Account(id, money, name), interestInfo(interInfo)
{}
virtual void Deposit(int money)
{
if (money < 0)
throw MinusException(money);
Account::Deposit(money);
Account::Deposit(money * (interestInfo * 0.01));
}
};
#endif
2. HighCreditAccount.h
#ifndef __HIGHCREDIT_H__
#define __HIGHCREDIT_H__
#include "NormalAccount.h"
class HighCreditAccount : public NomalAccount
{
private:
int rating;
public:
HighCreditAccount(const int id, int money, const String name, int interInfo, int rate)
:NomalAccount(id, money, name, interInfo), rating(rate)
{}
virtual void Deposit(int money)
{
if (money < 0)
throw MinusException(money);
NomalAccount::Deposit(money);
switch (rating)
{
case 1:
Account::Deposit((int)money * 0.07);
break;
case 2:
Account::Deposit((int)money * 0.04);
break;
case 3:
Account::Deposit((int)money * 0.02);
break;
}
}
};
#endif
3. Account.cpp
#include "Account.h"
#include "BankingCommonDec1.h"
#include "AccountException.h"
Account::Account(const int id, int money, const String name)
:accId(id), balance(money)
{
//cusName = new char[strlen(name) + 1];
//strcpy_s(cusName, strlen(name) + 1, name);
cusName = name;
}
//Account::Account(const Account& ac)
// :accId(ac.accId), balance(ac.balance)
//{
// cusName = new char[strlen(ac.cusName) + 1];
// strcpy_s(cusName, strlen(ac.cusName) + 1, ac.cusName);
//}
int Account::GetAccId() const
{
return accId;
}
void Account::Deposit(int money)
{
if (money < 0)
throw MinusException(money);
balance += money;
}
int Account::WithDraw(int money)
{
/*if (balance < money)
{
return 0;
}*/
if (money < 0)
throw MinusException(money);
if (balance < money)
throw InsuffException(balance, money);
balance -= money;
return money;
}
void Account::ShowAccount() const
{
cout << "계좌번호: " << accId << endl;
cout << "고객이름: " << cusName << endl;
cout << "잔 액: " << balance << endl << endl;
}
//08 Account 대입연산자 추가
//Account& Account::operator=(const Account& ac)
//{
// accId = ac.accId;
// balance = ac.balance;
// delete[]cusName;
// cusName = new char[strlen(ac.cusName) + 1];
// strcpy_s(cusName, strlen(ac.cusName) + 1, ac.cusName);
// return *this;
//}
//Account::~Account()
//{
// delete[]cusName;
//}
4. AccountHandler.cpp ( try catch문이 들어갈 파일)
#include "Account.h"
#include "BankingCommonDec1.h"
#include "AccountHandler.h"
#include "NormalAccount.h"
#include "HighCreditAccount.h"
#include "AccountException.h"
AccountHandler::AccountHandler()
:userIndex(0)
{ }
void AccountHandler::ShowMenu() {
cout << "----- Menu -----" << endl;
cout << "1. 계좌개설" << endl;
cout << "2. 입 금" << endl;
cout << "3. 출 금" << endl;
cout << "4. 계좌정보 전체 출력" << endl;
cout << "5. 프로그램 종료" << endl;
}
void AccountHandler::MakeAccount() {
int interInfo;
cout << "[계좌종류선택]" << endl;
cout << "1.보통예급계좌 2.신용신뢰계좌" << endl;
cout << "선택: ";
cin >> interInfo;
if (interInfo == NORMAL)
MakeNomalAccount();
else
MakeCreditAccount();
}
void AccountHandler::MakeNomalAccount()
{
int id;
int money = 0;
int interInfo;
String userName;
cout << "[보통예금계좌 개설]" << endl;
cout << "계좌ID: ";
cin >> id;
cout << "고객이름: ";
cin >> userName;
cout << "이자율: ";
cin >> interInfo;
userList[userIndex++] = new NomalAccount(id, money, userName, interInfo);
cout << endl;
}
void AccountHandler::MakeCreditAccount()
{
int id;
int money = 0;
int interInfo;
int rate;
String userName;
cout << "[신용신뢰계좌 개설]" << endl;
cout << "계좌ID: ";
cin >> id;
cout << "고객이름: ";
cin >> userName;
cout << "이자율: ";
cin >> interInfo;
cout << "신용등급(1toA, 2toB, 3toC): ";
cin >> rate;
userList[userIndex++] = new HighCreditAccount(id, money, userName, interInfo, rate);
cout << endl;
}
void AccountHandler::DepositMoney() {
int tempId;
int tempBalance;
cout << "[입 금]" << endl;
cout << "입금할 계좌번호: ";
cin >> tempId;
while (1)
{
cout << "입금할 금액: ";
cin >> tempBalance;
try
{
for (int i = 0; i < userIndex; i++)
{
if (tempId == userList[i]->GetAccId())
{
userList[i]->Deposit(tempBalance);
cout << endl;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl << endl;
}
catch (MinusException& e)
{
e.ShowExceptionInfo();
cout << "입금액을 재입력하세요." << endl;
}
}
}
void AccountHandler::WithdrawMoney() {
int tempId;
int tempBalance;
cout << "[출 금]" << endl;
cout << "출금할 계좌번호: ";
cin >> tempId;
while (1)
{
cout << "출금할 금액: ";
cin >> tempBalance;
try
{
for (int i = 0; i < userIndex; i++)
{
if (tempId == userList[i]->GetAccId())
{
if (userList[i]->WithDraw(tempBalance) == 0)
{
cout << "잔액 부족" << endl << endl;
return;
}
cout << "출금완료" << endl;
cout << endl;
return;
}
}
cout << "유효하지 않은 ID 입니다." << endl << endl;
}
catch (MinusException& e)
{
e.ShowExceptionInfo();
cout << "입금액을 재입력하세요." << endl;
}
catch (InsuffException& e)
{
e.ShowExceptionInfo();
cout << "출금액을 재입력하세요." << endl;
}
}
}
void AccountHandler::ShowAllAccount() {
for (int i = 0; i < userIndex; i++)
{
userList[i]->ShowAccount();
}
}
AccountHandler::~AccountHandler()
{
for (int i = 0; i < userIndex; i++)
delete userList[i];
}
'C++' 카테고리의 다른 글
숫자입력해 달팽이 배열 (0) | 2024.12.21 |
---|---|
열혈 C++ 프로그래밍 OOP 프로젝트 10 (0) | 2024.04.05 |
열혈 C++ 프로그래밍 OOP 프로젝트 09 (1) | 2024.04.03 |
열혈 C++ 프로그래밍 OOP 프로젝트 08 (1) | 2024.04.03 |
연산자 오버로딩 공부01 (2) | 2024.04.01 |