项目源码服务 - 专业的Java毕业设计源码服务网       JAVA毕业设计源码定制,上CNITSTUDY.CN
首页 源码 Java 基于JAVA控制台实现银行管理系统_S7501

基于JAVA控制台实现银行管理系统_S7501

学习次数:615

源码类别:Java

源码类型:优质源码

下载源码
  • 源码介绍

  • 源码说明

JAVA控制台实现简单的银行卡基本功能,包含银行卡开户 取款 存款 转账及退卡等操作,使用mysql数据库

项目结构如:

主菜单如:

 

主功能代码如:

package cn.cnitstudy.view;

import java.util.LinkedList;
import java.util.Scanner;

import cn.cnitstudy.po.Account;
import cn.cnitstudy.service.AccountService;
import cn.cnitstudy.service.AccountServiceImpl;


public class AccountTest {

	public static void main(String[] args) {
		AccountTest ui = new AccountTest();
		ui.loadmenu();

	}
	
	//主菜单
	public void menu(){
		System.out.println("******欢迎光临银行管理系统******");
		System.out.println("1.开户2.存款3.取款4.转账5.退卡");
		System.out.println("请选择输入操作序号进行操作:");
	}
	
	
	
	public void loadmenu(){
		Scanner in = new Scanner(System.in);
		AccountService accountService = new AccountServiceImpl();
		// 展示主界面
		menu();
		while (true) {
			// 根据输入值进入界面
			
			try {
				int type = in.nextInt();
				switch (type) {
				case 1:
					//进入开户
					newAccount(in);
					break;
				case 2:
					//存款
					cunkuanAccount(in); 
					break;
				case 3:
					//取款
					qukuanAccount(in); 
					break;	
				case 4:
					//转账
					zhuanzhangAccount(in); 
					break;	
					//退出
				case 5:
					System.out.println("感谢您的光临,欢迎您下次再来!");
					return;
				default:
					System.out.println("输入错误,请重新输入");
					menu();
				}
			
			} catch (Exception e) {
				System.out.println("输入错误,请重新输入");
				loadmenu();
				break;
			}
		}
	}
	
	//开户
	public void newAccount(Scanner in){
			
		AccountService accountService = new AccountServiceImpl();
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入身份证号:");
		String incardID = scanner.next();
		
		if(accountService.selectByCardID(incardID)!=null) {
			System.out.println("存在重复的身份证号:请换一个号码录入");
			newAccount(in);
		}
		
		System.out.println("请输入姓名:");
		String inname = scanner.next();
		
		System.out.println("请输入密码:");
		String inpassword = scanner.next();
		
		Account account = new Account();
		account.setBalance(-10.0);
		account.setCardID(incardID);
		account.setName(inname);
		account.setPassword(inpassword);
		
		accountService.insertAccount(account);
		System.out.println("开户成功");	
		loadmenu();

	}
	
	//登录
	public String login(Scanner in){
		AccountService accountService = new AccountServiceImpl();
		System.out.println("请输入身份证号和密码:");
		String cardID = in.next();
		String password = in.next();

		if(cardID.equals("") || password.equals("")){
			System.out.println("身份证号和密码不能为空!请重新操作");
			login(in);
		}else {
			Account account =accountService.selectByName(cardID, password);
			if(account!=null) {
				return cardID;
			}else {
				System.out.println("身份证号或密码错误,请重新操作");
				login(in);
			}
		}
		return null;
		
		
	}
	
	
	
	//取款
	public void qukuanAccount(Scanner in){
		
		String cardID = login(in);
		AccountService accountService = new AccountServiceImpl();

		Account account =accountService.selectByCardID(cardID);
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入取款金额:");
		String balance = scanner.next();
		Double b = Double.parseDouble(balance);
		if(b>account.getBalance()) {
			System.out.println("余额不足");
			loadmenu();
		}else {
			account.setBalance(account.getBalance()-b);
			accountService.updateAccount(account);
			System.out.println("取款成功");
			loadmenu();
		}	
	}
	
	//存款
	public void cunkuanAccount(Scanner in){
		
		String cardID = login(in);
		AccountService accountService = new AccountServiceImpl();

		Account account =accountService.selectByCardID(cardID);
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入存款金额:");
		String balance = scanner.next();
		account.setBalance(Double.parseDouble(balance)+account.getBalance());
		accountService.updateAccount(account);
		System.out.println("存款成功");
		loadmenu();
	}
	
	
	//转账
	public void zhuanzhangAccount(Scanner in){
		
		String cardID = login(in);
		AccountService accountService = new AccountServiceImpl();

		Account account =accountService.selectByCardID(cardID);
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入收款账号:");
		String incardID = scanner.next();
		Account inaccount =accountService.selectByCardID(incardID);
		if(inaccount==null) {
			System.out.println("输入的收款账号错误或者不存在:");
			zhuanzhangAccount(in);
		}else {
			System.out.println("请输入转账金额:");
			String balance = scanner.next();
			Double b = Double.parseDouble(balance);
			if(b>account.getBalance()) {
				System.out.println("余额不足,转账失败");
				loadmenu();
			}else {
				account.setBalance(account.getBalance()-Double.parseDouble(balance));
				accountService.updateAccount(account);
				
				inaccount.setBalance(inaccount.getBalance()+Double.parseDouble(balance));
				accountService.updateAccount(inaccount);
				
				System.out.println("你向"+incardID+"("+inaccount.getName()+")成功转账"+balance+"元");
				loadmenu();
			}	
		}
	}
	
	
	
}

 

数据库操作部分主要代码

@Override
	public Account selectByName(String cardID, String password) {
        // Connection 接口代表与特定的数据库连接  并在连接上下文中执行sql 语句
        Connection conn= getConn();
        String sql="select * from account where cardID = '" + cardID + "' and password = '" + password + "'";
        //PreparedStatement类型执行动态的sql语句     Statement 执行静态语句
        PreparedStatement pstmt;

        try {
            //执行sql语句
            pstmt = conn.prepareStatement(sql);
            // 执行给定的sql语句返回单个 ResultSet 对象
            ResultSet rs = pstmt.executeQuery();
            // getMetaData()  检索此 ResultSet对象列的数量,类型和属性。 getColumnCount() 返回ResultSet对象列的数量
            if(rs.next()){
            	Account account = new Account();
            	account.setCardID(rs.getString("cardID"));
            	account.setName(rs.getString("name"));
            	account.setBalance(rs.getDouble("balance"));
				return account;
			}

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

	}

	//根据id查询账户
	@Override
	public Account selectByCardID(String cardID) {
		  // Connection 接口代表与特定的数据库连接  并在连接上下文中执行sql 语句
        Connection conn= getConn();
        String sql="select * from account where cardID = '" + cardID + "' ";
        //PreparedStatement类型执行动态的sql语句     Statement 执行静态语句
        PreparedStatement pstmt;

        try {
            //执行sql语句
            pstmt = conn.prepareStatement(sql);
            // 执行给定的sql语句返回单个 ResultSet 对象
            ResultSet rs = pstmt.executeQuery();
            // getMetaData()  检索此 ResultSet对象列的数量,类型和属性。 getColumnCount() 返回ResultSet对象列的数量
            if(rs.next()){
            	Account account = new Account();
            	account.setCardID(rs.getString("cardID"));
            	account.setName(rs.getString("name"));
            	account.setBalance(rs.getDouble("balance"));
				return account;
			}

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
	}

	//更新账户余额
	@Override
	public int updateAccount(Account b) {
		// Connection 接口代表与特定的数据库连接  并在连接上下文中执行sql 语句
        Connection conn= getConn();
        int i=0;
        String sql =  "update account set balance='" + b.getBalance() + "' where cardID='" + b.getCardID() + "'";
        // PreparedStatement 执行动态的sql语句 如增删改
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
                 //执行动态的sql语句  包含insert update delete
            i = pstmt.executeUpdate();
            if(i>0) {
                //System.out.println("修改成功!");
            }else {
                //System.out.println("未找到需要修改的数据!");
            }
            // 关闭
            pstmt.close();
            //关闭数据库连接
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       return i;

	}

	//开户信息保存
	@Override
	public int insertAccount(Account b) {
		b.setBalance(-10.0);
		 Connection conn = getConn();
	        int i=0;
	        // sql 语句的插入的数据属性
	        String sql="insert into account(cardID,name,password,balance) values(?,?,?,?)";
	        PreparedStatement pstmt;

	        try {
	            //执行 动态的sql 语句
	            pstmt = conn.prepareStatement(sql);

	            pstmt.setString(1, b.getCardID());
	            pstmt.setString(2, b.getName());
	            pstmt.setString(3, b.getPassword());
	            pstmt.setDouble(4, b.getBalance());
	            i=pstmt.executeUpdate();
	            pstmt.close();
	            conn.close();
	        } catch (SQLException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }

	        return i;

	}

 

 

本站源码均可完美运行,如使用过程中有疑问请联系,还有大量优质新开发源码不在网站上,可微信咨询 另外如需远程部署代码讲解及定制开发请加QQ:274071845 或者 微信:cnitstudy ,价格优惠合理

7*24小时在线
优质源码
请加微信联系
计算机毕业设计
项目修改
项目定制开发