package view;
import service.AtmService;
import util.BaseFrame;
import util.MySpring;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("all")
public class AtmFrame extends BaseFrame {
private AtmFrame(String aname){
super("操作窗口");
this.aname = aname;
this.init();
}
private static AtmFrame atmFrame;
public synchronized static AtmFrame getAtmFrame(String aname){
if(atmFrame==null){
atmFrame = new AtmFrame(aname);
}
return atmFrame;
}
private String aname;
private AtmService service = MySpring.getBean("service.AtmService");
private JPanel mainPanel = new JPanel();
private JLabel logoLabel = new JLabel();
private JLabel titleLabel = new JLabel("银行");
private JLabel balanceLabelCN = new JLabel();
private JLabel balanceLabelEN = new JLabel();
private JLabel selectServerLabelCN = new JLabel("您好!请选择所需服务");
private JLabel selectServerLabelEN = new JLabel("Please Select Service");
private JButton messageButton = new JButton("销户");
private JButton exitButton = new JButton("退出");
private JButton depositButton = new JButton("存款");
private JButton withdrawalButton = new JButton("取款");
private JButton transferButton = new JButton("转账");
protected void setFontAndSoOn() {
mainPanel.setLayout(null);
logoLabel.setBounds(20,20,80,80);
logoLabel.setIcon(this.drawImage("src//img//duyi.jpg",80,80));
titleLabel.setBounds(120,30,160,60);
titleLabel.setFont(new Font("微软雅黑",Font.ITALIC,32));
balanceLabelCN.setBounds(250,200,300,40);
balanceLabelCN.setFont(new Font("微软雅黑",Font.BOLD,24));
balanceLabelCN.setHorizontalAlignment(JTextField.CENTER);
balanceLabelCN.setText("账户余额:¥"+service.inquire(aname));
balanceLabelEN.setBounds(240,240,320,40);
balanceLabelEN.setFont(new Font("微软雅黑",Font.BOLD,24));
balanceLabelEN.setHorizontalAlignment(JTextField.CENTER);
balanceLabelEN.setText("Account Balance:¥"+service.inquire(aname));
selectServerLabelCN.setBounds(250,370,300,40);
selectServerLabelCN.setFont(new Font("微软雅黑",Font.BOLD,16));
selectServerLabelCN.setHorizontalAlignment(JTextField.CENTER);
selectServerLabelEN.setBounds(250,400,300,40);
selectServerLabelEN.setFont(new Font("微软雅黑",Font.BOLD,16));
selectServerLabelEN.setHorizontalAlignment(JTextField.CENTER);
messageButton.setBounds(10,150,120,40);
messageButton.setFont(new Font("微软雅黑",Font.BOLD,14));
messageButton.setBackground(Color.lightGray);
messageButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
exitButton.setBounds(10,390,120,40);
exitButton.setFont(new Font("微软雅黑",Font.BOLD,14));
exitButton.setBackground(Color.lightGray);
exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
depositButton.setBounds(670,150,120,40);
depositButton.setFont(new Font("微软雅黑",Font.BOLD,14));
depositButton.setBackground(Color.lightGray);
depositButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
withdrawalButton.setBounds(670,270,120,40);
withdrawalButton.setFont(new Font("微软雅黑",Font.BOLD,14));
withdrawalButton.setBackground(Color.lightGray);
withdrawalButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
transferButton.setBounds(670,390,120,40);
transferButton.setFont(new Font("微软雅黑",Font.BOLD,14));
transferButton.setBackground(Color.lightGray);
transferButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
protected void addElements() {
mainPanel.add(logoLabel);
mainPanel.add(titleLabel);
mainPanel.add(balanceLabelCN);
mainPanel.add(balanceLabelEN);
mainPanel.add(selectServerLabelCN);
mainPanel.add(selectServerLabelEN);
mainPanel.add(messageButton);
mainPanel.add(exitButton);
mainPanel.add(depositButton);
mainPanel.add(withdrawalButton);
mainPanel.add(transferButton);
this.add(mainPanel);
}
protected void addListener() {
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int value = JOptionPane.showConfirmDialog(AtmFrame.this,"确认退出系统么?");
if(value==0){
System.exit(0);
}
}
});
messageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int value = JOptionPane.showConfirmDialog(AtmFrame.this,"确认消除当前账号的所有信息么?");
if(value==0){
if(service.closeAccount(aname)==1){
JOptionPane.showMessageDialog(AtmFrame.this,"销户成功");
System.exit(0);
}else{
JOptionPane.showMessageDialog(AtmFrame.this,"清除失败");
}
}
}
});
depositButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String value = JOptionPane.showInputDialog(AtmFrame.this,"请输入存款金额");
if(value!=null && !"".equals(value)){
Float depositMoney = Float.parseFloat(value);
if(depositMoney<=0){
throw new NumberFormatException();
}
int count = service.deposit(aname,depositMoney);
if(count==1){
JOptionPane.showMessageDialog(AtmFrame.this,"存款成功");
balanceLabelCN.setText("账户余额:¥" + service.inquire(aname));
balanceLabelEN.setText("Account Balance:¥"+service.inquire(aname));
}else{
JOptionPane.showMessageDialog(AtmFrame.this,"存款失败");
}
}
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(AtmFrame.this,"输入金额有误");
}
}
});
withdrawalButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String value = JOptionPane.showInputDialog(AtmFrame.this,"请输入取款金额");
if(value!=null && !"".equals(value)){
Float withdrawalMoney = Float.parseFloat(value);
if(withdrawalMoney<=0){
throw new NumberFormatException();
}
int count = service.withdrawal(aname,withdrawalMoney);
if(count==1){
JOptionPane.showMessageDialog(AtmFrame.this,"取款成功");
balanceLabelCN.setText("账户余额:¥" + service.inquire(aname));
balanceLabelEN.setText("Account Balance:¥"+service.inquire(aname));
}else if(count==-1){
JOptionPane.showMessageDialog(AtmFrame.this,"对不起,余额不足");
}else{
JOptionPane.showMessageDialog(AtmFrame.this,"存款失败");
}
}
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(AtmFrame.this,"输入金额有误");
}
}
});
transferButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String name = JOptionPane.showInputDialog(AtmFrame.this,"请输入转账账户");
if(name!=null && !"".equals(name) && service.isExist(name)){
String value = JOptionPane.showInputDialog(AtmFrame.this,"请输入转账金额");
if(value!=null && !"".equals(value)){
Float transferMoney = Float.parseFloat(value);
if(transferMoney<=0){
throw new NumberFormatException();
}
int count = service.transfer(aname,name,transferMoney);
if(count==2){
JOptionPane.showMessageDialog(AtmFrame.this,"转账成功");
balanceLabelCN.setText("账户余额:¥" + service.inquire(aname));
balanceLabelEN.setText("Account Balance:¥"+service.inquire(aname));
}else if(count==-1){
JOptionPane.showMessageDialog(AtmFrame.this,"对不起,余额不足");
}else{
JOptionPane.showMessageDialog(AtmFrame.this,"存款失败");
}
}
}else{
JOptionPane.showMessageDialog(AtmFrame.this,"输入的账号不存在");
}
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(AtmFrame.this,"输入金额有误");
}
}
});
}
protected void setFrameSelf() {
this.setBounds(300,200,800,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
}
package view;
import service.AtmService;
import util.BaseFrame;
import util.MySpring;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("all")
public class LoginFrame extends BaseFrame {
private LoginFrame(){
super("登录窗口");
this.init();
}
private static LoginFrame loginFrame;
public synchronized static LoginFrame getLoginFrame(){
if(loginFrame==null){
loginFrame = new LoginFrame();
}
return loginFrame;
}
private JPanel mainPanel = new JPanel();
private JLabel logoLabel = new JLabel();
private JLabel titleLabel = new JLabel("请 您 登 录 系 统");
private JLabel accountLabel = new JLabel("请输入账号:");
private JTextField accountField = new JTextField();
private JLabel passwordLabel = new JLabel("请输入密码:");
private JPasswordField passwordField = new JPasswordField();
private JButton loginButton = new JButton("登 录");
private JButton registButton = new JButton("注 册");
private RegistFrame registFrame = null;
protected void setFontAndSoOn() {
mainPanel.setLayout(null);
logoLabel.setBounds(135,40,40,40);
logoLabel.setIcon(this.drawImage("src//img//duyi.jpg",40,40));
titleLabel.setBounds(185,40,200,40);
titleLabel.setFont(new Font("微软雅黑",Font.BOLD,24));
accountLabel.setBounds(40,100,140,40);
accountLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
accountField.setBounds(170,105,260,32);
accountField.setFont(new Font("微软雅黑",Font.BOLD,20));
passwordLabel.setBounds(40,150,140,40);
passwordLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
passwordField.setBounds(170,155,260,32);
passwordField.setFont(new Font("微软雅黑",Font.BOLD,20));
loginButton.setBounds(120,210,100,32);
loginButton.setFont(new Font("微软雅黑",Font.BOLD,14));
loginButton.setBackground(Color.LIGHT_GRAY);
registButton.setBounds(260,210,100,32);
registButton.setFont(new Font("微软雅黑",Font.BOLD,14));
registButton.setBackground(Color.LIGHT_GRAY);
}
protected void addElements() {
mainPanel.add(logoLabel);
mainPanel.add(titleLabel);
mainPanel.add(accountLabel);
mainPanel.add(accountField);
mainPanel.add(passwordLabel);
mainPanel.add(passwordField);
mainPanel.add(loginButton);
mainPanel.add(registButton);
this.add(mainPanel);
}
protected void addListener() {
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String aname = accountField.getText();
String apassword = new String(passwordField.getPassword());
AtmService service = MySpring.getBean("service.AtmService");
String result = service.login(aname,apassword);
if(result.equals("登录成功")){
LoginFrame.this.setVisible(false);
AtmFrame.getAtmFrame(aname);
}else{
JOptionPane.showMessageDialog(LoginFrame.this,"对不起,"+result);
LoginFrame.this.reset();
}
}
});
registButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginFrame.this.setVisible(false);
if(registFrame==null) {
registFrame = RegistFrame.getRegistFrame();
}else{
registFrame.setVisible(true);
registFrame.reset();
}
}
});
}
void reset(){
accountField.setText("");
passwordField.setText("");
}
protected void setFrameSelf() {
this.setBounds(400,200,500,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
}
package view;
import service.AtmService;
import util.BaseFrame;
import util.MySpring;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("all")
public class RegistFrame extends BaseFrame {
private RegistFrame(){
super("注册窗口");
this.init();
}
private static RegistFrame registFrame;
public synchronized static RegistFrame getRegistFrame(){
if(registFrame==null){
registFrame = new RegistFrame();
}
return registFrame;
}
private LoginFrame loginFrame = LoginFrame.getLoginFrame();
private JPanel mainPanel = new JPanel();
private JLabel logoLabel = new JLabel();
private JLabel titleLabel = new JLabel("请 您 填 写 信 息");
private JLabel accountLabel = new JLabel("请输入账号:");
private JTextField accountField = new JTextField();
private JLabel passwordLabel = new JLabel("请输入密码:");
private JTextField passwordField = new JTextField();
private JLabel balanceLabel = new JLabel("请输入金额:");
private JTextField balanceField = new JTextField();
private JButton registButton = new JButton("注 册");
private JButton resetButton = new JButton("重 置");
private JButton backButton = new JButton("返 回");
protected void setFontAndSoOn() {
mainPanel.setLayout(null);
logoLabel.setBounds(135,40,40,40);
logoLabel.setIcon(this.drawImage("src//img//duyi.jpg",40,40));
titleLabel.setBounds(185,40,200,40);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD,24));
accountLabel.setBounds(40,100,140,40);
accountLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
accountField.setBounds(170,105,260,32);
accountField.setFont(new Font("微软雅黑",Font.BOLD,20));
passwordLabel.setBounds(40,150,140,40);
passwordLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
passwordField.setBounds(170,155,260,32);
passwordField.setFont(new Font("微软雅黑",Font.BOLD,20));
balanceLabel.setBounds(40,200,140,40);
balanceLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
balanceField.setBounds(170,205,260,32);
balanceField.setFont(new Font("微软雅黑",Font.BOLD,20));
registButton.setBounds(60,260,100,32);
registButton.setFont(new Font("微软雅黑",Font.BOLD,14));
registButton.setBackground(Color.LIGHT_GRAY);
resetButton.setBounds(190,260,100,32);
resetButton.setFont(new Font("微软雅黑",Font.BOLD,14));
resetButton.setBackground(Color.LIGHT_GRAY);
backButton.setBounds(320,260,100,32);
backButton.setFont(new Font("微软雅黑",Font.BOLD,14));
backButton.setBackground(Color.LIGHT_GRAY);
}
protected void addElements() {
mainPanel.add(logoLabel);
mainPanel.add(titleLabel);
mainPanel.add(accountLabel);
mainPanel.add(accountField);
mainPanel.add(passwordLabel);
mainPanel.add(passwordField);
mainPanel.add(balanceLabel);
mainPanel.add(balanceField);
mainPanel.add(registButton);
mainPanel.add(resetButton);
mainPanel.add(backButton);
this.add(mainPanel);
}
protected void addListener() {
registButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
String aname = accountField.getText();
String apassword = passwordField.getText();
String abalance = balanceField.getText();
AtmService service = MySpring.getBean("service.AtmService");
if(service.isExist(aname)){
JOptionPane.showMessageDialog(RegistFrame.this,"账号已存在");
RegistFrame.this.reset();
}else {
try{
service.regist(aname,apassword,Float.parseFloat(abalance));
JOptionPane.showMessageDialog(RegistFrame.this, "注册成功,请登录系统");
RegistFrame.this.back();
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(RegistFrame.this, "对不起,您输入的金额有误");
RegistFrame.this.reset();
}
}
}
});
resetButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
RegistFrame.this.reset();
}
});
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
RegistFrame.this.setVisible(false);
loginFrame.setVisible(true);
loginFrame.reset();
}
});
}
void reset(){
accountField.setText("");
passwordField.setText("");
balanceField.setText("");
}
private void back(){
this.setVisible(false);
loginFrame.setVisible(true);
}
protected void setFrameSelf() {
this.setBounds(430,200,500,360);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
}
package domain;
public class Atm {
private String aname;
private String apassword;
private Float abalance;
public Atm() {}
public Atm(String aname, String apassword, Float abalance) {
this.aname = aname;
this.apassword = apassword;
this.abalance = abalance;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getApassword() {
return apassword;
}
public void setApassword(String apassword) {
this.apassword = apassword;
}
public Float getAbalance() {
return abalance;
}
public void setAbalance(Float abalance) {
this.abalance = abalance;
}
}
package dao;
import domain.Atm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@SuppressWarnings("all")
public class AtmDao {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/atm?useSSL=true";
private String user = "root";
private String password = "root";
public Atm selectOne(String aname){
Atm atm = null;
String sql = "SELECT ANAME,APASSWORD,ABALANCE FROM ATM WHERE ANAME = ?";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
PreparedStatement pstat = conn.prepareStatement(sql);
pstat.setString(1,aname);
ResultSet rs = pstat.executeQuery();
if(rs.next()){
atm = new Atm();
atm.setAname(rs.getString("aname"));
atm.setApassword(rs.getString("apassword"));
atm.setAbalance(rs.getFloat("abalance"));
}
rs.close();
pstat.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return atm;
}
public int insert(Atm atm){
int count = 0;
String sql = "INSERT INTO ATM VALUES(?,?,?)";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
PreparedStatement pstat = conn.prepareStatement(sql);
pstat.setString(1,atm.getAname());
pstat.setString(2,atm.getApassword());
pstat.setFloat(3,atm.getAbalance());
count = pstat.executeUpdate();
pstat.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
public int update(Atm atm){
int count = 0;
String sql = "UPDATE ATM SET APASSWORD = ? , ABALANCE = ? WHERE ANAME = ?";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
PreparedStatement pstat = conn.prepareStatement(sql);
pstat.setString(1,atm.getApassword());
pstat.setFloat(2,atm.getAbalance());
pstat.setString(3,atm.getAname());
count = pstat.executeUpdate();
pstat.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
public int delete(String aname){
int count = 0;
String sql = "DELETE FROM ATM WHERE ANAME = ?";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
PreparedStatement pstat = conn.prepareStatement(sql);
pstat.setString(1,aname);
count = pstat.executeUpdate();
pstat.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
}
package service;
import dao.AtmDao;
import domain.Atm;
import util.MySpring;
public class AtmService {
private AtmDao dao = MySpring.getBean("dao.AtmDao");
public String login(String aname,String apassword){
Atm atm = dao.selectOne(aname);
if(atm!=null && atm.getApassword().equals(apassword)){
return "登录成功";
}
return "用户名或密码错误";
}
public Float inquire(String aname){
return dao.selectOne(aname).getAbalance();
}
public int regist(String aname,String apassword,Float abalance){
Atm atm = new Atm(aname,apassword,abalance);
return dao.insert(atm);
}
public boolean isExist(String aname){
if(dao.selectOne(aname)!=null){
return true;
}
return false;
}
public int deposit(String aname,Float depositMoney){
Atm atm = dao.selectOne(aname);
atm.setAbalance(atm.getAbalance()+depositMoney);
return dao.update(atm);
}
public int withdrawal(String aname,Float withdrawalMoney){
Atm atm = dao.selectOne(aname);
if(atm.getAbalance()>=withdrawalMoney){
atm.setAbalance(atm.getAbalance()-withdrawalMoney);
return dao.update(atm);
}else{
return -1;
}
}
public int transfer(String outName,String inName,Float transferMoney){
Atm outAtm = dao.selectOne(outName);
Atm inAtm = dao.selectOne(inName);
if(outAtm.getAbalance()>=transferMoney){
outAtm.setAbalance(outAtm.getAbalance() - transferMoney);
inAtm.setAbalance(inAtm.getAbalance() + transferMoney);
return dao.update(outAtm) + dao.update(inAtm);
}else{
return -1;
}
}
public int closeAccount(String aname){
return dao.delete(aname);
}
}
package util;
import javax.swing.*;
import java.awt.*;
public abstract class BaseFrame extends JFrame {
public BaseFrame(){}
public BaseFrame(String title){
super(title);
}
protected void init(){
this.setFontAndSoOn();
this.addElements();
this.addListener();
this.setFrameSelf();
}
protected ImageIcon drawImage(String path,int width,int height){
ImageIcon imageIcon = new ImageIcon(path);
imageIcon.setImage(imageIcon.getImage().getScaledInstance(width,height, Image.SCALE_DEFAULT));
return imageIcon;
}
protected abstract void setFontAndSoOn();
protected abstract void addElements();
protected abstract void addListener();
protected abstract void setFrameSelf();
}
package util;
import java.util.HashMap;
public class MySpring {
private static HashMap<String,Object> beanBox = new HashMap<>();
public static <T>T getBean(String className){
T obj = null;
try {
obj = (T)beanBox.get(className);
if(obj == null){
Class clazz = Class.forName(className);
obj = (T)clazz.newInstance();
beanBox.put(className,obj);
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
package test;
import view.LoginFrame;
public class TestMain {
public static void main(String[] args){
LoginFrame.getLoginFrame();
}
}