银行管理系统简单实现

银行管理系统(含数据库操作)

用户接口:
package cn.bankinterface;
/*
 * 此接口仅提供用户使用
 */


import cn.business.Customers;

public interface CustomersInterface {
	public boolean out(Customers customer,int money);//取钱
	public boolean save(Customers customer,int money);//存钱
	public boolean modify(Customers customer,String password);//修改密码
	public boolean tranfer(Customers customers1,String account,String money);//转账
	public boolean login(String account,String password);//登录
}

jdbc的封装:
package cn.bankmanagesystem.jdbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcTools {
	public Connection conn = null;
	public ResultSet rs = null;
	public Properties prop = new Properties();
	public PreparedStatement ps = null;

//	1.加载文件
	public JdbcTools() {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("src/config/jdbc.properties");
			prop.load(fis);
		} catch (FileNotFoundException e) {
			System.out.println("驱动类加载失败");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		2.加载驱动类
		try {
			Class.forName(prop.getProperty("driver"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

//	3.创建连接
	public Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("username"),
					prop.getProperty("password"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

//	4.执行查询
	public ResultSet executeQuery(String sql, Object... obj) {
//		one:获取连接
		conn = getConnection();
//		two:构建sql
		try {
			ps = conn.prepareStatement(sql);
//			three:遍历并且设置值
			for (int i = 0; i < obj.length; i++) {
				ps.setObject(i + 1, obj[i]);// 给一个行的第几列设置对应的值
			}
//			four:执行sql
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		返回结果集
		return rs;
	}

//	5.执行更新
	public int executeUpdate(String sql, Object... obj) {
//		步骤同查询操作
		int count = 0;
		conn = getConnection();
		try {
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < obj.length; i++) {
				ps.setObject(i + 1, obj[i]);
			}
			count = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

//	6.关闭
	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

用户功能:
package cn.business;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.bankinterface.CustomersInterface;
import cn.bankmanagesystem.jdbc.JdbcTools;

public class CustomerFunction implements CustomersInterface {
	JdbcTools tools = new JdbcTools();
	Customers customer = new Customers();
	static String account;
	static int balance;//转账用户的余额
	static String tranferAccount;
	static int thisBalance;

	@Override
	public boolean out(Customers customer, int money) {
		if((customer.getBalance()<=0)||(customer.getBalance()<money)) {
			System.out.println("余额不足");
		}else {
			int balance = customer.getBalance() - money;
			customer.setBalance(balance);
			String sql = "update customers set balance = "+customer.getBalance()+" where account = "+customer.getAccount();
			tools.executeUpdate(sql);
			return true;
		}
		return false;
	}

	@Override
	public boolean save(Customers customer, int money) {
		int balance = customer.getBalance()+money;
		customer.setBalance(balance);
		String sql = "update customers set balance = "+customer.getBalance()+" where account = "+customer.getAccount();
		tools.executeUpdate(sql);
		return true;
	}

	@Override
	public boolean modify(Customers customer, String password) {//bug返回最初登录页面
		String sql = "update customers set user_password = "+password+" where account = "+customer.getAccount();
		tools.executeUpdate(sql);
		return true;
	}


	@Override
	public boolean tranfer(Customers customers1, String account,String money) {
		String sql = "select account from customers where account = "+account;
		ResultSet rset = tools.executeQuery(sql);
		try {
			while(rset.next()) {
				tranferAccount = rset.getString("account");
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		if(tranferAccount.equals(account)&&(customers1.getBalance()>=Integer.parseInt(money))) {
			//获取转账用户的余额
			String sql1 = "select balance from customers where account = "+account;
			ResultSet rs = tools.executeQuery(sql1);
			try {
				while(rs.next()) {
					balance = rs.getInt("balance");//获取该用户的余额
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			int newBalance = balance+Integer.parseInt(money);
			String sql2 = "update customers set balance = "+newBalance+" where account = "+account;
			tools.executeUpdate(sql2);
			
			//转账后本用户减少金额
			String sql3 = "select balance from customers where account = "+customers1.getAccount();
			ResultSet rs2 = tools.executeQuery(sql3);
			try {
				while(rs2.next()) {
					thisBalance = rs2.getInt("balance");//获取该用户的余额
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			int Balance = thisBalance-Integer.parseInt(money);
			String sql4 = "update customers set balance = "+Balance+" where account = "+customers1.getAccount();
			tools.executeUpdate(sql4);
			return true;
		}else {
			System.out.println("没有此用户(本机余额不足)");
			return false;
		}
	}

	@Override
	public boolean login(String account, String password) {
//		String sql = "select account,password from customers where account "+account+" password "+password;
		String sql = "select * from customers where account = "+account;
		ResultSet rs = tools.executeQuery(sql);
		CustomerFunction.account = account;
		
		try {
			while (rs.next()) {
				if (account.equals(rs.getString("account")) && password.equals(rs.getString("user_password"))) {
					return true;
				} else {
					System.out.println("账号密码错误");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Customers getCustomers() {//important
		String sql = "select * from customers where account = " + account;
		ResultSet rs = tools.executeQuery(sql);
		try {
			while (rs.next()) {
				customer.setAccount(rs.getString("account"));
				customer.setPassword(rs.getString("user_password"));
				customer.setId(rs.getString("id"));
				customer.setBalance(rs.getInt("balance"));
				customer.setAddress(rs.getString("address"));
				customer.setName(rs.getString("name"));
				customer.setSex(rs.getString("sex"));
				customer.setTel(rs.getString("tel"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return customer;

	}

}

用户类:
package cn.business;


public class Customers{
	private String account;//账号
	private String password;//密码
	private int balance;//余额
	
	private String name;//姓名
	private String id;//身份证
	private String sex;//性别
	private String address;//家庭住址
	private String tel;//电话号码
	public Customers() {
	}
	public Customers(String account, String password, int balance, String name, String id, String sex, String address,
			String tel) {
		super();
		this.account = account;
		this.password = password;
		this.balance = balance;
		this.name = name;
		this.id = id;
		this.sex = sex;
		this.address = address;
		this.tel = tel;
	}
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	
	@Override
	public String toString() {
		return "Customers [account=" + account + ", balance=" + balance + ", name=" + name
				+ ", sex=" + sex + "]";
	}
	
	
	
}

开户操作:
package cn.business;


import java.util.Random;

import cn.bankmanagesystem.jdbc.JdbcTools;

public class OpenAccount {
	JdbcTools tools = new JdbcTools();//开户
	public boolean openAccount(Customers customer) {
		
		customer.setAccount((accountNum()+""));
		String sql = "insert into customers values(?,?,?,?,?,?,?,?)";
		int count = tools.executeUpdate(sql,customer.getId(),customer.getName(),
				customer.getSex(),customer.getAddress(),customer.getTel(),customer.getAccount(),customer.getPassword(),customer.getBalance());
		if(count > 0) {
			return true;
		}
		return false;
	}
	public long accountNum() {
		StringBuffer base = new StringBuffer();
		Random random = new Random();
		base.append("6");
		for(int i = 0;i < 18;i++) {
			int j = random.nextInt(10);
			base.append(j);
		}
		return Long.parseLong(base.toString());
	}
}

登录界面:
package cn.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.bankmanagesystem.jdbc.JdbcTools;
import cn.business.CustomerFunction;
import cn.business.Customers;
import cn.business.OpenAccount;

public class Register extends JFrame{
	Label text = new Label("欢迎使用银行管理系统");
	JTextField account = null;
	JTextField password = null;
	JButton login = null;
	JButton zhuce = null;
	
	
	
	class RegisterUser implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource()==login) {//登录操作
				CustomerFunction customer1 = new CustomerFunction();
				if(customer1.login(account.getText(), password.getText())) {
					Register register = new Register();
					if(register.isVisible()) {
						register.setVisible(false);
					}
					Function function = new Function();
					function.setVisible(true);
				}
			}
			
			if(e.getSource() == zhuce) {
				//执行一个注册操作
				new ZhuCe();
			}
		}
		
	}
	
	public Register(){
		super("登录");
		this.setResizable(false);
		JPanel container = new JPanel(new BorderLayout());
		RegisterUser ru = new RegisterUser();
		
		JPanel top = new JPanel();//顶部
		text.setFont(new Font("微软雅黑",3,28));
		top.add(text);
		top.setBackground(Color.gray);
		container.add(top, BorderLayout.NORTH);
		
		JPanel center = new JPanel(new GridLayout(2,2));
		Label ac = new Label("账号:");
		ac.setFont(new Font("楷体",3,28));
		center.add(ac);
		account = new JTextField();
		center.add(account);
		Label pw = new Label("密码:");
		pw.setFont(new Font("楷体",3,28));
		center.add(pw);
		password = new JTextField();
		center.add(password);
		container.add(center, BorderLayout.CENTER);
		center.setBackground(Color.white);
		
		JPanel bottom = new JPanel();
		container.add(bottom,BorderLayout.SOUTH);
		bottom.setBackground(Color.LIGHT_GRAY);
		login = new JButton("登录");
		zhuce = new JButton("注册");
		login.addActionListener(ru);
		zhuce.addActionListener(ru);
		bottom.setLayout(new GridLayout(1,2));
		bottom.add(login);
		bottom.add(zhuce);
		
		add(container);
		this.setSize(400,300);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
	}
	
}

注册界面:
package cn.gui;

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.business.Customers;
import cn.business.OpenAccount;

public class ZhuCe extends JFrame {
	Label label[] = new Label[6];
	JTextField text[] = new JTextField[6];
//	ArrayList<Customers> list = new ArrayList<>();
	Customers customer = new Customers();
	JButton btn1 = null;
	JButton btn2 = null;
	OpenAccount open = new OpenAccount();

	class ZhuceFunc implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {

			customer.setName(text[0].getText());
			customer.setId(text[1].getText());
			customer.setSex(text[2].getText());
			customer.setAddress(text[3].getText());
			customer.setTel(text[4].getText());
			customer.setPassword(text[5].getText());
			if (e.getSource() == btn1) {
//				  list.add(customer);
				if (open.openAccount(customer)) {
					class openSuccess extends JFrame {
						public openSuccess() {
							this.setSize(500, 100);
							this.setVisible(true);
							JPanel jp1 = new JPanel(new GridLayout(1, 2));
							Label label = new Label("恭喜您开户成功,您的账号为:");
							jp1.add(label);
							JTextField jtext = new JTextField();
							jtext.setText(customer.getAccount());
							jp1.add(jtext);
							this.add(jp1);
						}
					}
					new openSuccess();
				} else {
					System.out.println("开户失败");
				}
			}
		}
	}

	public ZhuCe() {
		super("注册");
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(7, 2));
		ZhuceFunc zf = new ZhuceFunc();

		label[0] = new Label("姓名");
		text[0] = new JTextField();
		label[1] = new Label("身份证");
		text[1] = new JTextField();
		label[2] = new Label("性别");
		text[2] = new JTextField();
		label[3] = new Label("家庭住址");
		text[3] = new JTextField();
		label[4] = new Label("电话号码");
		text[4] = new JTextField();
		label[5] = new Label("密码");
		text[5] = new JTextField();
		btn1 = new JButton("提交");
		btn1.addActionListener(zf);
		btn2 = new JButton("重置");
		btn2.addActionListener(zf);
		for (int i = 0; i < label.length; i++) {
			jpanel.add(label[i]);
			jpanel.add(text[i]);
			text[i].addActionListener(zf);

		}
		jpanel.add(btn1);
		jpanel.add(btn2);

		this.add(jpanel);
	}
}

用户功能区:
package cn.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.business.CustomerFunction;
import cn.business.Customers;

public class Function extends JFrame {
	JButton[] btn = new JButton[6];
	
	
	class ExecuteFunction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==btn[0]) {//取钱
				Out outMoney = new Out();
			}else if(e.getSource()==btn[1]) {
				Save saveMoney = new Save();
			}else if(e.getSource()==btn[2]) {
				Update update = new Update();
			}else if(e.getSource()==btn[3]) {
				Select select = new Select();
			}else if(e.getSource()==btn[4]) {
				Tranfer tranfer = new Tranfer();
			}else if(e.getSource()==btn[5]) {
				System.exit(-1);
			}
		}
		
	}
	
	public Function() {
		super("功能区");
		this.setResizable(false);
		this.setSize(500,500);
		this.setLocationRelativeTo(null);
		ExecuteFunction ef = new ExecuteFunction();
		
		JPanel function = new JPanel(new GridLayout(2,3));
		String[] str = new String[] {
				"取钱(OutMoney)","存钱(SaveMoney)",
				"修改密码(ModifyPassword)","查询余额(SelectResidue)",
				"转账(TranferMoney)","退出"
		};
		for(int i = 0;i < str.length;i++) {
			btn[i] = new JButton(str[i]);
			function.add(btn[i]);
			btn[i].addActionListener(ef);
		}
		
		
		add(function);
		
	}
}
class Out extends JFrame{
	JTextField money1 = new JTextField();
	CustomerFunction cf = new CustomerFunction();
	
	class Write implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==money1) {
				String money = e.getActionCommand();
				Customers customer = cf.getCustomers();
				if((!money.equals(""))&&cf.out(customer, Integer.parseInt(money))) {
					System.out.println("用户取出"+money+"元");
					System.out.println("取钱成功");
				}else {
					System.out.println("取钱失败");
				}
			}
			
			
		}
		
	}
	public Out() {
		super("取钱");
		this.setResizable(false);
		this.setSize(250,200);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		Write write = new Write();

		JPanel jpanel = new JPanel(new GridLayout());
		jpanel.add(money1,BorderLayout.CENTER);
		money1.addActionListener(write);
		
		add(jpanel);
	}
}

class Save extends JFrame{
	JTextField money1 = new JTextField();
	CustomerFunction cf = new CustomerFunction();
	
	class Write implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==money1) {
				String money = e.getActionCommand();
				Customers customer = cf.getCustomers();
				if(cf.save(customer, Integer.parseInt(money))) {
					System.out.println("用户存入"+money+"元");
					System.out.println("存钱成功");
				}else {
					System.out.println("存钱失败");
				}
			}
			
			
		}
		
	}
	public Save() {
		super("存钱");
		this.setResizable(false);
		this.setSize(250,200);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		Write write = new Write();

		JPanel jpanel = new JPanel(new GridLayout());
		jpanel.add(money1,BorderLayout.CENTER);
		money1.addActionListener(write);
		
		add(jpanel);
	}
}
class Update extends JFrame{
	JTextField[] text = new JTextField[2];
	CustomerFunction cf = new CustomerFunction();
	
	class Write implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Customers customer = cf.getCustomers();
			if(customer.getPassword().equals(text[0].getText())) {
				String newPassword = text[1].getText();
				if(cf.modify(customer, newPassword)) {
					System.out.println("修改成功");
					System.exit(-1);
				}else {
					System.out.println("修改失败");
				}
			}else {
				System.out.println("输入密码和原密码不符");
			}
			
		}
		
	}
	public Update() {
		super("修改密码");
		this.setResizable(false);
		this.setSize(250,200);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		Write write = new Write();
		
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(2,2));
		
		Label label1 = new Label("请输入旧密码");
		jpanel.add(label1);
		text[0] = new JTextField();
		jpanel.add(text[0]);
		text[0].addActionListener(write);
		
		Label label2 = new Label("请输入新密码");
		jpanel.add(label2);
		text[1] = new JTextField();
		jpanel.add(text[1]);
		text[1].addActionListener(write);
		
		add(jpanel);
	}
}
class Select extends JFrame{
	JTextField[] text = new JTextField[2];
	CustomerFunction cf = new CustomerFunction();
	
	public Select() {
		super("余额");
		this.setSize(250,200);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		Customers customer = cf.getCustomers();
		int balance = customer.getBalance();
		Label label = new Label("您的余额为:"+balance);
		add(label);
		
	}
}
class Tranfer extends JFrame{
	JTextField[] text = new JTextField[2];
	CustomerFunction cf = new CustomerFunction();
	
	class TranferListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String account = text[0].getText();
			String money = text[1].getText();
			Customers customer = cf.getCustomers();
			if(cf.tranfer(customer, account, money)) {
				System.out.println("转账成功");
			}else {
				System.out.println("转账失败");
			}
		}
		
	}
	
	public Tranfer() {
		super("转账");
		this.setSize(250,200);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		TranferListener tl = new TranferListener();
		
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(2,2));
		
		Label label1 = new Label("请输入账号:");
		jpanel.add(label1);
		text[0] = new JTextField();
		jpanel.add(text[0]);
		text[0].addActionListener(tl);
		
		Label label2 = new Label("请输入金额");
		jpanel.add(label2);
		text[1] = new JTextField();
		jpanel.add(text[1]);
		text[1].addActionListener(tl);
		
		add(jpanel);
	}
}

测试:
测试:
package cn.test;

import cn.gui.Register;

public class TestBank {
	public static void main(String[] args) {
		new Register();
	}
}

数据库操作:
create table if not exists customers(
	id varchar(18) primary key comment '身份证号',
	name varchar(5) not null comment '姓名',
	sex varchar(1) not null comment '性别',
	address varchar(20) not null comment	'住址',
	tel varchar(11) not null unique comment '电话号码',
	account varchar(19) not null unique comment '银行账户',
	user_password varchar(6) not null comment '密码',
	balance int(10) default 0 comment '余额'
);

说明:
用Java-Swing技术实现的一个简单的银行管理系统,适合初学者参考,如有侵权,及时联系删除。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值