您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > java编写小程序 ATM自助取款机
importjava.sql.*;importjava.io.*;importjava.util.*;publicclassoperate{Stringstr1=sun.jdbc.odbc.JdbcOdbcDriver;Stringstr2=jdbcdbc:my;Connectioncon=null;Statementsta=null;ResultSetrs=null;cardca;privateStringnum;privateStringnum()throwsException{Stringsql=select帐号fromatm;sta.execute(sql);returnnum;}publicoperate()throwsException{ca=newcard();Class.forName(str1);con=DriverManager.getConnection(str2);sta=con.createStatement();}publicvoidinsert(Scannerf)throwsException{//形成一个新的帐户//由于此ATM机的特殊性,为了实现存款业务,必须要求用户输入最准确的信息便于日后追查未经银行同意而非法存款的用户,并作出相应的处罚//银行需要公安部门提供相关户口信息,并以此为根据建立一个数据库(ID)System.out.println(温馨提示:);System.out.println(您所输入的姓名与码必须相符,否则不能开通新帐户);System.out.println(请输入身份证号);StringIDnum=f.next();System.out.println(请输入姓名);Stringname=f.next();Stringsql1=select姓名fromID+where身份证号码='+IDnum+';//查询姓名为name的身份证号码rs=sta.executeQuery(sql1);while(rs.next()){if(rs.getString(姓名).equals(name)){Stringsql3=select帐号fromatm;rs=sta.executeQuery(sql3);booleanbe1=true;//控制接下来的while循环while(be1){Strings1=62270022;intk=(int)Math.round(Math.random()*100000+100000);//随机产生一个六位整数Strings2=Integer.toString(k);//把整数转换成为十进制字符串intw=(int)Math.round(Math.random()*10000+10000);//随机产生一个五位整数Strings3=Integer.toString(w);//把整数转换成为十进制字符串Stringnum=s1+s2+s3;//产生一个19位卡号(字符串)while(rs.next()){if(!rs.getString(帐号).equals(num)){//判断数据库中是否已有与此相同的卡号be1=false;//结束第一个while循环(也就是上面所要控制的循环)System.out.println(num);//输出卡号}}}intpassword=(int)Math.round(Math.random()*100000+100000);//随机产生一个六位整数作为密码System.out.println(password);//输出随机产生的六位密码System.out.println(是否需要修改密码1:修改2:以后再说);Scannersc1=newScanner(System.in);if(sc1.nextInt()==1){booleanbe2=true;while(be2){System.out.println(请输入新密码);Scannersc2=newScanner(System.in);intp1=sc2.nextInt();System.out.println(请输入新密码);Scannersc3=newScanner(System.in);intp2=sc3.nextInt();if(p1==p2){password=p1;//重新赋值给p(新密码)be2=false;}elseSystem.out.println(密码输入有误,请重新输入);}}doublebalance=0;//初始帐户余额为0System.out.println(是否需要存款1:存款2:以后再说);Scannersc2=newScanner(System.in);if(sc2.nextInt()==1){System.out.println(请输入所要存款的数额);//此项功能需由银行同意并给出证明,否则视为违法操作,会被处罚!!Scannersc3=newScanner(System.in);balance=sc3.nextDouble();}Stringsql4=insertintoatm('ss','IDnum','password','name','balance');sta.execute(sql4);System.out.println(新帐户已建立,各项内容亦修改成功);}else{System.out.println(输入错误!);System.out.println(请重新输入!);IDnum=f.next();}}}publicvoidupdate(Scannerf)throwsException{//修改信息booleanbe1=true;while(be1){System.out.println(请输入新密码);intp1=f.nextInt();System.out.println(请输入再次输入新密码);intp2=f.nextInt();if(p1==p2){be1=false;intpassword=p1;Stringsql1=updateatm+set密码=+password++where帐号='+num+';sta.execute(sql1);//重新把密码写入数据库中}else{System.out.println(密码输入有误!!!);System.out.println(请从新输入密码);}}System.out.println(密码修改成功!);}publicvoiddelete(Scannerf)throwsException{//注销帐户System.out.println(请输入所要注销的帐户);Stringnum=f.next();Stringsql1=select帐号fromatm;rs=sta.executeQuery(sql1);while(rs.next()){booleanbe1=true;while(be1){if(rs.getString(帐号).equals(num)){be1=false;//结束while循环System.out.println(请输入银行卡密码);}else{System.out.println(卡号有误,请重新输入);num=f.next();rs=sta.executeQuery(sql1);}intpassword=f.nextInt();Stringsql2=select密码fromatm;rs=sta.executeQuery(sql2);booleanbe=true;while(rs.next()){while(be){if(rs.getInt(密码)==password){be=false;Stringsql=deletefromatmwhere帐号='+num+';sta.execute(sql);}else{System.out.println(密码输入有误,请重新输入);password=f.nextInt();rs=sta.executeQuery(sql2);}}}}}}publicvoidquery(Scannerf,Stringnum)throwsException{//查询余额或者通过身份证号来查询帐号System.out.println(====请选择所要查询的内容====);System.out.println(1:查询余额2:查询帐号);inta=f.nextInt();if(a==1){Stringsql1=select余额fromatm+where帐号='+num+';rs=sta.executeQuery(sql1);while(rs.next()){System.out.println(您的余额为:+rs.getInt(余额));}}else{System.out.println(请输入身份证号码);StringIDnum=f.next();Stringsql1=select身份证号fromatm;rs=sta.executeQuery(sql1);booleanbe=true;while(be){while(rs.next()){if(rs.getString(身份证号).equalsIgnoreCase(IDnum)){be=false;Stringsql2=select帐号fromatm+where身份证号='+IDnum+';//通过身份证号来查询银行卡卡号rs=sta.executeQuery(sql2);System.out.println(您所查询的帐号如下:);intcount=0;while(rs.next()){count=count+1;System.out.println(rs.getString(帐号));//输出所要查询的卡号}if(count==0){System.out.println(没有相关信息,请检查身份证号码是否输入正确!);System.out.println(1:重新输入2:退出);if(f.nextInt()==1){System.out.println(请输入身份证号码);IDnum=f.next();rs=sta.executeQuery(sql1);}else{operateop=newoperate();op.contuine();}}}}}}}publicvoidfentch(Scannerf)throwsException{//取款System.out.println(----温馨提示:);System.out.println(您所取款的数额最多不超过5000元,若大于5000元请到柜台办理!);System.out.println(*****请选择取款数额*****);System.out.println(100元300元);System.out.println(500元1000元);System.out.println(2000元3000元);intb=f.nextInt();Stringsql3=select余额fromatm+where帐号='+num()+';rs=sta.executeQuery(sql3);while(rs.next()){intbalance=rs.getInt(余额);if(balance=b){System.out.println(-*-取款中请稍候-*-);balance=balance-b;Stringsql4=updateatm+set余额=balance+where帐号=num;//取款使帐户中余额减少System.out.println(请取走您的钱币);System.out.println(感谢您的使用,祝您好运);}else{booleanbe3=true;while(be3){System.out.println(您的余额少于+b+元!不能完成取款。对于您的不便我们表示抱歉);System.out.println(您是否重新选择取款的数额?);System.out.printl
本文标题:java编写小程序 ATM自助取款机
链接地址:https://www.777doc.com/doc-7026281 .html