【无标题】

package com.lsh.bean;

import java.util.Date;

public class Cart {

    private int cart_id;
    private int user_id;
    private int pro_id;
    private double cart_money;
    private Date cart_time;

    public Cart() {
    }

    public Cart(int cart_id, int user_id, int pro_id, double cart_money, Date cart_time) {
        this.cart_id = cart_id;
        this.user_id = user_id;
        this.pro_id = pro_id;
        this.cart_money = cart_money;
        this.cart_time = cart_time;
    }

    public int getCart_id() {
        return cart_id;
    }

    public void setCart_id(int cart_id) {
        this.cart_id = cart_id;
    }

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public int getPro_id() {
        return pro_id;
    }

    public void setPro_id(int pro_id) {
        this.pro_id = pro_id;
    }

    public double getCart_money() {
        return cart_money;
    }

    public void setCart_money(double cart_money) {
        this.cart_money = cart_money;
    }

    public Date getCart_time() {
        return cart_time;
    }

    public void setCart_time(Date cart_time) {
        this.cart_time = cart_time;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "cart_id=" + cart_id +
                ", user_id=" + user_id +
                ", pro_id=" + pro_id +
                ", cart_money=" + cart_money +
                ", cart_time=" + cart_time +
                '}';
    }


}
package com.lsh.bean;

public class Product {

    private int pro_id;
    private String pro_name;
    private double pro_price;
    private int pro_num;
    private String pro_info;
    private int pro_good;
    private int role_id;

    public int getRole_id() {
        return role_id;
    }

    public void setRole_id(int role_id) {
        this.role_id = role_id;
    }

    //    private Role role;
//
//    public Role getRole() {
//        return role;
//    }
//
//    public void setRole(Role role) {
//        this.role = role;
//    }

    public Product() {
    }

    public Product(int pro_id, String pro_name, double pro_price, int pro_num, String pro_info, int pro_good) {
        this.pro_id = pro_id;
        this.pro_name = pro_name;
        this.pro_price = pro_price;
        this.pro_num = pro_num;
        this.pro_info = pro_info;
        this.pro_good = pro_good;
    }

    public int getPro_id() {
        return pro_id;
    }

    public void setPro_id(int pro_id) {
        this.pro_id = pro_id;
    }

    public String getPro_name() {
        return pro_name;
    }

    public void setPro_name(String pro_name) {
        this.pro_name = pro_name;
    }

    public double getPro_price() {
        return pro_price;
    }

    public void setPro_price(double pro_price) {
        this.pro_price = pro_price;
    }

    public int getPro_num() {
        return pro_num;
    }

    public void setPro_num(int pro_num) {
        this.pro_num = pro_num;
    }

    public String getPro_info() {
        return pro_info;
    }

    public void setPro_info(String pro_info) {
        this.pro_info = pro_info;
    }

    public int getPro_good() {
        return pro_good;
    }

    public void setPro_good(int pro_good) {
        this.pro_good = pro_good;
    }


    @Override
    public String toString() {
        return  pro_id +
                "\t\t\t" + pro_name +
                "\t\t\t" + pro_price +
                "\t\t\t" + pro_num +
                "\t\t\t" + pro_info +
                "\t\t\t" + pro_good;
    }

}

package com.lsh.bean;

public class Role {
    private int r_id;
    private String r_name;
    private String r_username;
    private String r_password;
    private int power_id;

    public Role() {
    }

    public int getR_id() {
        return r_id;
    }

    public void setR_id(int r_id) {
        this.r_id = r_id;
    }

    public String getR_name() {
        return r_name;
    }

    public void setR_name(String r_name) {
        this.r_name = r_name;
    }

    public String getR_username() {
        return r_username;
    }

    public void setR_username(String r_username) {
        this.r_username = r_username;
    }

    public String getR_password() {
        return r_password;
    }

    public void setR_password(String r_password) {
        this.r_password = r_password;
    }

    public int getPower_id() {
        return power_id;
    }

    public void setPower_id(int power_id) {
        this.power_id = power_id;
    }

    @Override
    public String toString() {
        return "Role{" +
                "r_id=" + r_id +
                ", r_name='" + r_name + '\'' +
                ", r_username='" + r_username + '\'' +
                ", r_password='" + r_password + '\'' +
                ", power_id=" + power_id +
                '}';
    }
}
package com.lsh.dao;
import java.util.List;
import java.sql.SQLException;
import com.lsh.bean.Role;

public interface RoleDao {
    List<Role> selectRoleByAll();
    Role selectRoleByUsernameAndPassword(String username,
                                         String password);
    List showAllProduct();
    List showAllCart();
    int insertCartById(int productId, int user_id);
    int dealCartAndIndent(int user_id, int productId);
    int insertProductById(int id,String name,
                          double price,int num,
                          String info,
                          int good,
                          int bus_id)throws SQLException;
    int deleteProductById(int id) throws SQLException;

}

package com.lsh.dao;

import com.sun.tools.jconsole.JConsoleContext;
import com.lsh.bean.Cart;
import com.lsh.bean.Product;
import com.lsh.bean.Role;
import com.lsh.utils.jdbcUtils;
import org.junit.After;
import org.junit.Before;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class RoleDaoImpl implements RoleDao{
    private static Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;



    @Override
    public List<Role> selectRoleByAll() {
//        创建集合存放数据
        List roles = new ArrayList<>();

//        编写sql语句
        String sql = "select * from role";

//        防止sql语句携带参数
        try {
            ps = conn.prepareStatement(sql);
            //        执行sql语句
            rs = ps.executeQuery(sql);
//           遍历结果集
            while (rs.next()){
                Role role = new Role();
                role.setR_id(rs.getInt(1));
                role.setR_name(rs.getString(2));
                role.setR_username(rs.getString(3));
                role.setR_password(rs.getString(4));
                role.setR_id(rs.getInt(5));
                roles.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return roles;
    }

    @Override
    public Role selectRoleByUsernameAndPassword(String username, String password) {
        conn = jdbcUtils.getConnection();
        Role role = new Role();
        String sql = "select * from role where r_username = ? and r_password = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);

            rs = ps.executeQuery();
            if (!rs.next()){
                System.out.println("用户名密码错误");
            }else {
                role.setR_id(rs.getInt(1));
                role.setR_name(rs.getString(2));
                role.setPower_id(rs.getInt(5));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return role;
    }

    //    alt +  enter
    @Override
    public List<Product> showAllProduct() {
        conn = jdbcUtils.getConnection();
//        创建集合存放数据
        List products = new ArrayList<>();

//        编写sql语句
        String sql = "select * from product";

//        防止sql语句携带参数
        try {
            ps = conn.prepareStatement(sql);
            //        执行sql语句
            rs = ps.executeQuery(sql);
//           遍历结果集
            while (rs.next()){
                Product product = new Product();
                product.setPro_id(rs.getInt(1));
                product.setPro_name(rs.getString(2));
                product.setPro_price(rs.getDouble(3));
                product.setPro_num(rs.getInt(4));
                product.setPro_info(rs.getString(5));
                product.setPro_good(rs.getInt(6));
                products.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return products;
    }

    @Override
    public List<Cart> showAllCart(){
        conn = jdbcUtils.getConnection();
//        创建集合存放数据
        List carts = new ArrayList<>();

//        编写sql语句
        String sql = "select * from cart";

//        防止sql语句携带参数
        try {
            ps = conn.prepareStatement(sql);
            //        执行sql语句
            rs = ps.executeQuery(sql);
//           遍历结果集
            while (rs.next()){
                Cart cart=new Cart();
                cart.setCart_id(rs.getInt(1));
                cart.setUser_id(rs.getInt(2));
                cart.setPro_id(rs.getInt(3));
                cart.setCart_money(rs.getDouble(4));
                cart.setCart_time(rs.getTimestamp(5));
                carts.add(cart);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return carts;
    }


    @Override
    public int insertCartById(int productId, int user_id) {
        conn = jdbcUtils.getConnection();
        int res = 0;
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String sql = "insert into cart(user_id,pro_id,cart_time) VALUES(?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user_id);
            ps.setInt(2,productId);
            ps.setString(3,df.format(date));

            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return res;
    }

    //    1、生成账单
//    2、清空购物车
    @Override
    public int dealCartAndIndent(int user_id, int productId) {
        conn = jdbcUtils.getConnection();
//        存在的因素,是否有多个商品结算,存在多条记录
//                  1个商家 1几个地址,只需要一条记录

//        订单编号
        String ind_id = "";
//        1、统计购买的数量和金额
        int cartNum = 0;
        double cartMoney = 0;
        String sql = "select sum(cart_money),count(cart_id) from cart WHERE user_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user_id);
            rs =  ps.executeQuery();
            if (!rs.next()){
                System.out.println("你尚未添加任何商品,请先添加至购物车或者直接购买");
            }else {
                cartNum = rs.getInt(2);
                cartMoney = rs.getDouble(1);
//                根据不同的商品编号找到不同的商家,进行统计分类(忽略)

//                通过user_id获取一下地址

//                生成订单,然后清除购物车
                sql = "insert into indent(ind_id,user_id,ind_money,ind_state) values(?,?,?,?)";
                Date date = new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
                ind_id = UUID.randomUUID()+ df.format(date);

                ps = conn.prepareStatement(sql);
                ps.setString(1,ind_id);
                ps.setInt(2,user_id);
                ps.setDouble(3,cartMoney);
                ps.setInt(4,1);

                int intIndent = ps.executeUpdate();
                if (intIndent>0){
//                    清除购物车
                    sql = "DELETE FROM cart WHERE user_id = ?";
                    ps = conn.prepareStatement(sql);
                    ps.setInt(1,user_id);
                    int cartRes = ps.executeUpdate();
                    if (cartRes != cartNum){
                        return 0;
                    }else {
                        return cartNum;
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int insertProductById(int id, String name, double price, int num, String info, int good,int bus_id) throws SQLException {
        conn=jdbcUtils.getConnection();
        String sql="insert into product(pro_id,Pro_name,pro_price,pro_num,pro_info,pro_good,bus_id) values(?,?,?,?,?,?,?)";
        ps=conn.prepareStatement(sql);
        ps.setInt(1,id);
        ps.setString(2,name);
        ps.setDouble(3,price);
        ps.setInt(4,num);
        ps.setString(5,info);
        ps.setInt(6,good);
        ps.setInt(7,bus_id);
        int rs=ps.executeUpdate();

        return rs;
    }

    @Override
    public int deleteProductById(int id) throws SQLException{
        conn=jdbcUtils.getConnection();
        String sql="delete from Product where pro_id=?";
        ps=conn.prepareStatement(sql);
        ps.setInt(1,id);
        int rs=ps.executeUpdate();

        return rs;
    }

//    添加到购物车

}

package com.lsh.service;
import java.util.List;
import java.sql.SQLException;
import com.lsh.bean.Role;
public interface RoleService {
    //    封装方法
    List<Role> selectAllRole();
    Role loginRole(String username, String password);
    List selectAllProduct();
    List selectAllCart();
    int insertProduct(int id,
                      String name,
                      double price,
                      int num,
                      String info,
                      int good,
                      int buss_id)throws SQLException;
    int deleteProduct(int id)throws SQLException;
    int insertCart(int productId,int user_id);
    int clearCart(int r_id, int productId);
}

 

package com.lsh.service;

import java.sql.SQLException;
import java.util.List;
import com.lsh.bean.Role;
import com.lsh.dao.RoleDao;
import com.lsh.dao.RoleDaoImpl;

public class RoleServiceImpl implements RoleService{

    public RoleDao roleDao;

    //    通过roleDao调用其实现类的返回值
    public RoleServiceImpl() {
        roleDao = new RoleDaoImpl();
    }

    @Override
    public List<Role> selectAllRole() {
        return roleDao.selectRoleByAll();
    }

    @Override
    public Role loginRole(String username, String password) {
        return roleDao.selectRoleByUsernameAndPassword(username,password);
    }

    @Override
    public List selectAllProduct() {
        return roleDao.showAllProduct();
    }

    @Override
    public int insertProduct(int id, String name, double price, int num, String info, int good,int buss_id) throws SQLException {
        return roleDao.insertProductById(id,name,price,num,info,good,buss_id);
    }

    @Override
    public int deleteProduct(int id) throws SQLException {
        return roleDao.deleteProductById(id);
    }


    @Override
    public List selectAllCart(){
        return roleDao.showAllCart();
    }

    @Override
    public int insertCart(int productId, int user_id) {
        return roleDao.insertCartById(productId,user_id);
    }

    @Override
    public int clearCart(int r_id, int productId) {
        return roleDao.dealCartAndIndent(r_id,productId);
    }


}
package com.lsh.utils;

import java.sql.Connection;

import java.sql.*;

public class jdbcUtils {

    public static Connection conn;
    public static Connection getConnection(){

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");


            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/shopping_manager?serverTimezone=UTC&UserSSL=false",
                    "root",
                    "12345678" );

        }catch (ClassNotFoundException | SQLException e){

            e.printStackTrace();
        }

        return conn;
    }

    public static void closeConnection(ResultSet rs, PreparedStatement ps,Connection conn) {
        try {

            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        System.out.print(getConnection());
    }
}

package com.lsh;

import com.lsh.bean.Product;
import com.lsh.bean.Role;
import com.lsh.bean.Cart;
import com.lsh.dao.RoleDao;
import com.lsh.dao.RoleDaoImpl;
import com.lsh.service.RoleService;
import com.lsh.service.RoleServiceImpl;
import com.lsh.utils.jdbcUtils;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class main {

    public static HttpServletRequest request;


    //    主程序调用实际操作
    public static void main(String[] args) throws SQLException {
        RoleService roleService = new RoleServiceImpl();
        Scanner sc = new Scanner(System.in);

        //       1、先进行登录
        System.out.println("请输入用户名,密码");
        System.out.print("用户名:");
        String username = sc.next();
        System.out.print("密码:");
        String password = sc.next();

        Role role = roleService.loginRole(username, password);

        switch (role.getPower_id()) {
            case 1:
                jumpToAdmin(role.getR_name(), role);
                break;
            case 2:
                jumpToBussiness(role.getR_name(), role);
                break;
            case 3:
                jumpToUser(role);
                break;
        }
//        分用户、管理员和商家  判断登录信息是否正确,如果正确,再判断power_id值


//        获取到返回值了,可以对其做任意的操作
//        List<Role> roles = roleService.selectAllRole();
//        System.out.println(roles.toString());
    }

    //  HttpServletRequest request
    private static void jumpToUser(Role role) {
//        将一些常用的且一直存在的数据放在session中
//        HttpSession session = request.getSession();
//        session.setAttribute("user_id",role.getR_id());
//
//        System.out.println(session.getAttribute("user_id"));

        System.out.println("欢迎回来,尊贵的用户:" + role.getR_name());
        System.out.println("----------------------在线商城-------------------");
//        进入个人中心:1、修改个人信息、 2、查看订单  3、查看购物车 4、进入商城
        RoleService roleService = new RoleServiceImpl();
        Scanner sc = new Scanner(System.in);

        List<Product> products = roleService.selectAllProduct();
        System.out.println("商品ID\t\t\t" + "商品名称\t\t\t" + "商品价格\t\t\t" + "剩余数量\t\t\t"
                + "商品描述\t\t\t" + "最新热度");
        for (Product product : products) {
            System.out.println(product.toString());
        }
        System.out.println("1、添加购物车  2、前往下单");
        int i = sc.nextInt();
        if (i == 1){
            //        加入购物车
            addCart(role);
        }else {
            goToCheckOut(role,2101013);
        }
    }

    /*单独封装,复用*/
    public static void addCart(Role role){
        RoleService roleService = new RoleServiceImpl();
        Scanner sc = new Scanner(System.in);

        System.out.println("------请选择心仪的商品加入购物车------");
        System.out.print("输入商品ID:");
        int productId = sc.nextInt();

        System.out.println(role.getR_id()+"==================");
        int res = roleService.insertCart(productId,role.getR_id());

        if (res >= 1 ){
            System.out.println("添加成功");
            System.out.println("1、继续添加商品  2、前往下单 3、查看购物车");
            int i = sc.nextInt();
            switch (i){
                case 1:
//                    已经加入购物车内容,可以显示出来
                    addCart(role);
                    break;
                case 2:
//                    下单(用户编号,地址,付款金额,商家发货地址)
                    goToCheckOut(role,productId);
                case 3:
                    List<Cart> carts = roleService.selectAllCart();
                    System.out.println("购物车ID\t\t\t" + "用户ID\t\t\t" + "商品ID\t\t\t" + "商品价格\t\t\t"
                            + "加入时间");
                    for (Cart cart : carts) {
                        System.out.println(cart.toString());
                    }
            }
        }else{
            System.out.println("添加失败,请重新选择");
            addCart(role);
        }
    }

    //   下单
    private static void goToCheckOut(Role role, int productId) {
        RoleService roleService = new RoleServiceImpl();
        Scanner sc = new Scanner(System.in);

        int clearRes = roleService.clearCart(role.getR_id(),productId);
        if (clearRes>0){
            System.out.println("下单成功,订单号为:" + 122222222);
//            如果需要打印订单号 ,根据user_id查询订单表,获取订单信息
        }else {
            System.out.println("下单失败,请重新处理");
        }
    }


    private static void jumpToAdmin(String r_name ,Role role) throws SQLException{
        System.out.println("欢迎回来,高贵的管理员:" + r_name);
        System.out.println("----------------------在线商城-------------------");

        RoleService roleService = new RoleServiceImpl();
        Scanner sc = new Scanner(System.in);

        List<Product> products = roleService.selectAllProduct();
        System.out.println("商品ID\t\t\t" + "商品名称\t\t\t" + "商品价格\t\t\t" + "剩余数量\t\t\t"
                + "商品描述\t\t\t" + "最新热度");
        for (Product product : products) {
            System.out.println(product.toString());
        }
        System.out.println("1、下架商品  2、添加购物车  3、前往下单");
        int i = sc.nextInt();
        if (i == 1){
            System.out.println("输入要下架的产品的ID");
            Scanner input=new Scanner(System.in);
            int Pro_id;
            Pro_id=input.nextInt();
            roleService.deleteProduct(Pro_id);
            System.out.println("下架完成");
        }
        else if (i == 2){
            //        加入购物车
            addCart(role);
        }
        else if (i == 3){
            goToCheckOut(role,2101013);
        }


    }


    private static void jumpToBussiness(String r_name ,Role role) throws SQLException {
        System.out.println("欢迎回来,黑心的商家:" + r_name);
        System.out.println("----------------------在线商城-------------------");
//        进入个人中心:1、修改个人信息、 2、查看订单  3、查看购物车 4、进入商城
        RoleService roleService = new RoleServiceImpl();
        Scanner sc = new Scanner(System.in);

        List<Product> products = roleService.selectAllProduct();
        System.out.println("商品ID\t\t\t" + "商品名称\t\t\t" + "商品价格\t\t\t" + "剩余数量\t\t\t"
                + "商品描述\t\t\t" + "最新热度");
        for (Product product : products) {
            System.out.println(product.toString());
        }
        System.out.println("1、新增商品  2、下架商品  3、添加购物车  4、前往下单");
        int i = sc.nextInt();
        if (i == 1){
            System.out.println("输入要添加的产品的各类信息");
            Scanner input=new Scanner(System.in);
            int Pro_id;
            String Pro_name;
            Double Pro_price;
            int Pro_num;
            String Pro_info;
            int Pro_good;
            int Bus_id;
            System.out.println("输入商品ID");
            Pro_id=input.nextInt();
            System.out.println("输入商品名称");
            Pro_name=input.next();
            System.out.println("输入商品价格");
            Pro_price=input.nextDouble();
            System.out.println("输入剩余数量");
            Pro_num=input.nextInt();
            System.out.println("输入商品描述");
            Pro_info=input.next();
            System.out.println("输入最新热度");
            Pro_good=input.nextInt();
            System.out.println("输入商家ID");
            Bus_id=input.nextInt();
            roleService.insertProduct(Pro_id,Pro_name,Pro_price,Pro_num,Pro_info,Pro_good,Bus_id);
            System.out.println("添加完成");

        }
        if (i == 2){
            System.out.println("输入要下架的产品的ID");
            Scanner input=new Scanner(System.in);
            int Pro_id;
            Pro_id=input.nextInt();
            roleService.deleteProduct(Pro_id);
            System.out.println("下架完成");
        }
        else if (i == 3){
            //        加入购物车
            addCart(role);
        }
        else if (i == 4){
            goToCheckOut(role,2101013);
        }

    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值