package com.bank.unionpay; //银行卡的接口 public interface I_yinhangka { //抽象方法 //public abstract默认修饰抽象的 public abstract void cxyue();//查询余额 void cunkuan();//存款 void qukuan();//取款 void accound();//账户 }
package com.bank.unionpay; import java.util.ArrayList; import java.util.List; public class Zhujiemian { public void zhujiemian() { System.out.println("银行自助终端系统"); System.out.println(); System.out.println("************************"); List<String> li=new ArrayList<>(); li.add("开户"); li.add("查询"); li.add("存款"); li.add("取款"); li.add("退出"); for(int i=0;i<5;i++) { System.out.println(i+"------"+li.get(i)); } System.out.println(); System.out.println("请选择您要执行的功能"); } }
package com.bank.unionpay; public abstract class Atm implements I_yinhangka { String leixing;//银行卡类型 String accound;//账户 double qukuan;//取款 double cunkuan;//存款 double cxyue ;//余额 String name; String sfzheng;//省份证号 public Atm(String name,String sfzheng,String accound, double cxyue ) { super(); this.accound = accound; this.cxyue = cxyue; this.name = name; this.sfzheng = sfzheng; } public Atm() { super(); } @Override public void cxyue() { // 查询余额 cxyue-=qukuan; cxyue+=cunkuan; System.out.println("您的余额="+cxyue); } @Override public void cunkuan() { // 存款 cxyue+=cunkuan; System.out.println("存款成功,您的存款金额为"+cunkuan); System.out.println("本次操作后您的余额为:"+cxyue); } @Override public void qukuan() { // 取款 cxyue-=qukuan; if(cxyue>=0) { System.out.println("取款成功,本次取款金额= "+qukuan); System.out.println("本次操作后您的余额= "+cxyue); } else { System.out.println("余额不足,无法取款"); } } @Override public void accound() { // 账户 } //声明成员方法 public double qukuan(double qukuan)//取款 { cxyue-=qukuan; if(cxyue>=0) { System.out.println("取款成功,本次取款金额= "+qukuan); System.out.println("本次操作后您的余额= "+cxyue); } else { System.out.println("余额不足,无法取款"); } return qukuan; } public double cunkuan(double cunkuan)//存款 { cxyue+=cunkuan; System.out.println("存款成功,您的存款金额为"+cunkuan); System.out.println("本次操作后您的余额为:"+cxyue); return cunkuan; } public double cxyue(double cxyue) { cxyue-=qukuan; cxyue+=cunkuan; System.out.println("您的余额="+cxyue); return cxyue; } //添加开户的抽象方法 public abstract void kaihu(); }
package com.bank.unionpay; //银行枚举类型 public enum Leixing { ABC("农行"),ICBC("工行"),CCB("建行"),BOC("中行"); private final String leixing; public String getLeixing() { return this.leixing; } //构造方法 private Leixing(String leixing) { this.leixing=leixing; } }
package com.bank.abc; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.bank.unionpay.Leixing; //单利模式 public class Bank implements Serializable { private Bank()//禁止外部访问构造方法 { } private static Bank bank=new Bank();//内部产生唯一实例 public static Bank getBank()//开放一个方法给调用者返回这个实例 { return bank; } final Leixing lx=Leixing.ABC;//农行类型 private String yhName;//银行名称 public List <String>card=new ArrayList<>();//银行卡列表 //public Map<String, CxCard > account=new HashMap<>();//账号储蓄卡集合 }
package com.bank.abc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Random; import java.util.Scanner; import com.bank.unionpay.Atm; import com.bank.unionpay.Leixing; public class Atm2 extends Atm { public CxCard cx=new CxCard();// public Atm2(String name, String sfzheng, String accound, double cxyue) { super(name, sfzheng, accound, cxyue); // TODO 自动生成的构造函数存根 } public Atm2() { // TODO 自动生成的构造函数存根 super(); } final Leixing lx = Leixing.ABC; @Override public void kaihu()// 实现开户 { System.out.println("您选择的为开户功能"); Scanner sc0=new Scanner(System.in); System.out.println("请输入姓名"); String name=sc0.nextLine(); for(int i=1;i>0;i++) { System.out.println("请输入身份证号"); String sfzheng=sc0.nextLine(); if(sfzheng.length()==18)//判断身份证 { for(int j=1;j>0;j++) { System.out.println("请输入密码"); String mm="\\d{6}";//6位数字正则表达式 String password=sc0.nextLine(); if(password.matches(mm)) { //cx.setPassword(password);//写入密码 Random ra=new Random();//随机生成6位卡号 int[] ch=new int[6]; StringBuilder str=new StringBuilder(); String s;//用于下面拼接 for(int k=0;k<6;k++) { ch[k]=ra.nextInt(9);//0-9 } for(Integer c:ch) { str.append(c.toString());//遍历 拼接6位数字 } s=str.toString(); //cx.setCairNumber(s);//写入卡号 // Bank.getBank().card.add(cx.getCairNumber());//将生成的储蓄卡号存到银行的银行卡列表中 // // Bank.getBank().account.put(cx.getCairNumber(), cx); //id+储蓄卡对象 System.out.println("开户成功"); System.out.println("您的账号为: "+s); // 访问数据库 //1.加载驱动:加载数据库提供的驱动类 try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //2.获取数据库连接 Connection conn; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816", "123456"); System.out.println("连接成功"); //操作数据库:增删改查 //3.获得操作数据库的声明 Statement st; st = conn.createStatement(); int y=st.executeUpdate("insert into ATM(cairNumber,name,sfzheng,cxyue,password)" +"values(‘"+s+"‘,‘"+name+"‘,‘"+sfzheng+"‘,‘"+cx.getCxyue()+"‘,‘"+password+"‘)"); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } break; } else { System.out.println("请输入6位纯数字密码"); } } break; } else { System.out.println("请重新输入身份证号"); } } } }
package com.bank.abc; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import com.bank.unionpay.Leixing; //储蓄卡 //只有实现了Serializable和Externalizable接口的类的对象才能被序列化。 public class CxCard implements Serializable { //属性 final Leixing lx=Leixing.ABC; String name;//姓名 String sfzheng;//身份证号 String cairNumber2;//卡号 String password;//密码 double cxyue ;//余额 //生成卡号的get set方法 public String getCairNumber() { return cairNumber2; } public void setCairNumber(String cairNumber) { this.cairNumber2 = cairNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSfzheng() { return sfzheng; } public void setSfzheng(String sfzheng) { this.sfzheng = sfzheng; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getCxyue() { return cxyue; } public void setCxyue(double cxyue) { this.cxyue = cxyue; } public CxCard(String cairNumber, String password) { super(); this.cairNumber2 = cairNumber; this.password = password; } @Override public String toString() { return "姓名:" + name + "\n"+" 身份证:" + sfzheng + "\n"+"卡号: " + cairNumber2 +"\n"+ " 余额:" + cxyue; } public CxCard() { // TODO 自动生成的构造函数存根 } //声明成员方法 public void qukuan()//取款 { if(true) { System.out.println("您选择的为取款功能"); for(int i=1;i>0;i++) { Scanner sc1=new Scanner(System.in); System.out.println("请输入您要取款的卡号"); String c2=sc1.nextLine(); try { Class.forName("oracle.jdbc.driver.OracleDriver") ; Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816","123456") ; Statement st = conn.createStatement() ; //查询用户输入卡号的结果集 ResultSet rs =st.executeQuery("select * from ATM where cairnumber = ‘" +c2 +"‘ ") ; if(rs.next()) { String ss = rs.getString("password") ; for(int k = 0 ; k<3; k++ ) { System.out.println("请输入您的密码:"); Scanner mm = new Scanner(System.in) ; String mima = mm.nextLine(); if(ss.equals(mima)) { System.out.println("请输入取款金额"); Scanner sc =new Scanner(System.in); double qk =sc.nextDouble(); if(qk>0) { double yue = rs.getDouble("cxyue"); if(qk<=yue) { double sumyue =yue-qk; st.executeUpdate("update ATM set cxyue=‘"+sumyue+"‘ where cairnumber " + "= ‘" +c2 +"‘ "); System.out.println("取款成功"); break; } else { System.out.println("您的余额不足,无法取款"); } } else { System.out.println("您输入的取款金额,系统不识别"); } } else { System.out.println("密码不对"); } break; } break; } rs.close(); st.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } public void cunkuan()//存款 { if(true) { System.out.println("您选择的为存款功能"); for(int i=1;i>0;i++) { Scanner sc1=new Scanner(System.in); System.out.println("请输入您要存款的卡号"); String c2=sc1.nextLine(); try { Class.forName("oracle.jdbc.driver.OracleDriver") ; Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816","123456") ; Statement st = conn.createStatement() ; //查询用户输入卡号的结果集 ResultSet rs =st.executeQuery("select * from ATM where cairnumber = ‘" +c2 +"‘ ") ; if(rs.next()) { String ss = rs.getString("password") ; for(int k = 0 ; k<3; k++ ) { System.out.println("请输入您的密码:"); Scanner mm = new Scanner(System.in) ; String mima = mm.nextLine(); if(ss.equals(mima)) { System.out.println("请输入存款金额"); Scanner sc =new Scanner(System.in); double ck =sc.nextDouble(); if(ck>=0) { double yue = rs.getDouble("cxyue"); double sumyue =yue+ck; st.executeUpdate("update ATM set cxyue=‘"+sumyue+"‘ where cairnumber " + "= ‘" +c2 +"‘ "); System.out.println("存款成功"); break; } else { System.out.println("您输入的存款金额,系统不识别"); } } else { System.out.println("密码不对"); } break; } break; } rs.close(); st.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } public void cxyue()//查询 { if(true) { System.out.println("您选择的为查询功能"); for(int i=1;i>0;i++) { Scanner sc1=new Scanner(System.in); System.out.println("请输入您要查询的卡号"); String c2=sc1.nextLine(); try { Class.forName("oracle.jdbc.driver.OracleDriver") ; Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816","123456") ; Statement st = conn.createStatement() ; ResultSet rs =st.executeQuery("select * from ATM where cairnumber = ‘" +c2 +"‘ ") ; if(rs.next()) { String ss = rs.getString("password") ; for(int k = 0 ; k<3; k++ ) { System.out.println("请输入您的密码:"); Scanner mm = new Scanner(System.in) ; String mima = mm.nextLine(); if(ss.equals(mima)) { System.out.println("您的信息为:"); System.out.println("姓名:"+rs.getString("name")+"\t身份证号:"+rs.getString("sfzheng") +"\t账号:"+rs.getString("cairnumber")+"\t余额"+rs.getString("cxyue")); break; } else { System.out.println("密码不对"); } break; } break; } rs.close(); st.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } }
package com.bank.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import com.bank.abc.Atm2; import com.bank.abc.Bank; import com.bank.abc.CxCard; import com.bank.unionpay.Atm; import com.bank.unionpay.Leixing; import com.bank.unionpay.Zhujiemian; public class A { public static void main(String[] args) throws Exception { for(int i=1;i>0;i++) { Zhujiemian zjm=new Zhujiemian(); zjm.zhujiemian(); Scanner sc=new Scanner(System.in); int in=sc.nextInt(); if(in==0||in==1||in==2||in==3||in==4) { if(in==0) {//测试开户 Atm2 a2=new Atm2(); a2.kaihu(); continue; } //查询 if(in==1) { Atm2 a2=new Atm2(); a2.cx.cxyue(); continue; } if(in==2) { //存款 Atm2 a2=new Atm2(); a2.cx.cunkuan(); continue; } if(in==3) { //取款 Atm2 a2=new Atm2(); a2.cx.qukuan(); continue; } if(in==4) { //退出 System.out.println("系统即将退出"); // File fi=new File("d:/abc122"); // if(!fi.exists()) // { // fi.mkdirs(); // } // ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("d:/abc122/abc.obj")); // obj.writeObject(Bank.getBank().account); // System.out.println("数据保存完成"); // obj.close(); // break; } } else { System.out.println("该菜单不存在"); } } } }
时间: 2024-09-29 05:36:10