package com.sky.jadbc.jdbc.soso; import com.sky.jadbc.jdbc.utils.JDBCutil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Scanner; public class User { private int userid; private String username; private String password; private String uname; private String phoneNum; private String cardId; private double money; static Scanner sc=null; static String yudengluUsername=""; static String loginUsername=""; static int loginUid=0; static double nmoney=0; static { sc = new Scanner(System.in); } public static void index() throws SQLException { System.out.println("欢迎来到soso移动商城"); System.out.println("--1--注册--"); System.out.println("--2--登录--"); System.out.println("--0--退出--"); System.out.println("请选择"); Scanner sc=new Scanner(System.in); String num=sc.next(); if ("1".equals(num)) { System.out.println("注册"); register(); }else if("2".equals(num)){ System.out.println("登录"); login(); }else if ("0".equals(num)) { System.out.println("欢迎使用,程序退出"); System.exit(0); }else { System.out.println("数字不规范,重新输入"); index(); } } @Override public int hashCode() { return super.hashCode(); } public static void register() throws SQLException { System.out.println("请输入用户名"); String username=sc.next(); boolean isUsername=volidUsername(username); System.out.println(isUsername); System.out.println("请输入密码"); Scanner sc2=new Scanner(System.in); String pwd1=sc2.next(); System.out.println("请确认密码"); Scanner sc3=new Scanner(System.in); String pwd2=sc3.next(); boolean isPwdOk=volidpwd(pwd1, pwd2); String code=getCode(); System.out.println(code); System.out.println("请输入验证码"); Scanner sc4=new Scanner(System.in); String mycode=sc4.next(); if (isUsername&&isPwdOk&&mycode.equals(code)) { System.out.println("注册成功"); User user=new User(0,username,pwd1,null,null,null,10); int i= addUser(user); System.out.println( i); }else { System.out.println("注册失败"); } index(); } public static int addUser(User user) throws SQLException { Connection conn = JDBCutil.connection(); String sql="insert into user values (null,?,?,?,?,?,?)"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,user.getUsername()); pstmt.setString(2,user.getPassword()); pstmt.setString(3,user.getUname()); pstmt.setString(4,user.getPhoneNum()); pstmt.setString(5,user.getCardId()); pstmt.setDouble(6,user.getMoney()); int i=pstmt.executeUpdate(); JDBCutil.close(pstmt,conn); return i; } public static String getCode(){ String random=Math.random()+""; String s=random.substring(random.length()-4); return s; } // private static int getId() {//自动生成id // User user=users.get(users.size()-1); // int id=user.getuId(); // id++; // return id; // } public static boolean volidpwd(String pwd1,String pwd2) throws SQLException { boolean flag=true; if (pwd1.equals(pwd2)&&pwd1.length()>3&&pwd1.length()<11) { System.out.println("密码符合规范"); }else { System.out.println("密码不符合规范"); flag=false; register(); } return flag; } public static boolean chongfuUsername(String name) throws SQLException { boolean flag=true; Connection conn= JDBCutil.connection(); String sql="SELECT *\n" + "FROM `user`\n" + "WHERE user.username=?"; HashMap map=new HashMap(); PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setString(1,name); ResultSet rs=ptsmt.executeQuery(); if (rs.next()){ flag=false; String yudengluname= rs.getString("username"); yudengluUsername=yudengluname; } return flag; } public static boolean volidUsername(String username) throws SQLException {//校验用户名 boolean flag=true; if (username.length()>3&&username.length()<11) { if (chongfuUsername(username)==false) { System.out.println("用户名已被使用"); flag=false; register(); } }else { System.out.println("用户名不规范"); flag =false; register(); } return flag; } public static void login() throws SQLException { System.out.println("请输入用户名"); Scanner sc2=new Scanner(System.in); String username=sc2.next(); System.out.println("请输入密码"); Scanner sc3=new Scanner(System.in); String pwd=sc3.next(); boolean loginIsOk=voildLogin(username, pwd); if (loginIsOk==false) { loginUsername=username; String code=getCode(); System.out.println("验证码:"+code); System.out.println("请输入验证码"); Scanner sc4=new Scanner(System.in); String mycode=sc4.next(); if (mycode.equals(code)) { if ("admin".equals(username)) { loginUid(); adminView(); }else { loginUid(); userView(); } System.out.println("登录成功"); }else { System.out.println("验证码错误,登陆失败"); index(); } }else { System.out.println("用户名密码错误,登陆失败"); index(); } } public static void adminView() { } public static void userView() throws SQLException { System.out.println("欢迎您"+loginUsername); System.out.println("1.个人信息"); System.out.println("2.修改密码"); System.out.println("3.充值"); System.out.println("4.我的业务"); System.out.println("5.业务大厅"); System.out.println("6.业务办理"); System.out.println("7.业务终止"); Scanner sc=new Scanner(System.in); String num= sc.next(); switch (num) { case "1": showUser(); break; case "2": changePwd(); break; case "3": giveMoney(); break; case "4": showMyBusniess(); goback(); break; case "5": showBusniess(); goback(); break; case "6": addMyBusniess(); goback(); break; case "7": removeMyBusniess(); goback(); break; default: System.out.println("输入代码不对"); userView(); break; } } public static void removeMyBusniess() { } public static void loginUid() throws SQLException { Connection conn= JDBCutil.connection(); String sql="SELECT * FROM user WHERE username=?"; PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setString(1,loginUsername); ResultSet rs=ptsmt.executeQuery(); if (rs.next()){ int i=rs.getInt("userid"); double m=rs.getDouble("money"); loginUid=i; nmoney=m; System.out.println(loginUid); } } public static void addMyBusniess() throws SQLException { System.out.println("您现有的项目有"); showMyBusniess(); System.out.println("可办理的业务有:"); showBusniess(); System.out.println("输入您要办理的业务编号"); int i=sc.nextInt(); HashMap map=new HashMap(); Connection conn= JDBCutil.connection(); String sql="INSERT INTO ub VALUES(?,?,NULL)"; PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setInt(1,loginUid); ptsmt.setInt(2,i); String sql2="select * from busniess where bid=?"; PreparedStatement ptsmt2= conn.prepareStatement(sql2); ptsmt2.setInt(1,i); ResultSet rs=ptsmt2.executeQuery(); if (rs.next()){ double price=rs.getDouble("price"); nmoney=nmoney-price; } String sql3="UPDATE user SET money=? where userid=?"; PreparedStatement ptsmt3= conn.prepareStatement(sql3); ptsmt3.setDouble(1,nmoney); ptsmt3.setInt(2,loginUid); int z=ptsmt.executeUpdate(); int x=ptsmt3.executeUpdate(); changMoney(); } public static void showBusniess() throws SQLException { List<HashMap> list=new ArrayList<HashMap>(); Connection conn= JDBCutil.connection(); String sql="SELECT * FROM busniess WHERE status=1"; PreparedStatement ptsmt= conn.prepareStatement(sql); ResultSet rs=ptsmt.executeQuery(); while (rs.next()){ HashMap map=new HashMap(); map.put("bid",rs.getString("bid")); map.put("title",rs.getString("title")); map.put("desc",rs.getString("desc")); map.put("price",rs.getDouble("price")); list.add(map); } System.out.println(list); } public static void showMyBusniess() throws SQLException { List<HashMap> list=new ArrayList<HashMap>(); Connection conn= JDBCutil.connection(); String sql="SELECT * FROM user ,busniess,ub WHERE user.userid=ub.userid and ub.bid=busniess.bid and user.username=? "; PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setString(1,loginUsername); ResultSet rs=ptsmt.executeQuery(); if (rs.next()){ do { HashMap map=new HashMap(); map.put("title",rs.getString("title")); map.put("desc",rs.getString("desc")); map.put("price",rs.getDouble("price")); list.add(map); }while (rs.next()); System.out.println(list); }else { System.out.println("您暂时没有业务"); } } public static void giveMoney() { try { System.out.println("输入充值金额"); double giveMoney=sc.nextDouble(); if (giveMoney<=200&&giveMoney>=0) { nmoney=nmoney+giveMoney; changMoney(); // Connection conn= JDBCutil.connection(); // String sql="SELECT money FROM user where username=?"; // String sql2="UPDATE user SET money=? WHERE username=?"; // PreparedStatement ptsmt= conn.prepareStatement(sql); // ptsmt.setString(1,loginUsername); // ResultSet rs=ptsmt.executeQuery(); // if (rs.next()){ // double nowmoney=rs.getDouble("money"); // nowMoney=nowmoney; // nmoney=nowMoney; // } // nowMoney=nowMoney+giveMoney; // PreparedStatement ptsmt2= conn.prepareStatement(sql2); // ptsmt2.setDouble(1,nowMoney); // ptsmt2.setString(2,loginUsername); // ptsmt2.executeUpdate(); }else { System.out.println("充值金额必须在0-200之间"); } goback(); } catch (Exception e) { System.out.println("接受的不是小数类型"); e.printStackTrace(); } } public static void changePwd() throws SQLException { System.out.println("请输入密码"); Scanner sc2=new Scanner(System.in); String pwd1=sc2.next(); System.out.println("请确认密码"); Scanner sc3=new Scanner(System.in); String pwd2=sc3.next(); boolean isPwdOk=volidpwd(pwd1, pwd2); if (isPwdOk) { Connection conn= JDBCutil.connection(); String sql="UPDATE user SET password=? WHERE username=?"; HashMap map=new HashMap(); PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setString(1,pwd1); ptsmt.setString(2,loginUsername); int i=ptsmt.executeUpdate(); } userView(); } public static void changMoney() throws SQLException { Connection conn= JDBCutil.connection(); String sql="UPDATE user SET money=? WHERE username=?"; PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setDouble(1,nmoney); ptsmt.setString(2,loginUsername); int i=ptsmt.executeUpdate(); } public static void showUser() throws SQLException { Connection conn= JDBCutil.connection(); String sql="SELECT * FROM user WHERE username=?"; HashMap map=new HashMap(); PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setString(1,loginUsername); ResultSet rs=ptsmt.executeQuery(); if (rs.next()){ String username=rs.getString("username"); String password=rs.getString("password"); String uname=rs.getString("uname"); String phoneNum=rs.getString("phoneNum"); String cardId=rs.getString("cardId"); double money=rs.getDouble("money"); System.out.println("用户名:"+username+"\t"+"密码:"+password+"\t"+"真名:"+uname+"\t"+"电话:"+phoneNum+"\t"+"卡号"+cardId+"\t"+"余额"+nmoney); } goback(); } public static boolean voildLogin(String username,String pwd) throws SQLException { boolean flag=false; //加密 //查有无相同用户 if (chongfuUsername(username)){ Connection conn= JDBCutil.connection(); String sql="SELECT password FROM user WHERE username=?"; HashMap map=new HashMap(); PreparedStatement ptsmt= conn.prepareStatement(sql); ptsmt.setString(1,yudengluUsername); ResultSet rs=ptsmt.executeQuery(); if (rs.next()){ String nowPassword=rs.getString("password"); if (pwd==nowPassword){ flag=true; } } } return flag; } public User() { } public User(int userid, String username, String password, String uname, String phoneNum, String cardId, double money) { this.userid = userid; this.username = username; this.password = password; this.uname = uname; this.phoneNum = phoneNum; this.cardId = cardId; this.money = money; } public static void goback() throws SQLException { System.out.println("1.返回--2.退出"); Scanner sc=new Scanner(System.in); String s=sc.next(); if ("1".equals(s)) { if ("admin".equals(loginUsername)) { adminView(); }else { userView(); } }else { System.exit(0); } } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } }
5.2user
最新推荐文章于 2024-11-13 17:07:29 发布