-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM_Controller.cpp
187 lines (166 loc) · 6.17 KB
/
ATM_Controller.cpp
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
179
180
181
182
183
184
185
186
/* **********************************************************************************************
* Simple ATM Controller
* Author: Luxi Huang
*
* Introduction:
* This program is a simple ATM controller to interact with user。
*
* It has 4 process:
* insert card -> enter password -> service(cheking balance, withdraw, and widraw) -> Exit
*
* The correct password of your card is : 1234.
* Please use as the commands on window, and enjoy!
*
* More infomation please view at: https://github.com/luxi-huang/ATM-Controller
***********************************************************************************************/
#include <iostream>
using namespace std;
class AtmController {
private:
int option = 0;
double balance_1 = 1, balance_2 = 1; // balance on accout 1 and balance on account 2;
int real_password = 1234;
int pin = 0;
int flag = 1;
int account = 0;
public:
AtmController() {
cout << "Welcome to ATM Machine!" << endl;
do {
switch (flag) {
// case 1: Insert Card.
case 1:
cout << "Please insert card and press enter key: " << endl;
cin.ignore();
// case 2: Enter password, if all three time in wrong pasword, the program exit.
case 2:
if (!enterPassword()) {
flag = 5;
break;
}
// choose account, if all three times in wrong account, the program exit
case 3:
selectAccount();
if (account != 1 && account != 2) {
flag = 5;
break;
}
// balance checking, deposit, and withdraw service
case 4:
if (account == 1) {
balance_1 = bankService(balance_1);
} else if (account == 2){
balance_2 = bankService(balance_2);
}
if (flag == 3) {
break;
}
// Exit progra1m;
case 5:
cout << "ATM Program exit" << "\n";
flag = 5;
break;
}
} while (flag != 5);
}
bool enterPassword() {
// Enter password, if all three time in wrong pasword, the program exit.
int count = 0;
do {
cout << "Enter you pin and press enter key: " << endl;
cin >> pin;
if (pin != real_password) {
count ++;
cout << "wrong password." << endl;
cout << " " << endl;
if (count >= 3) {
cout << "Too many times in wrong password. ATM program exit!" << endl;
return false;
}
} else {
return true;
}
} while (1);
}
void selectAccount() {
// choose account, if all three times in wrong account, the program exit
int count = 0;
do {
// show account menu
cout << "" << endl;
cout << "**********ACCOUNT MENU *****" << endl;
cout << "1. Checking Account" << endl;
cout << "2. Saving Account" << endl;
cout << "******************************" << endl;
cout << "Please select account and then press enter key: " << endl;
cin >> account;
if (account == 1 || account == 2) {
break;
} else {
count ++;
cout << "account not exist." << endl;
if (count >= 3) {
cout << "Too many time choosing wrong account. ATM program exist." << endl;
break;
}
}
} while (1);
}
// show menu fuction
void showMenu() {
cout << " " << endl;
cout << "******* SERVICE MENU *********" << endl;
cout << "1. Check balance" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdraw" << endl;
cout << "4. Back to last account choosing menu" << endl;
cout << "5. Exit" << endl;
cout << "******************************" << endl;
}
// balance checking, deposit, and withdraw service
double bankService(double balance) {
do {
showMenu();
cout << "Please choose an option of service and then press enter key: ";
cin >> option;
switch(option) {
//case 1: Balance case
case 1:
cout << "Balance: $ " << balance << endl;
break;
// case2: Deposit
case 2:
cout << "Please enter deposit amount and press enter key: ";
double depositAmount;
cin >> depositAmount;
balance += depositAmount;
cout << "Please insert money into cashbin" << endl;
cin.ignore();
cout << "Deposit Success!" << endl;
break;
// case3: Withdraw
case 3:
cout << "Please enter withdraw amount and press enter key: ";
double withdrawAmount;
cin >> withdrawAmount;
if (withdrawAmount <= balance) {
cout << "Please insert money and press enter key: ";
balance -= withdrawAmount;
cout << "Please get money into cash bin" << endl;
cin.ignore();
cout << "Withdraw success!" << endl;
} else {
cout << "Not enough money." << endl;
}
break;
case 4:
flag = 3;
}
} while (option !=5 && option != 4);
return balance;
}
};
int main() {
AtmController atmprogram;
return 1;
}