C++

열혈 C++ 프로그래밍 OOP 프로젝트 02

나른한여우 2024. 3. 20. 00:50

OOP 프로젝트 02에서는 class를 추가해 private, public, 정보은닉과 캡슐화에 대해 추가함

 

1. 구조체 -> class로 변경, 생성자 추가, 멤버 이니셜라이저 추가

2. 객체 배열에서 객체 포인터 배열로 변경

#include <iostream>
#include <cstring>
using namespace std;

enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };

class Account
{
private:
	int accId;
	int balance;
	char* cusName;
public:
	Account(int id, int money, char* name)
		:accId(id), balance(money)
	{
		cusName = new char[strlen(name) + 1];
		strcpy_s(cusName, strlen(name) + 1, name);
	}
	int GetAccId()
	{
		return accId;
	}
	void Deposit(int money)
	{
		balance += money;
	}
	int WithDraw(int money)
	{
		if (balance < money)
		{
			return 0;
		}
		balance -= money;
		return money;
	}
	void ShowAccount()
	{
		cout << "계좌번호: " << accId << endl;
		cout << "고객이름: " << cusName << endl;
		cout << "잔 액: " << balance << endl<<endl;
	}	
};		

//typedef struct {
//	int accId;
//	int balance;
//	char cusName[20];
//} Account;

//Account userList[100];
Account* userList[100];
int userIndex=0;

void ShowMenu();
void MakeAccount();
void ShowAllAccount();
void DepositMoney();
void WithdrawMoney();

int main(void) 
{
	int choice;

	while (1) 
	{
		ShowMenu();
		cout << "선택: ";
		cin >> choice;
		cout << endl;

		switch(choice)
		{
			case MAKE:
				MakeAccount();
				break;
			case DEPOSIT:
				DepositMoney();
				break;
			case WITHDRAW:
				WithdrawMoney();
				break;
			case INQUIRE:
				ShowAllAccount();
				break;
			case EXIT:
				for (int i = 0; i < userIndex; i++)
					delete userList[i];
				return 0;
		}
	}

	return 0;
}

void ShowMenu() {
	cout << "----- Menu -----" << endl;
	cout << "1. 계좌개설" << endl;
	cout << "2. 입 금" << endl;
	cout << "3. 출 금" << endl;
	cout << "4. 계좌정보 전체 출력" << endl;
	cout << "5. 프로그램 종료" << endl;
}

void MakeAccount() {
	int id;
	int money =0;
	char userName[30];

	cout << "[계좌개설]" << endl;
	cout << "계좌번호: ";
	cin >> id;
	cout << "고객이름: ";
	cin >> userName;
	userList[userIndex++] = new Account(id, money, userName);
	cout << endl;
}

void DepositMoney() {
	int tempId;
	int tempBalance;

	cout << "[입 금]" << endl;
	cout << "입금할 계좌번호: ";
	cin >> tempId;
	for (int i = 0; i < userIndex; i++) 
	{
		if (tempId == userList[i]->GetAccId()) 
		{
			cout << "입금할 금액: ";
			cin >> tempBalance;
			userList[i]->Deposit(tempBalance);
			cout << endl;
			return;
		}
		cout << userList[0]->GetAccId() << endl;
		cout << tempId << endl;
	}
	cout << "유효하지 않은 ID 입니다." << endl << endl;
}

void WithdrawMoney() {
	int tempId;
	int tempBalance;

	cout << "[출 금]" << endl;
	cout << "출금할 계좌번호: ";
	cin >> tempId;
	for (int i = 0; i < userIndex; i++) 
	{
		if (tempId == userList[i]->GetAccId()) 
		{
			cout << "출금할 금액: ";
			cin >> tempBalance;
			if (userList[i]->WithDraw(tempBalance)==0) 
			{
				cout << "잔액 부족" << endl << endl;
				return;
			}
			cout << "출금완료" << endl;
			cout << endl;
			return;
		}
	}
	cout << "유효하지 않은 ID 입니다." << endl << endl;
}

void ShowAllAccount() {
	for (int i = 0; i < userIndex; i++) 
	{
		userList[i]->ShowAccount();
	}
}

'C++' 카테고리의 다른 글

열혈 C++ 프로그래밍 OOP 프로젝트 03  (0) 2024.03.20
열혈 C++ 프로그래밍 OOP 프로젝트 01  (0) 2024.03.20
스택 프레임  (0) 2023.07.06
C++ 열거형  (0) 2023.07.03
C++ 유의사항 1)  (0) 2023.06.21