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;
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) { 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); }
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); 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; }
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();
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(); } }
|