Java模拟ATM机,供大家参考,具体内容如下
实现登录,查询,转账,取款,修改密码,退出功能。
源码
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
|
package bank; import java.io.*; import java.util.Scanner; //ATM类 public class Atm { private String[] user; //用户全部信息 private double money; //修改钱数 private double userMoney; //用户的钱 private String newPassword; private String userInFo; private int index; private int a = 0 ; private int count = 10 ; public void show(){ //显示界面 index = logIn(); if (index != - 1 ){ working(); } } private String[] newStringUser(String[] str){ count=count+ 10 ; String[] newUser = new String[count]; for ( int i= 0 ;i<a;i++) newUser[i] = str[i]; return newUser; } private void getUser(){ //从文件获取全部用户 String str; String[] strings = new String[count]; File file = new File( "src\\bank\\user" ); FileReader fileReader = null ; BufferedReader bufferedReader = null ; try { fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); while ((str = bufferedReader.readLine())!= null ){ if (a<=count) strings[a++] = str; else strings = newStringUser(strings); } user = new String[a]; for ( int i= 0 ;i<a;i++) user[i] = strings[i]; strings = null ; } catch (Exception e){ e.printStackTrace(); if ((fileReader!= null )&&(bufferedReader!= null )){ try { bufferedReader.close(); fileReader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } private int logIn(){ //用户登录 getUser(); String name,password,user; String[] a; int i = 0 ; int number = - 1 ; Scanner input = new Scanner(System.in); a: while (i< 3 ){ System.out.println( "请输入用户名:" ); name = input.nextLine(); System.out.println( "请输入用户密码:" ); password = input.nextLine(); user = name + "*" + password; for ( int j= 0 ;j< this .user.length;j++){ a = this .user[j].split( "\\*" ); userInFo = a[ 0 ]+ "*" +a[ 1 ]; if (userInFo.equals(user)){ number = j; break a; } } i++; System.out.println( "账户或密码错误请重新输入。。。" ); } if (number!=- 1 ){ System.out.println( "登录成功" ); try { Thread.sleep( 1000 ); } catch (Exception e){ e.printStackTrace(); } } else System.out.println( "您已输入错误三次,卡已被吞!请到银行柜台询问!" ); return number; } private int anthorLogin(){ //查询转账用户是否存在 Scanner input = new Scanner(System.in); String antherUserName; String[] a; int x=- 1 ; System.out.println( "请输入要转账的用户名:" ); antherUserName = input.nextLine(); for ( int i= 0 ;i<user.length;i++){ a = this .user[i].split( "\\*" ); if (a[ 0 ].equals(antherUserName)){ x=i; break ; } } return x; } private void show1(){ System.out.println( "**********************" ); System.out.println( "\t欢迎使用ATM" ); System.out.println( "1,账户余额查询\n2,存钱\n3,取钱\n4,转账\n5,修改用户密码\n6,退出系统\n" ); System.out.println( "**********************" ); } private void changeUser( int x){ //改变用户数组里的数据 String[] str = user[index].split( "\\*" ); if (x== 1 ) user[index] = str[ 0 ]+ "*" +newPassword+ "*" +str[ 2 ]; else user[index] = str[ 0 ]+ "*" +str[ 1 ]+ "*" +userMoney; } private void working(){ //atm办理的业务 String number; setMoney(); do { show1(); System.out.println( "请输入要办理的业务序号:" ); Scanner input = new Scanner(System.in); number = input.nextLine(); switch (number){ case "1" : look(); break ; case "2" : saveMoney(); break ; case "3" : getMoney(); break ; case "4" : giveMoney(); break ; case "5" : changePassword(); break ; case "6" : System.out.println( "欢迎下次光临!" ); write(); break ; default : System.out.println( "您输入有误,请重新输入。。。。" ); } } while (!number.equals( "6" )); } private void setMoney(){ String u = user[index]; userMoney = Double.parseDouble(u.split( "\\*" )[ 2 ]); } private void look(){ //办理查看余额业务 System.out.println( "用户余额为:" +userMoney); try { Thread.sleep( 2000 ); } catch (Exception e){ e.printStackTrace(); } } private void saveMoney(){ //办理存钱业务 money = howMuch( "存钱" ); userMoney = userMoney+money; changeUser( 2 ); look(); if (isContinue()) saveMoney(); } private void getMoney(){ //办理取钱业务 money = howMuch( "取钱" ); if (money <= userMoney){ userMoney = userMoney-money; changeUser( 2 ); look(); if (isContinue()) getMoney(); } else System.out.println( "您的余额不足!" ); } private void giveMoney(){ //办理转账业务 int anthorIndex = anthorLogin(); if (anthorIndex!=- 1 ){ money = howMuch( "转账" ); if (money <= userMoney){ userMoney = userMoney - money; changeUser( 2 ); String anthorUser = user[anthorIndex]; String[] str =anthorUser.split( "\\*" ); double money1 = Double.parseDouble(str[ 2 ]); money = money + money1; user[anthorIndex] = str[ 0 ]+ "*" +str[ 1 ]+ "*" +money; System.out.println( "转账成功!" ); look(); } else System.out.println( "您的余额不足!" ); } else System.out.println( "该用户不存在。。。。" ); } private double howMuch(String str){ System.out.println( "欢迎办理" +str+ "业务。。。。。。" ); System.out.println( "请输入金额(只能是整数且是100的倍数,最多为10000):" ); Scanner input = new Scanner(System.in); double money = input.nextDouble(); if (money% 10 == 0 ) return money; else { System.out.println( "您输入有误!" ); return 0.0 ; } } private void changePassword(){ //办理修改密码业务 System.out.println( "请输入新密码:" ); Scanner input = new Scanner(System.in); newPassword = input.nextLine(); changeUser( 1 ); System.out.println( "密码修改成功!" ); } private boolean isContinue(){ System.out.println( "是否继续办理该项业务?(请输入Y(y)/N(n))" ); Scanner input = new Scanner(System.in); String str = input.nextLine(); if (str.equalsIgnoreCase( "y" )) return true ; else return false ; } private void write(){ String str = "" ; String s; for ( int i= 0 ;i<user.length;i++){ s = user[i]; if (i!=user.length- 1 ) str = str + s + "\n" ; else str = str + s; } File file = new File( "src\\bank\\user" ); FileWriter out = null ; try { out = new FileWriter(file); out.write(str); out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null ){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package bank; //银行类 public class Bank { private Atm atm = new Atm(); public void welcome(User user){ System.out.println( "欢迎使用atm" ); user.useAtm(atm); } } |
1
2
3
4
5
6
7
8
9
|
package bank; //用户类 public class User { public void useAtm(Atm atm){ atm.show(); } } |
1
2
3
4
|
//创建user文件当数据库 张三*456*100.0 李四*123*300.0 王五*789*200.0 |
1
2
3
4
5
6
7
8
9
10
|
package bank; //测试类 public class Text { public static void main(String[] args){ Bank bank = new Bank(); User user = new User(); bank.welcome(user); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_46085748/article/details/112017717