Bank小项目(控制台)

在这里插入图片描述
Dao–>Dao.Impl–>Biz–>Main
数据访问层 业务逻辑层 表示层
实体类:
Account:账户编号、姓名、金额
Log:日志编号、账户编号、姓名、金额、时间

时间类

自定义异常类

Dao层:
BaseDao作为连接数据库的方法方便后面直接调用
AccountDao:向account表中增加一条记录
从account表根据accid列查询该账户信息
在account表中修改一条记录,根据accid将money列变化value元
从account表中删除一条记录,按accid删除
在account表中查询所有记录,封装成一个集合
accidOut账户相accidId账户转value元**

服务层:
开户
销户
存钱
取钱
转账
查询所有账户
查询所有日志

创建数据库

-- account
CREATE TABLE account(
accid INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30) NOT NULL,
money DOUBLE NOT NULL
);

-- log
CREATE TABLE LOG(
logid INT PRIMARY KEY AUTO_INCREMENT,
accid INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
money DOUBLE NOT NULL,
DATE DATETIME NOT NULL
);

package demo.entity;(实体类)
Account

package demo.entity;

public class Account {
	private int accid;
	private String name;
	private double money;
	public Account(int accid, String name, double money) {
		this.accid = accid;
		this.name = name;
		this.money = money;
	}
	public Account() {
	}
	public int getAccid() {
		return accid;
	}
	public void setAccid(int accid) {
		this.accid = accid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "账户编号:" + accid + ", 账户姓名:" + name + ", 账户金额" + money;
	}
	
}

Log

package demo.entity;

public class Log {
	private int logid;
	private int accid;
	private String name;
	private double money;
	private String date;
	public Log(int logid, int accid, String name, double money, String date) {
		this.logid = logid;
		this.accid = accid;
		this.name = name;
		this.money = money;
		this.date = date;
	}
	public Log() {
	}
	@Override
	public String toString() {
		return "日志编号:" + logid + ", 账户编号:" + accid + ", 账户姓名:" + name + ", 账户金额:" + money + ", 操作时间:" + date;
	}
	public int getLogid() {
		return logid;
	}
	public void setLogid(int logid) {
		this.logid = logid;
	}
	public int getAccid() {
		return accid;
	}
	public void setAccid(int accid) {
		this.accid = accid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	
}

util(工具类)
package demo.util;
DateFormat

package demo.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat {
	//yyyy-MM-dd hh:mm:ss
	public static String nowToDateTime() {
		String strDate=null;
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		strDate=sdf.format(date);
		return strDate;
	}
	
}

Dao层作为接口
package demo.dao;
BaseDao(JDBC)

package demo.dao;
import java.sql.*;

import demo.entity.Account;
public class BaseDao {
	protected Connection con;
	protected PreparedStatement pst;
	protected ResultSet rs;
	
	//数据库连接
	public Connection getConn() {
		Connection con=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf-8","root","111111");
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	//关闭数据库连接及其对象
	public void closeAll() {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(pst!=null) {
				pst.close();
			}
			if(con!=null) {
				con.close();
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
}

AccountDao

package demo.dao;

import java.util.List;

import demo.entity.Account;

public interface AccountDao {
	//向account表中增加一条记录
	public int addAccount(Account account);//返回accid
	//从account表中根据accid列查询该账户信息
	public Account selectById(int accid);//返回account
	//在account表中修改一条记录。根据accid将money类变化value元
	public int updateAccount(int accid,double value);//返回影响的行数
	//从account删除一条记录,按accid删除
	public int deleteAccount(int accid);//返回影响的行数
	//在account表中查询所有记录,封装成一个集合
	public List<Account> selectAll();//返回accoutList集合
	//accidOut账户向accidIn账户转value元
	public int turnMoney(int accidOut,int accidIn,double value);//返回两步结果各自影响的行数的乘积
}

LogDao

package demo.dao;

import java.util.List;

import demo.entity.Log;

public interface LogDao {
	//向log表中增加一条记录
	public int addLog(Log log);
    //从log表中查询所有记录,封装成一个集合
	public List<Log> selectAll();
}

Dao.impl(Dao层接口实现类)
package demo.dao.impl;
AccountDaoImpl

package demo.dao.impl;

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

import demo.dao.AccountDao;
import demo.dao.BaseDao;
import demo.entity.Account;

public class AccountDaoImpl extends BaseDao implements AccountDao{

	@Override
	//添加账户
	public int addAccount(Account account) {
		con=getConn();
		int result=0;
		String sql1="insert into account values(null,?,?)";
		String sql2="SELECT LAST_INSERT_ID()";
		int accid=0;
		try {
			pst=con.prepareStatement(sql1);
			pst.setString(1, account.getName());
			pst.setDouble(2, account.getMoney());
			result=pst.executeUpdate();//影响的行数
			if(result==1) {//如果sql1执行成功那么执行sql2
				pst=con.prepareStatement(sql2);
				rs=pst.executeQuery();//查询结果
				if(rs.next()) {
					accid=rs.getInt(1);
				}
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return accid;
	}

	@Override
	//根据accid查找账户
	public Account selectById(int accid) {
		Account account=null;
		con=getConn();
		String sql="select * from account where accid=?";
		try {
			pst=con.prepareStatement(sql);
			pst.setInt(1, accid);
			rs=pst.executeQuery();
			if(rs.next()) {
				account=new Account(rs.getInt(1),rs.getString(2),rs.getDouble(3));
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return account;
	}

	@Override
	//更新账户里的金额
	public int updateAccount(int accid, double value) {
		int result=0;
		con=getConn();
		String sql="update account set money =money+? where accid=?";
		try {
			pst=con.prepareStatement(sql);
			pst.setDouble(1, value);
			pst.setInt(2, accid);
			result=pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	//根据accid删除账户
	public int deleteAccount(int accid) {
		int result=0;
		con=getConn();
		String sql="delete from account where accid=?";
		try {
			pst=con.prepareStatement(sql);
			pst.setInt(1, accid);
			result=pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	//查找账户
	public List<Account> selectAll() {
		List<Account> accountList=new ArrayList<Account>();
		con=getConn();
		String sql="select * from account";
		try {
			pst=con.prepareStatement(sql);
			rs=pst.executeQuery();
			while(rs.next()) {
				Account account=new Account();
				account.setAccid(rs.getInt(1));
				account.setName(rs.getString(2));
				account.setMoney(rs.getDouble(3));
				accountList.add(account);
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return accountList;
	}

	@Override
	//转账
	public int turnMoney(int accidOut, int accidIn, double value) {
		int result1=0;
		int result2=0;
		con=getConn();
		String sql1="update account set money=money-? where accid=?";
		String sql2="update account set money=money+? where accid=?";
		try {
			con.setAutoCommit(false);//关闭自动提交功能
			pst=con.prepareStatement(sql1);
			pst.setDouble(1, value);
			pst.setInt(2, accidOut);
			result1=pst.executeUpdate();
			
			pst=con.prepareStatement(sql2);
			pst.setDouble(1, value);
			pst.setInt(2, accidIn);
			result2=pst.executeUpdate();
			if(result1*result2==1) {
				con.commit();
			}else {
				con.rollback();
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return result1*result2;
	}

}

LogDaoImpl

package demo.dao.impl;

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

import demo.dao.BaseDao;
import demo.dao.LogDao;
import demo.entity.Log;
import demo.util.DateFormat;

public class LogDaoImpl extends BaseDao implements LogDao{

	@Override
	public int addLog(Log log) {
		con=getConn();
		int result=0;
		String sql="insert into log values(null,?,?,?,?)";
		try {
			pst=con.prepareStatement(sql);
			pst.setInt(1, log.getAccid());
			pst.setString(2, log.getName());
			pst.setDouble(3, log.getMoney());
			pst.setString(4, DateFormat.nowToDateTime());
			result=pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Log> selectAll() {
		List<Log> logList=new ArrayList<Log>();
		con=getConn();
		String sql="select * from log";
		try {
			pst=con.prepareStatement(sql);
			rs=pst.executeQuery();
			while(rs.next()) {
				Log log=new Log();
				log.setLogid(rs.getInt(1));
				log.setAccid(rs.getInt(2));
				log.setName(rs.getString(3));
				log.setMoney(rs.getDouble(4));
				log.setDate(rs.getString(5));
				logList.add(log);
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return logList;
	}

}

Exception(自定义异常类)
package demo.exception;
InputValueException

package demo.exception;

public class InputValueException extends Exception{
	public InputValueException(double value) {
		super("您输入的金额为"+value+",这是一个负数。");
	}
}

NotEnoughMoneyException

package demo.exception;

public class NotEnoughMoneyException extends Exception{
	public NotEnoughMoneyException(double money,double value) {
		super("现有金额为"+money+",取款金额为"+value+",余额不足。");
	}
}

NotFoundAccountException

package demo.exception;

public class NotFoundAccountException extends Exception{
	public NotFoundAccountException(int accid) {
		super("抱歉,编号为"+accid+"的账户不存在。");
	}
}

Biz(业务逻辑层也用接口)
package demo.biz;

package demo.biz;

import java.util.List;

import demo.entity.Account;
import demo.entity.Log;
import demo.exception.InputValueException;
import demo.exception.NotEnoughMoneyException;
import demo.exception.NotFoundAccountException;

public interface BankBiz {
	//开户
	public boolean openAccount(Account account) throws InputValueException;
	//销户
	public boolean delAccount(int accid) throws NotFoundAccountException, InputValueException, NotEnoughMoneyException;
	//存钱
	public boolean saveMoney(int accid,double value) throws NotFoundAccountException, InputValueException;
	//取钱
	public boolean fetchMoney(int accid,double value) throws NotFoundAccountException, InputValueException, NotEnoughMoneyException;
	//转账
	public boolean turnMoney(int accidOut,int accidIn,double value) throws NotFoundAccountException, InputValueException, NotEnoughMoneyException;
	//查询所有账户
	public List<Account> selectAllAccounts();
	//查询所有日志
	public List<Log> selectAllLogs();
}

biz.impl(业务逻辑层接口实现类)
BankBizImpl

package demo.biz.impl;

import java.util.List;

import demo.biz.BankBiz;
import demo.dao.AccountDao;
import demo.dao.LogDao;
import demo.dao.impl.AccountDaoImpl;
import demo.dao.impl.LogDaoImpl;
import demo.entity.Account;
import demo.entity.Log;
import demo.exception.InputValueException;
import demo.exception.NotEnoughMoneyException;
import demo.exception.NotFoundAccountException;

public class BankBizImpl implements BankBiz{

	AccountDao accountDao=new AccountDaoImpl();
	LogDao logDao=new LogDaoImpl();
	
	@Override
	//开户
	public boolean openAccount(Account account) throws InputValueException {
		//1.account增加一条记录
		//2.log增加一条记录
		//开户时的存钱金额小于零
		if(account.getMoney()<0) {
			throw new InputValueException(account.getMoney());
		}
		int accid=accountDao.addAccount(account);
		Log log=new Log();
		log.setAccid(accid);
		log.setName(account.getName());
		log.setMoney(account.getMoney());
		int result=logDao.addLog(log);
		if(accid*result>0) {
			return true;
		}else {
			return false;
		}	
	}

	@Override
	//销户
	public boolean delAccount(int accid) throws NotFoundAccountException, InputValueException, NotEnoughMoneyException {
		//1.取钱---从account表中编号为accid的账户的所有金额全部取出
		//2.从account表删除accid
		Account account=new Account();
		account=accountDao.selectById(accid);
		if(account==null) {
			throw new NotFoundAccountException(accid);
		}
		boolean fetchMoney=fetchMoney(accid, account.getMoney());
		int deleteAccount=accountDao.deleteAccount(accid);
		return fetchMoney&&(deleteAccount>0);
	}

	@Override
	public boolean saveMoney(int accid, double value) throws NotFoundAccountException, InputValueException {
		//1.从account表中根据accid查询一条记录
		//2.修改account表中的记录,根据accid修改money列,增加value元
		//3.向log表中增加一条记录
		Account account=accountDao.selectById(accid);
		if(account==null) {
			throw new NotFoundAccountException(accid);
		}
		if(value<0) {
			throw new InputValueException(value);
		}
		int result1=accountDao.updateAccount(accid, value);
		Log log=new Log();
		log.setAccid(accid);
		log.setName(account.getName());
		log.setMoney(account.getMoney());
		int result2=logDao.addLog(log);
		return result1*result2>0?true:false;
	}

	@Override
	public boolean fetchMoney(int accid, double value) throws NotFoundAccountException, InputValueException, NotEnoughMoneyException {
		//1.从account表中根据accid查询一条记录
		//2.修改account表中的记录,根据accid修改money列,减少value元
		//3.向log表中增加一条记录
		Account account=accountDao.selectById(accid);
		if(account==null) {
			throw new NotFoundAccountException(accid);
		}
		if(value<0) {
			throw new InputValueException(value);
		}
		if(account.getMoney()<value) {
			throw new NotEnoughMoneyException(account.getMoney(), value);
		}
		int result1=accountDao.updateAccount(accid, -1*value);
		Log log=new Log();
		log.setAccid(accid);
		log.setName(account.getName());
		log.setMoney(account.getMoney()-value);
		int result2=logDao.addLog(log);
		return result1*result2>0?true:false;
	}

	@Override
	public boolean turnMoney(int accidOut, int accidIn, double value) throws NotFoundAccountException, InputValueException, NotEnoughMoneyException {
		//1.从account表中根据accidIn、accidOut查询两条记录
		//2.修改account表中的记录,根据accid修改money列,减少/增加value元
		//3.向log表中增加两条记录 
		Account account1=accountDao.selectById(accidOut);
		Account account2=accountDao.selectById(accidIn);
		if(account1==null) {
			throw new NotFoundAccountException(accidOut);
		}
		if(account2==null) {
			throw new NotFoundAccountException(accidIn);
		}
		if(value<0) {
			throw new InputValueException(value);
		}
		if(account1.getMoney()<value) {
			throw new NotEnoughMoneyException(account1.getMoney(), value);
		}
		int result=accountDao.turnMoney(accidOut, accidIn, value);
		Log log1=new Log();
		log1.setAccid(accidOut);
		log1.setName(account1.getName());
		log1.setMoney(account1.getMoney()-value);
		int addlog1=logDao.addLog(log1);
		
		Log log2=new Log();
		log2.setAccid(accidIn);
		log2.setName(account2.getName());
		log2.setMoney(account2.getMoney()+value);
		int addlog2=logDao.addLog(log2);
		return result*addlog1*addlog2>0?true:false;
	}

	@Override
	public List<Account> selectAllAccounts() {
		return accountDao.selectAll();
	}

	@Override
	public List<Log> selectAllLogs() {
		return logDao.selectAll();
	}

}

UI层
package demo.UI;
Main

package demo.UI;

import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;

import demo.biz.BankBiz;
import demo.biz.impl.BankBizImpl;
import demo.entity.Account;
import demo.entity.Log;
import demo.exception.InputValueException;
import demo.exception.NotEnoughMoneyException;
import demo.exception.NotFoundAccountException;

public class Main {
	public static BankBiz bankBiz=new BankBizImpl();
	public static Scanner sc=new Scanner(System.in);
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		while(true) {
			System.out.println("欢迎使用银行管理系统");
			System.out.println("您可以使用以下操作");
			System.out.println("1-开户 2-销户 3-存钱 4-取钱 5-转账 6-查询所有账户 7-查看日志表 0-退出程序");
			
			int op=reader.nextInt();
			if(op==0) {
				break;
			}
			switch(op) {
			case 1:
				openAccount();
				break;
			case 2:
				delAccount();
				break;
			case 3:
				saveMoney();
				break;
			case 4:
				fetchMoney();
				break;
			case 5:
				turnMoney();
				break;
			case 6:
				selectAllAccount();
				break;
			case 7:
				selectAllLog();
				break;
			}
		}
		System.out.println("感谢您的使用!");
	}
	//开户
	public static void openAccount() {
		Account account=new Account();
		System.out.println("请输入账户姓名:");
		account.setName(sc.next());
		System.out.println("请输入账户初始金额:");
		account.setMoney(sc.nextDouble());
		boolean result=false;
		try {
			result=bankBiz.openAccount(account);
		} catch (InputValueException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		if(result) {
			System.out.println("恭喜您开户成功!");
		}
	}
	//销户
	public static void delAccount() {
		System.out.println("请输入您待销户的账户编号:");
		boolean result=false;
		try {
			result=bankBiz.delAccount(sc.nextInt());
		} catch (NotFoundAccountException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (InputValueException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (NotEnoughMoneyException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		if(result) {
			System.out.println("销户成功,感谢您的使用。");
		}
	}
	//存钱
	public static void saveMoney() {
		System.out.println("请输入存钱账户的编号:");
		int accid=sc.nextInt();
		System.out.println("请输入存钱金额:");
		double money=sc.nextDouble();
		boolean result=false;
		try {
			result=bankBiz.saveMoney(accid, money);
		} catch (NotFoundAccountException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (InputValueException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		if(result) {
			System.out.println("存钱成功!");
		}
	}
	//取钱
	public static void fetchMoney() {
		System.out.println("请输入取钱账户的编号:");
		int accid=sc.nextInt();
		System.out.println("请输入取钱金额:");
		double money=sc.nextDouble();
		boolean result=false;
		try {
			result=bankBiz.fetchMoney(accid, money);
		} catch (NotFoundAccountException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (InputValueException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (NotEnoughMoneyException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		if(result) {
			System.out.println("取钱成功!");
		}
	}
	//转账
	public static void turnMoney() {
		System.out.println("请输入转出账户的编号:");
		int accidOut=sc.nextInt();
		System.out.println("请输入转入账户的编号:");
		int accidIn=sc.nextInt();
		System.out.println("请输入转账金额:");
		double money=sc.nextDouble();
		boolean result=false;
		try {
			result=bankBiz.turnMoney(accidOut, accidIn, money);
		} catch (NotFoundAccountException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (InputValueException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		} catch (NotEnoughMoneyException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		if(result) {
			System.out.println("转账成功!");
		}
	}
	//查询所有账户
	public static void selectAllAccount() {
		List<Account> accountList=bankBiz.selectAllAccounts();
		System.out.println("所有的账户信息如下:");
		for (Account account : accountList) {
			System.out.println(account);
		}
	}
	//查看日志表
	public static void selectAllLog() {
		List<Log> logList=bankBiz.selectAllLogs();
		System.out.println("所有的日志信息如下:");
		for (Log log : logList) {
			System.out.println(log);
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值