转转好物交易平台

dao包

BaseDao类

package item.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver;//数据库驱动字符串
    private static String url;//连接URL字符串
    private static String user;//数据库用户名
    private static String password;//登录密码

    Connection conn = null;
    //数据连接对象
    static {//静态代码块,在类加载的时候执行
        init();
    }

    /**
     *初始化连接参数,从配置文件里获得
     * */
    public static void init() {
        Properties params = new Properties();
        String configFile = "database.properties";//配置文件路径
        //加载配置文件到输入流中
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
        try{
            //从输入流中读取属性列表
            params.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据指定的键获取对应的值
        driver = params.getProperty("driver");
        url = params.getProperty("url");
        user = params.getProperty("username");
        password = params.getProperty("password");
    }
    /**
     * 获取数据库连接对象
     */
    public Connection getConnection() {
        try{
            if (conn == null || conn.isClosed()) {
                // 获取连接并捕获异常
                try {
                    Class.forName(driver);
                    conn = DriverManager.getConnection(url, user, password);
                } catch (Exception e) {
                    e.printStackTrace(); //异常处理

                }
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;       //返回连接对象
    }

    /**
     *关闭数据库连接
     * @param conn 数据库连接
     * @param stmt Statement对象
     *  @param rs 结果集
     * */
    public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
        // 若结果集对象不为空,则关闭
        if (rs !=null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若Statement对象不为空,则关闭
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 若数据库连接对象不为空,则关闭
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 增、删、改的操作
     *  @param preparedsql 预编译的 SQL 语句
     *  @param param 参数的字符串数组
     * @return 影响的行数
     */
    public int exceuteupdate (String preparedsql, Object[] param) {
        PreparedStatement pstmt = null;
        conn = getConnection();
        try {
            pstmt = conn.prepareStatement(preparedsql);
            if (param != null&&param.length>0) {
                for (int i = 0; i < param.length; i++) {
                    //为预编译的SQL语句设置参数
                    pstmt.setObject(i + 1, param[i]);
                }
            }
            return pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
            return -1;
        }finally {
            closeAll(conn,pstmt,null);
        }
    }
}

UserDao

package item.dao;

import item.entity.User;

import java.util.List;

public interface UserDao {
    /**
     * @desc 根据用户姓名和密码返回用户
     * */
    User getUserByNameAndPwd(String name, String password);

    /**
     * 查看用户列表
     * */
    List<User> querUserList();

}

AdminDao接口

package item.dao;

import item.entity.Admin;

public interface AdminDao {
    /**
     * 根据管理员姓名返回管理员对象
     * */
    Admin getAdminByNameAndPwd(String name, String password);

    /**
     *下架不合格商品
     * */
    int soldOutGoods(int id);

    /**
     * 封禁用户账号
     * */
    int blockedAccount(int id);
}

GoodsDao接口

package item.dao;

import item.entity.Goods;
import item.entity.User;
import item.vo.GoodsDetail;

import java.util.List;

public interface GoodsDao {
    /**
     * 查看好物列表
     * */
    List<Goods> queryGoodsList(int cPage,int tPage);

    /**
     * 查看好物详情
     * */
    List<GoodsDetail> queryGoodsDetaiil(int id);

    /**
     * 实现购买好物的功能
     * */
    int buyGoods(Goods goods, User user,GoodsDetail goodsDetail,String info);

    /**
     * 实现好物上架
     * */
    int goodsAdd(Goods goods ,int id);

    /**
     * 查看我的新上架好物
     * */
    List<GoodsDetail> queryNewGooods(int id);


}

TradelogDao

package item.dao;

import item.vo.TradelogDetail;

import java.util.List;

public interface TradelogDao {
    /**
     * 实现查看交易记录功能
     * */
    List<TradelogDetail> queryDealRecord();

    /**
     * 实现生生成交易报告功能
     * */
    List<TradelogDetail> report();
}

Dao的实现类

UserDaoImpl

package item.dao.impl;

import item.dao.BaseDao;
import item.dao.UserDao;
import item.entity.User;
import item.test.Main;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    private static Logger logger= LogManager.getLogger(Main.class.getName());
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs =null;
    User user=null;
    @Override
    public User getUserByNameAndPwd(String name, String password) {
        conn=getConnection();
        try {
            pstmt=conn.prepareStatement("SELECT id,name,password,balance FROM user WHERE name=? and password=?");
            pstmt.setString(1,name);
            pstmt.setString(2,password);
            rs=pstmt.executeQuery();
             if (rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setBalance(rs.getDouble("balance"));
            }
             return user;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }

    @Override
    public List<User> querUserList() {
        conn=getConnection();
        try {
            pstmt=conn.prepareStatement("select id,name,password,balance,breakLaw from user");
            List<User> list= new ArrayList<>();

            rs=pstmt.executeQuery();
            while (rs.next()){
            user=new User(rs.getInt("id"),rs.getString("name"),rs.getString("password"),rs.getInt("balance"),rs.getInt("breakLaw"));
            list.add(user);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }

}

AdminDaoImpl

package item.dao.impl;

import item.dao.AdminDao;
import item.dao.BaseDao;
import item.entity.Admin;
import item.entity.User;
import item.test.Main;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    private static Logger logger= LogManager.getLogger(Main.class.getName());
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs =null;
    Admin admin=null;
    @Override
    public Admin getAdminByNameAndPwd(String name, String password) {
        conn=getConnection();
        try {
            pstmt=conn.prepareStatement("SELECT id,name,password FROM admin WHERE name=? and password=?");
            pstmt.setString(1,name);
            pstmt.setString(2,password);
            rs=pstmt.executeQuery();
            if (rs.next()){
                admin=new Admin();
                admin.setId(rs.getInt("id"));
                admin.setName(rs.getString("name"));
                admin.setPassword(rs.getString("password"));
            }
            return admin;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }

    @Override
    public int soldOutGoods(int id) {
        String sql="update goods set isOnSell=0  where id=?";

        Object[] parmas={id};
        int  i=super.exceuteupdate(sql,parmas);
        return i;
    }

    @Override
    public int blockedAccount(int id) {
        String sql ="update user set breakLaw=1 where id=?";
        Object[] paemas={id};
        int i=super.exceuteupdate(sql,paemas);
        return i;
    }
}

GoodsDaoImpl

package item.dao.impl;

import item.dao.BaseDao;
import item.dao.GoodsDao;
import item.entity.Goods;
import item.entity.Type;
import item.entity.User;
import item.test.Main;
import item.vo.GoodsDetail;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl extends BaseDao implements GoodsDao {
    private static Logger logger= LogManager.getLogger(Main.class.getName());
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs =null;
    User user=null;
    @Override
    public List<Goods> queryGoodsList(int cPage,int tPage) {
        try {
            String sql ="select `id`, `name`, `price`, `description`, `addTime`, `isOnSell`, `typeID`, `ownerID`  from goods where isOnSell=1 limit ?,5 ";
            conn=getConnection();
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,cPage);
            List<Goods> list=new ArrayList<>();
            rs= pstmt.executeQuery();
            Goods  goods=null;
            while (rs.next()){
                goods=new Goods(rs.getInt("id"),rs.getString("name"),rs.getDouble("price")
                ,rs.getString("description"),rs.getDate("addTime"),rs.getInt("isOnSell"),
                        rs.getInt("typeID"),rs.getInt("ownerID"));
                list.add(goods);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }

    @Override
    public List<GoodsDetail> queryGoodsDetaiil(int id) {
        try {
            String sql ="\n" +
                    "select s.id, s.`name`,s.ownerID,t.`name`,s.price,u.`name`,s.description,s.addTime\n" +
                    "from goods s \n" +
                    "inner join type t on t.id=s.typeID\n" +
                    "inner join `user` u on u.id=s.ownerID\n" +
                    "where s.isOnSell=1 and s.id=? ";
            conn=getConnection();

            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            List<GoodsDetail> list=new ArrayList<>();
            GoodsDetail goodsDetail=null;
            rs=pstmt.executeQuery();
            while (rs.next()){
                //goodsDetail=new GoodsDetail();
                goodsDetail=new GoodsDetail(rs.getInt("s.id"),rs.getString("s.name"),rs.getString("t.name"),
                        rs.getDouble("s.price"),rs.getString("u.name"),rs.getInt("s.ownerID"),rs.getString("s.description"),rs.getDate("s.addTime"));
                list.add(goodsDetail);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }

    @Override
    public int buyGoods(Goods goods,User user,GoodsDetail goodsDetail,String info) {
        String sql="begin;\n" +
                "update `user` set balance -= ? where name = ?;\n" +
                "update `user` set balance += ? where name = ?;\n" +
                "update goods set isOnSell = 0 ;\n" +
                "insert into tradelog(`buyerID`, `goodsID`, `sellerID`, `tradeTime`, `receiveInfo`) values (?, ?, ?, NOW(), ?);\n" +
                "commit;\n";


        String sql1="update `user` set balance -= ? where name = ?;";
        String sql2="update `user` set balance += ? where name = ?;";
        String sql3="update goods set isOnSell = ?;";
        String sql4="insert into tradelog(`buyerID`, `goodsID`, `sellerID`, `tradeTime`, `receiveInfo`) values (?, ?, ?, NOW(), ?);";
        Object[] params={goodsDetail.getPrice(),user.getName(),goodsDetail.getPrice(),goodsDetail.getName(),
        user.getId(),goods.getId(),goods.getOwnerID(),info};

        int i=super.exceuteupdate(sql,params);
        return i;
    }

    public int buyGoods2(double price,String name1,double price2,String name2,int buyerID,int goodsID,int sellerID,String receiveInfo,int id) {
        String sql="begin;\n" +
                "update `user` set balance=balance-? where name=?;\n" +
                "update `user`set balance=balance+? where name =?;\n" +
                "update goods set isOnSell=0 where id=?;\n" +
                "insert into tradelog(`buyerID`, `goodsID`, `sellerID`, `tradeTime`, `receiveInfo`) values(?,?,?,NOW(),?);\n" +
                "commit;\n";


        String sql1="update `user` set balance =balance- ? where name = ?;";
        String sql2="update `user` set balance =balance+? where name = ?;";
        String sql3="update goods set isOnSell = 0 where id=?;";
        String sql4="insert into tradelog(`buyerID`, `goodsID`, `sellerID`, `tradeTime`, `receiveInfo`) values (?, ?, ?, NOW(), ?);";
        Object[] params1={price,name1} ;
        Object[] params2={price2,name2};
        Object[] params3={id};
        Object[] params4={buyerID,goodsID,sellerID,receiveInfo};
        int a=super.exceuteupdate(sql1,params1);
        int b=super.exceuteupdate(sql2,params2);
        int c=super.exceuteupdate(sql3,params3);
        int d=super.exceuteupdate(sql4,params4);
        return a*b*c*d;
    }

    public void buyGoods3(User buyer,GoodsDetail goods){
        String sql="begin;\n" +
                "update `user` set balance=balance-? where name=?;\n" +
                "update `user`set balance=balance+? where name =?;\n" +
                "update goods set isOnSell=0 where id=?;\n" +
                "insert into tradelog(`buyerID`, `goodsID`, `sellerID`, `tradeTime`, `receiveInfo`) values(?,?,?,NOW(),?);\n" +
                "commit;\n";
        Object[] params ={goods.getPrice(),buyer.getName(),goods.getPrice(),goods.getOwner(),goods.getId(),buyer.getId(),goods.getId(),};
    }

    @Override
    public int goodsAdd(Goods goods,int id) {
        String sql="insert into goods(`name`, `price`, `description`, `addTime`, `isOnSell`, `typeID`, `ownerID`) values (?,?,?,now(),1,?,?)";
        Object[] params={goods.getName(),goods.getPrice(),goods.getDescription(),goods.getTypeId(),id};
        int i=super.exceuteupdate(sql,params);
        return i;
    }

    @Override
    public List<GoodsDetail> queryNewGooods(int id) {
        String sql ="select s.id,s.`name`,t.`name`,s.price,s.addTime,s.description from goods s INNER JOIN type t on t.id=s.typeID where s.ownerID=?;";
        try {
            conn=getConnection();
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            List<GoodsDetail> list =new ArrayList<>();
            GoodsDetail goodsDetail=null;
            rs= pstmt.executeQuery();
            while (rs.next()){
                goodsDetail=new GoodsDetail(rs.getInt("s.id"),rs.getString("s.name"),rs.getString("t.name")
                ,rs.getDouble("s.price"),rs.getDate("s.addTime"),rs.getString("description"));
                list.add(goodsDetail);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();;
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }
}

TradelogDaoImpl

package item.dao.impl;

import item.dao.BaseDao;
import item.dao.TradelogDao;
import item.entity.User;
import item.test.Main;
import item.vo.TradelogDetail;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TradelogDaoImpl extends BaseDao implements TradelogDao {
    private static Logger logger= LogManager.getLogger(Main.class.getName());
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs =null;
    User user=null;
    @Override
    public List<TradelogDetail> queryDealRecord() {
        String sql="SELECT t.id, g.`name`, g.price,\n" +
                "       (SELECT `name` FROM `user` WHERE id=sellerID) AS seller_name,\n" +
                "       (SELECT name FROM `user` u1 WHERE u1.id=buyerID) AS buyer_name\n" +
                ",t.tradeTime FROM tradelog t\n" +
                "INNER JOIN goods g ON g.id=t.goodsID\n" +
                "INNER JOIN `user` u ON u.id=t.sellerID \n" +
                "INNER JOIN `user` u1 ON u1.id=buyerID";
        try {
            conn=getConnection();
            pstmt= conn.prepareStatement(sql);
            List<TradelogDetail> list=new ArrayList<>();
            TradelogDetail tradelogDetail=null;
            rs= pstmt.executeQuery();
            while (rs.next()){
                tradelogDetail=new TradelogDetail(rs.getInt("t.id"),rs.getString("g.name"),rs.getDouble("g.price")
                ,rs.getString("seller_name"),rs.getString("buyer_name"),rs.getDate("t.tradeTime"));
                list.add(tradelogDetail);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }

    @Override
    public List<TradelogDetail> report() {
        String sql="SELECT B.name AS buyer_name, x.name AS seller_name, count(A.id) AS total_transactions\n" +
                "FROM tradelog A\n" +
                "JOIN user B ON A.buyerID = B.id \n" +
                "INNER JOIN `user` x ON A.sellerID=x.id\n" +
                "GROUP BY A.buyerID, B.id, x.name\n" +
                "ORDER BY total_transactions DESC LIMIT 0,3;";
        try {
            conn=getConnection();
            pstmt= conn.prepareStatement(sql);
            List<TradelogDetail> list=new ArrayList<>();
            TradelogDetail tradelogDetail=null;
            rs= pstmt.executeQuery();
            while (rs.next()){
                tradelogDetail=new TradelogDetail(rs.getString("buery_name"),rs.getString("seller_name"),rs.getInt("total_transactions"));
                list.add(tradelogDetail);
            }
            return list;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return null;
    }
}

实体类

Admin

package item.entity;
/**
 * 管理员实体类
 * */
public class Admin {
    private int id;//管理员ID
    private String name;//管理员姓名
    private String password;//管理员密码

    public Admin(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public Admin() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }
}

User

package item.entity;
/**
 * 用户实体类
 * */
public class User {
    private int id;//用户ID
    private String name;//用户姓名
    private String password;//用户密码
    private double balance;//账户余额
    private int breakLaw;//是否违规

    public User(int id, String name, String password, double balance, int breakLaw) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.balance = balance;
        this.breakLaw = breakLaw;
    }

    public int getBreakLaw() {
        return breakLaw;
    }

    public void setBreakLaw(int breakLaw) {
        this.breakLaw = breakLaw;
    }

    public User(int id, String name, String password, double balance) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.balance = balance;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }
}

Goods

package item.entity;

import java.util.Date;

/**
 * 商品实体类
 * */
public class Goods {
    private int id;//商品ID
    private String name;//商品名称
    private double price;//商品价格
    private String description;//商品描述
    private Date addTime;//添加时间
    private  int isOnSell;//是否在售 用0或1表示
    private int typeId;//商品类型ID
    private int ownerID;//商品主人ID

    public Goods(int id, String name, double price, String description, Date addTime, int isOnSell, int typeId, int ownerID) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.description = description;
        this.addTime = addTime;
        this.isOnSell = isOnSell;
        this.typeId = typeId;
        this.ownerID = ownerID;
    }

    public Goods() {
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

    public int getIsOnSell() {
        return isOnSell;
    }

    public void setIsOnSell(int isOnSell) {
        this.isOnSell = isOnSell;
    }

    public int getTypeId() {
        return typeId;
    }

    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    public int getOwnerID() {
        return ownerID;
    }

    public void setOwnerID(int ownerID) {
        this.ownerID = ownerID;
    }
}

Tradelog

package item.entity;

import java.util.Date;

/**
 * 交易记录实体
 * */
public class Tradelog {
    private int id;//交易记录ID
    private int buyerId;//购买者ID
    private int goodsId;//商品ID
    private int sellerId;//出售者ID
    private Date tradeTime;//交易时间
    private String receiveInfo;//接受信息

    public Tradelog(int id, int buyerId, int goodsId, int sellerId, Date tradeTime, String receiveInfo) {
        this.id = id;
        this.buyerId = buyerId;
        this.goodsId = goodsId;
        this.sellerId = sellerId;
        this.tradeTime = tradeTime;
        this.receiveInfo = receiveInfo;
    }

    public Tradelog() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBuyerId() {
        return buyerId;
    }

    public void setBuyerId(int buyerId) {
        this.buyerId = buyerId;
    }

    public int getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(int goodsId) {
        this.goodsId = goodsId;
    }

    public int getSellerId() {
        return sellerId;
    }

    public void setSellerId(int sellerId) {
        this.sellerId = sellerId;
    }

    public Date getTradeTime() {
        return tradeTime;
    }

    public void setTradeTime(Date tradeTime) {
        this.tradeTime = tradeTime;
    }

    public String getReceiveInfo() {
        return receiveInfo;
    }

    public void setReceiveInfo(String receiveInfo) {
        this.receiveInfo = receiveInfo;
    }
}

Type

package item.entity;
/**
 * 好物类别实体
 * */
public class Type {
    private int id;//好物类型ID
    private String name;//好物类型名称

    public Type(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Type() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

service包的话就是中间层,Mian方法借助service来用dao类面的东西,这里我就不发了

Mian方法

package item.test;

import item.dao.BaseDao;
import item.dao.GoodsDao;
import item.dao.impl.GoodsDaoImpl;
import item.entity.Admin;
import item.entity.Goods;
import item.entity.User;
import item.service.AdminService;
import item.service.GoodsService;
import item.service.TradelogService;
import item.service.UserService;
import item.service.impl.AdminServiceImpl;
import item.service.impl.GoodsServiceImpl;
import item.service.impl.TradelogServiceImpl;
import item.service.impl.UserServiceImpl;
import item.vo.GoodsDetail;
import item.vo.TradelogDetail;

import java.util.List;
import java.util.Scanner;

public class Main {
    public static Scanner input = new Scanner(System.in);
   public static UserService userService = new UserServiceImpl();
   public static AdminService adminService=new AdminServiceImpl();
   public static GoodsService goodsService=new GoodsServiceImpl();
   public static User user =null;
   public static Admin admin=null;
   public static boolean flag;
   public static Goods goods=null;
   private static GoodsDetail goodsDetail=null;
   public static List<GoodsDetail> goodsDetails=null;
   public static TradelogService tradelogService=new TradelogServiceImpl();
   public static AdminService adminService2=new AdminServiceImpl();
    public static void main(String[] args) {
        System.out.println("账号登录,账号类型:1.普通用户\t2.管理员");
        System.out.println("请输入账号类型:");
        String choice = input.next();
        if (choice.equals("1")) {
            if (login()) {
                System.out.println("登录成功!");
                System.out.println("----------------你的基本信息-------------------");
                System.out.println("用户名:" + user.getName() + "\n用户余额:" + user.getBalance());
                System.out.println("1.查看好物列表\n2.上架新的好物\n3.查看我的在售好物\n4.账户操作\n0.退出系统\n请输入要执行的操作:");
                choice = input.next();
                switch (choice) {
                    case "1":
                        int cPage = 0;
                        int tPage = 5;
                        List<Goods> list = goodsService.queryGoodsList(++cPage, tPage);
                        System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                        for (Goods goods : list) {
                            System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                        }
                        flag = true;
                        do {
                            System.out.println("1.查看好物详细\t\t2.上一页\t3.下一页\t0.返回上一级");
                            choice = input.next();
                            switch (choice) {
                                case "1":
                                    System.out.println("请输入要查看的好物编号:");
                                    int select = input.nextInt();
                                    List<GoodsDetail> list1 = goodsService.queryGoodsDetaiil(select);
                                    double price = 0;
                                    String name = null;
                                    int owner = 0;
                                    String owner2 = null;
                                    int id = 0;
                                    int id2 = 0;
                                    for (GoodsDetail goodsDetail : list1) {
                                        System.out.println("好物名称:" + goodsDetail.getName() + "\n好物类型:" + goodsDetail.getType() + "\n好物价格:" +
                                                goodsDetail.getPrice() + "\n好物主人:" + goodsDetail.getOwner() + "\n好物介绍:" + goodsDetail.getDescribe() + "\n上架时间:" + goodsDetail.getTime());
                                        owner += goodsDetail.getOwnerID();
                                        price = goodsDetail.getPrice();//好物价格
                                        name = goodsDetail.getName();//好物名称
                                        owner2 = goodsDetail.getOwner();//好物主人
                                        id = goodsDetail.getId();//序号
                                    }
                                    System.out.println("1.够买此物\t其他输入返回上一级 \t请输入要执行的操作");
                                    choice = input.next();
                                    if (choice.equals("1")) {
                                        System.out.println("请输入收获信息!");
                                        String info = input.next();
                                        if (user.getBalance() > price && !user.getName().equals(owner2)) {
                                            GoodsDaoImpl goodsDao = new GoodsDaoImpl();
                                            int j = goodsDao.buyGoods2(price, user.getName(), price, owner2, user.getId(), id, owner, info, id);
                                            if (j > 0) {
                                                System.out.println("购买成功!");
                                            } else {
                                                System.out.println("购买失败!");
                                            }
                                        } else {
                                            System.out.println("你的余额不够支付此商品!并且你不能购买自己上架的物品");
                                        }
                                    } else {
                                        flag = false;
                                    }
                                    break;
                                case "2":
                                    cPage--;
                                    if (cPage <= 0) {
                                        cPage = 1;
                                        System.out.println("已到首页,无法先前翻页");
                                    }
                                    list = goodsService.queryGoodsList(cPage, tPage);
                                    System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                                    for (Goods goods : list) {
                                        System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                                    }
                                    break;
                                case "3":
                                    ++cPage;
                                    list = goodsService.queryGoodsList(cPage, tPage);
                                    System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                                    if (list.isEmpty()) {
                                        System.out.println("已到尾页无法接续翻页");
                                        continue;
                                    }
                                    for (Goods goods : list) {
                                        System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                                    }
                                    break;
                                case "0":
                                    flag = false;
                                    break;
                            }
                        } while (flag);
                        break;
                    case "2":
                        goods = new Goods();
                        System.out.println("请输入好物名称:");
                        goods.setName(input.next());
                        System.out.println("请输入好物类别(1.创意好物,2.精致手作,3.奇趣宠物)");
                        goods.setTypeId(input.nextInt());
                        System.out.println("请输入好物价格");
                        goods.setPrice(input.nextDouble());
                        System.out.println("请输入好物介绍");
                        goods.setDescription(input.next());
                        int id = user.getId();
                        int i = goodsService.goodsAdd(goods, id);
                        if (i > 0) {
                            System.out.println("好物上架成功!");
                        } else {
                            System.out.println("好物上架失败!");
                        }
                        break;
                    case "3":
                        System.out.println("序号\t\t名称\t\t类型\t\t价格\t\t上架时间\t\t描述");
                        int num = 0;
                        goodsDetails = goodsService.queryNewGooods(user.getId());
                        for (GoodsDetail goodsDetail : goodsDetails) {
                            System.out.println(++num + "\t\t" + goodsDetail.getName() + "\t\t" + goodsDetail.getType() + "\t\t" + goodsDetail.getPrice() + "\t\t" + goodsDetail.getTime() + "\t\t" + goodsDetail.getDescribe());
                        }
                        System.out.println("1.上一页2.下一页0.返回上一级");
                        break;
                    case "4":
                        System.out.println("1.切换身份2.注销账户");
                        choice=input.next();
                        switch (choice){
                            case "1":
                                if (login2()) {
                                    GLY();
                                } else {
                                    System.out.println("账号或者密码错误");
                                }
                                break;
                            case "2":
                                System.out.println("暂时还不支持注销账户");
                                break;
                        }
                        break;
                    case "0":
                        break;
                }
            } else {
                System.out.println("登录失败!");
            }
        } else if (choice.equals("2")) {

            if (login2()) {
                System.out.println("登录成功!");
                System.out.println("1.查看交易记录\n2.生成交易报告\n3.查看好物列表\n4.下架不合格商品\n5.封禁违规账号\n0.退出系统");
                choice = input.next();
                switch (choice) {
                    case "1":
                        int i = 0;
                        List<TradelogDetail> list = tradelogService.queryDealRecord();
                        System.out.println("序号\t\t交易编号\t\t名称\t\t价格\t\t卖家\t\t买家\t\t交易时间");
                        for (TradelogDetail tradelogDetail : list) {
                            System.out.println(++i + "\t\t" + tradelogDetail.getId() + "\t\t" + tradelogDetail.getName() + "\t\t" + tradelogDetail.getPrice() + "\t\t" +
                                    tradelogDetail.getSeller() + "\t\t" + tradelogDetail.getBuyer() + "\t\t" + tradelogDetail.getTradeTime());
                        }
                        break;
                    case "2":
                        List<TradelogDetail> report = tradelogService.report();
                        String name=null;
                        String name2=null;
                        int iio=0;
                        for (TradelogDetail tradelogDetail:report){
                            name=tradelogDetail.getBuyer();
                            name2=tradelogDetail.getSeller();
                            iio=tradelogDetail.getId();
                        }
                        System.out.println("------------------------------交易报告------------------------------");
                        System.out.println("一共产生共计"+iio+"笔数据");
                        System.out.println("----------------------------------------------------------------------");
                        System.out.println("销售冠军是"+name2);
                        System.out.println("----------------------------------------------------------------------");
                        System.out.println("赛季土豪是"+name);
                        break;
                    case "3":
                        int cPage2 = 0;
                        int tPage2 = 5;
                        List<Goods> list2 = goodsService.queryGoodsList(++cPage2, tPage2);
                        System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                        for (Goods goods : list2) {
                            System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                        }
                        flag = true;
                        do {
                            System.out.println("1.查看好物详细\t\t2.上一页\t3.下一页\t0.返回上一级");
                            choice = input.next();
                            switch (choice) {
                                case "1":
                                    System.out.println("请输入要查看的好物编号:");
                                    int select = input.nextInt();
                                    List<GoodsDetail> list1 = goodsService.queryGoodsDetaiil(select);
                                    double price = 0;
                                    //String name2= null;
                                    int owner = 0;
                                    String owner2 = null;
                                    int id = 0;
                                    int id2 = 0;
                                    for (GoodsDetail goodsDetail : list1) {
                                        System.out.println("好物名称:" + goodsDetail.getName() + "\n好物类型:" + goodsDetail.getType() + "\n好物价格:" +
                                                goodsDetail.getPrice() + "\n好物主人:" + goodsDetail.getOwner() + "\n好物介绍:" + goodsDetail.getDescribe() + "\n上架时间:" + goodsDetail.getTime());
                                        owner += goodsDetail.getOwnerID();
                                        price = goodsDetail.getPrice();//好物价格
                                        name = goodsDetail.getName();//好物名称
                                        owner2 = goodsDetail.getOwner();//好物主人
                                        id = goodsDetail.getId();//序号
                                    }
                                    System.out.println("1.够买此物\t其他输入返回上一级 \t请输入要执行的操作");
                                    choice = input.next();
                                    if (choice.equals("1")) {
                                        System.out.println("请输入收获信息!");
                                        String info = input.next();
                                        if (user.getBalance() > price && !user.getName().equals(owner2)) {
                                            GoodsDaoImpl goodsDao = new GoodsDaoImpl();
                                            int j = goodsDao.buyGoods2(price, user.getName(), price, owner2, user.getId(), id, owner, info, id);
                                            if (j > 0) {
                                                System.out.println("购买成功!");
                                            } else {
                                                System.out.println("购买失败!");
                                            }
                                        } else {
                                            System.out.println("你的余额不够支付此商品!并且你不能购买自己上架的物品");
                                        }
                                    } else {
                                        flag = false;
                                    }
                            }
                        }while (flag) ;
                                    break;
                                case "4":
                                    int cPage = 0;
                                    int tPage = 5;
                                    List<Goods> list22 = goodsService.queryGoodsList(++cPage, tPage);
                                    System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                                    for (Goods goods : list22) {
                                        System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                                    }
                                    flag = true;
                                    do {
                                        System.out.println("1.下架商品\t\t2.上一页\t3.下一页\t0.返回上一级");
                                        choice = input.next();
                                        switch (choice) {
                                            case "1":
                                                System.out.println("请输入要下架的商品编号");
                                                int id31 = input.nextInt();
                                                int choice2 = adminService2.soldOutGoods(id31);
                                                if (choice2 > 0) {
                                                    System.out.println("下架成功");
                                                } else {
                                                    System.out.println("下架失败");
                                                }
                                                break;
                                            case "2":
                                                cPage--;
                                                if (cPage <= 0) {
                                                    cPage = 1;
                                                    System.out.println("已到首页,无法先前翻页");
                                                }
                                                list2 = goodsService.queryGoodsList(cPage, tPage);
                                                System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                                                for (Goods goods : list2) {
                                                    System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                                                }
                                                break;
                                            case "3":
                                                ++cPage;
                                                list2 = goodsService.queryGoodsList(cPage, tPage);
                                                System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
                                                if (list2.isEmpty()) {
                                                    System.out.println("已到尾页无法接续翻页");
                                                    continue;
                                                }
                                                for (Goods goods : list2) {
                                                    System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getAddTime());
                                                }
                                                break;
                                            case "0":
                                                flag = false;
                                                break;
                                        }
                                    } while (flag);
                                    break;
                                case "5":
                                    List<User> list11 = userService.querUserList();
                                    System.out.println("用户编号\t\t用户姓名\t\t用户密码\t\t用户资产\t\t用户是否封禁(1,表示封禁 其他表示没有封禁)");
                                    for (User user1 : list11) {
                                        System.out.println(user1.getId() + "\t\t" + user1.getName() + "\t\t" + user1.getPassword() + "\t\t" + user1.getBalance() + "\t\t" + user1.getBreakLaw());
                                    }
                                    System.out.println("请选择要封禁账号的ID");
                                    int id11 = input.nextInt();
                                    int i2 = adminService2.blockedAccount(id11);
                                    if (i2 > 0) {
                                        System.out.println("封禁成功,");
                                    } else {
                                        System.out.println("封禁失败");
                                    }
                                    break;
                                case "0":
                                    break;
                            }

                    }
                }else {
                System.out.println("登录失败,账号或者密码错误");
            }
            }


            //用户登录方法
            private static boolean login () {
                System.out.print("请输入账号:");
                String name = input.next();
                System.out.print("请输入密码:");
                String password = input.next();
                user = new User();
                user = userService.getUserByNameAndPwd(name, password);
                return user != null;
            }
            //管理员登录方法
            private static boolean login2 () {
                System.out.print("请输入账号:");
                String name = input.next();
                System.out.print("请输入密码:");
                String password = input.next();
                admin = new Admin();
                admin = adminService.getAdminByNameAndPwd(name, password);
                return admin != null;
            }

            private static void GLY () {
                if (login2()) {
                    System.out.println("登录成功!");
                    System.out.println("1.查看交易记录\n2.生成交易报告\n3.查看好物列表\n4.下架不合格商品\n5.封禁违规账号\n0.退出系统");
                    String choice = input.next();
                    switch (choice) {
                        case "1":
                            int i = 0;
                            List<TradelogDetail> list = tradelogService.queryDealRecord();
                            System.out.println("序号\t\t交易编号\t\t名称\t\t价格\t\t卖家\t\t买家\t\t交易时间");
                            for (TradelogDetail tradelogDetail : list) {
                                System.out.println(++i + "\t\t" + tradelogDetail.getId() + "\t\t" + tradelogDetail.getName() + "\t\t" + tradelogDetail.getPrice() + "\t\t" +
                                        tradelogDetail.getSeller() + "\t\t" + tradelogDetail.getBuyer() + "\t\t" + tradelogDetail.getTradeTime());
                            }
                            break;
                        case "2":
                            break;
                        case "3":
                            break;
                        case "4":
                            break;
                        case "0":
                            break;
                    }
                } else {
                    System.out.println("登录失败!");
                }
            }
        }

vo包

GoodsDetail类

package item.vo;

import java.util.Date;

/**
 * 好物信息
 * */
public class GoodsDetail {
    /**
     * 查看好物详详情
     * */
    private int id;//好物编号
    private String name;//好物名称
    private String type;//好物类型
    private double price;//好物价格
    private String owner;//好物主人
    private int ownerID;//好物主人ID
    private String describe;//好物介绍
    private Date time;//上架时间

    public GoodsDetail(int id,String name, String type, double price, String owner, String describe, Date time) {
        this.id=id;
        this.name = name;
        this.type = type;
        this.price = price;
        this.owner = owner;
        this.describe = describe;
        this.time = time;
    }

    public GoodsDetail(int id, String name, String type, double price, String owner, int ownerID, String describe, Date time) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.price = price;
        this.owner = owner;
        this.ownerID = ownerID;
        this.describe = describe;
        this.time = time;
    }

    public GoodsDetail(int anInt, String string, String string1, double aDouble, java.sql.Date date, String description) {
        this.id=anInt;
        this.name=string;
        this.type=string1;
        this.price=aDouble;
        this.time=date;
        this.describe=description;
    }

    public int getOwnerID() {
        return ownerID;
    }

    public void setOwnerID(int ownerID) {
        this.ownerID = ownerID;
    }

    public GoodsDetail() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}

TradelogDetail

package item.vo;

import java.util.Date;

/**
 * 交易记录信息
 * */
public class TradelogDetail {
    private int id;//序号
    private int tradeId;//交易编号
    private String name;//名称
    private double price;//价格
    private String seller;;//卖家
    private String buyer;//买家
    private Date tradeTime;//交易时间

    public TradelogDetail(int id, int tradeId, String name, double price, String seller, String buyer, Date tradeTime) {
        this.id = id;
        this.tradeId = tradeId;
        this.name = name;
        this.price = price;
        this.seller = seller;
        this.buyer = buyer;
        this.tradeTime = tradeTime;
    }

    public TradelogDetail() {
    }

    public TradelogDetail(int tradeId, String name, double price, String seller, String buyer, Date tradeTime) {
        this.id = tradeId;
        this.name = name;
        this.price = price;
        this.seller = seller;
        this.buyer = buyer;
        this.tradeTime = tradeTime;
    }

    public TradelogDetail(String bueryName, String sellerName, int totalTransactions) {
        this.buyer=bueryName;
        this.seller=sellerName;
        this.id=totalTransactions;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getTradeId() {
        return tradeId;
    }

    public void setTradeId(int tradeId) {
        this.tradeId = tradeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getSeller() {
        return seller;
    }

    public void setSeller(String seller) {
        this.seller = seller;
    }

    public String getBuyer() {
        return buyer;
    }

    public void setBuyer(String buyer) {
        this.buyer = buyer;
    }

    public Date getTradeTime() {
        return tradeTime;
    }

    public void setTradeTime(Date tradeTime) {
        this.tradeTime = tradeTime;
    }
}

如果service不会写的话,可以私信我,感谢观看!

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值