////////////////////////////////////////////////
// Globals
enum class AccountType
{
Saving = 0
};
// AccountType을 string으로 변환하는 함수
const char* toString(AccountType type) noexcept
{
if (type == AccountType::Saving)
return "Saving";
return "NULL";
}
////////////////////////////////////////////////
// Classes
class BankAccount
{
public:
BankAccount(uint32_t balance, const string& accNum, const string& bankName, AccountType accountType)
:balance(balance), accNum(accNum), bankName(bankName), accountType(accountType) { }
// 입금
bool Deposit(uint32_t money)
{
const uint32_t before = balance;
balance += money;
writeStatement(before, money, balance, statementOption::DEPOSIT);
return true;
}
// 출금
bool WithDrawal(uint32_t money)
{
if (balance < money)
return false;
const uint32_t before = balance;
balance -= money;
writeStatement(before, money, balance, statementOption::WITHDRAWAL);
return true;
}
// 계좌 정보 출력
void Print()
{
cout << "Account: " << accNum
<< " | Bank: " << bankName
<< " | Type: " << toString(accountType) << endl;
for (auto d : statement)
{
cout << d << endl;
}
}
private:
enum statementOption
{
DEPOSIT,
WITHDRAWAL
};
// 계좌 내역 저장
void writeStatement(uint32_t before, uint32_t money, uint32_t after, statementOption option)
{
string t = "Balance: $" + std::to_string(before) + " -> ";
if (option == statementOption::DEPOSIT)
t += "Deposit $" + std::to_string(money) + " -> ";
if (option == statementOption::WITHDRAWAL)
t += "Withdrawal $" + std::to_string(money) + " -> ";
t += "Balance: $" + std::to_string(after);
statement.push_back(t);
}
private:
uint32_t balance;
string accNum;
string bankName;
AccountType accountType;
vector<string> statement; // 거래 내역 저장
};
// Class: Customer
class Customer
{
public:
Customer(const string& name, const string& phoneNum, const string& address, size_t age)
:name(name), phoneNum(phoneNum), address(address), age(age), acc(nullptr) {
}
~Customer()
{
if (acc != nullptr)
delete acc;
}
// 계좌 생성
bool AddAccount(uint32_t balance, string accNum, string bankName, AccountType accountType)
{
if (acc != nullptr)
return false;
acc = new BankAccount(balance, accNum, bankName, accountType);
return true;
}
// 입금
bool Deposit(uint32_t money)
{
if (acc == nullptr)
return false;
return acc->Deposit(money);
}
// 출금
bool WithDrawal(uint32_t money)
{
if (acc == nullptr)
return false;
return acc->WithDrawal(money);
}
// 고객 정보 출력
void PrintCustomer()
{
cout << "Customer: " << name
<< " | Phone: " << phoneNum
<< " | Age: " << age << endl;
}
// 고객 계좌 정보 출력
void PrintAccount()
{
if (acc == nullptr)
cout << "No Account" << endl;
else
acc->Print();
}
private:
string name;
string phoneNum;
string address;
size_t age;
BankAccount* acc;
};
////////////////////////////////////////////////
// Main
int main()
{
Customer alice = { "Alice", "010-1234-5678", "Korea", 27 };
alice.AddAccount(1500, "123-456", "Hana", AccountType::Saving);
alice.Deposit(200);
alice.WithDrawal(500);
alice.PrintCustomer();
alice.PrintAccount();
return 0;
}