ATM小项目

该博客介绍了一个ATM项目,这是学习中编写的JFrame界面项目,运用SQL Server Management Studio 2012数据库和eclipse开发工具。包含数据库创建、表设计、数据插入,以及连接数据库、编写实体类、DAO接口等代码,最后进行程序运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ATM

1.项目简介

此项目是在学习过程中自己编写的Jframe界面项目,运用了SQL Server Management Studio 2012版本数据库,eclipse。

2、数据代码

1.数据库

此数据库脚本将创建ATM数据库、Users和Journals两个表,并插入初始数据。Users表用来储存用户基本信息,包括账号,密码,身份证,余额等。Journals表用来储存日志信息,我怕数据库日期转换麻烦就将日期定义成字符串类型。

use master
go
if exists(select * from sysdatabases where name='ATM')
drop database ATM
go
create database ATM
go
use ATM
go
create table Users(
userId int not null primary key,
userName varchar(20) not null,
userPassword int not null,
userMoney money,
mobile varchar(20),
idcard varchar(20) not null,
gender varchar(4)
)
create table Journals(
journalId int identity(1,1) not null primary key ,
Journal varchar(500),--记录
journalDate varchar(50),
userId int not null
constraint id_fk foreign key (userId) references Users (userId)
)
insert into Users(userId,userName,userPassword,userMoney,mobile,idcard,gender)values (11111111,'dd',123456,2354,'13117380586','45646456454545','男')
insert into Users(userId,userName,userPassword,userMoney,mobile,idcard,gender)values (123456,'dd',123456,2354,'1311738058','4564645645455','男')
insert into Journals(Journal,userId)values('asd',11111111)
--update Users set userName='',userPassword=0,userMoney=0.00,mobile='',idcard='',gender='',journal='' where userId=222

select Journal from Journals where userId=11111111

select * from Journals
select * from Users

2.连接数据库

  

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DBConn {
//设置常量
private static String DRIVER_CLASS_NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String DATABASE_URL="jdbc:sqlserver://localhost:1433;databaseName=ATM";
private static String USERNAME="sa";
private static String PASSWORD="123456";
private static Properties prop = null;
static{
try {
//1: 加载连接驱动,Java反射原理
Class.forName(DRIVER_CLASS_NAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//创建数据库的连接
public static Connection getConntion(){
Connection conn = null;
try {
//2:创建Connection接口对象,用于获取数据库的连接对象。三个参数:url连接字符串 账号 密码
conn = DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
System.out.println("数据库连接成功!");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//测试是否连接成功
public static void main(String[] args) {
getConntion();
}

//关闭数据库的方法
public static void closeConnection(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

3.user实体类

package entity;

public class Users {
private int userId;// *(必填)
private String userName;// *
private int userPassword;// *
private double userMoney;
private String mobile;// 手机号码
private String idcard;// *
private String gender;
private String journal;

public String getJournal() {
return journal;
}

public void setJournal(String journal) {
this.journal = journal;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getUserPassword() {
return userPassword;
}

public void setUserPassword(int userPassword) {
this.userPassword = userPassword;
}

public double getUserMoney() {
return userMoney;
}

public void setUserMoney(double userMoney) {
this.userMoney = userMoney;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getIdcard() {
return idcard;
}

public void setIdcard(String idcard) {
this.idcard = idcard;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public Users(int userId, String userName, int userPassword, double userMoney, String mobile, String idcard,
String gender,String journal) {
super();
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.userMoney = userMoney;
this.mobile = mobile;
this.idcard = idcard;
this.gender = gender;
this.journal=journal;
}

 

@Override
public String toString() {
return "Users [userId=" + userId + ", userName=" + userName + ", userPassword=" + userPassword + ", userMoney="
+ userMoney + ", mobile=" + mobile + ", idcard=" + idcard + ", gender=" + gender + ", journal="
+ journal + "]";
}

/**
* 必填的构造方法
*
* @param userId
* 用户id
* @param userName
* 用户姓名
* @param userPassword
* 用户密码
* @param idcard
* 身份证号
*/
public Users(int userId, String userName, int userPassword, String idcard) {
super();
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.idcard = idcard;
this.userMoney=0.0;
}

}

 

4.编写DAO接口

package dao;

import java.sql.SQLException;
import java.util.List;

import entity.Users;

public interface UserDao {

/**
* 添加用户
*
* @param user
* @throws Exception
*/
void add(Users user) throws Exception;

/**
* 修改用户
*
* @param user
* @throws Exception
*/
void edit(Users user) throws Exception;

/**
* 删除用户
*
* @param uid
* @throws Exception
*/
void delete(int userId) throws Exception;

/**
* 查找用户
*
* @param uid
* @return
* @throws Exception
*/
Users findById(int userId) throws Exception;

/**
* 用户列表
*
* @return
* @throws Exception
*/
List<Users> list() throws Exception;

/**
* 修改密码
*
* @param userId
* @param userPassword
* 要修改的密码
* @throws Exception
*/
void changePassword(int userId, int userPassword) throws Exception;

/**
* 存款
*
* @param userId
* @param money
* @param userPassword
*/
void deposit(int userId, double money) throws Exception;

/**
* 取款
*
* @param userId
* @param money
* @param userPassword
*/
void withdraw(int userId, double money, int userPassword) throws Exception;

/**
* 转账
*
* @param userId
* @param accountId
* 对方账户
* @param money
* @param userPassword
* @param accountName
* 确认对方姓名
*/
void transfer_accounts(int userId, int accountId, double money, int userPassword) throws Exception;

/**
* 生成日志
*
* @param user
* @param journal
* 日志类型
* @throws Exception
*/
void journal(int userId, String journal) throws Exception;

/**
* 密码是否正确
*
* @param userId
* @param password
* @throws Exception
*/
boolean isPassword(int userId, int password) throws Exception;

/**
* 用户是否存在
*
* @param userId
* @return
* @throws Exception
*/
boolean haveCustomer(int userId) throws Exception;

/**
* 返回用户金额
*
* @param userId
* @return
* @throws Exception
*/
double money(int userId) throws Exception;

/**
* 返回用户姓名
*
* @param userId
* @return
* @throws Exception
*/
String userName(int userId) throws Exception;

/**
* 返回日志信息
*
* @param userId
* @param genre
* 需要返回的数据种类
* @return
*/
List<String> checkJournal(int userId, String genre) throws Exception;
/**
* 返回当次使用ATM记录
* @param loginDate
* @param userId
* @return
* @throws SQLException
*/
public List<String> dayJournal(String loginDate,int userId) throws SQLException;
}

5.实现Dao接口

package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import dao.UserDao;
import db.DBConn;
import entity.Users;

public class UserDaoImpl implements UserDao {

@Override
public void add(Users user) throws Exception {
Connection conn = DBConn.getConntion();// 创建数据库连接
String sql = "insert into Users(userId,userName,userPassword,userMoney,mobile,idcard,gender,journal)values (?,?,?,?,?,?,?,?)";
// 创建SQL语句执行对象
PreparedStatement psmt = conn.prepareStatement(sql);
// 给各参数赋值
psmt.setInt(1, user.getUserId());
psmt.setString(2, user.getUserName());
psmt.setInt(3, user.getUserPassword());
psmt.setDouble(4, user.getUserMoney());
psmt.setString(5, user.getMobile());
psmt.setString(6, user.getIdcard());
psmt.setString(7, user.getGender());
psmt.setString(8, user.getJournal());
int n = psmt.executeUpdate();// 执行SQL语句
if (n < 1)
throw new Exception("插入失败");
}

@Override
public void edit(Users user) throws Exception {
Connection conn = DBConn.getConntion();// 创建数据库连接
String sql = "update Users set userName=?,userPassword=?,userMoney=?,mobile=?,idcard=?,gender=?,journal=? where userId=?";
// 创建SQL语句执行对象
PreparedStatement psmt = conn.prepareStatement(sql);
// 给各参数赋值
psmt.setString(1, user.getUserName());
psmt.setInt(2, user.getUserPassword());
psmt.setDouble(3, user.getUserMoney());
psmt.setString(4, user.getMobile());
psmt.setString(5, user.getIdcard());
psmt.setString(6, user.getGender());
psmt.setString(7, user.getJournal());
psmt.setInt(8, user.getUserId());
int n = psmt.executeUpdate();// 执行SQL语句
if (n < 1)
throw new Exception("修改失败");
}

@Override
public void delete(int userId) throws Exception {
Connection conn = DBConn.getConntion();
String sql = "delete from Users where userId=? ";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
int n = psmt.executeUpdate();
if (n < 1)
throw new Exception("删除失败");
}

@Override
public Users findById(int userId) throws Exception {
Connection conn = DBConn.getConntion();
String sql = "select userId,userName,userPassword,userMoney,mobile,idcard,gender,journal from Users where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
rs.next();

String userName = rs.getString("userName");
int userPassword = rs.getInt("userPassword");
double userMoney = rs.getDouble("userMoney");
String mobile = rs.getString("mobile");
String idcard = rs.getString("idcard");
String gender = rs.getString("gender");
String journal = rs.getString("journal");
Users u = new Users(userId, userName, userPassword, userMoney, mobile, idcard, gender, journal);

return u;
}

@Override
public List<Users> list() throws Exception {
Connection conn = DBConn.getConntion();
String sql = "select userId,userName,userPassword,userMoney,mobile,idcard,gender,journal from Users";
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
List<Users> list = new ArrayList<Users>();
while (rs.next()) {
int userId = rs.getInt("userId");
String userName = rs.getString("userName");
int userPassword = rs.getInt("userPassword");
double userMoney = rs.getDouble("userMoney");
String mobile = rs.getString("mobile");
String idcard = rs.getString("idcard");
String gender = rs.getString("gender");
String journal = rs.getString("journal");

Users u = new Users(userId, userName, userPassword, userMoney, mobile, idcard, gender, journal);
list.add(u);
}
return list;
}

@Override
public void changePassword(int userId, int userPassword) throws Exception {
String journal = "";
Connection conn = DBConn.getConntion();
String sql = "update Users set userPassword=? where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userPassword);
psmt.setInt(2, userId);
int n = psmt.executeUpdate();// 执行SQL语句
if (n < 1)
throw new Exception("修改失败");
journal = "修改密码成功";
journal(userId, "修改密码:" + journal);
JOptionPane.showMessageDialog(null, "修改密码成功", "提示", JOptionPane.INFORMATION_MESSAGE);
}

@Override
public void deposit(int userId, double money) throws Exception {
double userMoney = money(userId);
Connection conn = DBConn.getConntion();
String sql = "update Users set userMoney=? where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setDouble(1, userMoney + money);
psmt.setInt(2, userId);
int n = psmt.executeUpdate();// 执行SQL语句
if (n < 1)
throw new Exception("存款失败");
journal(userId, "存款:存款"+money+"元成功");
JOptionPane.showMessageDialog(null, "存款成功", "提示", JOptionPane.INFORMATION_MESSAGE);
}

@Override
public void withdraw(int userId, double money, int userPassword) throws Exception {
double userMoney = money(userId);
if (userMoney >= money) {
if (isPassword(userId, userPassword)) {
Connection conn = DBConn.getConntion();
String sql = "update Users set userMoney=? where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setDouble(1, userMoney - money);
psmt.setInt(2, userId);
int n = psmt.executeUpdate();// 执行SQL语句
if (n < 1)
throw new Exception("取款失败");
JOptionPane.showMessageDialog(null, "取款成功", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.ERROR_MESSAGE);
}
journal(userId, "取款:取款"+money+"成功");
} else {
JOptionPane.showMessageDialog(null, "用户余额不足", "提示", JOptionPane.ERROR_MESSAGE);
}
}

@Override
public void transfer_accounts(int userId, int accountId, double money, int userPassword) throws Exception {
if (isPassword(userId, userPassword)) {
double userMoney = money(userId);
double accountMoney = money(accountId);
Connection conn = DBConn.getConntion();
String sql = "update Users set userMoney=? where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
PreparedStatement psmt1 = conn.prepareStatement(sql);
psmt.setDouble(1, userMoney - money);
psmt.setInt(2, userId);
psmt1.setDouble(1, accountMoney + money);
psmt1.setInt(2, accountId);

int n = psmt.executeUpdate();// 执行SQL语句
int n1 = psmt1.executeUpdate();// 执行SQL语句
if (n < 1 || n1 < 1)
throw new Exception("转账失败");
journal(userId, "转账:转账给用户" + accountId + " " + money + "元");
journal(accountId, "转账:用户" + userId + "转账给您 " + money + "元");
JOptionPane.showMessageDialog(null, "转账成功", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.ERROR_MESSAGE);
}

}

@Override
public void journal(int userId, String journal) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
String date = sdf.format(new Date());
System.out.println(date);
Connection conn = DBConn.getConntion();
String sql = "insert into Journals(Journal,journalDate,userId)values(?,?,?)";
PreparedStatement psmt = conn.prepareStatement(sql);

psmt.setString(1, journal);
psmt.setString(2, date);
psmt.setInt(3, userId);
int n = psmt.executeUpdate();// 执行SQL语句
if (n < 1)
throw new Exception("日志上传失败");
}

@Override
public boolean isPassword(int userId, int password) throws Exception {
Connection conn = DBConn.getConntion();
String sql = "select userPassword from Users where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
rs.next();
int userPassword = rs.getInt("userPassword");
return password == userPassword;
}

@Override
public boolean haveCustomer(int userId) throws SQLException {
Connection conn = DBConn.getConntion();
String sql = "select * from Users where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
return rs.next();
}

@Override
public double money(int userId) throws Exception {
Connection conn = DBConn.getConntion();
String sql = "select userMoney from Users where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
rs.next();
return rs.getDouble("userMoney");
}

@Override
public String userName(int userId) throws Exception {
if (haveCustomer(userId)) {
Connection conn = DBConn.getConntion();
String sql = "select userName from Users where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
rs.next();
return rs.getString("userName");
}
return null;
}

@Override
public List<String> checkJournal(int userId, String genre) throws Exception {
Connection conn = DBConn.getConntion();
String sql = "select Journal,journalDate from Journals where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
String src;
List<String> list = new ArrayList<String>();
while (rs.next()) {
src = rs.getString("Journal")+rs.getString("journalDate");
if (genre.equals(getStringGenre(src))) {
System.out.println(src);
list.add(src);
}
}
return list;
}

/**
* 获取一个字符串中 “:” 号前数据
*
* @param str
* @return
*/
public String getStringGenre(String str) {
char[] src = str.toCharArray();
String genre = "";
for (char c : src) {
if (c == ':') {
break;
}
genre += c;
}
return genre;

}

public List<String> dayJournal(String loginDate,int userId) throws SQLException{
Connection conn = DBConn.getConntion();
String sql = "select Journal,journalDate from Journals where userId=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, userId);
ResultSet rs = psmt.executeQuery();
String src;
String date;
List<String> list = new ArrayList<String>();
boolean add=false;
while (rs.next()) {
date=rs.getString("journalDate");
src = rs.getString("Journal")+date;
if (date.equals(loginDate)) {
System.out.println("1."+date);
add=true;
}if(add) {
System.out.println(src);
list.add(src);
}
}
return list;
}
}

6.JFrame界面

package view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

import dao.UserDao;
import dao.impl.UserDaoImpl;

public class ATMView extends JFrame {
private static int USERID;
private static JFrame loginView;
private static JPanel welcomeView, welcome_btn, operationView, inquireView, depositView, withdrawView, transferView,
changePasswordView;
private static UserDao userDao = new UserDaoImpl();
private static JButton btn_login, btn_Wexit, btn_inquire, btn_deposit, btn_withdraw, btn_transfer, btn_Oexit,
btn_inquireMoney, btn_withdrawJournal, btn_depositJournal, btn_transferJournal, btn_Jexit,
btn_changePassword;
private static JTextField tLoginUser, jt_withdrawmoney, jt_accountId, jt_transfermoney ;
private static JPasswordField tLoginPassword,jt_password1, jt_password2;
private static JLabel jl_welcom;
private static String DATE;

/**
* 欢迎界面
*/
public void welcomeView() {
this.setTitle("ATM自助系统");
this.setResizable(false);
this.setDefaultCloseOperation(3);

welcomeView = new JPanel();
welcomeView.setLayout(null);

jl_welcom = new JLabel("欢迎");
jl_welcom.setBounds(225, 60, 230, 100);
jl_welcom.setFont(new Font("宋体", Font.ITALIC, 100));
jl_welcom.setForeground(Color.YELLOW);
btn_login = new JButton("登录");
btn_Wexit = new JButton("退出");
;

welcome_btn = new JPanel(new FlowLayout());
welcome_btn.add(btn_login);
welcome_btn.add(btn_Wexit);
welcome_btn.setBounds(225, 200, 250, 100);
welcomeView.add(jl_welcom);

welcomeView.add(welcome_btn);
welcome_btn.setOpaque(false);

// 登录按钮的点击事件
btn_login.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
loginView();
// operationView();
// USERID = 11111111;
}
});
// 退出
btn_Wexit.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

setBackGround(welcomeView);
this.setLocationRelativeTo(null);
this.setVisible(true);

}

/**
* 设置背景
*
* @param jpanel
*/
public void setBackGround(JPanel jpanel) {
// 加载图片
ImageIcon icon = new ImageIcon("image/ATMWelcome.png");
JLabel label = new JLabel(icon);
// 设置label的大小
label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
// 获取窗口的第二层,将label放入
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
// 获取frame的顶层容器,并设置为透明
JPanel j = (JPanel) this.getContentPane();
j.setOpaque(false);
jpanel.setOpaque(false);
this.setSize(icon.getIconWidth(), icon.getIconHeight());
this.add(jpanel);
this.setVisible(true);
}

/**
* 登录界面
*/
public void loginView() {
loginView = new JFrame();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
loginView.setBounds(((int) dimension.getWidth() - 200) / 2, ((int) dimension.getHeight() - 300) / 2, 200, 150);
loginView.setResizable(false);
loginView.setLayout(null);

JLabel label1 = new JLabel("卡号:");
label1.setBounds(10, 10, 100, 30);
loginView.add(label1);

JLabel label2 = new JLabel("密码:");
label2.setBounds(10, 40, 100, 30);
loginView.add(label2);

tLoginUser = new JTextField();
tLoginUser.setBounds(50, 15, 130, 20);
loginView.add(tLoginUser);

tLoginPassword = new JPasswordField();
tLoginPassword.setBounds(50, 45, 130, 20);
loginView.add(tLoginPassword);

btn_login = new JButton("Login");
btn_login.setBounds(10, 75, 170, 40);
btn_login.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {
int userId = Integer.valueOf(tLoginUser.getText());
int password = Integer.valueOf(new String(tLoginPassword.getPassword()));
if (userDao.haveCustomer(userId)) {
if (userDao.isPassword(userId, password)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
USERID = userId;
DATE = sdf.format(new Date());
userDao.journal(USERID, "登录:成功!");
JOptionPane.showMessageDialog(null, "登陆成功", "提示", JOptionPane.INFORMATION_MESSAGE);
loginView.setVisible(false);
operationView();
} else {
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.ERROR_MESSAGE);
tLoginPassword.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "用户不存在", "提示", JOptionPane.ERROR_MESSAGE);
tLoginPassword.setText("");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});
loginView.add(btn_login);
loginView.setVisible(true);
}

/**
* 业务界面
*/
public void operationView() {
operationView = new JPanel();
operationView.setLayout(null);

btn_inquire = new JButton("查询");
btn_inquire.addActionListener(new BL());
btn_changePassword = new JButton("修改密码");
btn_changePassword.addActionListener(new BL());
btn_deposit = new JButton("存款");
btn_deposit.addActionListener(new BL());
btn_withdraw = new JButton("取款");
btn_withdraw.addActionListener(new BL());
btn_transfer = new JButton("转账");
btn_transfer.addActionListener(new BL());
btn_Oexit = new JButton("退出");
btn_Oexit.addActionListener(new BL());
operationView.add(btn_inquire);
operationView.add(btn_deposit);
operationView.add(btn_withdraw);
operationView.add(btn_transfer);
operationView.add(btn_Oexit);
operationView.add(btn_changePassword);
btn_inquire.setBounds(2, 50, 100, 50);
btn_transfer.setBounds(2, 175, 100, 50);
btn_changePassword.setBounds(2, 300, 100, 50);
btn_deposit.setBounds(550, 50, 100, 50);
btn_withdraw.setBounds(550, 175, 100, 50);
btn_Oexit.setBounds(550, 300, 100, 50);

this.remove(welcomeView);
setBackGround(operationView);
}

/**
* operation内部类
*
* @author Administrator
*
*/
class BL implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == btn_inquire) {
System.out.println("查询");
inquireView();
}
if (o == btn_deposit) {
System.out.println("取款");
depositView();
}
if (o == btn_withdraw) {
System.out.println("存款");
withdrawView();
}
if (o == btn_transfer) {
System.out.println("转账");
transferView();
}
if (o == btn_changePassword) {
System.out.println("修改密码");
changePasswordView();
}
if (o == btn_Oexit) {
JFrame jf = new JFrame("凭条");
Container contentpane = jf.getContentPane();
jf.setLayout(new GridLayout(1, 1));
try {
ListModel<String> mode;
mode = new ListModel<String>() {
List<String> list = userDao.dayJournal(DATE, USERID);

@Override
public int getSize() {
return list.size();
}

@Override
public String getElementAt(int index) {
return (index + 1) + "." + list.get(index++);
}

@Override
public void addListDataListener(ListDataListener l) {
// TODO Auto-generated method stub

}

@Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto-generated method stub

}
};

JList<String> list = new JList<>(mode);
contentpane.add(new JScrollPane(list));

} catch (SQLException e1) {
e1.printStackTrace();
}
jf.pack();
jf.setLocationRelativeTo(null);
jf.setResizable(false);
ATMView.this.remove(operationView);
welcomeView();
jf.setVisible(true);
}

}

}

/**
* 查询界面
*/
public void inquireView() {
inquireView = new JPanel();
inquireView.setLayout(null);

btn_inquireMoney = new JButton("查询账户余额");
btn_inquireMoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(null, "余额:" + userDao.money(USERID) + "元", "余额",
JOptionPane.INFORMATION_MESSAGE);
System.out.println(userDao.money(USERID));
} catch (Exception e1) {
e1.printStackTrace();
}
}
});

btn_withdrawJournal = new JButton("查询存款记录");
btn_withdrawJournal.addActionListener(new IBJ());
btn_depositJournal = new JButton("查询取款记录");
btn_depositJournal.addActionListener(new IBJ());
btn_transferJournal = new JButton("查询转账记录");
btn_transferJournal.addActionListener(new IBJ());
btn_Jexit = new JButton("退出");
btn_Jexit.addActionListener(new IBJ());

inquireView.add(btn_inquireMoney);
inquireView.add(btn_withdrawJournal);
inquireView.add(btn_depositJournal);
inquireView.add(btn_transferJournal);
inquireView.add(btn_Jexit);
btn_inquireMoney.setBounds(2, 100, 150, 50);
btn_withdrawJournal.setBounds(2, 250, 150, 50);
btn_depositJournal.setBounds(500, 50, 150, 50);
btn_transferJournal.setBounds(500, 175, 150, 50);
btn_Jexit.setBounds(550, 300, 100, 50);

this.remove(operationView);
setBackGround(inquireView);
}

class IBJ implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
try {
if (o == btn_withdrawJournal) {
journalView("存款");
}
if (o == btn_depositJournal) {
journalView("取款");
}
if (o == btn_transferJournal) {
journalView("转账");
}
} catch (Exception e1) {
e1.printStackTrace();
}
if (o == btn_Jexit) {
ATMView.this.remove(inquireView);
setBackGround(operationView);
}
}

}

/**
* 记录界面
*
* @throws Exception
*/
public void journalView(String genre) throws Exception {
JFrame jf = new JFrame(genre);
Container contentpane = jf.getContentPane();
jf.setLayout(new GridLayout(1, 1));

ListModel<String> mode = new ListModel<String>() {
List<String> list = userDao.checkJournal(USERID, genre);

@Override
public int getSize() {
return list.size();
}

@Override
public String getElementAt(int index) {
return (index + 1) + "." + list.get(index++);
}

@Override
public void addListDataListener(ListDataListener l) {
// TODO Auto-generated method stub

}

@Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto-generated method stub

}
};

JList<String> list = new JList<>(mode);
contentpane.add(new JScrollPane(list));

jf.pack();
jf.setLocationRelativeTo(null);
jf.setResizable(false);
jf.setVisible(true);

}

/**
* 取款界面
*/
public void withdrawView() {
withdrawView = new JPanel();
withdrawView.setLayout(null);

JPanel jp1 = new JPanel(new GridLayout(2, 1));
JLabel jl_label = new JLabel(" 请输入取款金额");
jl_label.setFont(new Font("宋体", Font.ITALIC, 20));
jl_label.setForeground(Color.orange);
jt_withdrawmoney = new JTextField();
jp1.add(jl_label);
jp1.add(jt_withdrawmoney);
jp1.setOpaque(false);

JPanel jp2 = new JPanel(new FlowLayout());
JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("退出");
jp2.add(jb1);
jp2.add(jb2);
jp2.setOpaque(false);

withdrawView.add(jp1);
withdrawView.add(jp2);
jp1.setBounds(225, 60, 230, 100);
jp2.setBounds(250, 200, 150, 100);

jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double money = Double.valueOf(jt_withdrawmoney.getText());
if (money <= 0) {
JOptionPane.showMessageDialog(null, "请输入正确数值", "提示", JOptionPane.ERROR_MESSAGE);
jt_withdrawmoney.setText("");
return;
}
int userPassword = Integer.valueOf(JOptionPane.showInputDialog("请输入密码"));
userDao.withdraw(USERID, money, userPassword);
ATMView.this.remove(withdrawView);
operationView();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "金额或密码为空!", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});

jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(withdrawView);
operationView();
}
});
this.remove(operationView);
setBackGround(withdrawView);

}

/**
* 存款界面
*/
public void depositView() {
depositView = new JPanel();
depositView.setLayout(null);

JButton jb1 = new JButton("开始存款");
JButton jb2 = new JButton("退出存款");
depositView.add(jb1);
depositView.add(jb2);
jb1.setBounds(225, 60, 230, 100);
jb2.setBounds(550, 300, 100, 50);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String src = JOptionPane.showInputDialog("输入存放的金额");
double money = Double.valueOf(src);
if (money > 0) {
try {
userDao.deposit(USERID, money);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "请输入正确数值", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});

jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(depositView);
operationView();
}
});
this.remove(operationView);
setBackGround(depositView);
}

/**
* 转账界面
*/
public void transferView() {
transferView = new JPanel();
transferView.setLayout(null);

JPanel jp1 = new JPanel(new GridLayout(2, 1));
JLabel jl1 = new JLabel(" 对方账户");
jp1.add(jl1);
jt_accountId = new JTextField();
jp1.add(jt_accountId);
jl1.setFont(new Font("宋体", Font.ITALIC, 20));
jl1.setForeground(Color.YELLOW);
jp1.setOpaque(false);

JPanel jp2 = new JPanel(new GridLayout(2, 1));
JLabel jl2 = new JLabel(" 转账金额");
jp2.add(jl2);
jt_transfermoney = new JTextField();
jp2.add(jt_transfermoney);
jl2.setFont(new Font("宋体", Font.ITALIC, 20));
jl2.setForeground(Color.YELLOW);
jp2.setOpaque(false);

JPanel jp3 = new JPanel(new FlowLayout());
JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("退出");
jp3.setOpaque(false);
jp3.add(jb1);
jp3.add(jb2);

jp1.setBounds(200, 30, 200, 100);
jp2.setBounds(200, 140, 200, 100);
jp3.setBounds(200, 250, 200, 100);

transferView.add(jp1);
transferView.add(jp2);
transferView.add(jp3);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int accountId = 0;
double money = 0;
try {
accountId = Integer.valueOf(jt_accountId.getText());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入对方卡号!", "提示", JOptionPane.ERROR_MESSAGE);
}
try {
money = Double.valueOf(jt_transfermoney.getText());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示", JOptionPane.ERROR_MESSAGE);
}
if (accountId == USERID) {
JOptionPane.showMessageDialog(null, "不能转账给自己", "提示", JOptionPane.ERROR_MESSAGE);
jt_accountId.setText("");
return;
}
if (money <= 0) {
JOptionPane.showMessageDialog(null, "请输入正确金额", "提示", JOptionPane.ERROR_MESSAGE);
jt_transfermoney.setText("");
return;
}
try {
if (!userDao.haveCustomer(accountId)) {
JOptionPane.showMessageDialog(null, "账户不存在", "提示", JOptionPane.ERROR_MESSAGE);
jt_accountId.setText("");
return;
}
String accountName = JOptionPane.showInputDialog("请确认对方身份");
if (accountName.equals(userDao.userName(accountId))) {
int userPassword = Integer.valueOf(JOptionPane.showInputDialog("请输入密码"));
userDao.transfer_accounts(USERID, accountId, money, userPassword);
} else {
JOptionPane.showMessageDialog(null, "对方身份不符", "提示", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});

jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(transferView);
operationView();
}
});

this.remove(operationView);
setBackGround(transferView);
}

/**
* 修改密码界面
*/
public void changePasswordView() {
try {
int userPassword = Integer.valueOf(JOptionPane.showInputDialog("请输入密码"));
if (userDao.isPassword(USERID, userPassword)) {
changePasswordView = new JPanel();
changePasswordView.setLayout(null);

JPanel jp1 = new JPanel(new GridLayout(2, 1));
JLabel jl1 = new JLabel(" 请输入新密码");
jp1.add(jl1);
jt_password1 = new JPasswordField();
jp1.add(jt_password1);
jl1.setFont(new Font("宋体", Font.ITALIC, 20));
jl1.setForeground(Color.YELLOW);
jp1.setOpaque(false);

JPanel jp2 = new JPanel(new GridLayout(2, 1));
JLabel jl2 = new JLabel(" 请确认新密码");
jp2.add(jl2);
jt_password2 = new JPasswordField();
jp2.add(jt_password2);
jl2.setFont(new Font("宋体", Font.ITALIC, 20));
jl2.setForeground(Color.YELLOW);
jp2.setOpaque(false);

JPanel jp3 = new JPanel(new FlowLayout());
JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("退出");
jp3.setOpaque(false);
jp3.add(jb1);
jp3.add(jb2);

jp1.setBounds(200, 30, 200, 100);
jp2.setBounds(200, 140, 200, 100);
jp3.setBounds(200, 250, 200, 100);
changePasswordView.add(jp1);
changePasswordView.add(jp2);
changePasswordView.add(jp3);

jb1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {
int userPassword1 = Integer.valueOf(new String(jt_password1.getPassword()));
int userPassword2 = Integer.valueOf(new String(jt_password2.getPassword()));
if (userPassword1 == userPassword2) {
userDao.changePassword(USERID, userPassword1);
ATMView.this.remove(changePasswordView);
welcomeView();
} else {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致!", "提示", JOptionPane.ERROR_MESSAGE);
jt_password1.setText("");
jt_password2.setText("");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入新密码!", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});

jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(changePasswordView);
operationView();
}
});
this.remove(operationView);
setBackGround(changePasswordView);
} else {
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
System.out.println("取消输入");
}
}

public static void main(String[] args) {
ATMView atm = new ATMView();
atm.welcomeView();
}
}

代码就这些,有点简陋。本人界面不怎么会,所以界面运行出来不好看

3.程序运行

 

转载于:https://www.cnblogs.com/Wu9Li7/p/9544222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值