⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bankimpl.java

📁 一个简单的银行系统
💻 JAVA
字号:
package biz.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;

import dao.AccountDao;

import util.JdbcUtil;
import biz.Bank;

import entity.*;
import exception.*;
public class BankImpl implements Bank {
	private AccountDao ad;
	public BankImpl(AccountDao ad){
		this.ad=ad;
	}
	
	public Account register(String cardId,String pass,String pass2,String name,String personId,double balance,int type)throws Exception{
		if (!(pass.equals(pass2))){
			throw new RegisterException("----两次密码不一致----");
		}
		Account c=new Account(cardId,pass,name,personId,balance,type);
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			ad.insertAccount(c);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
		} 
		return c;
	}
	
	public Account deleteAccount(String cardId, String password) throws Exception {
		Account c=null;
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			c= ad.queryByCardId(cardId);
			if(c==null)throw new Exception("-----帐户不存在-----");
			if(c.getPassword().equals(password)){
				ad.deleteAccount(cardId);
				conn.commit();
			}
			else throw new Exception("-----密码不正确,无法执行删除操作-----");
		} catch(Exception e){
			conn.rollback();
		}
		return c;
	}
	
	public Account login(String cardId,String pass)throws Exception{
		Account c=null;
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			c = ad.queryByCardId(cardId);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
		}
		if(c==null)throw new LoginException("帐号不存在");
		String correctPass=c.getPassword();
		if (pass.equals(correctPass)){
			return c;
		}
		else{
			throw new LoginException("-------密码错误------");
		}
		
	}
	
	public void deposit(Account c,double money) throws Exception{
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			c.setBalance(c.getBalance()+money);
			ad.update(c);
			conn.commit();
		} catch (LoginException e) {
			conn.rollback();
		}
	}
	
	
	public void withdraw(Account c,double money)throws Exception{
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			if(c.getAccountType()==0){
				if(money<=c.getBalance())c.setBalance(c.getBalance()-money);
				else throw new BalanceNotEnoughException("帐号余额不足");
			}
			if(c.getAccountType()==1){
				if(money<=c.getBalance()+10000) c.setBalance(c.getBalance()-money);
				else throw new BalanceNotEnoughException("帐号余额不足");
			}
			ad.update(c);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
		}
	}
	
	public Account queryByCardId(String cardId) {
		Account c=null;
		try {
			c = ad.queryByCardId(cardId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}
	public List<Account> queryByPersonId(String personId){
		List<Account> l=null;
		try {
			l =ad.queryByPersonId(personId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return l;
	}
	

	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -