有三个类:Test.java、Customer.java、CustomerDAO.java、ATMSystem.javaATMSystem.java实现模拟ATM的功能Customer.java创建一个用户对象CustomerDAO.java实现连接数据库Test为主程序 ATMSystem.java:packag...
有三个类: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("注册密码:");
int mypwd=input.nextInt();
System.out.println("输入存入金额:");
int mymoney=input.nextInt();
dao.Regist(myid, mypwd, myname, mymoney);
}
public boolean login()
{
System.out.println("请输入密码:");
int mypwd=input.nextInt();
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=-1;
System.out.println("请选择功能:");
System.out.println("1.注册 2.登录 3.退出");
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;
}
}
}
Customer.java
package ATM;
public class Customer
{
private int id;
private int pwd;
private int money;
private String name;
public int getid()
{
return id;
}
public void setid(int newid)
{
id=newid;
}
public int getpwd()
{
return pwd;
}
public void setpwd(int 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(int mypwd)
{
String sql="select * from mysql where pwd = ?";
try
{
p=conn.prepareStatement(sql);
p.setInt(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.getInt("pwd"));
}
rs.close();
p.close();
conn.close();
}
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("取款成功!");
}
}
rs.close();
p.close();
conn.close();
}
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("存款成功!");
p.close();
conn.close();
}
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("卡号错误!");
}
rs.close();
p.close();
conn.close();
}
catch (Exception e) {
// TODO: handle exception
}
}
public void Regist(int myid,int 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("此卡已注册,请勿重复注册!!!!");
rs.close();
p.close();
conn.close();
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.setInt(2, mypwd);
p.setInt(3, mymoney);
p.setString(4, myname);
p.executeUpdate();
System.out.println("注册成功!");
p.close();
conn.close();
}
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();
}
}
}
沃梦达教程
本文标题为:Java项目——模拟ATM(控制台连接Mysql数据库)
基础教程推荐
猜你喜欢
- JSP实现页面右下角消息弹框 2023-07-30
- Java定时任务schedule和scheduleAtFixedRate的异同 2023-07-15
- 详解Spring的核心机制依赖注入 2023-07-30
- SpringCloud超详细讲解负载均衡组件Ribbon源码 2023-03-07
- Java中system.exit(0) 和 system.exit(1)区别 2023-07-15
- java – 当JSON对象作为字符串文字插入时,MySQL JSON列丢失小数精度 2023-11-04
- Spring Boot Filter 过滤器的使用方式 2023-05-24
- SpringBoot整合SpringSession实现分布式登录详情 2023-03-31
- java poi导出excel时如何设置手动换行 2022-12-07
- SpringBoot使用Shiro实现动态加载权限详解流程 2023-03-21