银行ATM系统技术选型分析:

  1. 面向对象编程:每个用户账户都是一个对象:所以需要设计账户类Account用于创建账户对象封装账户信息。
  2. 程序流程控制:需要结合分支、循环、跳转关键字等相关操作控制程序的业务逻辑。
  3. 使用集合容器:系统需要提供一个容器用于存储这些账户对象的信息,我们选ArrayList集合。
  4. 使用常见API:登录信息的内容比较,业务数据的分析,处理等都需要用到String等常用API来解决。

系统准备、首页设计

  • 系统准备内容分析:

    1. 每个用户的账户信息都是一个对象,需要提供账户类。
    2. 需要准备一个容器,用于存储系统全部账户对象信息。
    3. 首页只需要包含:登录和注册2个功能。
  • 实现步骤:

    1. 定义账户类,用于后期创建账户对象封装用户的账户信息。
    2. 账户类中的信息至少需要包含(卡号、姓名、密码、余额、取现额度)
    3. 需要准备一个ArrayList的集合,用于存储系统用户的账户对象。
    4. 需要展示欢迎页包含2个功能:开户功能、登录账户。
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
package atm;

public class Account {
private String id; //卡号
private String password; //密码
private String name; //姓名
private double money; //余额
private double limitMoney; //取现限额

public Account() {
}

public Account(String id, String password, String name, double money, double limitMoney) {
this.id = id;
this.password = password;
this.name = name;
this.money = money;
this.limitMoney = limitMoney;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getMoney() {
return money;
}

public void setMoney(double money) {
this.money = money;
}

public double getLimitMoney() {
return limitMoney;
}

public void setLimitMoney(double limitMoney) {
this.limitMoney = limitMoney;
}
}

用户开户功能实现

  • 分析:开户功能其实就是就是往系统的集合容器中存入一个新的账户对象的信息。

  • 开户功能实现步骤

    1. 开户应该定义成一个方法,并传入账户集合:
      public static void register(ArrayList<Account> accounts) {…}
    2. 创建一个Account账户类的对象用于封装账户信息(姓名、密码、卡号)
    3. 键盘录入姓名、密码、确认密码(需保证两次密码一致)
    4. 生成账户卡号,卡号必须由系统自动生成8位数字(必须保证卡号的唯一)
    5. 把Account账户对象存入到集合accounts中去。
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
import java.util.Scanner;

/**
* ATM
*/
public class AccountTest {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();

boolean flag = true;
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("╔════欢迎使用ATM系统════╗");
System.out.println(" 1.登录系统 ");
System.out.println(" 2.注册开户 ");
System.out.println(" 3.退出系统 ");
System.out.println("╚═════════════════════╝");
System.out.print("请输入命令(1 or 2 or 3):");

int option = scanner.nextInt();

switch (option) {
case 1:
//登录系统
break;
case 2:
//注册开户
register(accounts);
break;
case 3:
//退出系统
break;
default:
System.out.println("输入指令错误!");
}
}
// System.out.println("感谢您的使用!");
}

// 注册
private static Account register(ArrayList<Account> accounts){
Account account = new Account();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名:");
String name = scanner.next();

// 设置密码
String password = "";
String confirmPassword = "";
while (true) {
System.out.print("请设置您的密码:");
password = scanner.next();
System.out.print("请确认您的密码:");
confirmPassword = scanner.next();
if (password.equals(confirmPassword)) {
break;
}
System.out.println("两次密码不一致,请重新设置您的密码:");
}

// 设置取现额度
System.out.print("请输入您的取现额度:");
double limitMoney = scanner.nextDouble();

// 随机生成8位数账号
Random random = new Random();
String id = "";
for (int i = 0; i < 8; i++) {
int num = random.nextInt(10);
if (i==0){
//第一位
while (num == 0) {
num = random.nextInt(10);
}
}
id += num;
}

// 把当前账户放进集合
account.setId(id);
account.setPassword(password);
account.setName(name);
account.setLimitMoney(limitMoney);
accounts.add(account);
System.out.println("恭喜您开户成功,您的卡号为" + id);

return account;
}
}

用户登录功能实现

  • 分析:
    1. 登录功能应该定义成一个方法,并传入账户集合:
      public static void login(ArrayList<Account> accounts) {…}
    2. 让用户输入卡号,根据卡号去账户集合中查询账户对象。
    3. 如果没有找到账户对象,说明登录卡号不存在,提示继续输入卡号。
    4. 如果找到了账户对象,说明卡号存在,继续输入密码。
    5. 如果密码不正确,提示继续输入密码
    6. 如果密码也正确,登陆成功!!
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
package atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
* ATM
*/
public class AccountTest {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();

boolean flag = true;
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("╔════欢迎使用ATM系统════╗");
System.out.println(" 1.登录系统 ");
System.out.println(" 2.注册开户 ");
System.out.println(" 3.退出系统 ");
System.out.println("╚═════════════════════╝");
System.out.print("请输入命令(1 or 2 or 3):");

int option = scanner.nextInt();

switch (option) {
case 0:
// 管理员查看用户列表
show(accounts);
break;
case 1:
//登录系统
login(accounts);
break;
case 2:
//注册开户
register(accounts);
break;
case 3:
//退出系统
break;
default:
System.out.println("输入指令错误!");
}
}
// System.out.println("感谢您的使用!");
}

// 管理员查看用户列表
private static void show(ArrayList<Account> accounts) {
System.out.println("═════════════用户查看界面═════════════");
if(accounts.size() == 0) {
System.out.println("没有一个人开户,什么破银行!");
} else {
System.out.println("共有" + accounts.size() + "位客户,分别为:");
for (Account account : accounts) {
System.out.println(account);
}
}

System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

// 注册
private static Account register(ArrayList<Account> accounts){
System.out.println("═════════════注册界面═════════════");
Account account = new Account();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名:");
String name = scanner.next();

// 设置密码
String password = "";
String confirmPassword = "";
while (true) {
System.out.print("请设置您的密码:");
password = scanner.next();
System.out.print("请确认您的密码:");
confirmPassword = scanner.next();
if (password.equals(confirmPassword)) {
break;
}
System.out.println("两次密码不一致,请重新设置您的密码:");
}

// 设置取现额度
System.out.print("请输入您的取现额度:");
double limitMoney = scanner.nextDouble();

// 随机生成8位数账号
Random random = new Random();
String id = "";
for (int i = 0; i < 8; i++) {
int num = random.nextInt(10);
if (i==0){
//第一位
while (num == 0) {
num = random.nextInt(10);
}
}
id += num;
}

// 把当前账户放进集合
account.setId(id);
account.setPassword(password);
account.setName(name);
account.setLimitMoney(limitMoney);
accounts.add(account);
System.out.println("恭喜您开户成功,您的卡号为" + id);
System.out.println();
System.out.println("-------------------------------------");
System.out.println();

return account;
}

// 登录
private static Account login(ArrayList<Account> accounts) {
System.out.println("═════════════登录界面═════════════");
Scanner scanner = new Scanner(System.in);
Account accountSearchResult = null;

boolean flag = true;
while (flag) {
System.out.print("请输入您的卡号:");
String idInput = scanner.next();
//判断卡号是否存在
for (Account account : accounts) {
if (account.getId().equals(idInput)) {
accountSearchResult = account;
break;
}
}
//没找到
if (accountSearchResult == null) {
System.out.println("账号不存在!");
continue; //进入下一次循环
}

//判断密码(3次)
int num = 3;
for (int i = 0; i < 3; i++) {
System.out.print("请输入密码:");
String passwordInput = scanner.next();
//密码匹配
if (passwordInput.equals(accountSearchResult.getPassword())) {
System.out.println("登陆成功,您的账户信息如下:");
System.out.println("卡号:" + accountSearchResult.getId());
System.out.println("姓名:" + accountSearchResult.getName());
System.out.println("余额:" + accountSearchResult.getMoney());
System.out.println("取现限额:" + accountSearchResult.getLimitMoney());
flag = false;
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return accountSearchResult;
} else {
num--;

if (num == 0) {
System.out.println("密码输错次数达到上限!");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return null;
}
System.out.print("密码输入错误,您还有" + num + "次机会,");
}
}
}
return accountSearchResult;
}
}

用户操作页设计、查询账户、退出账户功能实现

  • 户操作页设计、查询账户、退出账户功能分析
    1. 用户登录成功后,需要进入用户操作页。
    2. 查询就是直接展示当前登录成功的账户对象的信息。
    3. 退出账户是需要回到首页的。
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
* ATM
*/
public class AccountTest {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();
accounts.add(new Account("88888888", "123456", "董事长", 2080000, 100000));

boolean flag = true;
Scanner scanner = new Scanner(System.in);

while (flag) {
System.out.println("╔════欢迎使用ATM系统════╗");
System.out.println(" 1.登录系统 ");
System.out.println(" 2.注册开户 ");
System.out.println(" 3.退出系统 ");
System.out.println("╚═════════════════════╝");
System.out.print("请输入命令(1 or 2 or 3):");

int option = scanner.nextInt();

switch (option) {
case 0:
//管理员查看用户列表
show(accounts);
break;
case 1:
//登录系统
Account account = login(accounts);
if(account != null) {
index(account, accounts); //登陆成功,进入主页
}
break;
case 2:
//注册开户
register(accounts);
break;
case 3:
//退出系统
flag = false;
break;
default:
System.out.println("输入指令错误!");
}
}
System.out.println("感谢您的使用,祝您事业飞黄腾达!");
}

//展示主页
private static void index(Account account, ArrayList<Account> accounts) {
System.out.println("欢迎您," + account.getName() + "!您的卡号为:" + account.getId());
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("╔════您已登录ATM系统════╗");
System.out.println(" 1.账户查询 ");
System.out.println(" 2.立即存款 ");
System.out.println(" 3.立即取款 ");
System.out.println(" 4.立即转账 ");
System.out.println(" 5.修改密码 ");
System.out.println(" 6.退出账户 ");
System.out.println(" 7.注销账户 ");
System.out.println("╚═════════════════════╝");
System.out.print("请输入命令:");
int option = scanner.nextInt();

switch (option) {
case 0:
//管理员查看用户列表
show(accounts);
break;
case 1:
//账户查询
accountInquiry(account);
break;
case 2:
//立即存款
break;
case 3:
//立即取款
break;
case 4:
//立即转账
break;
case 5:
//修改密码
break;
case 6:
//退出账户
flag =false;
break;
case 7:
//注销账户
break;
default:
System.out.println("输入指令错误!");
}
}
System.out.println("账号已安全退出");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

//账户查询
private static void accountInquiry(Account account) {
System.out.println("══════════账户查询══════════");
System.out.println("姓名:" + account.getName());
System.out.println("卡号:" + account.getId());
System.out.println("余额:" + account.getMoney());
System.out.println("取现限额:" + account.getLimitMoney());
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

//登录
private static Account login(ArrayList<Account> accounts) {
System.out.println("═════════════登录界面═════════════");
Scanner scanner = new Scanner(System.in);
Account accountSearchResult = null;

boolean flag = true;
while (flag) {
System.out.print("请输入您的卡号:");
String idInput = scanner.next();
//判断卡号是否存在
for (Account account : accounts) {
if (account.getId().equals(idInput)) {
accountSearchResult = account;
break;
}
}
//没找到
if (accountSearchResult == null) {
System.out.println("账号不存在!");
continue; //进入下一次循环
}

//判断密码(3次)
int num = 3;
for (int i = 0; i < 3; i++) {
System.out.print("请输入密码:");
String passwordInput = scanner.next();
//密码匹配
if (passwordInput.equals(accountSearchResult.getPassword())) {
System.out.println("登陆成功!");
flag = false;
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return accountSearchResult;
} else {
num--;

if (num == 0) {
System.out.println("密码输错次数达到上限!");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return null;
}
System.out.print("密码输入错误,您还有" + num + "次机会,");
}
}
}
return accountSearchResult;
}

//注册
private static Account register(ArrayList<Account> accounts){
System.out.println("═════════════注册界面═════════════");
Account account = new Account();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名:");
String name = scanner.next();

// 设置密码
String password = "";
String confirmPassword = "";
while (true) {
System.out.print("请设置您的密码:");
password = scanner.next();
System.out.print("请确认您的密码:");
confirmPassword = scanner.next();
if (password.equals(confirmPassword)) {
break;
}
System.out.println("两次密码不一致,请重新设置您的密码:");
}

// 设置取现额度
System.out.print("请输入您的取现额度:");
double limitMoney = scanner.nextDouble();

// 随机生成8位数账号
Random random = new Random();
String id = "";
for (int i = 0; i < 8; i++) {
int num = random.nextInt(10);
if (i==0){
//第一位
while (num == 0) {
num = random.nextInt(10);
}
}
id += num;
}

// 把当前账户放进集合
account.setId(id);
account.setPassword(password);
account.setName(name);
account.setLimitMoney(limitMoney);
accounts.add(account);
System.out.println("恭喜您开户成功,您的卡号为" + id);
System.out.println();
System.out.println("-------------------------------------");
System.out.println();

return account;
}

//管理员查看用户列表
private static void show(ArrayList<Account> accounts) {
System.out.println("═════════════管理员后台界面═════════════");
if(accounts.size() <= 1) {
System.out.println("老板,除了您没有一个人开户,咱这什么破银行!");
for (Account account : accounts) {
System.out.println(account);
}
} else {
System.out.println("亲爱的老板,本银行加上您共有" + accounts.size() + "位客户,分别为:");
for (Account account : accounts) {
System.out.println(account);
}
}

System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}
}

用户存款功能实现

  • 存款分析
    1. 存款就是拿到当前账户对象。
    2. 然后让用户输入存款的金额。
    3. 调用账户对象的setMoney方法将账户余额修改成存钱后的余额。
    4. 存钱后需要查询一下账户信息,确认是否存钱成功了!
1
2
3
4
5
6
7
8
9
10
11
//存款
private static void deposit(Account account) {
System.out.println("══════════立即存款══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入存款金额:");
double money = scanner.nextDouble();

account.setMoney(account.getMoney() + money);
System.out.println("存款成功!目前您的账户信息如下");
accountInquiry(account);
}

用户取款功能实现

  • 取款分析
    1. 取款需要先判断账户是否有钱。
    2. 有钱则拿到自己账户对象。
    3. 然后让用户输入取款金额
    4. 判断取款金额是否超过了当次限额,以及余额是否足够
    5. 满足要求则调用账户对象的setMoney方法完成金额的修改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//取款
private static void withdraw(Account account) {
System.out.println("══════════立即存款══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入取款金额:");
double money = scanner.nextDouble();
if (money > account.getLimitMoney()) {
System.out.println("取款失败,超出您设置的取款限额。");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
} else {
account.setMoney(account.getMoney() - money);
System.out.println("取款成功!目前您的账户信息如下");
accountInquiry(account);
}
}

用户转账功能实现

  • 分析
    1. 转账功能需要判断系统中是否有2个账户对象及以上。
    2. 同时还要判断自己账户是否有钱。
    3. 接下来需要输入对方卡号,判断对方账户是否存在。
    4. 对方账户存在还需要认证对方户主的姓氏。
    5. 满足要求则可以把自己账户对象的金额修改到对方账户对象中去。
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
* ATM
*/
public class AccountTest {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();
accounts.add(new Account("88888888", "123456", "董事长", 2080000, 100000));
accounts.add(new Account("22222222", "123", "总经理", 5000, 100));

boolean flag = true;
Scanner scanner = new Scanner(System.in);

while (flag) {
System.out.println("╔════欢迎使用ATM系统════╗");
System.out.println(" 1.登录系统 ");
System.out.println(" 2.注册开户 ");
System.out.println(" 3.退出系统 ");
System.out.println("╚═════════════════════╝");
System.out.print("请输入命令(1 or 2 or 3):");

int option = scanner.nextInt();

switch (option) {
case 0:
//管理员查看用户列表
show(accounts);
break;
case 1:
//登录系统
Account account = login(accounts);
if(account != null) {
index(account, accounts); //登陆成功,进入主页
}
break;
case 2:
//注册开户
register(accounts);
break;
case 3:
//退出系统
flag = false;
break;
default:
System.out.println("输入指令错误!");
}
}
System.out.println("感谢您的使用,祝您事业飞黄腾达!");
}

//展示主页
private static void index(Account account, ArrayList<Account> accounts) {
System.out.println("欢迎您," + account.getName() + "!您的卡号为:" + account.getId());
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("╔════您已登录ATM系统════╗");
System.out.println(" 1.账户查询 ");
System.out.println(" 2.立即存款 ");
System.out.println(" 3.立即取款 ");
System.out.println(" 4.立即转账 ");
System.out.println(" 5.修改密码 ");
System.out.println(" 6.退出账户 ");
System.out.println(" 7.注销账户 ");
System.out.println("╚═════════════════════╝");
System.out.print("请选择您的操作命令:");
int option = scanner.nextInt();

switch (option) {
case 0:
//管理员查看用户列表
show(accounts);
break;
case 1:
//账户查询
accountInquiry(account);
break;
case 2:
//立即存款
deposit(account);
break;
case 3:
//立即取款
withdraw(account);
break;
case 4:
//立即转账
transfer(account, accounts);
break;
case 5:
//修改密码
break;
case 6:
//退出账户
flag =false;
break;
case 7:
//注销账户
break;
default:
System.out.println("输入指令错误!");
}
}
System.out.println("账号已安全退出");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

//转账
private static void transfer(Account account, ArrayList<Account> accounts) {
//账户数<2无法转账
if (accounts.size() < 2) {
System.out.println("老板,在咱银行开户的就你一个人,没法转...");
return;
}

System.out.println("══════════立即转账══════════");
//输入对方卡号
Scanner scanner = new Scanner(System.in);
System.out.print("请输入对方卡号:");
String id = scanner.next();

Account accountSearchResult = searchAccountById(id, accounts);
while (true) {
//判断账号是否存在
if (accountSearchResult == null){
System.out.print("卡号不存在,请重新输入:");
id = scanner.next();
accountSearchResult = searchAccountById(id, accounts);
} else {
break;
}
}

//输入转账金额
System.out.print("请输入转账金额:");
double money = scanner.nextDouble();

while (money > account.getMoney()) {
System.out.print("您的余额不足,请重新输入:");
money = scanner.nextDouble();
}

//转账操作
Account collectionAccount = searchAccountById(id, accounts); //收款账户
//自己的账户扣钱
account.setMoney(account.getMoney() - money);
//对方的账户加钱
collectionAccount.setMoney(collectionAccount.getMoney() + money);
System.out.println("转账"+money+"元成功!目前您的账户信息如下");
accountInquiry(account);
}

//通过卡号ID查询账户
private static Account searchAccountById(String id, ArrayList<Account> accounts) {
Account collectionAccount = null;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getId().equals(id)) {
collectionAccount = accounts.get(i);
//System.out.println(collectionAccount); //打印账户
return collectionAccount;
}
}
return collectionAccount;
}

//取款
private static void withdraw(Account account) {
System.out.println("您将进行取款操作,以下是您的余额信息:");
accountInquiry(account);

System.out.println("══════════立即取款══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入取款金额:");
double money = scanner.nextDouble();
//余额不足
while (money > account.getMoney()) {
System.out.print("余额不足,请重新输入:");
money = scanner.nextDouble();
}
//超出限额
while (money > account.getLimitMoney()) {
System.out.print("取款失败,超过您设置的取款限额,请重新输入:");
money = scanner.nextDouble();
}
//取款成功
account.setMoney(account.getMoney() - money);
System.out.println("取款成功!目前您的账户信息如下");
accountInquiry(account);
}

//存款
private static void deposit(Account account) {
System.out.println("══════════立即存款══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入存款金额:");
double money = scanner.nextDouble();

account.setMoney(account.getMoney() + money);
System.out.println("存款成功!目前您的账户信息如下");
accountInquiry(account);
}

//账户查询
private static void accountInquiry(Account account) {
System.out.println("══════════账户查询══════════");
System.out.println("姓名:" + account.getName());
System.out.println("卡号:" + account.getId());
System.out.println("余额:" + account.getMoney());
System.out.println("取现限额:" + account.getLimitMoney());
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

//登录
private static Account login(ArrayList<Account> accounts) {
System.out.println("═════════════登录界面═════════════");
Scanner scanner = new Scanner(System.in);
Account accountSearchResult = null;

boolean flag = true;
while (flag) {
System.out.print("请输入您的卡号:");
String idInput = scanner.next();
//判断卡号是否存在
for (Account account : accounts) {
if (account.getId().equals(idInput)) {
accountSearchResult = account;
break;
}
}
//没找到
if (accountSearchResult == null) {
System.out.println("账号不存在!");
continue; //进入下一次循环
}

//判断密码(3次)
int num = 3;
for (int i = 0; i < 3; i++) {
System.out.print("请输入密码:");
String passwordInput = scanner.next();
//密码匹配
if (passwordInput.equals(accountSearchResult.getPassword())) {
System.out.println("登陆成功!");
flag = false;
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return accountSearchResult;
} else {
num--;

if (num == 0) {
System.out.println("密码输错次数达到上限!");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return null;
}
System.out.print("密码输入错误,您还有" + num + "次机会,");
}
}
}
return accountSearchResult;
}

//注册
private static Account register(ArrayList<Account> accounts){
System.out.println("═════════════注册界面═════════════");
Account account = new Account();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名:");
String name = scanner.next();

// 设置密码
String password = "";
String confirmPassword = "";
while (true) {
System.out.print("请设置您的密码:");
password = scanner.next();
System.out.print("请确认您的密码:");
confirmPassword = scanner.next();
if (password.equals(confirmPassword)) {
break;
}
System.out.println("两次密码不一致,请重新设置您的密码:");
}

// 设置取现额度
System.out.print("请输入您的取现额度:");
double limitMoney = scanner.nextDouble();

// 随机生成8位数账号
Random random = new Random();
String id = "";
for (int i = 0; i < 8; i++) {
int num = random.nextInt(10);
if (i==0){
//第一位
while (num == 0) {
num = random.nextInt(10);
}
}
id += num;
}

// 把当前账户放进集合
account.setId(id);
account.setPassword(password);
account.setName(name);
account.setLimitMoney(limitMoney);
accounts.add(account);
System.out.println("恭喜您开户成功,您的卡号为" + id);
System.out.println();
System.out.println("-------------------------------------");
System.out.println();

return account;
}

//管理员查看用户列表
private static void show(ArrayList<Account> accounts) {
System.out.println("═════════════管理员后台界面═════════════");
if(accounts.size() <= 1) {
System.out.println("老板,除了您没有一个人开户,咱这什么破银行!");
for (Account account : accounts) {
System.out.println(account);
}
} else {
System.out.println("亲爱的老板,本银行加上您共有" + accounts.size() + "位客户,分别为:");
for (Account account : accounts) {
System.out.println(account);
}
}

System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}
}

用户密码修改、销户功能实现

  • 分析
    1. 修改密码就是把当前对象的密码属性使用set方法进行更新。
    2. 销户是从集合对象中删除当前对象,并回到首页。
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package atm;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

/**
* ATM
*/
public class AccountTest {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();
accounts.add(new Account("88888888", "123456", "董事长", 2080000, 100000));
accounts.add(new Account("22222222", "123", "大堂经理", 5000, 100));

boolean flag = true;
Scanner scanner = new Scanner(System.in);

while (flag) {
System.out.println("╔════欢迎使用ATM系统════╗");
System.out.println(" 1.登录系统 ");
System.out.println(" 2.注册开户 ");
System.out.println(" 3.退出系统 ");
System.out.println("╚═════════════════════╝");
System.out.print("请输入命令(1 or 2 or 3):");

int option = scanner.nextInt();

switch (option) {
case 0:
//管理员查看用户列表
show(accounts);
break;
case 1:
//登录系统
Account account = login(accounts);
if(account != null) {
index(account, accounts); //登陆成功,进入主页
}
break;
case 2:
//注册开户
register(accounts);
break;
case 3:
//退出系统
flag = false;
break;
default:
System.out.println("输入指令错误!");
}
}
System.out.println("感谢您的使用,祝您事业飞黄腾达!");
}

//展示主页
private static void index(Account account, ArrayList<Account> accounts) {
System.out.println("欢迎您," + account.getName() + "!您的卡号为:" + account.getId());
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("╔════您已登录ATM系统════╗");
System.out.println(" 1.账户查询 ");
System.out.println(" 2.立即存款 ");
System.out.println(" 3.立即取款 ");
System.out.println(" 4.立即转账 ");
System.out.println(" 5.修改密码 ");
System.out.println(" 6.退出账户 ");
System.out.println(" 7.注销账户 ");
System.out.println("╚═════════════════════╝");
System.out.print("请选择您的操作命令:");
int option = scanner.nextInt();

switch (option) {
case 0:
//管理员查看用户列表
show(accounts);
break;
case 1:
//账户查询
accountInquiry(account);
break;
case 2:
//立即存款
deposit(account);
break;
case 3:
//立即取款
withdraw(account);
break;
case 4:
//立即转账
transfer(account, accounts);
break;
case 5:
//修改密码
changePassword(account);
break;
case 6:
//退出账户
flag = false;
break;
case 7:
//注销账户
Boolean res = unsubscribe(account, accounts);
if (res == true) {
flag = false;
}
break;
default:
System.out.println("输入指令错误!");
}
}
System.out.println("账号已安全退出");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

//注销账户
private static Boolean unsubscribe(Account account, ArrayList<Account> accounts) {
System.out.println("══════════注销账户══════════");
Boolean flag = false;
Scanner scanner = new Scanner(System.in);
System.out.print("您确认要注销吗?(yes/no):");
String reply = scanner.next();
if (reply.equals("yes")) {
accounts.remove(account);
System.out.println("账号已注销,即将返回首页");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
flag = true;
} else {
System.out.println("已取消注销");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}
return flag;
}

//修改密码
private static void changePassword(Account account) {
System.out.println("══════════修改密码══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入旧密码:");
String oldPasswordInput = scanner.next();
//验证密码
while (!account.getPassword().equals(oldPasswordInput)) {
System.out.print("密码不正确,请重新输入:");
oldPasswordInput = scanner.next();
}

//修改密码
String newPassword = "";
String confirmPassword = "";
while (true) {
System.out.print("请设置您的新密码:");
newPassword = scanner.next();
System.out.print("请确认您的新密码:");
confirmPassword = scanner.next();
if (newPassword.equals(confirmPassword)) {
break;
}
System.out.println("两次密码不一致,请重新设置您的密码!");
}

account.setPassword(newPassword);
System.out.println("密码修改成功!");
}

//转账
private static void transfer(Account account, ArrayList<Account> accounts) {
//账户数<2无法转账
if (accounts.size() < 2) {
System.out.println("老板,在咱银行开户的就你一个人,没法转...");
return;
}

System.out.println("══════════立即转账══════════");
//输入对方卡号
Scanner scanner = new Scanner(System.in);
System.out.print("请输入对方卡号:");
String id = scanner.next();

Account accountSearchResult = searchAccountById(id, accounts);
while (true) {
//判断账号是否存在
if (accountSearchResult == null){
System.out.print("卡号不存在,请重新输入:");
id = scanner.next();
accountSearchResult = searchAccountById(id, accounts);
} else {
break;
}
}

//输入转账金额
System.out.print("请输入转账金额:");
double money = scanner.nextDouble();

while (money > account.getMoney()) {
System.out.print("您的余额不足,请重新输入:");
money = scanner.nextDouble();
}

//转账操作
Account collectionAccount = searchAccountById(id, accounts); //收款账户
//自己的账户扣钱
account.setMoney(account.getMoney() - money);
//对方的账户加钱
collectionAccount.setMoney(collectionAccount.getMoney() + money);
System.out.println("转账"+money+"元成功!目前您的账户信息如下");
accountInquiry(account);
}

//通过卡号ID查询账户
private static Account searchAccountById(String id, ArrayList<Account> accounts) {
Account collectionAccount = null;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getId().equals(id)) {
collectionAccount = accounts.get(i);
//System.out.println(collectionAccount); //打印账户
return collectionAccount;
}
}
return collectionAccount;
}

//取款
private static void withdraw(Account account) {
System.out.println("您将进行取款操作,以下是您的余额信息:");
accountInquiry(account);

System.out.println("══════════立即取款══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入取款金额:");
double money = scanner.nextDouble();
//余额不足
while (money > account.getMoney()) {
System.out.print("余额不足,请重新输入:");
money = scanner.nextDouble();
}
//超出限额
while (money > account.getLimitMoney()) {
System.out.print("取款失败,超过您设置的取款限额,请重新输入:");
money = scanner.nextDouble();
}
//取款成功
account.setMoney(account.getMoney() - money);
System.out.println("取款成功!目前您的账户信息如下");
accountInquiry(account);
}

//存款
private static void deposit(Account account) {
System.out.println("══════════立即存款══════════");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入存款金额:");
double money = scanner.nextDouble();

account.setMoney(account.getMoney() + money);
System.out.println("存款成功!目前您的账户信息如下");
accountInquiry(account);
}

//账户查询
private static void accountInquiry(Account account) {
System.out.println("══════════账户查询══════════");
System.out.println("姓名:" + account.getName());
System.out.println("卡号:" + account.getId());
System.out.println("余额:" + account.getMoney());
System.out.println("取现限额:" + account.getLimitMoney());
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}

//登录
private static Account login(ArrayList<Account> accounts) {
System.out.println("═════════════登录界面═════════════");
Scanner scanner = new Scanner(System.in);
Account accountSearchResult = null;

boolean flag = true;
while (flag) {
System.out.print("请输入您的卡号:");
String idInput = scanner.next();
//判断卡号是否存在
for (Account account : accounts) {
if (account.getId().equals(idInput)) {
accountSearchResult = account;
break;
}
}
//没找到
if (accountSearchResult == null) {
System.out.println("账号不存在!");
continue; //进入下一次循环
}

//判断密码(3次)
int num = 3;
for (int i = 0; i < 3; i++) {
System.out.print("请输入密码:");
String passwordInput = scanner.next();
//密码匹配
if (passwordInput.equals(accountSearchResult.getPassword())) {
System.out.println("登陆成功!");
flag = false;
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return accountSearchResult;
} else {
num--;

if (num == 0) {
System.out.println("密码输错次数达到上限!");
System.out.println();
System.out.println("-------------------------------------");
System.out.println();
return null;
}
System.out.print("密码输入错误,您还有" + num + "次机会,");
}
}
}
return accountSearchResult;
}

//注册
private static Account register(ArrayList<Account> accounts){
System.out.println("═════════════注册界面═════════════");
Account account = new Account();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名:");
String name = scanner.next();

// 设置密码
String password = "";
String confirmPassword = "";
while (true) {
System.out.print("请设置您的密码:");
password = scanner.next();
System.out.print("请确认您的密码:");
confirmPassword = scanner.next();
if (password.equals(confirmPassword)) {
break;
}
System.out.println("两次密码不一致,请重新设置您的密码!");
}

// 设置取现额度
System.out.print("请输入您的取现额度:");
double limitMoney = scanner.nextDouble();

// 随机生成8位数账号
Random random = new Random();
String id = "";
for (int i = 0; i < 8; i++) {
int num = random.nextInt(10);
if (i==0){
//第一位
while (num == 0) {
num = random.nextInt(10);
}
}
id += num;
}

// 把当前账户放进集合
account.setId(id);
account.setPassword(password);
account.setName(name);
account.setLimitMoney(limitMoney);
accounts.add(account);
System.out.println("恭喜您开户成功,您的卡号为" + id);
System.out.println();
System.out.println("-------------------------------------");
System.out.println();

return account;
}

//管理员查看用户列表
private static void show(ArrayList<Account> accounts) {
System.out.println("═════════════管理员后台界面═════════════");
if(accounts.size() <= 1) {
System.out.println("老板,除了您没有一个人开户,咱这什么破银行!");
for (Account account : accounts) {
System.out.println(account);
}
} else {
System.out.println("亲爱的老板,本银行加上您共有" + accounts.size() + "位客户,分别为:");
for (Account account : accounts) {
System.out.println(account);
}
}

System.out.println();
System.out.println("-------------------------------------");
System.out.println();
}
}