欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java模拟ATM功能(控制台连接Mysql数据库)

程序员文章站 2023-11-09 19:01:22
本文实例为大家分享了java模拟atm功能的具体代码,供大家参考,具体内容如下有三个类:test.java、customer.java、customerdao.java、atmsystem.javaa...

本文实例为大家分享了java模拟atm功能的具体代码,供大家参考,具体内容如下

有三个类:test.java、customer.java、customerdao.java、atmsystem.java

atmsystem.java实现模拟atm的功能

customer.java创建一个用户对象

customerdao.java实现连接数据库

test为主程序 

atmsystem.java:

package atm;
 
import java.util.scanner;
 
public class atmsystem 
{
 customerdao dao=new customerdao();
 scanner input=new scanner(system.in);
 private int id;
 public atmsystem()
 {
 
 }
 public void regist()
 {
 system.out.println("注册卡号:");
 int myid=input.nextint();
 system.out.println("注册用户名:");
 string myname=input.next();
 system.out.println("注册密码:");
 string mypwd=input.next();
 system.out.println("输入存入金额:");
 int mymoney=input.nextint();
 dao.regist(myid, mypwd, myname, mymoney);
 }
 public boolean login()
 {
 system.out.println("请输入密码:");
 string mypwd=input.next();
 id=dao.login(mypwd);
 if(id==-1)
 {
  system.out.println("登录失败!");
  return false;
 }
 else
 {
  system.out.println("登陆成功!");
  return true;
 }
 }
 public void query(int id)
 {
 customer cust=dao.query(id);
 system.out.println("********************");
 system.out.println("卡号:"+cust.getid());
 system.out.println("余额:"+cust.getmoney());
 system.out.println("用户名:"+cust.getname());
 system.out.println("********************");
 }
 public void save()
 {
 system.out.println("输入存款金额:");
 int money=input.nextint();
 dao.update_save(id, money);
 }
 public void get()
 {
 system.out.println("********************");
 system.out.println("输入取款金额:");
 int money=input.nextint();
 dao.update_get(id, money);
 system.out.println("********************");
 }
 public void change()
 {
 system.out.println("请输入转账卡号:");
 int inid=input.nextint();
 system.out.println("请输入转账金额:");
 int money=input.nextint();
 dao.change(id, inid, money);
 }
 public void begin()
 {
 int num=-1;
 int num=0;
 system.out.println("请选择功能:");
 system.out.println("1.注册      2.登录");
 num=input.nextint();
 switch(num)
 {
 case 1:
  regist();
  break;
 case 2:
  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)
   {
   case 1:
   save();
   break;
   case 2:
   get();
   break;
   case 3:
   query(id);
   break;
   case 4:
   change();
   break;
   case 0:
   system.out.println("谢谢使用!");
   break;
   }
  }
  }
  else
  {
  system.out.println("登录失败,账号、密码错误!");
  }
 break;
 default:
  break;
 }
 }
}

customer.java

package atm;
 
public class customer 
{
 private int id;
 private string pwd;
 private int money;
 private string name;
 public int getid()
 {
 return id;
 }
 public void setid(int newid)
 {
 id=newid;
 }
 public string getpwd()
 {
 return pwd;
 }
 public void setpwd(string newpwd)
 {
 pwd=newpwd;
 }
 public string getname()
 {
 return name;
 }
 public void setname(string newname)
 {
 name=newname;
 }
 public int getmoney()
 {
 return money;
 }
 public void setmoney(int newmoney)
 {
 money=newmoney;
 }
}

customerdao.java:

package atm;
 
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
 
 
public class customerdao
{
 private connection conn=null;
 private java.sql.preparedstatement p=null;
 public customerdao()
 {
 try {
  class.forname("org.gjt.mm.mysql.driver");
 } catch (classnotfoundexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 try {
  conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/mysql", "root", "");
 } catch (sqlexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 }
 public int login(string mypwd)
 {
 string sql="select * from mysql where pwd = ?";
 try 
 {
  p=conn.preparestatement(sql);
  p.setstring(1, mypwd);
  resultset rs=p.executequery();
  if(rs.next())
  {
  return rs.getint("id");
  }
  rs.close();
  p.close();
  conn.close();
 } 
 catch (sqlexception e) 
 {
  e.printstacktrace();
 }
 return -1;
 }
 public customer query(int id)
 {
 customer cust=new customer();
 try
 {
  string sql="select * from mysql where id = ?";
  p=conn.preparestatement(sql);
  p.setint(1, id);
  resultset rs=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 (exception e) {
  // todo: handle exception
 }
 return cust;
 
 }
 public void update_get(int id,int money) 
 {
 string sql="select * from mysql where id = ?";
 try 
 {
  p=conn.preparestatement(sql);
  p.setint(1, id);
  resultset rs=p.executequery();
  if(rs.next())
  {
  if(rs.getint("money")<money)
  {
   system.out.println("余额不足!");
  }
  else
  {
   sql="update mysql set money = money - ? where id = ?";
   p=conn.preparestatement(sql);
   p.setint(1, money);
   p.setint(2, id);
   p.executeupdate();
   system.out.println("取款成功!");
  }
  }
 }
 catch (exception e)
 {
  // todo: handle exception
 }
 }
 public void update_save(int id,int money)
 {
 string sql="update mysql set money = money + ? where id = ?";
 try 
 {
  p=conn.preparestatement(sql);
  p.setint(1, money);
  p.setint(2, id);
  p.executeupdate();
  system.out.println("存款成功!");
 }
 catch (exception e) 
 {
  // todo: handle exception
 }
 }
 public void change(int myid,int inid,int money)
 {
 try
 { string sql="select * from mysql where id = ?";
  p=conn.preparestatement(sql);
  p.setint(1, inid);
  resultset rs=p.executequery();
  if(rs.next())
  {
  sql="select * from mysql where id = ?";
  p=conn.preparestatement(sql);
  p.setint(1, myid);
  rs=p.executequery();
  if(rs.next())
  {
   if(rs.getint("money")<money)
   {
   system.out.println("余额不足!");
   return;
   }
   else
   {
   sql="update mysql set money = money - ? where id = ?";
   p=conn.preparestatement(sql);
   p.setint(1, money);
   p.setint(2, myid);
   p.executeupdate();
   sql="update mysql set money = money + ? where id = ?";
   p=conn.preparestatement(sql);
   p.setint(1, money);
   p.setint(2, inid);
   p.executeupdate();
   }
  }
  }
  else
  {
  system.out.println("卡号错误!");
  }
 }
 catch (exception e) {
  // todo: handle exception
 }
 }
 public void regist(int myid,string mypwd,string myname,int mymoney)
 {
 string sql;
 try
 {
  sql="select * from mysql where id = ?";
  p=conn.preparestatement(sql);
  p.setint(1, myid);
  resultset rs=p.executequery();
  if(rs.next())
  {
  system.out.println("此卡已注册,请勿重复注册!!!!");
  return;
  }
 }
 catch (exception e)
 {
  // todo: handle exception
 }
 
 try 
 {
  sql="insert into mysql(id,pwd,money,name) values(?,?,?,?)";
  p=conn.preparestatement(sql);
  p.setint(1, myid);
  p.setstring(2, mypwd);
  p.setint(3, mymoney);
  p.setstring(4, myname);
  p.executeupdate();
  system.out.println("注册成功!");
 }
 catch (exception e) 
 {
  // todo: handle exception
 }
 }
}

test.java:

package atm;
 
public class test 
{
 public static void main(string[] args) 
 {
 // todo auto-generated method stub
 atmsystem atm=new atmsystem();
 while(true)
 {
  atm.begin();
 }
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。