练习~虚拟线上银行
數據庫操作,網絡編程及多線程的綜合練習~
?
?
客戶端:
UI層
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class ATMUI{ 6 7 //主方法 8 public static void main(String [] args){ 9 ATMUI ui = new ATMUI(); 10 ui.initBO(); 11 ui.initFrame(); 12 ui.showLogin(); 13 } 14 15 //初始化業務對象 16 //業務對象,每個用戶對于一個業務對象并保存該用戶數據 17 //因此,啟動程序和每次退出都要創建一個新的業務對象給下一個用戶使用 18 19 public ATMBO bo = null; 20 public void initBO(){ 21 bo = new ATMBO(); 22 } 23 24 //初始化主窗口 25 26 //界面寬與高 27 public int width = 960; 28 public int height = 660; 29 30 //界面窗口 31 public JFrame jFrame = null; 32 //層疊容器 33 public JLayeredPane layeredPane = null; 34 35 //背景層 36 public JPanel backLayer = null; 37 //前景層 38 public JPanel frontLayer = null; 39 //前景層布局器 40 public CardLayout cardLayout = null; 41 42 public void initFrame(){ 43 44 //-----初始化窗口與層疊容器------ 45 //創建窗口對象,窗口標題為———— 46 jFrame = new JFrame("ATM觸摸屏系統"); 47 48 //創建層疊容器對象 49 layeredPane = new JLayeredPane(); 50 //設置層疊容器大小 51 layeredPane.setPreferredSize(new Dimension(width, height)); 52 53 //把層疊容器添加到窗口中 54 jFrame.add(layeredPane); 55 //設置窗口不能放大縮小 56 jFrame.setResizable(false); 57 //設置窗口大小適應內容(層疊容器)大小 58 jFrame.pack(); 59 //設置窗口居中顯示 60 jFrame.setLocationRelativeTo(null); 61 //設置窗口可見(默認不可見) 62 jFrame.setVisible(true); 63 //設置窗口關閉時程序關閉 64 jFrame.addWindowListener(new WindowAdapter(){ 65 public void windowClosing(WindowEvent e){ 66 bo.socclose(); 67 System.exit(0); 68 } 69 }); 70 71 //----在層疊容器中加背景層---- 72 //創建背景層對象 73 backLayer = new JPanel(); 74 //設置背景層布局器FlowLayout的水平間距為0(默認是5) 75 ((FlowLayout)backLayer.getLayout()).setHgap(0); 76 //設置背景層布局器FlowLayout的垂直間距為0(默認是5) 77 ((FlowLayout)backLayer.getLayout()).setVgap(0); 78 79 //設置背景層大小(同窗口) 80 backLayer.setSize(width, height); 81 //設置背景層位置(相對于窗口左上角) 82 backLayer.setLocation(0, 0); 83 84 //創建背景圖對象 85 JLabel bg = new JLabel (new ImageIcon("img/bg.jpg")); 86 //在背景層中添加背景圖 87 backLayer.add(bg); 88 89 //把背景層添加到層疊容器的較底層 90 layeredPane.add(backLayer, new Integer(0)); 91 92 //-----在層疊容器中增加前景層----- 93 //創建前景層對象 94 frontLayer = new JPanel(); 95 //創建CardLayout布局器對象,水平、垂直間距為0 96 cardLayout = new CardLayout(0, 0); 97 //將前景層的布局器設成CardLayout 98 frontLayer.setLayout(cardLayout); 99 //把前景層的背景色設成透明(這樣才能看到背景層) 100 frontLayer.setOpaque(false); 101 //設置前景層大小(同窗口) 102 frontLayer.setSize(width, height); 103 //設置前景層位置(相對于窗口左上角) 104 frontLayer.setLocation(0, 0); 105 106 //把前景層添加到層疊容器的較頂層 107 layeredPane.add(frontLayer, new Integer(1)); 108 109 110 } 111 112 //登錄界面 113 //本案例中,前景界面層使用了CardLayout,是希望做到調用對應的方法時,把對應的層面調到最頂層即可;另外,每個層面第一次調用時,則初始化,后面的調用就可以直接把已初始化的層面調出并把些必須的組件重置即可 114 115 //登錄層容器 116 public JPanel loginPane = null; 117 //登錄卡號輸入框 118 public JTextField loginCodeInput = null; 119 //登錄密碼輸入框 120 public JPasswordField loginPassInput = null; 121 //登錄提示信息 122 public JLabel loginTipsLabel = null; 123 124 public void showLogin(){ 125 if(loginPane == null){ 126 //-----登錄層未初始化時---- 127 128 //創建登錄層容器對象 129 loginPane = new JPanel(); 130 //把登錄層的背景色設成透明 131 loginPane.setOpaque(false); 132 133 //---往登錄層容器中添加組件--- 134 //創建一個垂直BOX容器 135 Box loginBox = Box.createVerticalBox(); 136 //在垂直BoX中添加180高度的距離 137 loginBox.add(Box.createVerticalStrut(170)); 138 139 //創建一個歡迎信息容器 140 JPanel welcome_panel = new JPanel(); 141 //把歡迎信息容器的背景色設成透明 142 welcome_panel.setOpaque(false); 143 //創建歡迎信息”歡迎使用海閣銀行” 144 JLabel welcome_label = new JLabel("歡迎使用線上銀行"); 145 //設置信息字體 146 welcome_label.setForeground(Color.WHITE); 147 welcome_label.setFont(new Font("華文行楷", Font.PLAIN, 35)); 148 //把歡迎信息添加到歡迎信息容器中 149 welcome_panel.add(welcome_label); 150 //把歡迎信息容器添加到垂直Box容器中 151 loginBox.add(welcome_panel); 152 153 //在垂直BOX中添加30高度的距離 154 loginBox.add(Box.createVerticalStrut(30)); 155 //創津一個卡號輸入容器 156 JPanel code_panel = new JPanel(); 157 //把卡號輸入容器的背景色設成透明 158 code_panel.setOpaque(false); 159 //創津提示輸入卡號信息"請輸入卡號:" 160 JLabel code_label = new JLabel("請輸入卡號:"); 161 //設置信息字體 162 code_label.setForeground(Color.WHITE); 163 code_label.setFont(new Font("華文仿宋", Font.BOLD, 25)); 164 //把提示輸入卡號信息添加到卡號輸入容器中 165 code_panel.add(code_label); 166 //創建卡號輸入框 167 loginCodeInput = new JTextField(10); 168 //設置卡號輸入框字體 169 loginCodeInput.setFont(new Font("華文仿宋", Font.PLAIN, 25)); 170 //把卡號輸入框添加到卡號輸入容器中 171 code_panel.add(loginCodeInput); 172 //把卡號輸入容器添加到垂直BOX容器中 173 loginBox.add(code_panel); 174 175 //在變直的X中添加20高度的距離 176 loginBox.add(Box.createVerticalStrut(20)); 177 178 //類似于卡號輸入塊,創建密碼輸入塊 179 JPanel pass_panel = new JPanel(); 180 pass_panel.setOpaque(false); 181 JLabel pass_label = new JLabel("請輸入密碼:"); 182 pass_label.setForeground(Color.WHITE); 183 pass_label.setFont(new Font("華文仿宋",Font.BOLD, 25)); 184 pass_panel.add(pass_label); 185 loginPassInput = new JPasswordField(10); 186 loginPassInput.setFont(new Font("華文仿宋" , Font.PLAIN , 25)); 187 pass_panel.add(loginPassInput); 188 loginBox.add(pass_panel); 189 190 //在垂直BoX中添加孔高度的距離 191 loginBox.add(Box.createVerticalStrut(30)); 192 193 //創建按鈕容器 194 JPanel btn_panel = new JPanel(); 195 //把按鈕容器的背景色設成透明 196 btn_panel.setOpaque(false); 197 //創建登錄按鈕并設置字體 198 JButton login_btn = new JButton("登 錄"); 199 login_btn.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); 200 //把登錄按鈕添加到按鈕容器中 201 btn_panel.add(login_btn); 202 203 //創建重置按鈕并設置字體 204 JButton reset_btn = new JButton("重 置"); 205 reset_btn.setFont ( new Font ("微軟雅黑", Font.PLAIN , 15)); 206 //把重置按鈕添加到按鈕容器中 207 btn_panel.add(reset_btn); 208 //把按鈕容器添加到垂直BOX容器中 209 loginBox.add(btn_panel); 210 211 //在垂直BoX中添加10高度的距離 212 loginBox.add(Box.createVerticalStrut(10)); 213 214 //創建登錄提示信息容器 215 JPanel tips_panel = new JPanel(); 216 //把登錄提示信息容器的背景色設成透明 217 tips_panel.setOpaque(false); 218 //創建登錄提示信息對象并設置字體(默認無提示文字) 219 loginTipsLabel = new JLabel(""); 220 loginTipsLabel.setForeground(new Color(238,32,32)); 221 loginTipsLabel.setFont(new Font("華文仿宋", Font.BOLD, 20)); 222 //把登錄提示信息添加到登錄提示信息容器中 223 tips_panel.add(loginTipsLabel); 224 //把登錄提示信息容器添加到垂直的boX容器中 225 loginBox.add(tips_panel); 226 //把垂直BoX容器添加到登錄層容器中 227 loginPane.add(loginBox); 228 229 //-----顯示登錄層----- 230 //把登錄層添加到前景層容器中 231 frontLayer.add("loginPane", loginPane); 232 //使登錄層在前景層容器中置于頂層顯示 233 cardLayout.show(frontLayer, "loginPane"); 234 //"刷新"前景層使其可視化 235 frontLayer.validate(); 236 //使卡號輸入框獲取焦點(方便直接輸入) 237 loginCodeInput.requestFocus(); 238 239 //-----監聽重置按鈕事件----- 240 reset_btn.addActionListener(new ActionListener(){ 241 public void actionPerformed(ActionEvent ae){ 242 //清空卡號輸入框、密碼輸入框和提示信息 243 loginCodeInput.setText(""); 244 loginPassInput.setText(""); 245 loginTipsLabel.setText(""); 246 //使卡號輸入框獲取焦點 247 loginCodeInput.requestFocus(); 248 } 249 }); 250 251 //-----監聽登錄按鈕事件------ 252 login_btn.addActionListener(new ActionListener(){ 253 public void actionPerformed(ActionEvent ae){ 254 //獲取用戶輸入的卡號和密碼 255 String code_str = loginCodeInput.getText(); 256 String pass_str = new String(loginPassInput.getPassword()); 257 //判斷輸入是否為空,為空則提示 258 if("".equals(code_str)){ 259 loginTipsLabel.setText("卡號不能為空!"); 260 loginCodeInput.requestFocus(); 261 }else if("".equals(pass_str)){ 262 loginTipsLabel.setText("密碼不能為空!"); 263 loginPassInput.requestFocus(); 264 }else{ 265 //若卡號和密碼輸入都不為空調用業務對象的登錄業務方法并返回結果 266 int login_rtn = bo.dologin(code_str, Integer.valueOf(pass_str)); 267 if(login_rtn == -2){ 268 //返回-2,表示網絡出現異常 269 loginTipsLabel.setText("出現異常!"); 270 }else if(login_rtn == -1){ 271 //返回-1,表示登錄成功,顯示主菜單界面 272 showMenu(); 273 }else if(login_rtn == 3){ 274 //返回3表示錯誤已超過3次,顯示吞卡提示界面 275 showTunka(); 276 }else{ 277 //返回非―1且非表示錯誤但未超過3次機會,提示輸入錯誤信息 278 loginCodeInput.setText(""); 279 loginPassInput.setText(""); 280 loginTipsLabel.setText("卡號或密碼錯誤,請重新輸入,您還有"+(3-login_rtn)+"次機會!"); 281 loginCodeInput.requestFocus(); 282 } 283 } 284 } 285 }); 286 }else{ 287 //-----登錄層已初始化---- 288 //使登錄層在前景層容器中置于頂層顯示 289 cardLayout.show(frontLayer, "loginPane"); 290 //重置 291 loginCodeInput.setText(""); 292 loginPassInput.setText(""); 293 loginTipsLabel.setText(""); 294 loginCodeInput.requestFocus(); 295 } 296 } 297 298 //吞卡提示界面 299 public JPanel tunkaPane = null; 300 301 public void showTunka(){ 302 if(tunkaPane == null){ 303 //----吞卡界面未初始化前----- 304 tunkaPane = new JPanel(); 305 tunkaPane.setOpaque(false); 306 307 //-----添加組件----- 308 Box tunkaBox = Box.createVerticalBox(); 309 310 tunkaBox.add(Box.createVerticalStrut(180)); 311 312 JPanel tunka_panel = new JPanel(); 313 tunka_panel.setOpaque(false); 314 JLabel tunka_label = new JLabel("您已超過3次輸入機會,系統吞卡,請聯系客服!"); 315 tunka_label.setForeground(Color.WHITE); 316 tunka_label.setFont(new Font("微軟雅黑",Font.PLAIN,25)); 317 tunka_panel.add(tunka_label); 318 tunkaBox.add(tunka_panel); 319 320 tunkaBox.add(Box.createVerticalStrut(30)); 321 322 JPanel btn_panel = new JPanel(); 323 btn_panel.setOpaque(false); 324 JButton tunka_btn = new JButton("確 定"); 325 tunka_btn.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); 326 btn_panel.add(tunka_btn); 327 JButton zuobi_btn = new JButton("忘記密碼(^-^)"); 328 zuobi_btn.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); 329 btn_panel.add(zuobi_btn); 330 tunkaBox.add(btn_panel); 331 332 tunkaPane.add(tunkaBox); 333 334 //吞卡提示界面 335 frontLayer.add("tunkaPane", tunkaPane); 336 cardLayout.show(frontLayer, "tunkaPane"); 337 frontLayer.validate(); 338 339 //監聽按鈕事件 340 zuobi_btn.addActionListener(new ActionListener(){ 341 public void actionPerformed(ActionEvent ae){ 342 //shownopassword(); 343 } 344 }); 345 346 tunka_btn.addActionListener(new ActionListener(){ 347 public void actionPerformed(ActionEvent ae){ 348 349 quit(); 350 } 351 }); 352 353 }else{ 354 //----吞卡界面已初始化----- 355 cardLayout.show(frontLayer, "tunkaPane"); 356 } 357 } 358 359 //免密碼界面 360 /* 361 public JPanel nopasswordPane = null; 362 public JTextField nopinput = null; 363 public JLabel nopl = null; 364 365 public void shownopassword(){ 366 if(nopasswordPane == null){ 367 nopasswordPane = new JPanel(); 368 nopasswordPane.setOpaque(false); 369 370 Box nopasswordbox = Box.createVerticalBox(); 371 nopasswordPane.add(nopasswordbox); 372 373 nopasswordbox.add(Box.createVerticalStrut(200)); 374 375 JPanel nopshu = new JPanel(); 376 nopshu.setOpaque(false); 377 JLabel nopshutips = new JLabel("賬號:"); 378 nopshutips.setForeground(Color.WHITE); 379 nopshutips.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); 380 nopshu.add(nopshutips); 381 nopinput = new JTextField(10); 382 nopinput.setFont(new Font("華文仿宋", Font.PLAIN, 25)); 383 nopshu.add(nopinput); 384 nopasswordbox.add(nopshu); 385 386 nopasswordbox.add(Box.createVerticalStrut(30)); 387 388 JPanel nopbtn = new JPanel(); 389 nopbtn.setOpaque(false); 390 JButton nopqb = new JButton("確 定"); 391 nopqb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 392 nopbtn.add(nopqb); 393 JButton nopfb = new JButton("返 回"); 394 nopfb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 395 nopbtn.add(nopfb); 396 nopasswordbox.add(nopbtn); 397 398 nopasswordbox.add(Box.createVerticalStrut(30)); 399 400 JPanel noptips = new JPanel(); 401 noptips.setOpaque(false); 402 nopl = new JLabel(); 403 nopl.setForeground(new Color(238,32,32)); 404 nopl.setFont(new Font("華文仿宋", Font.BOLD, 20)); 405 noptips.add(nopl); 406 nopasswordbox.add(noptips); 407 408 //監聽 409 nopqb.addActionListener(new ActionListener(){ 410 public void actionPerformed(ActionEvent ae){ 411 String nopget = nopinput.getText(); 412 bo.current_index = -1; 413 int p = -1; 414 bo.dologin(nopget, p); 415 if(bo.current_index == -1){ 416 nopinput.setText(""); 417 nopinput.requestFocus(); 418 nopl.setText("賬號輸入有誤!!"); 419 }else{ 420 showLogin(); 421 loginCodeInput.setText(serverBO.code[bo.current_index]); 422 Integer i = serverBO.password[bo.current_index]; 423 loginPassInput.setText(i.toString()); 424 bo.cs = 0; 425 nopl.setText(""); 426 nopinput.setText(""); 427 nopinput.requestFocus(); 428 } 429 } 430 }); 431 432 nopfb.addActionListener(new ActionListener(){ 433 public void actionPerformed(ActionEvent ae){ 434 nopinput.setText(""); 435 nopl.setText(""); 436 showLogin(); 437 bo.cs = 0; 438 } 439 }); 440 441 //設置界面 442 frontLayer.add("nopasswordPane", nopasswordPane); 443 cardLayout.show(frontLayer, "nopasswordPane"); 444 frontLayer.validate(); 445 nopinput.requestFocus(); 446 }else{ 447 cardLayout.show(frontLayer, "nopasswordPane"); 448 nopinput.requestFocus(); 449 } 450 }*/ 451 452 //主菜單界面 453 public JPanel menuPane = null; 454 455 public void showMenu(){ 456 if(menuPane == null){ 457 //----主菜單界面未初始化前---- 458 menuPane = new JPanel(); 459 menuPane.setOpaque(false); 460 menuPane.setLayout(new BorderLayout()); 461 462 //----添加組件---- 463 //頭部信息顯示快,位于北區 464 Box tipsBox = Box.createVerticalBox(); 465 menuPane.add(tipsBox, BorderLayout.NORTH); 466 467 tipsBox.add(Box.createVerticalStrut(150)); 468 469 JLabel tips_label = new JLabel("請選擇您要的服務"); 470 tips_label.setForeground(Color.WHITE); 471 tips_label.setFont(new Font("華文行楷", Font.PLAIN, 30)); 472 tips_label.setAlignmentX(Component.CENTER_ALIGNMENT); 473 tipsBox.add(tips_label); 474 475 //左欄按鈕區,位于西區 476 Box menuLeft = Box.createVerticalBox(); 477 menuPane.add(menuLeft, BorderLayout.WEST); 478 479 menuLeft.add(Box.createVerticalStrut(50)); 480 481 JButton chaxun_btn = new JButton("查詢余額"); 482 chaxun_btn.setFont(new Font("華文仿宋", Font.BOLD, 25)); 483 menuLeft.add(chaxun_btn); 484 485 menuLeft.add(Box.createVerticalStrut(100)); 486 487 JButton cunkuan_btn = new JButton("存 款"); 488 cunkuan_btn.setFont(new Font("華文仿宋", Font.BOLD, 25)); 489 menuLeft.add(cunkuan_btn); 490 491 menuLeft.add(Box.createVerticalStrut(100)); 492 493 JButton qukuan_btn = new JButton("取 款"); 494 qukuan_btn.setFont(new Font("華文仿宋", Font.BOLD, 25)); 495 menuLeft.add(qukuan_btn); 496 497 //右欄按鈕塊,位于東區 498 Box menuRight = Box.createVerticalBox(); 499 menuPane.add(menuRight, BorderLayout.EAST); 500 501 menuRight.add(Box.createVerticalStrut(50)); 502 503 JButton xiugai_btn = new JButton("修改密碼"); 504 xiugai_btn.setFont(new Font("華文仿宋", Font.BOLD, 25)); 505 xiugai_btn.setAlignmentX(Component.RIGHT_ALIGNMENT); 506 menuRight.add(xiugai_btn); 507 508 menuRight.add(Box.createVerticalStrut(100)); 509 510 JButton zhuanzhang_btn = new JButton("轉 賬"); 511 zhuanzhang_btn.setFont(new Font("華文仿宋", Font.BOLD, 25)); 512 zhuanzhang_btn.setAlignmentX(Component.RIGHT_ALIGNMENT); 513 menuRight.add(zhuanzhang_btn); 514 515 menuRight.add(Box.createVerticalStrut(100)); 516 517 JButton quit_btn = new JButton("退 卡"); 518 quit_btn.setFont(new Font("華文仿宋", Font.BOLD, 25)); 519 quit_btn.setAlignmentX(Component.RIGHT_ALIGNMENT); 520 menuRight.add(quit_btn); 521 522 //----顯示主菜單界面層---- 523 frontLayer.add("menuPane", menuPane); 524 cardLayout.show(frontLayer, "menuPane"); 525 frontLayer.validate(); 526 527 //監聽 528 chaxun_btn.addActionListener(new ActionListener(){ 529 public void actionPerformed(ActionEvent ae){ 530 531 showChaxun(); 532 } 533 }); 534 535 cunkuan_btn.addActionListener(new ActionListener(){ 536 public void actionPerformed(ActionEvent ae){ 537 538 showCunkuan(); 539 } 540 }); 541 542 543 qukuan_btn.addActionListener(new ActionListener(){ 544 public void actionPerformed(ActionEvent ae){ 545 546 showQukuan(); 547 } 548 }); 549 550 xiugai_btn.addActionListener(new ActionListener(){ 551 public void actionPerformed(ActionEvent ae){ 552 553 showXiugai(); 554 } 555 }); 556 557 zhuanzhang_btn.addActionListener(new ActionListener(){ 558 public void actionPerformed(ActionEvent ae){ 559 560 showZhuanzhang(); 561 } 562 }); 563 564 quit_btn.addActionListener(new ActionListener(){ 565 public void actionPerformed(ActionEvent ae){ 566 567 quit(); 568 } 569 }); 570 571 }else{ 572 //----界面初始化后---- 573 cardLayout.show(frontLayer, "menuPane"); 574 } 575 } 576 577 //公共提示界面 578 public JPanel tishiPane = null; 579 public JLabel tishil = null; 580 public void showtishi(){ 581 if(tishiPane == null){ 582 //----未初始化---- 583 tishiPane = new JPanel(); 584 tishiPane.setOpaque(false); 585 586 //-----添加組件---- 587 Box tishibox = Box.createVerticalBox(); 588 tishiPane.add(tishibox); 589 590 tishibox.add(Box.createVerticalStrut(200)); 591 592 //提示語 593 JPanel tishi_p = new JPanel(); 594 tishi_p.setOpaque(false); 595 tishil = new JLabel(); 596 tishil.setForeground(Color.WHITE); 597 tishil.setFont(new Font("微軟雅黑", Font.PLAIN, 25)); 598 tishi_p.add(tishil); 599 tishibox.add(tishi_p); 600 601 tishibox.add(Box.createVerticalStrut(30)); 602 603 //按鈕 604 JPanel fanghui = new JPanel(); 605 fanghui.setOpaque(false); 606 JButton fhbtn = new JButton("返 回"); 607 fhbtn.setFont(new Font("華文仿宋", Font.BOLD, 20)); 608 fanghui.add(fhbtn); 609 tishibox.add(fanghui); 610 611 //監聽 612 fhbtn.addActionListener(new ActionListener(){ 613 public void actionPerformed(ActionEvent ae){ 614 showMenu(); 615 } 616 }); 617 618 //設置界面 619 frontLayer.add("tishiPane", tishiPane); 620 cardLayout.show(frontLayer, "tishiPane"); 621 frontLayer.validate(); 622 623 }else{ 624 cardLayout.show(frontLayer, "tishiPane"); 625 } 626 } 627 628 //查詢界面 629 public void showChaxun(){ 630 showtishi(); 631 tishil.setText("您的賬戶余額是"+bo.dochaxun()+"元"); 632 } 633 634 //存款界面 635 public JPanel cunkuanPane = null; 636 public JTextField cuninput = null; 637 public JLabel cunl = null; 638 639 public void showCunkuan(){ 640 if(cunkuanPane == null){ 641 cunkuanPane = new JPanel(); 642 cunkuanPane.setOpaque(false); 643 644 Box cunkuanbox = Box.createVerticalBox(); 645 cunkuanPane.add(cunkuanbox); 646 647 cunkuanbox.add(Box.createVerticalStrut(200)); 648 649 JPanel cunshu = new JPanel(); 650 cunshu.setOpaque(false); 651 JLabel cunshutips = new JLabel("請輸入存款金額:"); 652 cunshutips.setForeground(Color.WHITE); 653 cunshutips.setFont(new Font("微軟雅黑", Font.PLAIN, 25)); 654 cunshu.add(cunshutips); 655 cuninput = new JTextField(5); 656 cuninput.setFont(new Font("華文仿宋", Font.PLAIN, 30)); 657 cunshu.add(cuninput); 658 cunkuanbox.add(cunshu); 659 660 cunkuanbox.add(Box.createVerticalStrut(30)); 661 662 JPanel cunbtn = new JPanel(); 663 cunbtn.setOpaque(false); 664 JButton cunqb = new JButton("確 定"); 665 cunqb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 666 cunbtn.add(cunqb); 667 JButton cunfb = new JButton("返 回"); 668 cunfb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 669 cunbtn.add(cunfb); 670 cunkuanbox.add(cunbtn); 671 672 cunkuanbox.add(Box.createVerticalStrut(30)); 673 674 JPanel cuntips = new JPanel(); 675 cuntips.setOpaque(false); 676 cunl = new JLabel(); 677 cunl.setForeground(new Color(238,32,32)); 678 cunl.setFont(new Font("華文仿宋", Font.BOLD, 25)); 679 cuntips.add(cunl); 680 cunkuanbox.add(cuntips); 681 682 //監聽 683 cunqb.addActionListener(new ActionListener(){ 684 public void actionPerformed(ActionEvent ae){ 685 String cunget = cuninput.getText(); 686 if("".equals(cunget)){ 687 cunl.setText("輸入不能為空!!"); 688 }else{ 689 double cunr = bo.docunkuan(Double.valueOf(cunget)); 690 if(cunr == 1){ 691 cuninput.setText(""); 692 cuninput.requestFocus(); 693 cunl.setText(""); 694 showCunkuanSuccess(); 695 }else{ 696 cunl.setText("您輸入的金額不是整百數值,請重新輸入!!"); 697 cuninput.setText(""); 698 cuninput.requestFocus(); 699 } 700 } 701 } 702 }); 703 704 cunfb.addActionListener(new ActionListener(){ 705 public void actionPerformed(ActionEvent ae){ 706 cuninput.setText(""); 707 cunl.setText(""); 708 showMenu(); 709 } 710 }); 711 712 //設置界面 713 frontLayer.add("cunkuanPane", cunkuanPane); 714 cardLayout.show(frontLayer, "cunkuanPane"); 715 frontLayer.validate(); 716 cuninput.requestFocus(); 717 }else{ 718 cardLayout.show(frontLayer, "cunkuanPane"); 719 cuninput.requestFocus(); 720 } 721 } 722 723 //存款成功提示界面 724 public void showCunkuanSuccess(){ 725 showtishi(); 726 tishil.setText("存款成功!您的賬戶余額為"+bo.dochaxun()+"元"); 727 } 728 729 //取款界面 730 public JPanel qukuanPane = null; 731 public JTextField quinput = null; 732 public JLabel qul = null; 733 734 public void showQukuan(){ 735 if(qukuanPane == null){ 736 qukuanPane = new JPanel(); 737 qukuanPane.setOpaque(false); 738 739 Box qukuanbox = Box.createVerticalBox(); 740 qukuanPane.add(qukuanbox); 741 742 qukuanbox.add(Box.createVerticalStrut(200)); 743 744 JPanel qushu = new JPanel(); 745 qushu.setOpaque(false); 746 JLabel qushutips = new JLabel("請輸入取款金額:"); 747 qushutips.setForeground(Color.WHITE); 748 qushutips.setFont(new Font("微軟雅黑", Font.PLAIN, 25)); 749 qushu.add(qushutips); 750 quinput = new JTextField(5); 751 quinput.setFont(new Font("華文仿宋", Font.PLAIN, 30)); 752 qushu.add(quinput); 753 qukuanbox.add(qushu); 754 755 qukuanbox.add(Box.createVerticalStrut(30)); 756 757 JPanel qubtn = new JPanel(); 758 qubtn.setOpaque(false); 759 JButton quqb = new JButton("確 定"); 760 quqb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 761 qubtn.add(quqb); 762 JButton qufb = new JButton("返 回"); 763 qufb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 764 qubtn.add(qufb); 765 qukuanbox.add(qubtn); 766 767 qukuanbox.add(Box.createVerticalStrut(30)); 768 769 JPanel qutips = new JPanel(); 770 qutips.setOpaque(false); 771 qul = new JLabel(); 772 qul.setForeground(new Color(238,32,32)); 773 qul.setFont(new Font("華文仿宋", Font.BOLD, 25)); 774 qutips.add(qul); 775 qukuanbox.add(qutips); 776 777 //監聽 778 quqb.addActionListener(new ActionListener(){ 779 public void actionPerformed(ActionEvent ae){ 780 String quget = quinput.getText(); 781 if("".equals(quget)){ 782 qul.setText("輸入不能為空!!"); 783 }else{ 784 int qur = bo.doqukuan(Double.valueOf(quget)); 785 if(qur == 1){ 786 quinput.setText(""); 787 quinput.requestFocus(); 788 qul.setText(""); 789 showQukuanSuccess(); 790 }else{ 791 qul.setText("您的賬戶余額不足!!"); 792 quinput.setText(""); 793 quinput.requestFocus(); 794 } 795 } 796 } 797 }); 798 799 qufb.addActionListener(new ActionListener(){ 800 public void actionPerformed(ActionEvent ae){ 801 quinput.setText(""); 802 qul.setText(""); 803 showMenu(); 804 } 805 }); 806 807 //設置界面 808 frontLayer.add("qukuanPane", qukuanPane); 809 cardLayout.show(frontLayer, "qukuanPane"); 810 frontLayer.validate(); 811 quinput.requestFocus(); 812 }else{ 813 cardLayout.show(frontLayer, "qukuanPane"); 814 quinput.requestFocus(); 815 } 816 } 817 818 //取款成功提示界面 819 public void showQukuanSuccess(){ 820 showtishi(); 821 tishil.setText("取款成功!您的賬戶余額為"+bo.dochaxun()+"元"); 822 } 823 824 //修改密碼界面 825 public JPanel xiugaiPane = null; 826 public JPasswordField jiuinput = null; 827 public JPasswordField xininput = null; 828 public JPasswordField queinput = null; 829 public JLabel xiul = null; 830 831 public void showXiugai(){ 832 if(xiugaiPane == null){ 833 xiugaiPane = new JPanel(); 834 xiugaiPane.setOpaque(false); 835 836 Box xiugaibox = Box.createVerticalBox(); 837 xiugaiPane.add(xiugaibox); 838 839 xiugaibox.add(Box.createVerticalStrut(180)); 840 841 //舊密碼輸入 842 JPanel jiushu = new JPanel(); 843 jiushu.setOpaque(false); 844 JLabel jiushutips = new JLabel("請輸入舊密碼:"); 845 jiushutips.setForeground(Color.WHITE); 846 jiushutips.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); 847 jiushu.add(jiushutips); 848 jiuinput = new JPasswordField(10); 849 jiuinput.setFont(new Font("華文仿宋", Font.PLAIN, 25)); 850 jiushu.add(jiuinput); 851 xiugaibox.add(jiushu); 852 853 xiugaibox.add(Box.createVerticalStrut(15)); 854 855 //新密碼輸入 856 JPanel xinshu = new JPanel(); 857 xinshu.setOpaque(false); 858 JLabel xinshutips = new JLabel("請輸入新密碼:"); 859 xinshutips.setForeground(Color.WHITE); 860 xinshutips.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); 861 xinshu.add(xinshutips); 862 xininput = new JPasswordField(10); 863 xininput.setFont(new Font("華文仿宋", Font.PLAIN, 25)); 864 xinshu.add(xininput); 865 xiugaibox.add(xinshu); 866 867 xiugaibox.add(Box.createVerticalStrut(15)); 868 869 //新密碼確定 870 JPanel queshu = new JPanel(); 871 queshu.setOpaque(false); 872 JLabel queshutips = new JLabel("請確認新密碼:"); 873 queshutips.setForeground(Color.WHITE); 874 queshutips.setFont(new Font("微軟雅黑", Font.PLAIN, 20)); 875 queshu.add(queshutips); 876 queinput = new JPasswordField(10); 877 queinput.setFont(new Font("華文仿宋", Font.PLAIN, 25)); 878 queshu.add(queinput); 879 xiugaibox.add(queshu); 880 881 xiugaibox.add(Box.createVerticalStrut(30)); 882 883 //按鈕 884 JPanel xiubtn = new JPanel(); 885 xiubtn.setOpaque(false); 886 JButton xiuqb = new JButton("確 定"); 887 xiuqb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 888 xiubtn.add(xiuqb); 889 JButton xiufb = new JButton("返 回"); 890 xiufb.setFont(new Font("華文仿宋", Font.BOLD, 20)); 891 xiubtn.add(xiufb); 892 xiugaibox.add(xiubtn); 893 894 xiugaibox.add(Box.createVerticalStrut(30)); 895 896 //提示語 897 JPanel xiutips = new JPanel(); 898 xiutips.setOpaque(false); 899 xiul = new JLabel(); 900 xiul.setForeground(new Color(238,32,32)); 901 xiul.setFont(new Font("華文仿宋", Font.BOLD, 25)); 902 xiutips.add(xiul); 903 xiugaibox.add(xiutips); 904 905 //監聽 906 xiuqb.addActionListener(new ActionListener(){ 907 public void actionPerformed(ActionEvent ae){ 908 String jiustr = new String(jiuinput.getPassword()); 909 String xinstr = new String(xininput.getPassword()); 910 String questr = new String(queinput.getPassword()); 911 912 if("".equals(jiustr)){ 913 xiul.setText("舊密碼不能為空!!"); 914 jiuinput.requestFocus(); 915 }else if("".equals(xinstr)){ 916 xiul.setText("新密碼不能為空!!"); 917 xininput.requestFocus(); 918 }else if("".equals(questr)){ 919 xiul.setText("請再次輸入新密碼!!"); 920 queinput.requestFocus(); 921 }else{ 922 int n = bo.doxiugai(Integer.valueOf(jiustr), Integer.valueOf(xinstr), Integer.valueOf(questr)); 923 924 if(n == -1){ 925 xiul.setText("出現異常!"); 926 }else if(n == 0){ 927 jiuinput.setText(""); 928 xininput.setText(""); 929 queinput.setText(""); 930 xiul.setText("舊密碼輸入有誤!!"); 931 }else if(n == 1){ 932 jiuinput.setText(""); 933 xininput.setText(""); 934 queinput.setText(""); 935 showXiugaiSuccess(); 936 }else{ 937 xininput.setText(""); 938 queinput.setText(""); 939 xiul.setText("兩次密碼輸入不一致!!"); 940 } 941 } 942 } 943 }); 944 945 xiufb.addActionListener(new ActionListener(){ 946 public void actionPerformed(ActionEvent ae){ 947 jiuinput.setText(""); 948 xininput.setText(""); 949 queinput.setText(""); 950 xiul.setText(""); 951 showMenu(); 952 } 953 }); 954 955 //界面設置 956 frontLayer.add("xiugaiPane", xiugaiPane); 957 cardLayout.show(frontLayer, "xiugaiPane"); 958 frontLayer.validate(); 959 jiuinput.requestFocus(); 960 }else{ 961 cardLayout.show(frontLayer, "xiugaiPane"); 962 jiuinput.setText(""); 963 xininput.setText(""); 964 queinput.setText(""); 965 xiul.setText(""); 966 jiuinput.requestFocus(); 967 } 968 } 969 970 //密碼修改成功提示界面 971 public JPanel xiusuc = null; 972 973 public void showXiugaiSuccess(){ 974 if(xiusuc == null){ 975 xiusuc = new JPanel(); 976 xiusuc.setOpaque(false); 977 978 //組件 979 Box xiusbox = Box.createVerticalBox(); 980 xiusuc.add(xiusbox); 981 982 xiusbox.add(Box.createVerticalStrut(200)); 983 984 JPanel xiusp = new JPanel(); 985 xiusp.setOpaque(false); 986 JLabel xiustips = new JLabel("修改密碼成功!請按確認重新登陸"); 987 xiustips.setForeground(Color.WHITE); 988 xiustips.setFont(new Font("微軟雅黑", Font.PLAIN, 25)); 989 xiusp.add(xiustips); 990 xiusbox.add(xiusp); 991 992 xiusbox.add(Box.createVerticalStrut(30)); 993 994 JPanel xiubq = new JPanel(); 995 xiubq.setOpaque(false); 996 JButton xiusbtn = new JButton("確 認"); 997 xiusbtn.setFont(new Font("華文仿宋", Font.BOLD, 20)); 998 xiubq.add(xiusbtn); 999 xiusbox.add(xiubq); 1000 1001 //監聽 1002 xiusbtn.addActionListener(new ActionListener(){ 1003 public void actionPerformed(ActionEvent ae){ 1004 bo.cs = 0; 1005 showLogin(); 1006 } 1007 }); 1008 1009 //設置界面 1010 frontLayer.add("xiusuc", xiusuc); 1011 cardLayout.show(frontLayer, "xiusuc"); 1012 frontLayer.validate(); 1013 1014 }else{ 1015 cardLayout.show(frontLayer, "xiusuc"); 1016 } 1017 } 1018 1019 //轉賬界面 1020 public JPanel zhuanpane = null; 1021 public JTextField kahaoinput = null; 1022 public JTextField jineinput = null; 1023 public JLabel zhuantips = null; 1024 public void showZhuanzhang(){ 1025 if(zhuanpane == null){ 1026 //組件 1027 zhuanpane = new JPanel(); 1028 zhuanpane.setOpaque(false); 1029 1030 Box zhuanbox = Box.createVerticalBox(); 1031 zhuanpane.add(zhuanbox); 1032 1033 zhuanbox.add(Box.createVerticalStrut(200)); 1034 1035 JPanel kahaop = new JPanel(); 1036 kahaop.setOpaque(false); 1037 JLabel kahaot = new JLabel("請輸入轉賬賬號:"); 1038 kahaot.setForeground(Color.WHITE); 1039 kahaot.setFont(new Font("微軟雅黑", Font.PLAIN, 25)); 1040 kahaoinput = new JTextField(10); 1041 kahaoinput.setFont(new Font("華文仿宋", Font.PLAIN, 30)); 1042 kahaop.add(kahaot); 1043 kahaop.add(kahaoinput); 1044 zhuanbox.add(kahaop); 1045 1046 zhuanbox.add(Box.createVerticalStrut(15)); 1047 1048 JPanel jinep = new JPanel(); 1049 jinep.setOpaque(false); 1050 JLabel jinet = new JLabel("請輸入轉賬金額:"); 1051 jinet.setForeground(Color.WHITE); 1052 jinet.setFont(new Font("微軟雅黑", Font.PLAIN, 25)); 1053 jineinput = new JTextField(10); 1054 jineinput.setFont(new Font("華文仿宋", Font.PLAIN, 30)); 1055 jinep.add(jinet); 1056 jinep.add(jineinput); 1057 zhuanbox.add(jinep); 1058 1059 zhuanbox.add(Box.createVerticalStrut(30)); 1060 1061 JPanel zhuanbp = new JPanel(); 1062 zhuanbp.setOpaque(false); 1063 JButton zhuanqbtn = new JButton("確 認"); 1064 zhuanqbtn.setFont(new Font("華文仿宋", Font.BOLD, 20)); 1065 JButton zhuanfbtn = new JButton("返 回"); 1066 zhuanfbtn.setFont(new Font("華文仿宋", Font.BOLD, 20)); 1067 zhuanbp.add(zhuanqbtn); 1068 zhuanbp.add(zhuanfbtn); 1069 zhuanbox.add(zhuanbp); 1070 1071 zhuanbox.add(Box.createVerticalStrut(30)); 1072 1073 JPanel zhuantp = new JPanel(); 1074 zhuantp.setOpaque(false); 1075 zhuantips = new JLabel(); 1076 zhuantips.setForeground(new Color(238,32,32)); 1077 zhuantips.setFont(new Font("華文仿宋", Font.BOLD, 25)); 1078 zhuantp.add(zhuantips); 1079 zhuanbox.add(zhuantp); 1080 1081 //監聽 1082 zhuanqbtn.addActionListener(new ActionListener(){ 1083 public void actionPerformed(ActionEvent ae){ 1084 String zh = kahaoinput.getText(); 1085 String je = jineinput.getText(); 1086 if("".equals(zh)){ 1087 zhuantips.setText("轉賬賬戶不能為空!"); 1088 kahaoinput.requestFocus(); 1089 }else if("".equals(je)){ 1090 zhuantips.setText("轉賬金額不能為空!"); 1091 jineinput.requestFocus(); 1092 }else{ 1093 int f = bo.dozhuanzhang(zh, Double.valueOf(je)); 1094 if(f == -1){ 1095 zhuantips.setText("出現異常!"); 1096 }else if(f == 0){ 1097 zhuantips.setText("轉賬賬戶不存在!"); 1098 }else if(f == 1){ 1099 zhuantips.setText("您不能為自己轉賬!"); 1100 }else if(f == 2){ 1101 zhuantips.setText("轉賬金額超過賬戶余額!"); 1102 }else{ 1103 showZhuanzhangsuccess(); 1104 } 1105 } 1106 } 1107 }); 1108 1109 zhuanfbtn.addActionListener(new ActionListener(){ 1110 public void actionPerformed(ActionEvent ae){ 1111 showMenu(); 1112 } 1113 }); 1114 1115 //界面 1116 frontLayer.add("zhuan", zhuanpane); 1117 cardLayout.show(frontLayer, "zhuan"); 1118 frontLayer.validate(); 1119 kahaoinput.requestFocus(); 1120 }else{ 1121 cardLayout.show(frontLayer, "zhuan"); 1122 zhuantips.setText(""); 1123 kahaoinput.setText(""); 1124 jineinput.setText(""); 1125 kahaoinput.requestFocus(); 1126 } 1127 } 1128 1129 //轉賬成功提示界面 1130 public void showZhuanzhangsuccess(){ 1131 showtishi(); 1132 tishil.setText("轉款成功!您的賬戶余額為"+bo.dochaxun()+"元"); 1133 } 1134 1135 //退卡 1136 public void quit(){ 1137 //重新初始化業務對象 1138 initBO(); 1139 //重新顯示登錄界面 1140 showLogin(); 1141 } 1142 1143 } View Code業務層
1 import java.io.*; 2 import java.net.*; 3 4 public class ATMBO{ 5 //服務端ip 6 public String ip = "127.0.0.1"; 7 //服務器端口 8 public int port = 5000; 9 //存儲當前賬號游標 10 public String current_code = null; 11 //儲存錯誤次數 12 public int cs = 0; 13 14 //創建Socket對象 15 Socket socket = null; 16 DataOutputStream output = null; 17 DataInputStream input = null; 18 19 //登陸業務 20 //1.用戶輸入的卡號和密碼作為登錄業務方法的兩個參數,有前面ATMUI類調用的時候傳入 21 //2.登陸業務處理后要“告知”ATMUI類結果,因此設一個int返回值,如果卡號和密碼都正確,登陸成功,則返回-1,若果卡號和密碼都不正確,則返回錯誤次數(超過3次吞卡),因此,當前賬戶登錄錯誤次數也要作為全局變量儲存起來。 22 23 24 public int dologin(String code_input, int password_input){ 25 int rtn = -1; 26 try{ 27 //通過服務器ip和端口,創建Socket對象,連接服務器 28 socket = new Socket(ip, port); 29 30 //獲取Socket輸出流,并包裝為DataOutputStream 31 output = new DataOutputStream(socket.getOutputStream()); 32 //拼接要傳輸的參數 33 String send = "dologin&" +code_input+"&"+password_input; 34 //加密輸出 35 encryptWrite(send, output); 36 37 //獲取Socket輸入流,并包裝為DataInputStream 38 input = new DataInputStream(socket.getInputStream()); 39 //獲取并解密,接收返回結果 40 String receive = readDecrypt(input); 41 if("-1".equals(receive)){ 42 cs++; 43 rtn = cs; 44 }else{ 45 current_code = receive; 46 rtn = -1; 47 } 48 return rtn; 49 }catch(Exception ex){ 50 ex.printStackTrace(); 51 return -2; 52 } 53 54 } 55 56 //查詢業務 57 public double dochaxun(){ 58 double rtn = 0; 59 try{ 60 String send = "dochaxun&"+current_code; 61 encryptWrite(send, output); 62 63 String receive = readDecrypt(input); 64 rtn = Double.valueOf(receive); 65 }catch(Exception ex){ 66 ex.printStackTrace(); 67 } 68 return rtn; 69 } 70 71 //存款業務 72 public double docunkuan(double cunkuan){ 73 double rtn = 0; 74 try{ 75 76 String send = "docunkuan&"+current_code+"&"+cunkuan; 77 encryptWrite(send, output); 78 79 String receive = readDecrypt(input); 80 rtn = Double.valueOf(receive); 81 }catch(Exception ex){ 82 ex.printStackTrace(); 83 } 84 return rtn; 85 } 86 87 //取款業務 88 public int doqukuan(double qukuan){ 89 int rtn = 0; 90 try{ 91 String send = "doqukuan&"+current_code+"&"+qukuan; 92 encryptWrite(send, output); 93 94 String receive = readDecrypt(input); 95 rtn = Integer.valueOf(receive); 96 97 }catch(Exception ex){ 98 ex.printStackTrace(); 99 } 100 return rtn; 101 } 102 103 //密碼修改業務 104 public int doxiugai(int jiu, int xin, int que){ 105 int rtn = -1; 106 try{ 107 String send = "doxiugai&"+current_code+"&"+ jiu +"&"+ xin +"&"+ que; 108 encryptWrite(send, output); 109 110 String receive = readDecrypt(input); 111 rtn = Integer.valueOf(receive); 112 113 }catch(Exception ex){ 114 ex.printStackTrace(); 115 } 116 return rtn; 117 } 118 119 //轉賬業務 120 public int dozhuanzhang(String z, double m){ 121 int rtn = -1; 122 try{ 123 String send = "dozhuanzhang&"+current_code+"&"+ z +"&"+ m; 124 encryptWrite(send, output); 125 126 String receive = readDecrypt(input); 127 rtn = Integer.valueOf(receive); 128 129 }catch(Exception ex){ 130 ex.printStackTrace(); 131 } 132 return rtn; 133 } 134 135 136 //加密輸出 137 public void encryptWrite(String src, DataOutputStream output) throws IOException{ 138 //將一個字符串轉換成字符數組 139 char[] char_arr = src.toCharArray(); 140 //對每個字符·進行加密操作 141 for(int i=0; i<char_arr.length; i++){ 142 output.writeChar(char_arr[i] + 12); 143 } 144 //在輸出內容后加一個特殊符號作為結束符 145 output.writeChar(33333); 146 //刷寫內容 147 output.flush(); 148 } 149 150 //讀取解密 151 public String readDecrypt(DataInputStream input) throws IOException{ 152 String rtn = ""; 153 154 while(true) { 155 //從輸出流中讀取一個字符,直到結束符 156 int char_src = input.readChar(); 157 if(char_src != 33333){ 158 rtn = rtn + (char)(char_src - 12); 159 }else{ 160 break; 161 } 162 } 163 return rtn; 164 } 165 166 public void socclose() { 167 try { 168 if(output != null){output.close();} 169 if(input != null){input.close();} 170 if(socket != null){socket.close();} 171 } catch (IOException e) { 172 e.printStackTrace(); 173 } 174 } 175 176 } View Code?
?
服務端:
1 import java.io.*; 2 import java.net.*; 3 import java.sql.*; 4 5 public class serverBO{ 6 7 public static void main(String [] args){ 8 //定義serverSocket,用于監聽某個端口,等待客戶端連接 9 ServerSocket server_socket = null; 10 11 try{ 12 //創建ServerSocket對象,監聽端口5000 13 server_socket = new ServerSocket(5000); 14 System.out.println("ready"); 15 while(true){ 16 //調用ServerSocket的accept方法,等待客戶端連接,成功連接之后,返回Socket對象 17 Socket socket = server_socket.accept(); 18 19 new thread(socket).start(); 20 } 21 }catch(Exception ex){ 22 ex.printStackTrace(); 23 }finally{ 24 try{ 25 //關閉ServerSocket 26 if(server_socket != null){ 27 server_socket.close(); 28 } 29 }catch(Exception ex){ 30 ex.printStackTrace(); 31 } 32 } 33 } 34 35 //登陸業務 36 //1.用戶輸入的卡號和密碼作為登錄業務方法的兩個參數,有前面ATMUI類調用的時候傳入 37 //2.登陸業務處理后要“告知”ATMUI類結果,因此設一個int返回值,如果卡號和密碼都正確,登陸成功,則返回-1,若果卡號和密碼都不正確,則返回錯誤次數(超過3次吞卡),因此,當前賬戶登錄錯誤次數也要作為全局變量儲存起來。 38 39 public String dologin(String code_input, int password_input){ 40 //定義數據庫連接對象 41 Connection conn = null; 42 //定義傳輸對象statement 43 Statement stmt = null; 44 //定義結果焦對象ResultSet 45 ResultSet rs = null; 46 try{ 47 //加載Driver驅動 48 Class.forName("com.mysql.jdbc.Driver"); 49 //創建數據庫連接對象Connection 50 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 51 //創建一個向數據庫發送命令并返回結果的傳送對象statement 52 stmt =conn.createStatement(); 53 54 //定義sql命令,查詢符合輸入的卡號和密碼的數據 55 String sql = "select * from account where code = '"+code_input + "' and password = " + password_input; 56 //將sql命令通過sql傳送對象Statement傳送到數據庫執行,并返回卡號 57 rs = stmt.executeQuery(sql); 58 59 //處理結果集 60 if(rs.next()){ 61 //如果存在符合輸入的卡號和密碼條件的數據,表示登錄成功,返回卡號 62 return code_input; 63 }else{ 64 //如果不存在該數據,表示登錄失敗,返回“-1” 65 return "-1"; 66 } 67 }catch(Exception e){ 68 e.printStackTrace(); 69 //有異常返回-1 70 return "-1"; 71 }finally{ 72 try{ 73 if(rs != null){ rs.close();} 74 if(stmt != null){stmt.close();} 75 if(conn != null){conn.close();} 76 }catch(Exception e){ 77 e.printStackTrace(); 78 } 79 } 80 81 } 82 83 //查詢業務 84 public double dochaxun(String current_code){ 85 double rtn = 0.0; 86 87 //定義數據庫連接對象 88 Connection conn = null; 89 //定義傳輸對象statement 90 Statement stmt = null; 91 //定義結果焦對象ResultSet 92 ResultSet rs = null; 93 94 try{ 95 //加載Driver驅動 96 Class.forName("com.mysql.jdbc.Driver"); 97 //創建數據庫連接對象Connection 98 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 99 //創建一個向數據庫發送命令并返回結果的傳送對象statement 100 stmt =conn.createStatement(); 101 102 //定義sql命令,查詢對應卡號的余額 103 String sql = "select money from account where code = '"+current_code + "'"; 104 //將sql命令通過sql傳送對象Statement傳送到數據庫執行,并返回結果集 105 rs = stmt.executeQuery(sql); 106 107 //處理結果集 108 if(rs.next()){ 109 //獲取余額信息 110 rtn = rs.getDouble("money"); 111 } 112 }catch(Exception e){ 113 e.printStackTrace(); 114 }finally{ 115 try{ 116 if(rs != null){ rs.close();} 117 if(stmt != null){stmt.close();} 118 if(conn != null){conn.close();} 119 }catch(Exception e){ 120 e.printStackTrace(); 121 } 122 } 123 return rtn; 124 } 125 126 //存款業務 127 public int docunkuan(String current_code, double cunkuan){ 128 double ck = cunkuan; 129 double p = ck/100; 130 int c = (int) p; 131 if(c == p) { 132 //定義數據庫連接對象 133 Connection conn = null; 134 //定義傳輸對象statement 135 Statement stmt = null; 136 137 try{ 138 //加載Driver驅動 139 Class.forName("com.mysql.jdbc.Driver"); 140 //創建數據庫連接對象Connection 141 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 142 //創建一個向數據庫發送命令并返回結果的傳送對象statement 143 stmt =conn.createStatement(); 144 145 //定義sql命令,修改對應卡號的余額 146 String sql = "update account set money = money + "+cunkuan+" where code = '" + current_code + "'"; 147 //將sql命令通過sql傳送對象Statement傳送到數據庫執行 148 stmt.executeUpdate(sql); 149 }catch(Exception e){ 150 e.printStackTrace(); 151 }finally{ 152 try{ 153 if(stmt != null){stmt.close();} 154 if(conn != null){conn.close();} 155 }catch(Exception e){ 156 e.printStackTrace(); 157 } 158 } 159 return 1; 160 }else{ 161 return 0; 162 } 163 } 164 165 //取款業務 166 public int doqukuan(String current_code, double qukuan){ 167 double money = dochaxun(current_code); 168 if(qukuan > money){ 169 return 0; 170 }else{ 171 Connection conn = null; 172 Statement stmt = null; 173 174 try{ 175 Class.forName("com.mysql.jdbc.Driver"); 176 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 177 stmt =conn.createStatement(); 178 179 String sql = "update account set money = money - "+ qukuan + " where code = '" + current_code + "'"; 180 stmt.executeUpdate(sql); 181 }catch(Exception e){ 182 e.printStackTrace(); 183 }finally{ 184 try{ 185 if(stmt != null){stmt.close();} 186 if(conn != null){conn.close();} 187 }catch(Exception e){ 188 e.printStackTrace(); 189 } 190 } 191 return 1; 192 } 193 } 194 195 //密碼修改業務 196 public int doxiugai(String current_code, int jiu, int xin, int que){ 197 Connection conn = null; 198 Statement stmt = null; 199 ResultSet rs = null; 200 try{ 201 Class.forName("com.mysql.jdbc.Driver"); 202 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 203 stmt =conn.createStatement(); 204 205 String sql = "select * from account where code = '"+current_code + "'"; 206 rs = stmt.executeQuery(sql); 207 208 //處理結果集 209 rs.next(); 210 int password = rs.getInt("password"); 211 212 //密碼修改 213 if(jiu == password){ 214 if(xin == que){ 215 String sql1 = "update account set password = " + xin + " where code = '"+current_code + "'"; 216 stmt.executeUpdate(sql1); 217 return 1; 218 }else{ 219 return 2; 220 } 221 }else{ 222 return 0; 223 } 224 225 }catch(Exception e){ 226 e.printStackTrace(); 227 return 0; 228 }finally{ 229 try{ 230 if(rs != null){ rs.close();} 231 if(stmt != null){stmt.close();} 232 if(conn != null){conn.close();} 233 }catch(Exception e){ 234 e.printStackTrace(); 235 } 236 } 237 } 238 239 //轉賬業務 240 public int dozhuanzhang(String current_code, String z, double m){ 241 Connection conn = null; 242 Statement stmt = null; 243 ResultSet rs = null; 244 245 try{ 246 Class.forName("com.mysql.jdbc.Driver"); 247 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 248 stmt = conn.createStatement(); 249 250 String sql = "select * from account where code = '"+z + "'"; 251 rs = stmt.executeQuery(sql); 252 if(!(rs.next())){ 253 return 0; 254 }else{ 255 String zcode = rs.getString("code"); 256 if(zcode.equals(current_code)){ 257 return 1; 258 } 259 } 260 double money = dochaxun(current_code); 261 if(m > money){ 262 return 2; 263 } 264 265 try{ 266 //設置事務的提交方式為非自動提交 267 conn.setAutoCommit(false); 268 String sql1 = "update account set money = money - "+ m + " where code = '" + current_code + "'"; 269 String sql2 = "update account set money = money + "+ m + " where code = '" + z + "'"; 270 stmt.executeUpdate(sql1); 271 stmt.executeUpdate(sql2); 272 //提交事務: 273 conn.commit(); 274 return 3; 275 }catch(SQLException e){ 276 conn.rollback(); 277 e.printStackTrace(); 278 return -1; 279 }finally{ 280 conn.setAutoCommit(true); 281 } 282 }catch(Exception e){ 283 e.printStackTrace(); 284 return -1; 285 }finally{ 286 try{ 287 if(rs != null){ rs.close();} 288 if(stmt != null){stmt.close();} 289 if(conn != null){conn.close();} 290 }catch(Exception e){ 291 e.printStackTrace(); 292 } 293 } 294 } 295 296 //加密輸出 297 public void encryptWrite(String src, DataOutputStream output) throws IOException{ 298 //將一個字符串轉換成字符數組 299 char[] char_arr = src.toCharArray(); 300 //對每個字符·進行加密操作 301 for(int i=0; i<char_arr.length; i++){ 302 output.writeChar(char_arr[i] + 12); 303 } 304 //在輸出內容后加一個特殊符號作為結束符 305 output.writeChar(33333); 306 //刷寫內容 307 output.flush(); 308 } 309 310 //讀取解密 311 public String readDecrypt(DataInputStream input) throws IOException{ 312 String rtn = ""; 313 314 while(true) { 315 //從輸出流中讀取一個字符,直到結束符 316 int char_src = input.readChar(); 317 if(char_src != 33333){ 318 rtn = rtn + (char)(char_src - 12); 319 }else{ 320 break; 321 } 322 } 323 return rtn; 324 } 325 326 } 327 328 //線程 329 class thread extends Thread{ 330 serverBO bo = new serverBO(); 331 332 Socket socket = null; 333 DataInputStream input; 334 DataOutputStream output; 335 public thread(Socket s){ 336 socket = s; 337 try { 338 input = new DataInputStream(socket.getInputStream()); 339 output = new DataOutputStream(socket.getOutputStream()); 340 } catch (IOException e) { 341 e.printStackTrace(); 342 } 343 } 344 345 public void run(){ 346 try { 347 while (true) { 348 //讀取并解密 349 String receive = bo.readDecrypt(input); 350 //現在從客戶端傳輸過來的數據格式為“參數1&參數2&參數3...”,因此要進行解析 351 //其中第一個參數是指要調用的業務方法 352 String[] param = receive.split("&"); 353 if ("dologin".equals(param[0])) { 354 //登陸業務:第二個參數是用戶輸入的卡號;第三個是密碼 355 String send = bo.dologin(param[1], Integer.valueOf(param[2])); 356 //把業務處理結果加密并輸出返回 357 bo.encryptWrite(String.valueOf(send), output); 358 } else if ("dochaxun".equals(param[0])) { 359 //查詢業務:第2個參數是當前賬號的游標 360 double send = bo.dochaxun(param[1]); 361 //把業務處理結果加密并輸出返回 362 bo.encryptWrite(String.valueOf(send), output); 363 } else if ("docunkuan".equals(param[0])) { 364 //存款業務:第二個參數是當前賬號的游標,第三個是用戶輸入的金額 365 int send = bo.docunkuan(param[1], 366 Double.valueOf(param[2])); 367 bo.encryptWrite(String.valueOf(send), output); 368 } else if ("doqukuan".equals(param[0])) { 369 //取款業務 370 int send = bo.doqukuan(param[1], 371 Double.valueOf(param[2])); 372 bo.encryptWrite(String.valueOf(send), output); 373 } else if ("dozhuanzhang".equals(param[0])) { 374 //轉帳業務 375 int send = bo.dozhuanzhang(param[1], 376 param[2], Double.valueOf(param[3])); 377 bo.encryptWrite(String.valueOf(send), output); 378 } else if ("doxiugai".equals(param[0])) { 379 //密碼修改業務 380 int send = bo.doxiugai(param[1], 381 Integer.valueOf(param[2]), 382 Integer.valueOf(param[3]), 383 Integer.valueOf(param[4])); 384 bo.encryptWrite(String.valueOf(send), output); 385 } 386 } 387 } catch (EOFException e) { 388 System.out.println("線程關閉"); 389 } catch (SocketException e) { 390 System.out.println("socket關閉,線程結束"); 391 } catch (IOException e) { 392 e.printStackTrace(); 393 } finally{ 394 try { 395 if(input != null){ input.close();} 396 if(output != null){ output.close();} 397 if(socket != null){ socket.close();} 398 } catch (IOException e) { 399 e.printStackTrace(); 400 } 401 } 402 } 403 } View Code?
轉載于:https://www.cnblogs.com/fu-feng/p/6766430.html
總結
- 上一篇: 芭比裤可以直接外穿吗(芭比裤怎么穿搭)
- 下一篇: QBC(Query By Criteri