C++ 공부

OOP_Project01_BankAccount(은행계좌관리프로그램 만들기)

Client Side 2025. 9. 5. 20:38

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;
}