每注冊一個用戶,該用戶的信息會自動存到數據庫中,並且下次再啟動查詢時信息不會丟失。

package com.Bank.unionpay; // ATM 抽象類 public abstract class ATM { //成員屬性 public ICard account ; // 抽象方法 public abstract void kaiHu() ; }
package com.Bank.unionpay; // 銀行枚舉 public enum Fourbank { // 首行放置 ABC("農行") , ICBC("工行") , CCB("建行") , BOC("中行"); private String name ; public String getName() { return name; } Fourbank(String name) { this.name = name ; } }
package com.Bank.unionpay; // 銀行卡接口 public interface ICard { // 查詢余額 public void chaXun( ) ; // 存款 public void cunKuan( ) ; // 取款 public void quKuan( ) ; }
package com.Bank.abc; import com.Bank.unionpay.Fourbank; public class ABCBank { //私有屬性 final Fourbank bank = Fourbank.ABC ; //銀行類型 private String bankName ; // 銀行名稱 //私有構造方法,防止該類被實例化 private ABCBank() { } //創建Bank的一個對象 private static ABCBank bk = new ABCBank() ; //獲取唯一可用對象 public static ABCBank getBk() { return bk ; } }
package com.Bank.abc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.Bank.unionpay.ATM; import com.Bank.unionpay.Fourbank; public class AutoATM extends ATM { // 屬性 final Fourbank bank = Fourbank.ABC; // /* // map集合 卡號映射密碼 // 不將密碼存入數據庫表格 // 防止用戶密碼泄露給數據庫管理員 // */ // Map<String, String> maps = new HashMap<String, String>(); // @Override public void kaiHu() { for (int i = 0; i > -1; i++) { // 分支語句,判斷用戶輸入,提供相關功能 System.out.println("************************************"); System.out.println("您選擇的為開戶功能!"); System.out.println("請輸入您的姓名:"); // 提供輸入,供用戶注冊名字 for (int j1 = 1; j1 > 0; j1++) { Scanner can = new Scanner(System.in); String ss = can.nextLine(); String regEx = "[\u4e00-\u9fa5]"; Pattern p = Pattern.compile(regEx); int num = 0; // 漢字長度 for (int s = 0; s < ss.length(); s++) { if (p.matches(regEx, ss.substring(s, s + 1))) { num++; } } // 判斷名字是否合法 if (num < 2) { System.out.println("您輸入的名字不合法!"); } else { System.out.println("您輸入的名字是:" + ss); // 判斷身份證號碼是否合法 for (int j2 = 1; j2 > 0; j2++) { System.out.println("請輸入您的身份證號:"); Scanner sf = new Scanner(System.in); String sfNum = sf.nextLine(); // 定義判別用戶身份證號的正則表達式(18位,最后一位可以為字母) Pattern idNumPattern = Pattern.compile("(\\d{17}[0-9a-zA-Z])"); // 通過Pattern獲得Matcher Matcher idNumMatcher = idNumPattern.matcher(sfNum); // 判斷用戶輸入是否為身份證號 if (idNumMatcher.matches()) { System.out.println("您的身份證號是:" + sfNum); // 判斷密碼是否合法 for (int j = 1; j > 0; j++) { System.out.println("請輸入您的密碼:"); Scanner mm = new Scanner(System.in); String mmNum = mm.nextLine(); // 定義判別用戶密碼的正則表達式(6位純數字) Pattern idNumPattern1 = Pattern.compile("(\\d{5}[0-9])"); // 通過Pattern獲得Matcher Matcher idNumMatcher1 = idNumPattern.matcher(mmNum); // 判斷用戶輸入的密碼格式是否正確 if (idNumMatcher.matches()) { while (true) { System.out.println("您的密碼是:" + mmNum); System.out.println("請再次輸入您的密碼,進行確認:"); Scanner mm1 = new Scanner(System.in); String mmNum1 = mm.nextLine(); if (mmNum1.equals(mmNum) == true) { System.out.println("開戶成功,謝謝使用!"); System.out.println("銀行自助終端系統"); //連接訪問數據庫 try { Class.forName("oracle.jdbc.driver.OracleDriver") ; //加載驅動 String url = "jdbc:oracle:thin:@localhost:1521:orcl" ; // 獲取數據庫連接 Connection conn = DriverManager.getConnection(url, "test0816", "934617699"); Statement st = conn.createStatement() ;// 獲得操作數據庫聲明 // //在數據庫中建立一個表格,用來存儲 // String sql ="CREATE TABLE account_Bank ( " + // "kahao number not null ," // +"name varchar(18) not null , " // +"sfNum varchar(20) not null , " // +"balance number(10,2) not null ) "; // // boolean rs1 = st.execute(sql) ; // CXCard cd = new CXCard() ; cd.setBalance(0); //初始賬戶余額為零 String a = kaHao() ; // //首先判斷生成卡號是否與數據庫中卡號重復 // ResultSet rt = st.executeQuery("select * from account_Bank where kahao = ' "+a+" ' ") ; // // //對卡號進行遍歷 // if(rt.next()) // { // kaHao() ; // } System.out.println("您的卡號為:"+a); // //將卡號密碼存入map集合 // maps.put(a, mmNum) ; //將聯系人卡號、姓名、身份證號、賬戶余額存入數據庫表格存入 st.executeUpdate("insert into account_Bank(kahao,name,sfNum,balance,password)" +"values(' "+a+" ' , ' "+ss+" ' , ' "+sfNum+" ' , ' "+cd.getBalance()+" ', ' "+mmNum+" ' )") ; System.out.println("3"); ResultSet rst = st.executeQuery("select * from account_Bank") ; while(rst.next()) { String kh = rst.getString("kahao") ; String xm = rst.getString("name") ; String sfz = rst.getString("sfNum") ; double ye = rst.getDouble("balance") ; System.out.println("kahao:"+kh+",name:"+xm+",sfNum:"+sfz+"balance:"+ye); } rst.close(); st.close();//關閉,釋放資源 conn.close(); //關閉,釋放資源 } catch (ClassNotFoundException | SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } System.out.println("銀行自助終端系統"); System.out.println("************************************"); System.out.println(); System.out.println("0——————開戶"); System.out.println("1——————查詢"); System.out.println("2——————存款"); System.out.println("3——————取款"); System.out.println("4——————退出"); System.out.println(); System.out.println("************************************"); System.out.println(); System.out.println("請選擇您要執行的功能:"); break; } else { System.out.println("您兩次輸入密碼不一樣,請重新設置密碼!"); } } break; } else { // 如果不是,輸出信息提示用戶 System.out.println("您輸入的密碼必須為六位數字!"); } } break; } else { // 如果不是,輸出信息提示用戶 System.out.println("請輸入合法的身份證號!"); } break; } break; } } break; } } //卡號獲取方法 public String kaHao() { String num = ""; for (int i = 0; i < 6; i++) { num += (int)(Math.random() * 10) + ""; // 生成六位隨機卡號 } return num; } }
package com.Bank.abc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import com.Bank.unionpay.ICard; //儲蓄卡 public class CXCard implements ICard { //屬性 private double balance ; //賬戶余額 public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } AutoATM am = new AutoATM() ; @Override public void chaXun() { System.out.println("************************************"); System.out.println("您選擇的為查詢功能!"); //提供用戶輸入渠道 while(true) { System.out.println("請輸入您的卡號:"); Scanner id = new Scanner(System.in) ; String idNum = id.nextLine(); //判斷用戶輸入卡號是否存在 try { Class.forName("oracle.jdbc.driver.OracleDriver") ; String url = "jdbc:oracle:thin:@localhost:1521:orcl" ; //本機地址 Connection conn = DriverManager.getConnection(url ,"test0816","934617699" );//建立連接 Statement st = conn.createStatement() ; //查詢用戶輸入卡號的結果集 ResultSet rt = st.executeQuery("select * from account_Bank where kahao = ' " +idNum +" ' ") ; // if else 判斷該結果集是否有數據 if(rt.next()) { String ss = rt.getString("password") ; for(int i = 0 ; i<3; i++ ) { System.out.println("請輸入您的密碼:"); Scanner mm = new Scanner(System.in) ; String mima = mm.nextLine(); if(ss.equals(mima)) { System.out.println("姓名:"+rt.getString("name")+" " +"\r卡號:"+rt.getString("kahao")+" " +"\r身份證號:"+rt.getString("sfNum")+" " +"\r賬戶余額:"+rt.getString("balance")); break ; } else { System.out.println("重新輸入密碼!"); } } break ; //跳出循環 } else { System.out.println("卡號不存在!"); } st.close(); rt.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } //驅動加載 } } @Override public void cunKuan() { System.out.println("************************************"); System.out.println("您選擇的為存款功能!"); //提供用戶輸入渠道 while(true) { System.out.println("請輸入您的卡號:"); Scanner id = new Scanner(System.in) ; String idNum = id.nextLine(); //判斷用戶輸入卡號是否存在 try { Class.forName("oracle.jdbc.driver.OracleDriver") ; String url = "jdbc:oracle:thin:@localhost:1521:orcl" ; //本機地址 Connection conn = DriverManager.getConnection(url ,"test0816","934617699" );//建立連接 Statement st = conn.createStatement() ; //查詢用戶輸入卡號的結果集 ResultSet rt = st.executeQuery("select * from account_Bank where kahao = ' " +idNum +" ' ") ; // if else 判斷該結果集是否有數據 if(rt.next()) { String ss = rt.getString("password") ; for(int i = 0 ; i<3; i++ ) { System.out.println("請輸入您的密碼:"); Scanner mm = new Scanner(System.in) ; String mima = mm.nextLine(); if(ss.equals(mima)) { System.out.println("姓名:"+rt.getString("name")+" " +"\r卡號:"+rt.getString("kahao")+" " +"\r身份證號:"+rt.getString("sfNum")+" " +"\r賬戶余額:"+rt.getString("balance")); System.out.println("請輸入您要存款的金額"); Scanner sr = new Scanner(System.in) ;//存款金額 double money = sr.nextDouble(); if(money>0) { double yue = rt.getDouble("balance") ; double sum =yue + money ; //將用戶新的余額修改在數據庫中 st.executeUpdate("update account_bank set balance= ' " +sum+" ' where kahao = ' " +idNum +" ' ") ; System.out.println("存款成功!\r 返回主界面請按1"); Scanner sss= new Scanner(System.in) ; int t = sss.nextInt() ; if(t==1) { System.out.println("銀行自助終端系統"); System.out.println("************************************"); System.out.println(); System.out.println("0——————開戶"); System.out.println("1——————查詢"); System.out.println("2——————存款"); System.out.println("3——————取款"); System.out.println("4——————退出"); System.out.println(); System.out.println("************************************"); System.out.println(); System.out.println("請選擇您要執行的功能:"); } break ; } else { System.out.println("您的存款金額不對!"); System.out.println("請重新輸入密碼,進行相關操作,您還有"+(3-i)+"次機會"); } } else { System.out.println("重新輸入密碼!"); } } break ; //跳出循環 } else { System.out.println("卡號不存在!"); } st.close(); rt.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } //驅動加載 } } @Override public void quKuan() { System.out.println("************************************"); System.out.println("您選擇的為取款功能!"); //提供用戶輸入渠道 while(true) { System.out.println("請輸入您的卡號:"); Scanner id = new Scanner(System.in) ; String idNum = id.nextLine(); //判斷用戶輸入卡號是否存在 try { Class.forName("oracle.jdbc.driver.OracleDriver") ; String url = "jdbc:oracle:thin:@localhost:1521:orcl" ; //本機地址 Connection conn = DriverManager.getConnection(url ,"test0816","934617699" );//建立連接 Statement st = conn.createStatement() ; //查詢用戶輸入卡號的結果集 ResultSet rt = st.executeQuery("select * from account_Bank where kahao = ' " +idNum +" ' ") ; // if else 判斷該結果集是否有數據 if(rt.next()) { String ss = rt.getString("password") ; for(int i = 0 ; i<3; i++ ) { System.out.println("請輸入您的密碼:"); Scanner mm = new Scanner(System.in) ; String mima = mm.nextLine(); if(ss.equals(mima)) { System.out.println("姓名:"+rt.getString("name")+" " +"\r卡號:"+rt.getString("kahao")+" " +"\r身份證號:"+rt.getString("sfNum")+" " +"\r賬戶余額:"+rt.getString("balance")); System.out.println("請輸入您要取款的金額"); Scanner sr = new Scanner(System.in) ;//存款金額 double money = sr.nextDouble(); if(money>0) { double yue = rt.getDouble("balance") ; if(money <= yue) { double sum =yue - money ; //將用戶新的余額修改在數據庫中 st.executeUpdate("update account_bank set balance= ' " +sum+" ' where kahao = ' " +idNum +" ' ") ; System.out.println("取款成功!\r 返回主界面請按1"); Scanner sss= new Scanner(System.in) ; int t = sss.nextInt() ; if(t==1) { System.out.println("銀行自助終端系統"); System.out.println("************************************"); System.out.println(); System.out.println("0——————開戶"); System.out.println("1——————查詢"); System.out.println("2——————存款"); System.out.println("3——————取款"); System.out.println("4——————退出"); System.out.println(); System.out.println("************************************"); System.out.println(); System.out.println("請選擇您要執行的功能:"); } break ; } else { System.out.println("您的賬戶余額不足!"); System.out.println("請重新輸入密碼,進行相關操作,您還有"+(3-i)+"次機會"); } } else { System.out.println("您的取款金額不對!"); System.out.println("請重新輸入密碼,進行相關操作,您還有"+(3-i)+"次機會"); } } else { System.out.println("重新輸入密碼!"); } } break ; //跳出循環 } else { System.out.println("卡號不存在!"); } st.close(); rt.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } //驅動加載 } } }
package com.Bank.test; import java.util.Scanner; import com.Bank.abc.AutoATM; import com.Bank.abc.CXCard; public class Test { public static void main(String[] args) { CXCard cd = new CXCard() ; AutoATM am = new AutoATM() ; System.out.println("銀行自助終端系統"); System.out.println("************************************"); System.out.println(); System.out.println("0——————開戶"); System.out.println("1——————查詢"); System.out.println("2——————存款"); System.out.println("3——————取款"); System.out.println("4——————退出"); System.out.println(); System.out.println("************************************"); System.out.println(); System.out.println("請選擇您要執行的功能:"); while(true) { //提供輸入 Scanner sc = new Scanner(System.in) ; int it = sc.nextInt( ) ; try { if(it==0) { am.kaiHu(); } if(it==1) { cd.chaXun(); } if(it==2) { cd.cunKuan(); } if(it==3) { cd.quKuan(); } if(it==4) { System.out.println("系統即將退出!"); break ; } } catch(Exception e) { System.out.println("輸入有誤!"); System.out.println("銀行自助終端系統"); System.out.println("************************************"); System.out.println(); System.out.println("0——————開戶"); System.out.println("1——————查詢"); System.out.println("2——————存款"); System.out.println("3——————取款"); System.out.println("4——————退出"); System.out.println(); System.out.println("************************************"); System.out.println(); System.out.println("請選擇您要執行的功能:"); } } } }











