java模拟ATM功能(控制台连接Mysql数据库)
本文实例为大家分享了java模拟ATM功能的具体代码,供大家参考,具体内容如下
有三个类:Test.java、Customer.java、CustomerDAO.java、ATMSystem.java
ATMSystem.java实现模拟ATM的功能
Customer.java创建一个用户对象
CustomerDAO.java实现连接数据库
Test为主程序
ATMSystem.java:
packageATM; importjava.util.Scanner; publicclassATMSystem { CustomerDAOdao=newCustomerDAO(); Scannerinput=newScanner(System.in); privateintid; publicATMSystem() { } publicvoidRegist() { System.out.println("注册卡号:"); intmyid=input.nextInt(); System.out.println("注册用户名:"); Stringmyname=input.next(); System.out.println("注册密码:"); Stringmypwd=input.next(); System.out.println("输入存入金额:"); intmymoney=input.nextInt(); dao.Regist(myid,mypwd,myname,mymoney); } publicbooleanlogin() { System.out.println("请输入密码:"); Stringmypwd=input.next(); id=dao.login(mypwd); if(id==-1) { System.out.println("登录失败!"); returnfalse; } else { System.out.println("登陆成功!"); returntrue; } } publicvoidquery(intid) { Customercust=dao.query(id); System.out.println("********************"); System.out.println("卡号:"+cust.getid()); System.out.println("余额:"+cust.getmoney()); System.out.println("用户名:"+cust.getname()); System.out.println("********************"); } publicvoidsave() { System.out.println("输入存款金额:"); intmoney=input.nextInt(); dao.update_save(id,money); } publicvoidget() { System.out.println("********************"); System.out.println("输入取款金额:"); intmoney=input.nextInt(); dao.update_get(id,money); System.out.println("********************"); } publicvoidchange() { System.out.println("请输入转账卡号:"); intinid=input.nextInt(); System.out.println("请输入转账金额:"); intmoney=input.nextInt(); dao.change(id,inid,money); } publicvoidbegin() { intnum=-1; intNum=0; System.out.println("请选择功能:"); System.out.println("1.注册2.登录"); Num=input.nextInt(); switch(Num) { case1: Regist(); break; case2: if(login()) { while(num!=0) { System.out.println("********************"); System.out.println("欢迎欢迎,热烈欢迎"); System.out.println("请选择功能:"); System.out.println("1.存款2.取款3.查询4.转账0.退出"); System.out.println("********************"); num=input.nextInt(); switch(num) { case1: save(); break; case2: get(); break; case3: query(id); break; case4: change(); break; case0: System.out.println("谢谢使用!"); break; } } } else { System.out.println("登录失败,账号、密码错误!"); } break; default: break; } } }
Customer.java
packageATM; publicclassCustomer { privateintid; privateStringpwd; privateintmoney; privateStringname; publicintgetid() { returnid; } publicvoidsetid(intnewid) { id=newid; } publicStringgetpwd() { returnpwd; } publicvoidsetpwd(Stringnewpwd) { pwd=newpwd; } publicStringgetname() { returnname; } publicvoidsetname(Stringnewname) { name=newname; } publicintgetmoney() { returnmoney; } publicvoidsetmoney(intnewmoney) { money=newmoney; } }
CustomerDAO.java:
packageATM; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.sql.SQLException; publicclassCustomerDAO { privateConnectionconn=null; privatejava.sql.PreparedStatementp=null; publicCustomerDAO() { try{ Class.forName("org.gjt.mm.mysql.Driver"); }catch(ClassNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } try{ conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root",""); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } publicintlogin(Stringmypwd) { Stringsql="select*frommysqlwherepwd=?"; try { p=conn.prepareStatement(sql); p.setString(1,mypwd); ResultSetrs=p.executeQuery(); if(rs.next()) { returnrs.getInt("id"); } rs.close(); p.close(); conn.close(); } catch(SQLExceptione) { e.printStackTrace(); } return-1; } publicCustomerquery(intid) { Customercust=newCustomer(); try { Stringsql="select*frommysqlwhereid=?"; p=conn.prepareStatement(sql); p.setInt(1,id); ResultSetrs=p.executeQuery(); if(rs.next()) { cust.setid(rs.getInt("id")); cust.setmoney(rs.getInt("money")); cust.setname(rs.getString("name")); cust.setpwd(rs.getString("pwd")); } } catch(Exceptione){ //TODO:handleexception } returncust; } publicvoidupdate_get(intid,intmoney) { Stringsql="select*frommysqlwhereid=?"; try { p=conn.prepareStatement(sql); p.setInt(1,id); ResultSetrs=p.executeQuery(); if(rs.next()) { if(rs.getInt("money")Test.java:
packageATM; publicclassTest { publicstaticvoidmain(String[]args) { //TODOAuto-generatedmethodstub ATMSystemATM=newATMSystem(); while(true) { ATM.begin(); } } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。