zoukankan      html  css  js  c++  java
  • 实现微信转账功能

    package com.wx.view;
    
    import java.util.Scanner;
    
    import com.wx.Money;
    import com.wx.User;
    import com.wx.dao.WxDao;
    
    public class Transfer {
        public static void main(String[] args) {
            System.out.println("*************微信转账模块**************");
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入您的微信账号:");
            String uname = sc.next();
            System.out.print("请输入您的微信密码:");
            String pwd = sc.next();
            //判断用户名和密码的结果
            User u = new WxDao().checkLogin(uname, pwd);
            if(u!=null){
                //登陆成功
                System.out.println("恭喜您登陆成功!");
                System.out.println("用户名:"+u.getUname());
                //根据用户名获取用户账号金额
                Money m = new WxDao().getMoney(uname);
                System.out.println("账户余额:"+m.getMoeny());
                //获取好友名称与金额
                System.out.print("请输入转账的好友名称:");
                String fname = sc.next();
                System.out.print("请输入转账金额:");
                double money = sc.nextDouble();
                //执行转账的操作
                new WxDao().Transfer(money, uname, fname);
                //重新查询账户金额
                 m = new WxDao().getMoney(uname);
                System.out.println("转账成功!您的当前账户余额为"+m.getMoeny());
            }else{
                //登陆失败
                System.out.println("您的账号或密码输入错误!!");
            }
        }
    }
    package com.wx.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.wx.Money;
    import com.wx.User;
    
    public class WxDao {
        //校验用户名和密码
        public User checkLogin(String user,String pwd){
            PreparedStatement psmt = null;
            ResultSet rs = null;
            Connection con = null;
            User u = null;
            con = Tool.getConn2();
            try {
                //根据用户输入的用户名和密码查找是否有匹配的数据
                psmt = con.prepareStatement("select * from wx_user where uname=? and pwd = ?");
                psmt.setString(1, user);
                psmt.setString(2, pwd);
                rs = psmt.executeQuery();
                if(rs.next()){
                    //将查询的用户名和密码封装到用户对象u中
                    u= new User();    
                    u.setUid(rs.getInt(1));
                    u.setUname(rs.getString(2));
                    u.setPwd(rs.getString(3));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                Tool.close(con, psmt);
            }
            return u;
        }
        
        //实现查询账户余额功能
            public Money getMoney(String user){
                PreparedStatement psmt = null;
                ResultSet rs = null;
                Connection con = null;
                Money m = new Money();
                con = Tool.getConn2();
                try {
                    psmt = con.prepareStatement("select * from wx_money where uname=? ");
                    psmt.setString(1, user);
                    rs = psmt.executeQuery();
                    if(rs.next()){
                        m.setUname(rs.getString(1));
                        m.setMoeny(rs.getDouble(2));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    Tool.close(con, psmt);
                }
                return m;
            }
            //实现转账功能
            public  void Transfer(double money,String uname,String fname){
                PreparedStatement psmt = null;
                Connection con = null;
                con = Tool.getConn2();
                try {
                    //减去用户账户中的金额
                    psmt = con.prepareStatement("update wx_money set money=money-? where uname=? ");
                    psmt.setDouble(1, money);
                    psmt.setString(2, uname);
                    psmt.executeUpdate();
                    //增加好友账户中的金额
                    psmt = con.prepareStatement("update wx_money set money=money+? where uname=? ");
                    psmt.setDouble(1, money);
                    psmt.setString(2, fname);
                    psmt.executeUpdate();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }
    package com.wx.dao;
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    public class Tool {
        
    
        public static Connection getConn2(){
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","sys");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
        
        
        public static void close(Connection con,PreparedStatement psmt){
            
            try {
                
                if(con!=null){
                    con.close();
                }
                if(psmt!=null){
                    psmt.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
    }
    package com.wx;
    
    public class Money {
        String uname;
        double moeny;
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public double getMoeny() {
            return moeny;
        }
        public void setMoeny(double moeny) {
            this.moeny = moeny;
        }
        
    }
    package com.wx;
    
    public class User {
        int uid;
        String uname;
        String pwd;
        public int getUid() {
            return uid;
        }
        public void setUid(int uid) {
            this.uid = uid;
        }
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        
    }
  • 相关阅读:
    mongodb MongoDB 聚合 group
    mongo数据库的各种查询语句示例
    Web测试方法总结
    python + selenium 自动化测试框架
    selenium关于断言的使用
    JavaScript利用键盘方向键(上下键)控制表格行选中
    TestNG 入门教程
    selenium+python之 辨识alert、window以及操作
    Selenium WebDriver中鼠标事件
    JS-运动基础(一)
  • 原文地址:https://www.cnblogs.com/wangzhanxin98/p/9221731.html
Copyright © 2011-2022 走看看