Account.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Account
{
private:
string name; // 이름
string accountNum; // 계좌번호
int balance; // 잔액
public:
// 기본생성자
Account(string name, string accounNum);
// getter
string getName();
string getAccountNum();
int getBalance();
// setter
void setName(string name);
void setAccountNum(string accountNum);
void setBalance(int balance);
// 입금함수
void deposit(int amount);
// 출금함수
void withdraw(int amount);
// 조회함수
void showInfo();
};
Account.cpp
#include "Account.h"
Account::Account(string name, string accountNum)
{
this->name = name;
this->accountNum = accountNum;
balance = 0;
}
void Account::deposit(int amount)
{
balance += amount;
}
void Account::withdraw(int amount)
{
balance -= amount;
}
void Account::showInfo()
{
cout << "이름 : " << name << endl;
cout << "계좌번호 : " << accountNum << endl;
cout << "잔액 : " << balance << endl;
}
// getter
string Account::getName() { return name; }
string Account::getAccountNum() { return accountNum; }
int Account::getBalance() { return balance; }
// setter
void Account::setName(string name) { this->name = name; }
void Account::setAccountNum(string accountNum) { this->accountNum = accountNum; }
void Account::setBalance(int balance){ this->balance = balance; }
Main.cpp
#include "Account.h"
#include <vector>
#include <memory>
int choiceNum;
vector<unique_ptr<Account>> accounts;
int main()
{
while (choiceNum != 5)
{
cout << "========M E N U ========" << endl;
cout << "1. 계좌개설" << endl;
cout << "2. 입 금" << endl;
cout << "3. 출 금" << endl;
cout << "4. 계좌정보 전체 출력" << endl;
cout << "5. 프로그램 종료" << endl;
cout << "번호를 선택해 주세요 : ";
cin >> choiceNum;
switch (choiceNum)
{
case 1:
{
cout << "================================================" << endl;
cout << "계좌를 개설합니다. 아래의 정보를 입력해주세요." << endl;
cout << "이름 : ";
string name;
string accountNum;
cin >> name;
cout << "계좌번호 : ";
cin >> accountNum;
// 신규 계좌 백터 입력
accounts.push_back(make_unique<Account>(name, accountNum));
cout << "계좌를 생성했습니다." << endl;
cout << endl;
accounts.back()->showInfo();
cout << endl;
break; // break가 없으면 case 2를 실행한다.
}
case 2:
{
int amount;
string accountNum;
cout << "입금을 시작합니다. 입금하실 계좌번호를 입력해주세요. : ";
cin >> accountNum;
auto it = find_if(accounts.begin(), accounts.end(), [&](const auto& account) { return account->getAccountNum() == accountNum; });
if (it != accounts.end())
{
cout << "======계좌번호 일치 확인========" << endl;
(*it)->showInfo();
}
cout << "입금하실 금액을 입력해주세요 : ";
cin >> amount;
(*it)->deposit(amount);
cout << "입금이 완료되었습니다. 잔액을 확인해주세요. 현재 잔액 : " << (*it)->getBalance() << endl;
break;
}
case 3:
{
int amount;
string accountNum;
cout << "출금을 시작합니다. 출금하실 계좌번호를 입력해주세요. : ";
cin >> accountNum;
auto it = find_if(accounts.begin(), accounts.end(), [&](const auto& account) { return account->getAccountNum() == accountNum; });
if (it != accounts.end())
{
cout << "======계좌번호 일치 확인========" << endl;
(*it)->showInfo();
}
cout << "출금하실 금액을 입력해주세요 : ";
cin >> amount;
(*it)->withdraw(amount);
cout << "출금이 완료되었습니다. 잔액을 확인해주세요. 현재 잔액 : " << (*it)->getBalance() << endl;
break;
}
case 4:
{
string accountNum;
cout << "계좌정보를 출력합니다. 계좌번호를 입력해주세요. : ";
cin >> accountNum;
auto it = find_if(accounts.begin(), accounts.end(), [&](const auto& account) { return account->getAccountNum() == accountNum; });
if (it != accounts.end())
{
cout << "======계좌번호 일치 확인========" << endl;
(*it)->showInfo();
}
break;
}
case 5:
cout << "계좌관리 프로그램을 종료합니다." << endl;
return 0;
default :
cout << "잘못된 번호입니다. 번호를 다시 입력해주세요." << endl;
}
}
return 0;
}'C++ 공부' 카테고리의 다른 글
| 최대공약수와 최소공배수를 구하는 함수 (0) | 2025.09.29 |
|---|---|
| 스마트포인터와 벡터를 이용한 객체 생성 기초 (+ 이름 검색하기) (0) | 2025.09.08 |
| C++ 기초_디자인패턴_싱글톤 패턴 (3) | 2025.09.05 |
| 인터페이스와 추상클래스의 차이점 (0) | 2025.09.02 |
| C++언어 기초 - 8. SOLID 원칙 (2) | 2025.09.01 |