三层架构 银行管理系统

三层架构 银行管理系统

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

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);
	}
}

局部演示图

先睡了,明天更新。

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Element-UI + Vue,孙鑫Java视频教程百度网盘

    Element-UI + Vue,孙鑫Java视频教程百度网盘},},}[]()分页功能的实现==========================================================================mysql分页查询:SELECTid,name,bir,sex,addressFROMt_userLIMIT#{start},#{rows}后端控制器:@RestController@RequestMapping(“/user”)..

  • 【强化学习】GAIL生成对抗模仿学习详解《Generative adversarial imitation learning》

    【强化学习】GAIL生成对抗模仿学习详解《Generative adversarial imitation learning》通过深度强化学习,我们能够让机器人针对一个任务实现从0到1的学习,但是需要我们定义出reward函数,在很多复杂任务,例如无人驾驶中,很难根据状态特征来建立一个科学合理的reward。人类学习新东西有一个重要的方法就是模仿学习,通过观察别人的动作来模仿学习,不需要知道任务的reward函数。模仿学习就是希望机器能够通过观察模仿专家的行为来进行学习。OpenAI,DeepMind,Google…

  • 使用css控制gridview控件的样式,GridView 样式美化及应用.doc[通俗易懂]

    使用css控制gridview控件的样式,GridView 样式美化及应用.doc[通俗易懂]GridView样式美化及应用GridView样式美化及应用摘要:ASP.NET4.0中GridView控件主要用于显示数据库的查询结果。文中对GridView控件的样式美化进行了研究,探讨了GridView控件在应用过程中的自动排序及动态添加列等功能,并给出了其实现方法。经过系统的应用与实践验证,基本实现了预期的效果。关键词:GridView;class绑定;事件中图分类号:TP311…

  • 查看端口常用命令以及关闭端口的方法

    查看端口常用命令以及关闭端口的方法dos命令行查询端口常用命令在windows命令行窗口下执行:运行–cmd–netstat-a显示所有活动的TCP连接以及计算机监听的TCP和UDP端口。netstat-e显示以太网发送和接收的字节数、数据包数等。netstat-n以数字形式显示所有活动的TCP连接的地址和端口号。netstat-o显示活动的TCP连接并包括每个连接的进程ID(PID)。netstat-s按协议显示

  • 用python字符画生日快乐_如何用特殊符号组成“生日快乐”?

    用python字符画生日快乐_如何用特殊符号组成“生日快乐”?♥¸¸.•*¨*•♫♪祝你生日2113快乐哟♥¸¸.•*¨*•♫♪║╚╦5261═╦4102═╦═╦╦╗║╚╦╦═╣╚╣╚╦╝╠═╦╦╗║║║╬║1653╬║╬║║║║╬║║╔╣╔╣║║╬║╬║║║╚╩╩╩╣╔╣╔╩╗║╚═╩╩╝╚═╩╩╩═╩╩╩╗║ƸӜƷ●•٠·˙❦ஐ❦G☺dBl姧U❦ஐ❦˙·٠•ƸӜƷ♪♫•*¨*•.¸¸HAPPYBIRTHDAY♪♫•*¨*•.¸¸生日快乐~…

  • 一张色环图教你搞定配色![通俗易懂]

    一张色环图教你搞定配色![通俗易懂]一张色环图教你搞定配色!不管是在平面设计或网页制作中,还是在平常生活中的衣服穿搭和室内装潢中,要想打造出非凡的视觉效果,合理的颜色搭配非常重要。下面介绍几种色彩搭配方案供您参考,让你轻易地一靶中的

发表回复

您的电子邮箱地址不会被公开。

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