三层架构 银行管理系统

三层架构 银行管理系统

首先还是要复习一下,常见的项目包结构

conf     配置文件  类似jdbc.properties

dao      orm中的数据访问层

            在本类上一个接口,加上一个继承接口的类,主要是接口具有耦合性搞的优点

entity    实体对象

service 也是一个接口,一个类。

SQL     项目相关的sql 

util       工具类

view     表现层代码

先看看表现层代码,相当于菜单

package view;

import java.util.Scanner;

import javax.swing.plaf.synth.Region;


import entity.Account;
import service.AccountService;
import service.AccountServiceImpl;

public class AccountView {
	
	private  static Scanner sc = new Scanner(System.in);
	static  AccountService accountService = new AccountServiceImpl();
	public static void main(String[] args) {
		
		while(true){
			System.out.println("*************欢迎进入银行系统***********");
			System.out.println("1、存钱*************************2、取钱");
			System.out.println("3、查询当前余额********************4、转账");
			System.out.println("5、注册账户********************6、更改密码");
			System.out.println("7、注销账户********************0、退出登录");
			System.out.println("请选择:");
			int n = sc.nextInt();
				switch(n){
			case 1:
				//存钱
				savemoney();
				break;
			case 2:
				//取钱
				drawmoney();
				break;
			case 3:
				//查询当前余额
				queryBalance();
				break;
			case 4:
				//转账
				transfor();
				break;
			case 5:
				//注册账户
				regist();
				break;
			case 6:
				//更改密码
				changeWord();
				break;
			case 7:
				//注销账户
				cancelAccount();
				 break;
			case 0:
				//退出登录
				System.out.println("大哥,欢迎下次再来哈!");
				System.exit(0);
				break;
				
			default:
				throw new RuntimeException("老哥,你的输入有错误!");
			
			}		
		}
		
	} 
	//注册
	public static void regist(){
		System.out.println("输入您的姓名:");
		String name = sc.next();
		System.out.println("输入的您的密码");
		String password = sc.next();
		System.out.println("大哥再确认下吧");
		String pass2 = sc.next();
		Account account = new Account(null,name,password,0.0);
		try{
			accountService.regist(account,pass2);
			System.out.println("注册成功");
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	//查询余额
	public  static void queryBalance(){
		System.out.println("请输入您的账号:");
		int cardid = sc.nextInt();
		System.out.println("请输入的您的密码");
		String password = sc.next();
		try{
			double balance = accountService.queryBalance(cardid,password);
			System.out.println("您的当前余额为" + balance);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	//存钱
	public static void savemoney(){
		System.out.println("请输入您的账号: ");
		int id = sc.nextInt();
		System.out.println("请输入您的密码: ");
		String password = sc.next();
		System.out.println("大哥,存款存多少呢:");
		double balance = sc.nextDouble();
		
		try{
			accountService.saveMoney(id,password,balance);
			System.out.println("存款成功!");
		}catch(Exception e ){
			System.out.println(e.getMessage());
		}
		
		
	}
	//取钱
	public static void drawmoney(){
		System.out.println("请输入您的账号: ");
		int id = sc.nextInt();
		System.out.println("请输入您的密码: ");
		String password = sc.next();
		System.out.println("大哥,您要取多少钱呢:");
		double balance = sc.nextDouble();
		try{
			accountService.drawmoney1(id,password,balance);
			System.out.println("取钱成功!");
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	//注销账户
	public static  void  cancelAccount(){
		
		System.out.println("大哥,您要注销账户?yes or no :");
		String ye = sc.next();
		if(ye.equals("yes")){
			System.out.println("请输入您的账号: ");
			int id = sc.nextInt();
			System.out.println("请输入您的密码: ");
			String password = sc.next();
			try{
				accountService.cancelAccountt(id, password);
				System.out.println("您的账号已注销!");
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
		}else{
			System.out.println("大哥,您的账号已取消创建。");
		}
	}
	//更改密码
	public static  void changeWord(){
		System.out.println("大哥,您是否要进行更改密码操作?yes or no :");
		String ye = sc.next();
		if(ye.equals("yes")){
			System.out.println("请输入您的账号: ");
			int id = sc.nextInt();
			System.out.println("请输入您的原密码: ");
			String password = sc.next();
			System.out.println("请输入您的新密码: ");
			String nepassword = sc.next();
			try{
				accountService.changepassword(id, password,nepassword);
				System.out.println("您的账号已更改密码!");
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
		}else{
			System.out.println("大哥,您的账号已取消更改密码");
		}
		
		
	}
	//转账功能
	public static void transfor(){
		System.out.println("请输入您的账号:");
		int fromcardid = sc.nextInt();
		System.out.println("请输入的您的密码");
		String password = sc.next();
		System.out.println("钱转给谁呢?请输入她的账号:");
		int tocardid = sc.nextInt();
		System.out.println("转账的金额");
		double money = sc.nextDouble();
		try{
			accountService.transforyou(fromcardid,password,tocardid,money);
			System.out.println("转账完成");
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		}
	}
	

 接着业务逻辑层代码

接口代码:

package service;

import entity.Account;

public interface AccountService {
	//业务功能
	//开户功能
	public   void regist(Account account,String pass2);
	//查询余额
	public double queryBalance(int cardid,String password);
	//转账
	public  void transforyou(int fromcardid,String password,int tocardid,double money);
	//存款功能
	public void saveMoney(int cardid,String password,double balance);
	//取款
	public void drawmoney1(int cardid,String password,double balance);
	//注销账户
	public void cancelAccountt(int cardid,String password);
	//更改密码
	public void changepassword(int id, String password, String nepassword);
	
}

核业务逻辑层心代码:

package service;

import java.sql.Connection;
import java.sql.SQLException;

import javax.management.RuntimeErrorException;

import dao.AccountDao;
import dao.AccountdDaoImpl;
import entity.Account;
import util.jdbcUtil3;

public class AccountServiceImpl implements AccountService {
	AccountDao accountdDao = new AccountdDaoImpl(); 
	Connection conn = null;
	@Override
	public void regist(Account account, String pass2){
		String name = account.getName();
		if(name==null){
			throw new RuntimeException("用户名为空");
		}
		//判断两次密码是否一致
		String password = account.getPassword();
		if(!password.equals(pass2)){
			throw new RuntimeException("两次密码不一致");
		}
		//创建连接,设置手动提交,控制事务

		try {
			conn = jdbcUtil3.getConnection();
			conn.setAutoCommit(false);
			//插入记录
			accountdDao.insertAccount(account);
			//提交
			conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException("回滚异常");
			}
		}finally{
			//关闭连接
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException("关闭连接异常");
			}	
		}
	}
	@Override
	public double queryBalance(int cardid, String password) {
		double balance = 0.0;
		Account account = accountdDao.queryAccountByCardid(cardid);
		//查看账号是否存在
		if(account==null){
			throw new RuntimeException("账户不存在");
		}
		if(!account.getPassword().equals(password)){
			throw new RuntimeException("密码输入错误!");
		}
	
		try {
			conn = jdbcUtil3.getConnection();
			balance = account.getBalance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("查询连接错误");
		}finally{
			try {
				jdbcUtil3.release(null, null, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("关闭连接异常!");
			}
		}
		return balance;
	}
	@Override
	public void transforyou(int fromcardid, String password, int tocardid, double money) {
		//判断我方用户是否存在
		Account account1 = accountdDao.queryAccountByCardid(fromcardid);
		Account	account2 = accountdDao.queryAccountByCardid(tocardid);
		if(account1 ==null){
			throw new RuntimeException("您好,你的账户为空!");
		}
		//对方账号是否存在
		if(account2==null){
			throw new RuntimeException("你好,你要转账的账户不存在!");
		}
		//判断密码上是否输入正确
		if(!account1.getPassword().equals(password)){
			throw new RuntimeException("密码不一致");
		}
		//我方余额是否大于转账金额
		if(account1.getBalance()<money){
			throw new RuntimeException("余额不足");
		}
		
		//可以转账,
		account1.setBalance(account1.getBalance()-money);
		account2.setBalance(account2.getBalance()+money);
		
		try{
			conn =  jdbcUtil3.getConnection(); 
			//手动提交
			conn.setAutoCommit(false);
			accountdDao.updateAccount(account1);
			accountdDao.updateAccount(account2);
			conn.commit();
		}catch(Exception e){
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				throw new RuntimeException("回滚有异常"); 
			}
		}finally{
			try {
				jdbcUtil3.release(null, null, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException("释放资源异常。。"); 
			}
		}
	}
	@Override
	public void saveMoney(int cardid, String password, double balance) {
		//通过账号获取这个账户
		Account accountt = accountdDao.queryAccountByCardid(cardid);
		//判断账户是否存在
		if(accountt==null){
			throw new RuntimeException("账户不存在");
		}
		//检查密码是否正确
		if(!accountt.getPassword().equals(password)){
			throw new RuntimeException("密码输入不正确!");
		}
		//可以存款
		double bala = balance + accountt.getBalance();
		
		try {
			conn = jdbcUtil3.getConnection();
			conn.setAutoCommit(false);
			accountdDao.savemoneybyCardid(bala,cardid);
			conn.commit();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException("回滚异常");
			}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				throw new RuntimeException("释放资源异常!");
			}
		}
	}
	@Override
	public void drawmoney1(int cardid, String password, double balance) {
		//妈耶,代码基本没啥差别啊
				Account accountt = accountdDao.queryAccountByCardid(cardid);
				//判断账户是否存在
				if(accountt==null){
					throw new RuntimeException("账户不存在");
				}
				//检查密码是否正确
				if(!accountt.getPassword().equals(password)){
					throw new RuntimeException("密码输入不正确!");
				}
				//可以存款
				double bala =  accountt.getBalance() - balance;
				
				try {
					conn = jdbcUtil3.getConnection();
					conn.setAutoCommit(false);
					accountdDao.drawmoneybyCardid(bala,cardid);
					conn.commit();
				} catch (Exception e) {
					try {
						conn.rollback();
					} catch (SQLException e1) {
						throw new RuntimeException("回滚异常");
					}
				}finally{
					try {
						conn.close();
					} catch (SQLException e) {
						throw new RuntimeException("释放资源异常!");
					}
				}
		
	}
	@Override
	public void cancelAccountt(int cardid, String password) {
		//通过账号获取这个账户
		Account accountt = accountdDao.queryAccountByCardid(cardid);
		//判断账户是否存在
		if(accountt==null){
			throw new RuntimeException("账户不存在!");
		}
		//检查密码是否正确
		if(!accountt.getPassword().equals(password)){
			throw new RuntimeException("密码输入不正确!");
		}
		//进行账号注销
		try {
			conn = jdbcUtil3.getConnection();
			conn.setAutoCommit(false);
			accountdDao.deleteCardid(cardid);
			conn.commit();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException("回滚异常");
			}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				throw new RuntimeException("释放资源异常!");
			}
		}
		
		
	}
	@Override
	public void changepassword(int id, String password, String nepassword) {
		//通过账号获取这个账户
		Account accountt = accountdDao.queryAccountByCardid(id);
		//判断账户是否存在
		if(accountt==null){
			throw new RuntimeException("您的账户不存在!");
		}
		//检查密码是否正确
		if(!accountt.getPassword().equals(password)){
			throw new RuntimeException("亲,您输入的密码不正确哦!");
		}
		//进行更改密码操作
		try {
			conn = jdbcUtil3.getConnection();
			conn.setAutoCommit(false);
			accountdDao.updateCardid(id,nepassword);
			conn.commit();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException("回滚异常");
			}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				throw new RuntimeException("释放资源异常!");
			}
		}
			
		
	}
}

数据访问层 dao 接口和代码

package dao;

import entity.Account;

public interface AccountDao {
	//数据插入 注册账号
	public void insertAccount(Account account);
	//根据卡号查看账户信息 从而查询余额
	public Account queryAccountByCardid(int cardid);
	//更新信息。进行转账
	public void updateAccount(Account account1);
	//存款
	public void savemoneybyCardid(double balance,int card);
	//取款
	public void drawmoneybyCardid(double bala, int cardid);
	//注销账户
	public void deleteCardid(int cardid);
	//更改密码操作
	public void updateCardid(int id, String nepassword);
}
package dao;

import entity.Account;
import rowmapper.AccountRowMapper;
import rowmapper.RowMapper;
import util.JdbcTemplate;

public  class AccountdDaoImpl<T> implements AccountDao {
	JdbcTemplate template = new JdbcTemplate();
	//多态来创建对象
	public void insertAccount(Account account) {
		template.update("insert into account(name,password,balance) values(?,?,?)", account.getName(),account.getPassword(),account.getBalance());
	}

	@Override
	public Account queryAccountByCardid(int cardid) {

		Account account =(Account) template.queryForObject("select * from account where cardid=? ",new AccountRowMapper(),cardid);
		return account;
	}

	@Override
	public void updateAccount(Account account1) {
		template.update("update account set balance=? where cardid=?",account1.getBalance(),account1.getCardid());
		
	}

	@Override
	public void savemoneybyCardid(double balance,int card) {
		template.update("update account set balance = ? where cardid =?",balance,card);
		
	}

	@Override
	public void drawmoneybyCardid(double bala, int cardid) {
		template.update("update account set balance = ? where cardid =?", bala , cardid);
	}

	@Override
	public void deleteCardid(int cardid) {
		template.update("delete from  account where cardid=?  ", cardid);
		
	}
	@Override
	public void updateCardid(int id, String nepassword) {
		template.update("update account set password = ? where cardid = ?", nepassword,id);
	}
}

局部演示图

先睡了,明天更新。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114776.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python:类基础「建议收藏」

    python:类基础「建议收藏」1、面向对象编程(oop)是一种程序设计思想。oop把对象作为程序的基本单元,一个对象包含数据和操作数据的函数2、在python中,所有数据类型都被视为对象,也可以自定义对象。自定义对象数据类型就是面向对象中类的概念1、类(Class):用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例2、方法:类中定义的函数3、类变量(属性):类变量在整个实例化的对象中是公用的。类变量定义在类中且在函数体(方法)之外。类变量通常不作为实例变量使用,类变量也称作属性4、数

    2022年7月23日
    9
  • 机器学习:Multinoulli分布与多项式分布

    机器学习:Multinoulli分布与多项式分布学习深度学习时遇见multinoulli分布,在此总结一下机器学习中常用的multinoulli分布与多项式分布之间的区别于关系,以便更好的理解其在机器学习和深度学习中的使用。首先介绍一下其他相关知识。Bernoulli分布(两点分布)Bernoulli分布是单个二值随机变量的分布。它由单个参数控制,给出了随机变量等于1的概率。             …

    2022年10月12日
    1
  • Stata Kendall 相关系数作图

    Stata Kendall 相关系数作图StataKendall相关系数作图回答Superficial.的问题,测试CSDN的markdown发帖功能如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入此帖目的有二:回答Superficial.的问题,测试CSDN的markdown发帖功能如何插入一段漂亮的代码片去博客设置页面,选择

    2022年6月17日
    26
  • 汇编语言中各种移位指令的区别与联系

    汇编语言中各种移位指令的区别与联系本文转载自:https://blog.csdn.net/richerg85/article/details/27558005SHL、SHR、SAL、SAR:移位指令;SHL(ShiftLeft):   逻辑左移;SHR(ShiftRight):   逻辑右移;SAL(ShiftArithmeticLeft):算术左移;SAR(Shift…

    2022年6月9日
    24
  • Java开发手册之模块命名「建议收藏」

    Java开发手册之模块命名「建议收藏」Java开发手册之模块命名

    2022年4月23日
    57
  • “仅三天可见” 的朋友圈有方法破解啦!

    “仅三天可见” 的朋友圈有方法破解啦!点击上方“逆锋起笔”,公众号回复PDF领取大佬们推荐的学习资料之前微博上出现过一个热搜话题:超一亿人朋友圈仅三天可见。微信创始人张小龙在年度演讲里说,这个开关,是微信里使用最多的。很多…

    2022年6月13日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号