Java 设计并实现一个基于Java SE平台的模拟银行业务的小型信息管理系统,JDBC

    技术2022-07-10  131

    具体要求: 1.在MySQL数据库中建立名为bank的数据库,在bank库中建立银行储户表account存储储户信息。 2. 在eclipse下建立项目。 3.在上述你创建的项目内,针对account表编写实体类Account。 4. 编写数据访问接口AccountDao及其实现类AccountDaoImpl;业务接口AccountService及其实现类AccountServiceImpl。

    实现以下业务需求: 1)储户开户功能,应输入cardID,姓名,密码,初次开户余额为交易额-10(卡费)。 2)储户根据cardID及password登录系统,只有账号和密码都正确才算登录成功,否则应给出“该储户不存在”或“密码错误”等提示。 3)成功登录系统后完成存款操作,存款成功后应提示“存款成功”! 4)成功登录系统后完成取款操作,当取款金额>账户余额时,应提示 “余额不足”。取款成功应提示“取款成功!” 5)成功登录系统后完成转账操作,转账给他人,应输入他人的cardID,如果账户id有误(不存在),应提示“该储户不存在!”;应保证你的转帐金额<=你的余额,注意两人账户余额的更新,转账成功后,应提示“你向XXX(账户姓名)成功转账XXX(具体金额)元”。 5.编写用户操作类AccountTest,可以给出具体的选项操作完成以上功能,如(1.开户2.存款3.取款4.转账5.退卡)。无论每个储户来银行办理什么具体业务,都首先应该欢迎,可打印“欢迎光临××银行”,当储户退卡离开,都应该表示欢迎其下次再来,可打印“感谢您的光临,欢迎您下次再来!”

    Account:

    import java.util.Scanner; public class Account { private int cardID; private String name; private String password; private double balance=0; private double many=0; public int getCardID() { return cardID; } public void setCardID(int cardID) { this.cardID = cardID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getMany() { return many; } public void setMany(double many) { this.many = many; } public Account() { super(); // TODO Auto-generated constructor stub } public Account(int cardID, String name, String password, double balance) { super(); this.cardID = cardID; this.name = name; this.password = password; this.balance = balance; } }

    AccountDao:

    import po.Account; public interface AccountDao { int addAccount(Account account); //开户 Account depositAccount(int cardID,double many); //存款 Account withdrawalAccount(int cardID,double many); //取款 Account transferAccount(int cardID,double many,int cardID2); //转账 Account foundAccount(int cardID); //用户查询 Account lookAccount(int cardID); //展示信息 }

    AccountDaoImpl:

    package jnxyjsj.lyh.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.PreparedStatement; import po.Account; public class AccountDaoImpl implements AccountDao{ /* * 对数据库增删改查的操作 * */ Connection con=null; PreparedStatement psmt=null; PreparedStatement psmt2=null; PreparedStatement psmt3=null; ResultSet rs=null; /* * 开户—————在数据库中增加用户 * */ @Override public int addAccount(Account account) { // TODO Auto-generated method stub int i=0; try { con=DButil.getConnection(); String sql="insert into account(cardID,name,password,balance) values(?,?,?,-10)"; psmt=con.prepareStatement(sql); psmt.setInt(1, account.getCardID()); psmt.setString(2, account.getName()); psmt.setString(3, account.getPassword()); i=psmt.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(psmt!=null) { try { psmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return i; } /* * 存款操作,改变某一ID账号所对应的balance值 * */ @Override public Account depositAccount(int cardID, double many) { // TODO Auto-generated method stub try { con=DButil.getConnection(); String sql="update account set balance=balance+? where cardID=?"; psmt=con.prepareStatement(sql); psmt.setDouble(1,many); psmt.setInt(2,cardID); int i=psmt.executeUpdate(); if(i>0) { System.out.println("存款成功!"); }else { System.out.println("存款失败!"); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(psmt!=null) { try { psmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return null; } /* * 取款操作,比较取款数额和数据库中账号所对应的余额大小 * 然后改变该ID账号所对应的balance值 * */ @Override public Account withdrawalAccount(int cardID, double many) { // TODO Auto-generated method stub try { con=DButil.getConnection(); String sql="select balance from account where cardID=?"; psmt=con.prepareStatement(sql); psmt.setInt(1, cardID); rs=psmt.executeQuery(); double a=0; while(rs.next()) { a=rs.getDouble("balance"); } if(a>=many) { String sql2="update account set balance=balance-? where cardID=?"; psmt2=con.prepareStatement(sql2); psmt2.setDouble(1, many); psmt2.setInt(2, cardID); int i=psmt2.executeUpdate(); if(i>0) { System.out.println("取款成功!"); }else { System.out.println("取款失败!"); } }else { System.out.println("您的余额不足!"); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(psmt!=null) { try { psmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(psmt2!=null) { try { psmt2.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } /* * 转账操作,比较甲方转账数额和数据库中账号所对应的余额大小 * 在甲方余额中提取对应金额,在乙方余额中增加对应金额 * */ @Override public Account transferAccount(int cardID, double many, int cardID2) { // TODO Auto-generated method stub try { con=DButil.getConnection(); String sql="select balance from account where cardID=?"; psmt=con.prepareStatement(sql); psmt.setInt(1, cardID); rs=psmt.executeQuery(); double a=0; while(rs.next()) { a=rs.getDouble("balance"); } if(a>=many) { String sql2="update account set balance=balance-? where cardID=?"; con.setAutoCommit(false); //禁止自动提交 psmt2=con.prepareStatement(sql2); psmt2.setDouble(1, many); psmt2.setInt(2, cardID); int from=psmt2.executeUpdate(); String sql3="update account set balance=balance+? where cardID=?"; psmt3=con.prepareStatement(sql3); psmt3.setDouble(1, many); psmt3.setInt(2, cardID2); int to=psmt3.executeUpdate(); if(from==1 && to==1) { System.out.println("您向账户: "+cardID2+" 成功转账: "+many+"元!"); con.commit(); //提交 }else { System.out.println("转账失败!"); con.rollback(); //回滚 } }else { System.out.println("余额不足!"); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(psmt!=null) { try { psmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(psmt2!=null) { try { psmt2.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(psmt3!=null) { try { psmt3.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } /* * 查看数据库中所对应ID账号的信息 * */ @Override public Account foundAccount(int cardID) { // TODO Auto-generated method stub Account account=null; try { con=DButil.getConnection(); String sql="select * from account where cardID=?"; psmt=con.prepareStatement(sql); psmt.setInt(1, cardID); rs=psmt.executeQuery(); if(rs.next()) { account=new Account(); account.setCardID(rs.getInt(1)); account.setName(rs.getString(2)); account.setPassword(rs.getString(3)); account.setBalance(rs.getDouble(4)); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return account; } /* * 对外展示用户基本信息 * */ @Override public Account lookAccount(int cardID) { // TODO Auto-generated method stub try { con=DButil.getConnection(); String sql="select * from account where cardID=?"; psmt=con.prepareStatement(sql); psmt.setInt(1, cardID); System.out.println("ID账号: "+rs.getInt("cardID")+"\t"+" 姓名: "+rs.getString("name")+"\t"+" 账户余额: "+rs.getDouble("balance")); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(psmt!=null) { try { psmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } }

    AccountService:

    import po.Account; public interface AccountService { boolean add(int cardID,String name,String password); //开户 boolean login(int cardID,String password); //登录 boolean deposit(int cardID,double many); //存款 boolean withdrawal(int cardID,double many); //取款 boolean transfer(int cardID,double many,int cardID2); //转账 boolean look(int cardID); //查看账户信息 boolean exist(int cardID2); //查询被转账人是否存在 }

    AccountServiceImpl:

    import dao.AccountDao; import dao.AccountDaoImpl; import po.Account; public class AccountServiceImpl implements AccountService{ AccountDao dao=new AccountDaoImpl(); /* * 实现开户操作 * */ @Override public boolean add(int cardID, String name, String password) { // TODO Auto-generated method stub Account account=dao.foundAccount(cardID); if(account==null) { account=new Account(cardID,name,password,-10); int i=dao.addAccount(account); if(i>0) { System.out.println("开户成功!"); }else { System.out.println("开户失败!"); } }else { System.out.println("该账户已经被注册!"); } return false; } /* * 实现登录操作 * */ @Override public boolean login(int cardID, String password) { // TODO Auto-generated method stub Account account=dao.foundAccount(cardID); if(account!=null) { if(account.getPassword().equals(password)) { return true; }else { System.out.println("密码错误!"); } }else { System.out.println("该用户不存在!"); } return false; } /* * 实现存款操作 * */ @Override public boolean deposit(int cardID, double many) { // TODO Auto-generated method stub Account account=dao.depositAccount(cardID,many); return true; } /* * 实现取款操作 * */ @Override public boolean withdrawal(int cardID, double many) { // TODO Auto-generated method stub Account account=dao.withdrawalAccount(cardID,many); return true; } /* * 实现转账操作 * */ @Override public boolean transfer(int cardID, double many, int cardID2) { // TODO Auto-generated method stub Account account=dao.transferAccount(cardID,many,cardID2); return true; } /* * 实现查看用户信息操作 * */ @Override public boolean look(int cardID) { // TODO Auto-generated method stub Account account=dao.lookAccount(cardID); return true; } /* * 实现判断被转账人是否存在 * */ @Override public boolean exist(int cardID2) { // TODO Auto-generated method stub Account account=dao.foundAccount(cardID2); if(account!=null) { return true; } System.out.println("该用户不存在!"); return false; } }

    AccountTest:

    import java.util.Scanner; import service.AccountService; import service.AccountServiceImpl; public class AccountTest { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("--------欢迎光临济宁银行--------"); AccountService service=new AccountServiceImpl(); Scanner scanner=new Scanner(System.in); while(true) { System.out.println("1 : 开户"); System.out.println("2 : 登录"); //功能列表 System.out.println("3 : 退卡"); System.out.println("请按照提示输入相应数字以实现相应功能:"); int num=scanner.nextInt(); if(num==1) { System.out.println("请输入您的cardID: "); int cardID=scanner.nextInt(); System.out.println("请输入您的密码: "); String password=scanner.next(); System.out.println("请输入您的姓名: "); String name=scanner.next(); service.add(cardID, name, password); } if(num==2) { System.out.println("请输入您的cardID: "); int cardID=scanner.nextInt(); System.out.println("请输入您的密码: "); String password=scanner.next(); if(service.login(cardID, password)) { System.out.println("登录成功!"); System.out.println("您的帐户信息如下:"); service.look(cardID); while(true) { System.out.println("1 : 存款"); System.out.println("2 : 取款"); System.out.println("3 : 转账"); //功能列表 System.out.println("4 : 退卡"); System.out.println("请按照提示输入相应数字以实现相应功能:"); int select=scanner.nextInt(); switch(select) { case 1: { System.out.println("请输入存款金额:"); double many=scanner.nextDouble(); service.deposit(cardID, many); }break; case 2: { System.out.println("请输入取款金额:"); double many=scanner.nextDouble(); service.withdrawal(cardID, many); }break; case 3: { System.out.println("请输入被转账人cardID:"); int cardID2=scanner.nextInt(); if(service.exist(cardID2)) { System.out.println("请输入转账金额:"); double many=scanner.nextDouble(); service.transfer(cardID, many, cardID2); } }break; case 4: System.out.println("----感谢您的光临,欢迎您下次再来!----"); System.exit(0); default: System.out.println("您输入的内容有误,请重新输入!"); break; } } } } if(num==3) { System.out.println("----感谢您的光临,欢迎您下次再来!----"); break; } } } }

    DBUtil:

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("org.gjt.mm.mysql.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/bank", "root", ""); return connection; } }
    Processed: 0.020, SQL: 9