C , C++, C#

c++ 기본 : 계좌관리 프로그램

vhxpffltm 2019. 2. 18. 21:57
반응형

이번 글은 C++의 클래스와 배운것들을 종합하여 이해하기 위해 코드를 보며 계좌관리 프로그램이 어떻게 돌아가는지 확인해보자


이 코드의 출처는 https://koocci.github.io 이다. 


약간의 변수이름 변경과 주석을 통해 우리가 클래스를 통해 어떻게 프로그램이 흘러가고 생성자 및 소멸자 그리고 데이터의 이동과 기능이 동작하는 방식을 이해하는것에 중점을 두면 좋을 것 같다.


생성자와 소멸자 맴버함수에 출력문을 만들어 확인한다면 어떻게 동작이 흘러가는지 알 수 있는 좋은 방법이다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
using namespace std;
 
const int NAME_LEN = 20;
enum { MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT };
 
class Account {// 정적으로 기능에서 이루어진 동작들의 데이터를 처리함
private:
    int ID;
    int bal;
    char * cusname;
public:
    Account(int id, int val, char * name); // 생성자
    Account(const Account &ref); // 깊은 복사를 위한 (디폴트) 복사생성자
 
    int getid() const;        
    void deposit(int money);
    int withdraw(int money);
    void showinfo();
    ~Account();
};
 
Account::Account(int id, int val, char * name) : ID(id), bal(val) {
    cusname = new char[strlen(name) + 1];
    strcpy(cusname, name);
}
 
Account::Account(const Account &ref) : ID(ref.ID), bal(ref.bal) { //깊은 복사를 진행하는 복사생성자
    cusname = new char[strlen(ref.cusname) + 1];
    strcpy(cusname, ref.cusname);
 
}
 
int Account::getid() const {// private 맴버변수인 고객의 ID를 return
    return ID; 
};
 
void Account::deposit(int money) {// 예금
    bal += money;
}
int Account::withdraw(int money) {// 인출
    if (bal < money) {
        return 0;
    }
    bal = bal - money;
    return money;
}
void Account::showinfo() {
    cout << "ID : " << ID << endl;
    cout << "bal : " << bal << endl;
    cout << "name : " << cusname << endl;
}
Account ::~Account() {// 동적 메모리 헤제
    delete[] cusname;
}
 
class AccountHandler {// 기능을 담당하기 위한 클래스, 모든 기능적 측면을 다룸
private:
    Account* accArr[100]; // Account객체의 저장을 위한 맴버
    int accNum;
public:
    AccountHandler();
    void showmenu() const;
    void makeaccount();
    void depositmo();
    void withdrawmo();
    void showallinfo() const;
    ~AccountHandler();
};
 
void AccountHandler::showmenu() const {
    cout << " ===== MENU ===== " << endl;
    cout << "1. 계좌개설 " << endl;
    cout << "2. 입금 " << endl;
    cout << "3. 출금 " << endl;
    cout << "4. 계좌정보 전체 출력 " << endl;
    cout << "5. 종료 " << endl;
}
 
void AccountHandler::makeaccount() {
    int id;
    char name[NAME_LEN];
    int balance;
 
    cout << "[계좌개설]" << endl;
    cout << "계좌 ID : "cin >> id;
    cout << "이름 : "cin >> name;
    cout << "입금액 : "cin >> balance;
    cout << endl;
 
    accArr[accNum++= new Account(id, balance, name);
    // Account객체를 생성, accArr배열은 그 자료형이 Account객체를 저장하는 자료형
    // 입력값들은 Account객체로 생성되어 입력값이 Account의 생성자로 들어간다.
}
 
void AccountHandler::depositmo() {
    int money;
    int id;
    cout << "[입 금]" << endl;
    cout << "계좌ID : "cin >> id;
    cout << "입금액 : "cin >> money;
 
    for (int i = 0; i < accNum; i++) {
        if (accArr[i]->getid() == id) {// 입금하는 이름이 같을때
            accArr[i]->deposit(money);
            cout << "입금완료" << endl;
            return;
        }
    }
    cout << "유효하지 않은 ID 입니다." << endl << endl;
}
 
void AccountHandler::withdrawmo() {
    int money;
    int id;
    cout << "[출 금]" << endl;
    cout << "계좌ID : "cin >> id;
    cout << "출금액 : "cin >> money;
    for (int i = 0; i < accNum; i++) {
        if (accArr[i]->getid() == id) {// 입금하는 이름이 같을 때
            if (accArr[i]->withdraw(money) == 0) {// 잔고가 없을때
                cout << "잔액부족" << endl << endl;
                return;
            }
 
            accArr[i]->withdraw(money);
            cout << "출금완료" << endl;
            return;
        }
    }
    cout << "유효하지 않은 ID 입니다." << endl << endl;
}
 
AccountHandler::AccountHandler() : accNum(0) {}
 
void AccountHandler::showallinfo() const {
    for (int i = 0; i < accNum; i++) {
        accArr[i]->showinfo();
        cout << endl;
    }
}
 
AccountHandler :: ~AccountHandler() {
    for (int i = 0; i < accNum; i++) {
        delete accArr[i];
    }
}
int main()
{
    AccountHandler man; // 핸들러 객체생성
    int choice;
    while (1) {
        man.showmenu();
        cin >> choice;
        cout << endl;
 
        switch (choice)
        {
        case MAKE:
            man.makeaccount();
            break;
        case DEPOSIT:
            man.depositmo();
break;
        case WITHDRAW:
            man.withdrawmo();
break;
        case INQUIRE:
            man.showallinfo();
break;
        case EXIT:
            return 0;
        default:
            break;
        }
    }
    return 0;
}

cs









반응형