项目目录修改
This commit is contained in:
		
							
								
								
									
										15
									
								
								JDBC/src/main/java/dao/abstractDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								JDBC/src/main/java/dao/abstractDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
package dao;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public interface abstractDAO<T> {
 | 
			
		||||
    int insert(T t); //增
 | 
			
		||||
 | 
			
		||||
    int delete(T t);//删
 | 
			
		||||
 | 
			
		||||
    int update(T t);//改
 | 
			
		||||
 | 
			
		||||
    List<T> search(T t);//查
 | 
			
		||||
 | 
			
		||||
    T searchID(Long id);//查 ByID
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								JDBC/src/main/java/dao/indentAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								JDBC/src/main/java/dao/indentAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
package dao;
 | 
			
		||||
 | 
			
		||||
import entities.Indent;
 | 
			
		||||
 | 
			
		||||
public interface indentAbstractDAO extends abstractDAO<Indent> {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								JDBC/src/main/java/dao/indentItemAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								JDBC/src/main/java/dao/indentItemAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
package dao;
 | 
			
		||||
 | 
			
		||||
import entities.IndentItem;
 | 
			
		||||
 | 
			
		||||
public interface indentItemAbstractDAO extends abstractDAO<IndentItem> {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								JDBC/src/main/java/dao/merchantsAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								JDBC/src/main/java/dao/merchantsAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
package dao;
 | 
			
		||||
 | 
			
		||||
import entities.Merchants;
 | 
			
		||||
 | 
			
		||||
public interface merchantsAbstractDAO extends abstractDAO<Merchants> {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								JDBC/src/main/java/dao/specification/abstractDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								JDBC/src/main/java/dao/specification/abstractDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package dao.specification;
 | 
			
		||||
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
 | 
			
		||||
import static util.SQLDatabaseConnection.close;
 | 
			
		||||
import static util.SQLDatabaseConnection.getConnection;
 | 
			
		||||
 | 
			
		||||
//@TODO 重构代码 合并userDAO和merchantsDAO中重复方法
 | 
			
		||||
public class abstractDAO<T> {
 | 
			
		||||
    public static int Delete(Connection ct, PreparedStatement ps, ResultSet rs,
 | 
			
		||||
                             String sql, Long id) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        try {
 | 
			
		||||
            ct = getConnection();
 | 
			
		||||
            ps = ct.prepareStatement(sql);
 | 
			
		||||
            ps.setLong(1, id);
 | 
			
		||||
            flag = ps.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(ct, ps, rs);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										168
									
								
								JDBC/src/main/java/dao/specification/indentDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										168
									
								
								JDBC/src/main/java/dao/specification/indentDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,168 @@
 | 
			
		||||
package dao.specification;
 | 
			
		||||
 | 
			
		||||
import dao.indentAbstractDAO;
 | 
			
		||||
import entities.Indent;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
import view.IndentItemView;
 | 
			
		||||
 | 
			
		||||
import java.sql.*;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
import static dao.specification.abstractDAO.Delete;
 | 
			
		||||
import static util.SQLDatabaseConnection.close;
 | 
			
		||||
import static util.SQLDatabaseConnection.getConnection;
 | 
			
		||||
 | 
			
		||||
public class indentDAO implements indentAbstractDAO {
 | 
			
		||||
 | 
			
		||||
    private Connection connection = null;
 | 
			
		||||
    private PreparedStatement preparedStatement = null;
 | 
			
		||||
    private ResultSet resultSet = null;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int insert(@NotNull Indent indent) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        //FIXME
 | 
			
		||||
        String insert_sql = "insert into Indent(userID, merchantsID,allPrice, message, createDate) " +
 | 
			
		||||
                "values(?,?,0,?,getdate()) ";
 | 
			
		||||
        String SetAllPrice = "update Indent " +
 | 
			
		||||
                "set allPrice = (select SUM(finalPrice) " +
 | 
			
		||||
                "from indentItem where Indent.id = indentItem.indentID group by indentID)" +
 | 
			
		||||
                "from indentItem where indentID = Indent.id and Indent.id = ?";
 | 
			
		||||
        String selectID = "select MAX(Indent.id) from Indent";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            IndentItemView iiv = new IndentItemView();
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(insert_sql);
 | 
			
		||||
            //用户ID
 | 
			
		||||
            preparedStatement.setLong(1, indent.getUserID().getId());
 | 
			
		||||
            //商家ID
 | 
			
		||||
            preparedStatement.setLong(2, indent.getMerchantsID().getId());
 | 
			
		||||
            //备注
 | 
			
		||||
            if (indent.getMessage() != null && !Objects.equals(indent.getMessage(), " ")) {
 | 
			
		||||
                preparedStatement.setString(3, indent.getMessage());
 | 
			
		||||
            } else {
 | 
			
		||||
                preparedStatement.setNull(3, Types.NVARCHAR);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
 | 
			
		||||
            //创建订单中的菜品
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectID);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
            //新的订单对象,获取当前订单ID
 | 
			
		||||
            indent = new Indent();
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                indent.setId(resultSet.getLong(1));
 | 
			
		||||
                iiv.CreateIItem(indent.getId());
 | 
			
		||||
            }
 | 
			
		||||
            //更新总价
 | 
			
		||||
            preparedStatement = connection.prepareStatement(SetAllPrice);
 | 
			
		||||
            preparedStatement.setLong(1, indent.getId());
 | 
			
		||||
            preparedStatement.executeUpdate();
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int delete(@NotNull Indent indent) {
 | 
			
		||||
        String delete_sql = "delete from Indent where id = ?";
 | 
			
		||||
        return Delete
 | 
			
		||||
                (connection, preparedStatement, resultSet, delete_sql, indent.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int update(@NotNull Indent indent) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        String update_sql = "update Indent " +
 | 
			
		||||
                "set userID = ?, merchantsID = ?, allPrice = ?, message = ?, " +
 | 
			
		||||
                "createDate = getdate() where id = ?";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(update_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setObject(1, indent.getUserID());
 | 
			
		||||
            preparedStatement.setObject(2, indent.getMerchantsID());
 | 
			
		||||
            preparedStatement.setFloat(3, indent.getAllPrice());
 | 
			
		||||
            preparedStatement.setString(4, indent.getMessage());
 | 
			
		||||
            preparedStatement.setLong(5, indent.getId());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Indent> search(Indent indent) {
 | 
			
		||||
        List<Indent> list = new ArrayList<>();
 | 
			
		||||
        String selectAll_sql = "select * from Indent";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectAll_sql);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                indent = new Indent();
 | 
			
		||||
                SetAttribute(indent, resultSet);
 | 
			
		||||
                list.add(indent);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Indent searchID(Long id) {
 | 
			
		||||
        Indent indent = null;
 | 
			
		||||
        String selectID_sql = "select * from Indent where id = ?";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectID_sql);
 | 
			
		||||
            preparedStatement.setLong(1, id);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                indent = new Indent();
 | 
			
		||||
                SetAttribute(indent, resultSet);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return indent;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SetAttribute(@NotNull Indent indent, @NotNull ResultSet rs) throws SQLException {
 | 
			
		||||
        indent.setId(rs.getLong("id"));
 | 
			
		||||
 | 
			
		||||
        //结果集是 Long 型数据
 | 
			
		||||
        userDAO user = new userDAO();
 | 
			
		||||
        indent.setUserID(user.searchID(rs.getLong("userID")));
 | 
			
		||||
        merchantsDAO merchant = new merchantsDAO();
 | 
			
		||||
        indent.setMerchantsID(merchant.searchID(rs.getLong("merchantsID")));
 | 
			
		||||
 | 
			
		||||
        indent.setAllPrice(rs.getFloat("allPrice"));
 | 
			
		||||
        indent.setMessage(rs.getString("message"));
 | 
			
		||||
        indent.setCreatedDate(rs.getDate("createDate"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										127
									
								
								JDBC/src/main/java/dao/specification/indentItemDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								JDBC/src/main/java/dao/specification/indentItemDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,127 @@
 | 
			
		||||
package dao.specification;
 | 
			
		||||
 | 
			
		||||
import dao.indentItemAbstractDAO;
 | 
			
		||||
import entities.IndentItem;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
 | 
			
		||||
import java.sql.*;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static dao.specification.abstractDAO.Delete;
 | 
			
		||||
import static util.SQLDatabaseConnection.close;
 | 
			
		||||
import static util.SQLDatabaseConnection.getConnection;
 | 
			
		||||
 | 
			
		||||
public class indentItemDAO implements indentItemAbstractDAO {
 | 
			
		||||
 | 
			
		||||
    private Connection connection = null;
 | 
			
		||||
    private PreparedStatement preparedStatement = null;
 | 
			
		||||
    private ResultSet resultSet = null;
 | 
			
		||||
 | 
			
		||||
    //TODO 这里逻辑不对,用户点菜的时候不能自己设置折扣,可能需要再多一个实体才可以
 | 
			
		||||
    @Override
 | 
			
		||||
    public int insert(@NotNull IndentItem IItem) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        String insert_sql = "insert into indentItem(name, initialPrice, discount, " +
 | 
			
		||||
                "finalPrice, description, indentID) values (?,?,?,?,?,?)";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(insert_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, IItem.getName());
 | 
			
		||||
            preparedStatement.setFloat(2, IItem.getInitialPrice());
 | 
			
		||||
            //折扣和折后价null值的判断
 | 
			
		||||
            if (IItem.getDiscount().compareTo(0F) > 0) {//折扣和0比较,返回1为合法值
 | 
			
		||||
                preparedStatement.setFloat(3, IItem.getDiscount());
 | 
			
		||||
                IItem.setFinalPrice(IItem.getInitialPrice() * IItem.getDiscount());//计算最终价格
 | 
			
		||||
                preparedStatement.setFloat(4, IItem.getFinalPrice());
 | 
			
		||||
            } else {
 | 
			
		||||
                preparedStatement.setNull(3, Types.FLOAT);
 | 
			
		||||
                //最终价格等于初始价格
 | 
			
		||||
                preparedStatement.setFloat(4, IItem.getInitialPrice());
 | 
			
		||||
            }
 | 
			
		||||
            //描述
 | 
			
		||||
            if (IItem.getDescription() != null && !IItem.getDescription().equals(" ")) {
 | 
			
		||||
                preparedStatement.setString(5, IItem.getDescription());
 | 
			
		||||
            } else {
 | 
			
		||||
                preparedStatement.setNull(5, Types.NVARCHAR);
 | 
			
		||||
            }
 | 
			
		||||
            //菜单ID
 | 
			
		||||
            preparedStatement.setObject(6, IItem.getIndentID().getId());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int delete(@NotNull IndentItem IItem) {
 | 
			
		||||
        String delete_sql = "delete from indentItem where id = ?";
 | 
			
		||||
        return Delete
 | 
			
		||||
                (connection, preparedStatement, resultSet, delete_sql, IItem.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override//TODO 这个要考虑的太多了,先跳过去
 | 
			
		||||
    public int update(IndentItem IItem) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<IndentItem> search(IndentItem IItem) {
 | 
			
		||||
        List<IndentItem> list = new ArrayList<>();
 | 
			
		||||
        String selectAll_sql = "select * from indentItem";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectAll_sql);
 | 
			
		||||
            preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                IItem = new IndentItem();
 | 
			
		||||
                SetAttribute(IItem, resultSet);
 | 
			
		||||
                list.add(IItem);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public IndentItem searchID(Long id) {
 | 
			
		||||
        IndentItem IItem = null;
 | 
			
		||||
        String selectID_sql = "select * from indentItem where id = ?";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectID_sql);
 | 
			
		||||
            preparedStatement.setLong(1, id);
 | 
			
		||||
            preparedStatement.executeQuery();
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                IItem = new IndentItem();
 | 
			
		||||
                SetAttribute(IItem, resultSet);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return IItem;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SetAttribute(@NotNull IndentItem item, @NotNull ResultSet rs) throws SQLException {
 | 
			
		||||
        item.setId(rs.getLong("id"));
 | 
			
		||||
        item.setName(rs.getString("name"));
 | 
			
		||||
        item.setInitialPrice(rs.getFloat("initialPrice"));
 | 
			
		||||
        item.setDiscount(rs.getFloat("discount"));
 | 
			
		||||
        item.setFinalPrice(rs.getFloat("finalPrice"));
 | 
			
		||||
        item.setDescription(rs.getString("description"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										147
									
								
								JDBC/src/main/java/dao/specification/merchantsDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										147
									
								
								JDBC/src/main/java/dao/specification/merchantsDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,147 @@
 | 
			
		||||
package dao.specification;
 | 
			
		||||
 | 
			
		||||
import dao.merchantsAbstractDAO;
 | 
			
		||||
import org.jetbrains.annotations.Contract;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
import entities.Merchants;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.sql.*;
 | 
			
		||||
 | 
			
		||||
import static dao.specification.abstractDAO.Delete;
 | 
			
		||||
import static util.SQLDatabaseConnection.close;
 | 
			
		||||
import static util.SQLDatabaseConnection.getConnection;
 | 
			
		||||
 | 
			
		||||
public class merchantsDAO implements merchantsAbstractDAO {
 | 
			
		||||
    private Connection connection = null;
 | 
			
		||||
    private PreparedStatement preparedStatement = null;
 | 
			
		||||
    private ResultSet resultSet = null;
 | 
			
		||||
 | 
			
		||||
    //新加商家
 | 
			
		||||
    @Override
 | 
			
		||||
    public int insert(@NotNull Merchants merchants) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        String insert_sql = "insert into Merchants(name,address,description,phoneNumber) " +
 | 
			
		||||
                "values(?,?,?,?)";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(insert_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, merchants.getName());
 | 
			
		||||
            preparedStatement.setString(2, merchants.getAddress());
 | 
			
		||||
 | 
			
		||||
            //NULL值判断和设定
 | 
			
		||||
            if (merchants.getDescription().equals(" ")) {
 | 
			
		||||
                preparedStatement.setString(3, merchants.getDescription());
 | 
			
		||||
            } else {
 | 
			
		||||
                preparedStatement.setNull(3, Types.NVARCHAR);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (merchants.getPhoneNumber().equals(" ")) {
 | 
			
		||||
                preparedStatement.setString(4, merchants.getPhoneNumber());
 | 
			
		||||
            } else {
 | 
			
		||||
                preparedStatement.setNull(4, Types.VARCHAR);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //删除商家
 | 
			
		||||
    @Override
 | 
			
		||||
    public int delete(@NotNull Merchants merchants) {
 | 
			
		||||
        String delete_sql = "delete from Merchants where id = ?";
 | 
			
		||||
        return Delete
 | 
			
		||||
                (connection, preparedStatement, resultSet, delete_sql, merchants.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //更新信息
 | 
			
		||||
    //@TODO 只能更新所有信息,不能选择性更新
 | 
			
		||||
    @Override
 | 
			
		||||
    public int update(@NotNull Merchants merchants) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        String update_sql = "update Merchants " +
 | 
			
		||||
                "set name = ?, address = ?, description = ?, phoneNumber= ? " +
 | 
			
		||||
                "where id = ?";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(update_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, merchants.getName());
 | 
			
		||||
            preparedStatement.setString(2, merchants.getAddress());
 | 
			
		||||
            preparedStatement.setString(3, merchants.getDescription());
 | 
			
		||||
            preparedStatement.setString(4, merchants.getPhoneNumber());
 | 
			
		||||
            preparedStatement.setLong(5, merchants.getId());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //查询所有商家
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Merchants> search(Merchants merchants) {
 | 
			
		||||
        List<Merchants> list = new ArrayList<>();
 | 
			
		||||
        String selectAll_sql = "select * from Merchants";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectAll_sql);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                merchants = new Merchants();
 | 
			
		||||
                SetAttribute(merchants, resultSet);
 | 
			
		||||
                list.add(merchants);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //通过id寻找商家
 | 
			
		||||
    @Override
 | 
			
		||||
    public Merchants searchID(Long id) {
 | 
			
		||||
        Merchants merchants = null;
 | 
			
		||||
        String selectID_sql = "select * from Merchants where id = ?";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectID_sql);
 | 
			
		||||
            preparedStatement.setLong(1, id);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                merchants = new Merchants();
 | 
			
		||||
                SetAttribute(merchants, resultSet);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return merchants;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //重构,合并方法
 | 
			
		||||
    private void SetAttribute(@NotNull Merchants merchants, @NotNull ResultSet rs) throws SQLException {
 | 
			
		||||
        merchants.setId(rs.getLong("id"));
 | 
			
		||||
        merchants.setName(rs.getString("name"));
 | 
			
		||||
        merchants.setPhoneNumber(rs.getString("phoneNumber"));
 | 
			
		||||
        merchants.setAddress(rs.getString("address"));
 | 
			
		||||
        merchants.setDescription(rs.getString("description"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										136
									
								
								JDBC/src/main/java/dao/specification/userDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										136
									
								
								JDBC/src/main/java/dao/specification/userDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,136 @@
 | 
			
		||||
package dao.specification;
 | 
			
		||||
 | 
			
		||||
import dao.userAbstractDAO;
 | 
			
		||||
import entities.Users;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static dao.specification.abstractDAO.Delete;
 | 
			
		||||
import static util.SQLDatabaseConnection.getConnection;
 | 
			
		||||
import static util.SQLDatabaseConnection.close;
 | 
			
		||||
 | 
			
		||||
public class userDAO implements userAbstractDAO {
 | 
			
		||||
    private Connection connection = null;
 | 
			
		||||
    private PreparedStatement preparedStatement = null;
 | 
			
		||||
    private ResultSet resultSet = null;
 | 
			
		||||
 | 
			
		||||
    //新建用户
 | 
			
		||||
    @Override
 | 
			
		||||
    public int insert(@NotNull Users users) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            String insert_sql = "insert into Users(name, phoneNumber, address, password) " +
 | 
			
		||||
                    "values (?,?,?,?)";
 | 
			
		||||
            preparedStatement = connection.prepareStatement(insert_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, users.getName());
 | 
			
		||||
            preparedStatement.setString(2, users.getPhoneNumber());
 | 
			
		||||
            preparedStatement.setString(3, users.getAddress());
 | 
			
		||||
            preparedStatement.setString(4, users.getPassword());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //删除
 | 
			
		||||
    @Override
 | 
			
		||||
    public int delete(@NotNull Users user) {
 | 
			
		||||
        String delete_sql = "delete from Users where id=?";
 | 
			
		||||
 | 
			
		||||
        return Delete
 | 
			
		||||
                (connection, preparedStatement, resultSet, delete_sql, user.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //更新用户个人信息
 | 
			
		||||
    //@TODO 只能更新所有信息,不能选择性更新
 | 
			
		||||
    @Override
 | 
			
		||||
    public int update(@NotNull Users users) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        String update_sql = "update Users " +
 | 
			
		||||
                "set name = ?, phoneNumber = ?, address = ? " +
 | 
			
		||||
                "where id = ?";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(update_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, users.getName());
 | 
			
		||||
            preparedStatement.setString(2, users.getPhoneNumber());
 | 
			
		||||
            preparedStatement.setString(3, users.getAddress());
 | 
			
		||||
            preparedStatement.setLong(4, users.getId());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //查询所有用户
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Users> search(Users user) {
 | 
			
		||||
        List<Users> list = new ArrayList<>();
 | 
			
		||||
        String selectAll_sql = "select * from Users";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectAll_sql);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                user = new Users();
 | 
			
		||||
                SetAttribute(user, resultSet);
 | 
			
		||||
                list.add(user);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //用ID查询用户
 | 
			
		||||
    @Override
 | 
			
		||||
    public Users searchID(Long id) {
 | 
			
		||||
        Users user = null;
 | 
			
		||||
        String selectID_sql = "select * from Users where id = ?";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectID_sql);
 | 
			
		||||
            preparedStatement.setLong(1, id);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                user = new Users();
 | 
			
		||||
                SetAttribute(user, resultSet);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SetAttribute(@NotNull Users user, @NotNull ResultSet rs) throws SQLException {
 | 
			
		||||
        user.setId(rs.getLong("id"));
 | 
			
		||||
        user.setName(rs.getString("name"));
 | 
			
		||||
        user.setPhoneNumber(rs.getString("phoneNumber"));
 | 
			
		||||
        user.setAddress(rs.getString("address"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								JDBC/src/main/java/dao/userAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								JDBC/src/main/java/dao/userAbstractDAO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
package dao;
 | 
			
		||||
 | 
			
		||||
import entities.Users;
 | 
			
		||||
 | 
			
		||||
public interface userAbstractDAO extends abstractDAO<Users> {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								JDBC/src/main/java/entities/Indent.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								JDBC/src/main/java/entities/Indent.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
package entities;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
//订单类
 | 
			
		||||
@Data
 | 
			
		||||
public class Indent {
 | 
			
		||||
    private Long id;//订单ID
 | 
			
		||||
 | 
			
		||||
    private Float allPrice;//菜品总价
 | 
			
		||||
    Date createdDate;//订单创建时间
 | 
			
		||||
    private String message;//下单备注
 | 
			
		||||
 | 
			
		||||
    private Users userID;//外键,下单用户ID
 | 
			
		||||
    private Merchants merchantsID;//外键,餐厅ID
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								JDBC/src/main/java/entities/IndentItem.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								JDBC/src/main/java/entities/IndentItem.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
package entities;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
//订单中的菜品
 | 
			
		||||
@Data
 | 
			
		||||
public class IndentItem {
 | 
			
		||||
    private Long id;//主键,菜品ID
 | 
			
		||||
    private String name;//菜品名字
 | 
			
		||||
    private Float initialPrice;//菜品原价
 | 
			
		||||
    private Float discount;//折扣
 | 
			
		||||
    private Float finalPrice;//最终价格
 | 
			
		||||
    private String description;//菜品描述
 | 
			
		||||
 | 
			
		||||
    private Indent indentID;//外键,订单ID
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								JDBC/src/main/java/entities/Merchants.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								JDBC/src/main/java/entities/Merchants.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
package entities;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
//商家类
 | 
			
		||||
@Data
 | 
			
		||||
public class Merchants {
 | 
			
		||||
    private Long id;//主键,商家编号
 | 
			
		||||
 | 
			
		||||
    private String name;//店铺名字
 | 
			
		||||
    private String address;//店铺地址
 | 
			
		||||
    private String description;//店铺描述
 | 
			
		||||
    private String phoneNumber;//商家联系方式
 | 
			
		||||
 | 
			
		||||
    public Merchants(String name, String address,
 | 
			
		||||
                     String description, String phoneNumber) {
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        this.address = address;
 | 
			
		||||
        this.description = description;
 | 
			
		||||
        this.phoneNumber = phoneNumber;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Merchants() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								JDBC/src/main/java/entities/Users.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								JDBC/src/main/java/entities/Users.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
package entities;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
//用户类
 | 
			
		||||
@Data
 | 
			
		||||
public class Users {
 | 
			
		||||
    private Long id;//主键,用户ID
 | 
			
		||||
    private String name;//用户姓名
 | 
			
		||||
    private String phoneNumber;//用户联系方式
 | 
			
		||||
    private String address;//家庭住址
 | 
			
		||||
    private String password;//用户密码
 | 
			
		||||
 | 
			
		||||
    public Users() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Users(String name, String phoneNumber,
 | 
			
		||||
                 String address, String password) {
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        this.phoneNumber = phoneNumber;
 | 
			
		||||
        this.address = address;
 | 
			
		||||
        this.password = password;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								JDBC/src/main/java/entities/dto/Login.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								JDBC/src/main/java/entities/dto/Login.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
package entities.dto;
 | 
			
		||||
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
import static util.SQLDatabaseConnection.close;
 | 
			
		||||
import static util.SQLDatabaseConnection.getConnection;
 | 
			
		||||
import static view.CommonUse.CreateIndent;
 | 
			
		||||
 | 
			
		||||
public class Login {
 | 
			
		||||
    static Connection conn = null;
 | 
			
		||||
    static PreparedStatement ps = null;
 | 
			
		||||
    static ResultSet rs = null;
 | 
			
		||||
 | 
			
		||||
    public static boolean UserLogin(Long id) {
 | 
			
		||||
        boolean flag = false;
 | 
			
		||||
        String sql = "select password from Users where id = ?";
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        try {
 | 
			
		||||
            conn = getConnection();
 | 
			
		||||
            ps = conn.prepareStatement(sql);
 | 
			
		||||
            ps.setLong(1, id);
 | 
			
		||||
            rs = ps.executeQuery();
 | 
			
		||||
 | 
			
		||||
            if (!rs.next()) {
 | 
			
		||||
                //TODO ID输入不正确后重新输入,但是这个函数调用的太乱了,有点偷懒了,有机会改一下
 | 
			
		||||
                System.out.println("查无此人");
 | 
			
		||||
                CreateIndent();//跳转到登录选项重新输入ID
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            System.out.println("请输入登录密码:");
 | 
			
		||||
            String password = reader.nextLine();
 | 
			
		||||
            flag = password.equals(rs.getString("password"));
 | 
			
		||||
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            close(conn, ps, rs);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								JDBC/src/main/java/example/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								JDBC/src/main/java/example/App.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
package example;
 | 
			
		||||
 | 
			
		||||
import static view.shellUI.run;
 | 
			
		||||
 | 
			
		||||
public class App {
 | 
			
		||||
    public static void main(String[] args) throws IllegalAccessException {
 | 
			
		||||
        run();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								JDBC/src/main/java/test.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								JDBC/src/main/java/test.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
public class test {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        String s = "";
 | 
			
		||||
        while (!reader.hasNext("end")){
 | 
			
		||||
            s = reader.nextLine();
 | 
			
		||||
        }
 | 
			
		||||
        String[] sn = s.split("\\s+");
 | 
			
		||||
        for (String item:sn) {
 | 
			
		||||
            System.out.println(item);
 | 
			
		||||
        }
 | 
			
		||||
        System.out.println(Arrays.toString(sn));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								JDBC/src/main/java/util/SQLDatabaseConnection.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								JDBC/src/main/java/util/SQLDatabaseConnection.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
package util;
 | 
			
		||||
 | 
			
		||||
import java.sql.*;
 | 
			
		||||
 | 
			
		||||
public class SQLDatabaseConnection {
 | 
			
		||||
    // Connect to database
 | 
			
		||||
    private static final String connectionUrl =
 | 
			
		||||
            "jdbc:sqlserver://152.136.182.168:1433;"
 | 
			
		||||
                    + "database=Elm;"
 | 
			
		||||
                    + "user=guest;"
 | 
			
		||||
                    + "password=20230504#Guest;"
 | 
			
		||||
                    + "encrypt=true;"
 | 
			
		||||
                    + "trustServerCertificate=true;"
 | 
			
		||||
                    + "loginTimeout=30;";
 | 
			
		||||
 | 
			
		||||
    public static Connection getConnection() {
 | 
			
		||||
        Connection connection = null;
 | 
			
		||||
        try {
 | 
			
		||||
            // Code here.
 | 
			
		||||
            connection = DriverManager.getConnection(connectionUrl);
 | 
			
		||||
        }
 | 
			
		||||
        // Handle any errors that may have occurred.
 | 
			
		||||
        catch (SQLException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        return connection;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //释放资源
 | 
			
		||||
    public static void close(Connection conn, Statement st, ResultSet rs) {
 | 
			
		||||
        try {
 | 
			
		||||
            if (rs != null) {
 | 
			
		||||
                rs.close();
 | 
			
		||||
            }
 | 
			
		||||
            if (st != null) {
 | 
			
		||||
                st.close();
 | 
			
		||||
            }
 | 
			
		||||
            if (conn != null) {
 | 
			
		||||
                conn.close();
 | 
			
		||||
            }
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										97
									
								
								JDBC/src/main/java/util/Toolset.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								JDBC/src/main/java/util/Toolset.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,97 @@
 | 
			
		||||
package util;
 | 
			
		||||
 | 
			
		||||
import org.jetbrains.annotations.Contract;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
import java.util.StringJoiner;
 | 
			
		||||
import java.util.concurrent.ConcurrentHashMap;
 | 
			
		||||
 | 
			
		||||
public class Toolset {
 | 
			
		||||
    public static int option() {
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        int chose;
 | 
			
		||||
        //读取选项
 | 
			
		||||
        while (true) {
 | 
			
		||||
            if (reader.hasNextInt()) {
 | 
			
		||||
                chose = reader.nextInt();
 | 
			
		||||
                break;
 | 
			
		||||
            } else {
 | 
			
		||||
                System.out.println("你的输入有误,请重新输入~");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return chose;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //shellUI格式化输出
 | 
			
		||||
    private static final Map<Class<?>, Field[]> CLASS_2_FIELD_NAME = new ConcurrentHashMap<>();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 将对象打印成表格
 | 
			
		||||
     */
 | 
			
		||||
    @SafeVarargs
 | 
			
		||||
    public static <T> String table(Class<T> clazz, T... objs) throws IllegalAccessException {
 | 
			
		||||
        Field[] fields = safeGetRefCtx(clazz);
 | 
			
		||||
        if (fields.length == 0) {
 | 
			
		||||
            return "";
 | 
			
		||||
        }
 | 
			
		||||
        // 计算每一列
 | 
			
		||||
        int[] maxLens = new int[fields.length];
 | 
			
		||||
        for (int i = 0; i < fields.length; i++) {
 | 
			
		||||
            final int ti = i;
 | 
			
		||||
            // 取出当前字段中最长的长度
 | 
			
		||||
            maxLens[i] = Arrays.stream(objs)
 | 
			
		||||
                    .map(item -> {
 | 
			
		||||
                        try {
 | 
			
		||||
                            return String.valueOf(fields[ti].get(item));
 | 
			
		||||
                        } catch (IllegalAccessException e) {
 | 
			
		||||
                            // pass
 | 
			
		||||
                        }
 | 
			
		||||
                        return "";
 | 
			
		||||
                    })
 | 
			
		||||
                    .mapToInt(String::length)
 | 
			
		||||
                    .max()
 | 
			
		||||
                    .orElse(0);
 | 
			
		||||
        }
 | 
			
		||||
        StringJoiner result = new StringJoiner("\n");
 | 
			
		||||
        for (T obj : objs) {
 | 
			
		||||
            // 竖线前后加一个空格分割
 | 
			
		||||
            StringJoiner sj = new StringJoiner(" | ");
 | 
			
		||||
            for (int i = 0; i < fields.length; i++) {
 | 
			
		||||
                sj.add(fields[i].getName() + ": " + padRight(String.valueOf(fields[i].get(obj)), maxLens[i]));
 | 
			
		||||
            }
 | 
			
		||||
            result.merge(sj);
 | 
			
		||||
        }
 | 
			
		||||
        return result.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 右侧补充字符
 | 
			
		||||
     */
 | 
			
		||||
    @Contract(pure = true)
 | 
			
		||||
    private static @NotNull String padRight(@NotNull String origin, int length) {
 | 
			
		||||
        StringBuilder originBuilder = new StringBuilder(origin);
 | 
			
		||||
        while (originBuilder.length() < length) {
 | 
			
		||||
            originBuilder.append(' ');
 | 
			
		||||
        }
 | 
			
		||||
        origin = originBuilder.toString();
 | 
			
		||||
        return origin;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static Field @NotNull [] safeGetRefCtx(Class<?> clazz) {
 | 
			
		||||
        Field[] fields = CLASS_2_FIELD_NAME.get(clazz);
 | 
			
		||||
        if (fields == null) {
 | 
			
		||||
            fields = clazz.getDeclaredFields();
 | 
			
		||||
            for (Field field : fields) {
 | 
			
		||||
                //打开私有访问
 | 
			
		||||
                field.setAccessible(true);
 | 
			
		||||
            }
 | 
			
		||||
            CLASS_2_FIELD_NAME.put(clazz, fields);
 | 
			
		||||
        }
 | 
			
		||||
        return fields;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										56
									
								
								JDBC/src/main/java/view/CommonUse.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								JDBC/src/main/java/view/CommonUse.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
package view;
 | 
			
		||||
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
import static entities.dto.Login.UserLogin;
 | 
			
		||||
 | 
			
		||||
//用户日常使用
 | 
			
		||||
public class CommonUse {
 | 
			
		||||
    public void use(int chose) {
 | 
			
		||||
        switch (chose) {
 | 
			
		||||
            case 0 -> System.exit(0);
 | 
			
		||||
            case 1 -> CreateIndent();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //TODO 这个部分和 Login 有点重合,代码可以重构一下
 | 
			
		||||
    public static void CreateIndent() {
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        Long userID = null;
 | 
			
		||||
        System.out.println("请输入你的ID:");
 | 
			
		||||
        //异常处理
 | 
			
		||||
        try {
 | 
			
		||||
            userID = reader.nextLong();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            System.out.println("你输入的ID不正确!\n你想退出吗?(Y/n)");
 | 
			
		||||
            reader = new Scanner(System.in);
 | 
			
		||||
            String s = reader.next();
 | 
			
		||||
            if (s.equals("Y")) {
 | 
			
		||||
                System.exit(0);
 | 
			
		||||
            } else {
 | 
			
		||||
                CreateIndent();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        while (true) {
 | 
			
		||||
            //密码判断
 | 
			
		||||
            boolean flag = UserLogin(userID);
 | 
			
		||||
            if (flag) {
 | 
			
		||||
                IndentView iv = new IndentView();
 | 
			
		||||
                iv.CreateIndent(userID);
 | 
			
		||||
                break;
 | 
			
		||||
            } else {
 | 
			
		||||
                System.out.println("你输入的密码不正确~\n你想退出吗?(Y/n)");
 | 
			
		||||
                reader = new Scanner(System.in);
 | 
			
		||||
                String s = reader.next();
 | 
			
		||||
                if (s.equals("Y")) {
 | 
			
		||||
                    System.exit(0);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										109
									
								
								JDBC/src/main/java/view/IndentItemView.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								JDBC/src/main/java/view/IndentItemView.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
package view;
 | 
			
		||||
 | 
			
		||||
import dao.indentItemAbstractDAO;
 | 
			
		||||
import dao.specification.indentDAO;
 | 
			
		||||
import dao.specification.indentItemDAO;
 | 
			
		||||
import entities.IndentItem;
 | 
			
		||||
import util.Toolset;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
public class IndentItemView {
 | 
			
		||||
    public void IndentItemDB(int chose) throws IllegalAccessException {
 | 
			
		||||
 | 
			
		||||
        switch (chose) {
 | 
			
		||||
            case 0 -> System.exit(0);
 | 
			
		||||
            case 1 -> DeleteIItem();
 | 
			
		||||
            case 2 -> UpdateIItem();
 | 
			
		||||
            case 3 -> SearchIItem(true);//查询全部
 | 
			
		||||
            case 4 -> SearchIItem(false);//查询ID
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void CreateIItem(Long indentID) {
 | 
			
		||||
        indentItemAbstractDAO DML_insert = new indentItemDAO();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        System.out.println("请输入:菜名|初始价格|折扣|描述 退出请输入end");
 | 
			
		||||
 | 
			
		||||
        int flag = 0;
 | 
			
		||||
        while (!reader.hasNext("end")) {
 | 
			
		||||
            IndentItem IItem = new IndentItem();
 | 
			
		||||
            String s;
 | 
			
		||||
            String[] sn;
 | 
			
		||||
            while (!reader.hasNext("end")) {
 | 
			
		||||
                s = reader.nextLine();
 | 
			
		||||
                sn = s.split("\\s+");
 | 
			
		||||
                //菜名
 | 
			
		||||
                IItem.setName(sn[0]);
 | 
			
		||||
                //初始价格
 | 
			
		||||
                IItem.setInitialPrice(Float.valueOf(sn[1]));
 | 
			
		||||
                //折扣
 | 
			
		||||
                if (sn[3] != null) {
 | 
			
		||||
                    IItem.setDiscount(Float.valueOf(sn[2]));
 | 
			
		||||
                } else {
 | 
			
		||||
                    IItem.setDiscount(-1F);
 | 
			
		||||
                }
 | 
			
		||||
                //描述
 | 
			
		||||
                if (sn[3] != null) {
 | 
			
		||||
                    IItem.setDescription(sn[3]);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
//            IItem.setName(reader.next());
 | 
			
		||||
//            IItem.setInitialPrice(reader.nextFloat());
 | 
			
		||||
 | 
			
		||||
//            if (reader.hasNextFloat()) {
 | 
			
		||||
//                IItem.setDiscount(reader.nextFloat());
 | 
			
		||||
//            } else {
 | 
			
		||||
//                IItem.setDiscount(-1F);
 | 
			
		||||
//            }
 | 
			
		||||
 | 
			
		||||
//            if (!reader.hasNext("end") && !Objects.equals(reader.nextLine(), " ")) {
 | 
			
		||||
//                IItem.setDescription(reader.nextLine());
 | 
			
		||||
//            }
 | 
			
		||||
            //菜单ID
 | 
			
		||||
            indentDAO indent = new indentDAO();
 | 
			
		||||
            IItem.setIndentID(indent.searchID(indentID));
 | 
			
		||||
 | 
			
		||||
            flag += DML_insert.insert(IItem);
 | 
			
		||||
        }
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void DeleteIItem() {
 | 
			
		||||
        indentItemAbstractDAO DML_delete = new indentItemDAO();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        IndentItem IItem = new IndentItem();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入你要删除的ID");
 | 
			
		||||
        IItem.setId(reader.nextLong());
 | 
			
		||||
 | 
			
		||||
        int flag = DML_delete.delete(IItem);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //TODO 更新订单菜品
 | 
			
		||||
    private void UpdateIItem() {
 | 
			
		||||
        indentItemAbstractDAO DML_update = new indentItemDAO();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        IndentItem IItem = new IndentItem();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SearchIItem(boolean b) throws IllegalAccessException {
 | 
			
		||||
        indentItemAbstractDAO DQL = new indentItemDAO();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        IndentItem IItem = new IndentItem();
 | 
			
		||||
 | 
			
		||||
        if (b) {
 | 
			
		||||
            for (IndentItem item : DQL.search(IItem)) {
 | 
			
		||||
                System.out.println(Toolset.table(IndentItem.class, item));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            System.out.println("请输入你要查询的ID:");
 | 
			
		||||
            IItem = DQL.searchID(reader.nextLong());
 | 
			
		||||
            System.out.println(Toolset.table(IndentItem.class, IItem));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										78
									
								
								JDBC/src/main/java/view/IndentView.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								JDBC/src/main/java/view/IndentView.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,78 @@
 | 
			
		||||
package view;
 | 
			
		||||
 | 
			
		||||
import dao.indentAbstractDAO;
 | 
			
		||||
import dao.specification.indentDAO;
 | 
			
		||||
import dao.specification.merchantsDAO;
 | 
			
		||||
import dao.specification.userDAO;
 | 
			
		||||
import entities.Indent;
 | 
			
		||||
import entities.Users;
 | 
			
		||||
import util.Toolset;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
public class IndentView {
 | 
			
		||||
    public void IndentDB(int chose) throws IllegalAccessException {
 | 
			
		||||
 | 
			
		||||
        switch (chose) {
 | 
			
		||||
            case 0 -> System.exit(0);
 | 
			
		||||
            case 1 -> DeleteIndent();
 | 
			
		||||
            case 2 -> System.out.println("这个功能还在修复呢,小主先看看其它功能吧~");//UpdateIndent();
 | 
			
		||||
            case 3 -> SearchIndent(true);//查询全部
 | 
			
		||||
            case 4 -> SearchIndent(false);//按ID查询
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void CreateIndent(Long userID) {
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        Indent indent = new Indent();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入:下单餐厅id|备注");
 | 
			
		||||
        //用户
 | 
			
		||||
        userDAO user = new userDAO();
 | 
			
		||||
        indent.setUserID(user.searchID(userID));
 | 
			
		||||
        //餐厅
 | 
			
		||||
        Long merchantID = reader.nextLong();
 | 
			
		||||
        merchantsDAO merchant = new merchantsDAO();
 | 
			
		||||
        indent.setMerchantsID(merchant.searchID(merchantID));
 | 
			
		||||
        //备注
 | 
			
		||||
        indent.setMessage(reader.nextLine());
 | 
			
		||||
 | 
			
		||||
        indentAbstractDAO DML_insert = new indentDAO();
 | 
			
		||||
        int flag = DML_insert.insert(indent);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void DeleteIndent() {
 | 
			
		||||
        indentAbstractDAO DML_delete = new indentDAO();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        Indent indent = new Indent();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入你要删除的订单ID");
 | 
			
		||||
        indent.setId(reader.nextLong());
 | 
			
		||||
 | 
			
		||||
        int flag = DML_delete.delete(indent);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //TODO 更新订单
 | 
			
		||||
    private void UpdateIndent() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SearchIndent(boolean b) throws IllegalAccessException {
 | 
			
		||||
        Indent indent = new Indent();
 | 
			
		||||
        indentAbstractDAO DQL = new indentDAO();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        if (b) {//b 为 true 查询全部
 | 
			
		||||
            for (Indent item : DQL.search(indent)) {
 | 
			
		||||
                System.out.println(Toolset.table(Indent.class, item));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            System.out.println("请输入你要查询的ID:");
 | 
			
		||||
            indent = DQL.searchID(reader.nextLong());
 | 
			
		||||
            System.out.println(Toolset.table(Indent.class, indent));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										75
									
								
								JDBC/src/main/java/view/MerchantsView.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								JDBC/src/main/java/view/MerchantsView.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,75 @@
 | 
			
		||||
package view;
 | 
			
		||||
 | 
			
		||||
import dao.merchantsAbstractDAO;
 | 
			
		||||
import dao.specification.merchantsDAO;
 | 
			
		||||
import entities.Merchants;
 | 
			
		||||
import util.Toolset;
 | 
			
		||||
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
//商家管理
 | 
			
		||||
public class MerchantsView {
 | 
			
		||||
    //商家数据库操作
 | 
			
		||||
    protected void merchantsDB(int chose) throws IllegalAccessException {
 | 
			
		||||
        switch (chose) {
 | 
			
		||||
            case 0 -> System.exit(0);
 | 
			
		||||
            case 1 -> CreateMerchant();
 | 
			
		||||
            case 2 -> DeleteMerchant();
 | 
			
		||||
            case 3 -> System.out.println("这个功能还在修复呢,小主先看看其它功能吧~");//UpdateMerchant();
 | 
			
		||||
            case 4 -> SearchMerchant(true);//查询所有
 | 
			
		||||
            case 5 -> SearchMerchant(false);//查询ID
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void CreateMerchant() {//@TODO 输入格式化 不换行
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        merchantsAbstractDAO DML_insert = new merchantsDAO();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入:店铺名字|地址|描述|电话");
 | 
			
		||||
        Merchants merchants = new Merchants
 | 
			
		||||
                (reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());
 | 
			
		||||
 | 
			
		||||
        int flag = DML_insert.insert(merchants);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void DeleteMerchant() {
 | 
			
		||||
        Merchants merchants = new Merchants();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        merchantsAbstractDAO DML_delete = new merchantsDAO();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入你要删除的ID");
 | 
			
		||||
        merchants.setId(reader.nextLong());
 | 
			
		||||
 | 
			
		||||
        int flag = DML_delete.delete(merchants);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //@FIXME update没有传值
 | 
			
		||||
    private void UpdateMerchant() {
 | 
			
		||||
        Merchants merchants = new Merchants();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
 | 
			
		||||
        merchantsAbstractDAO DML_update = new merchantsDAO();
 | 
			
		||||
        int flag = DML_update.update(merchants);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SearchMerchant(boolean b) throws IllegalAccessException {
 | 
			
		||||
        Merchants merchants = new Merchants();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        merchantsAbstractDAO DQL = new merchantsDAO();
 | 
			
		||||
        if (b) { // b 为 ture 查询所有商家
 | 
			
		||||
            //格式化输出
 | 
			
		||||
            for (Merchants item : DQL.search(merchants)) {
 | 
			
		||||
                System.out.println(Toolset.table(Merchants.class, item));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            System.out.println("请输入你要查询的ID:");
 | 
			
		||||
            merchants = DQL.searchID(reader.nextLong());
 | 
			
		||||
            //格式化输出
 | 
			
		||||
            System.out.println(Toolset.table(Merchants.class, merchants));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								JDBC/src/main/java/view/UserView.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								JDBC/src/main/java/view/UserView.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
			
		||||
package view;
 | 
			
		||||
 | 
			
		||||
import dao.specification.userDAO;
 | 
			
		||||
import dao.userAbstractDAO;
 | 
			
		||||
import entities.Users;
 | 
			
		||||
import util.Toolset;
 | 
			
		||||
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
//TODO 用户密码查找和修改
 | 
			
		||||
public class UserView {
 | 
			
		||||
    //用户数据库操作
 | 
			
		||||
    protected void userDB(int chose) throws IllegalAccessException {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        switch (chose) {
 | 
			
		||||
            case 0 -> System.exit(0);
 | 
			
		||||
            case 1 -> CreateUser();
 | 
			
		||||
            case 2 -> DeleteUser();
 | 
			
		||||
            case 3 -> UpdateUser();
 | 
			
		||||
            case 4 -> SearchUser(true);//查询全部
 | 
			
		||||
            case 5 -> SearchUser(false);//查询ID
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void CreateUser() {//TODO 非换行输入
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        userAbstractDAO DML_insert = new userDAO();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入:名字|电话|地址|密码");
 | 
			
		||||
        Users user = new Users
 | 
			
		||||
                (reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());
 | 
			
		||||
 | 
			
		||||
        int flag = DML_insert.insert(user);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void DeleteUser() {
 | 
			
		||||
        Users user = new Users();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
 | 
			
		||||
        userAbstractDAO DML_delete = new userDAO();
 | 
			
		||||
 | 
			
		||||
        System.out.println("请输入你要删除的ID:");
 | 
			
		||||
        user.setId(reader.nextLong());
 | 
			
		||||
        int flag = DML_delete.delete(user);
 | 
			
		||||
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void UpdateUser() {//@FIXME update没有传值
 | 
			
		||||
        Users user = new Users();
 | 
			
		||||
        ;
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
 | 
			
		||||
        userAbstractDAO DML_update = new userDAO();
 | 
			
		||||
        int flag = DML_update.update(user);
 | 
			
		||||
        System.out.println(flag + "行受影响");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SearchUser(boolean b) throws IllegalAccessException {
 | 
			
		||||
        Users user = new Users();
 | 
			
		||||
        Scanner reader = new Scanner(System.in);
 | 
			
		||||
        userAbstractDAO DQL = new userDAO();
 | 
			
		||||
        if (b) {
 | 
			
		||||
            //格式化输出
 | 
			
		||||
            for (Users item : DQL.search(user)) {
 | 
			
		||||
                System.out.println(Toolset.table(Users.class, item));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            System.out.println("请输入你要查询的ID:");
 | 
			
		||||
            user = DQL.searchID(reader.nextLong());
 | 
			
		||||
            //格式化输出
 | 
			
		||||
            System.out.println(Toolset.table(Users.class, user));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								JDBC/src/main/java/view/shellUI.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								JDBC/src/main/java/view/shellUI.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
			
		||||
package view;
 | 
			
		||||
 | 
			
		||||
import util.Toolset;
 | 
			
		||||
 | 
			
		||||
public class shellUI {
 | 
			
		||||
    public static void run() throws IllegalAccessException {
 | 
			
		||||
 | 
			
		||||
        System.out.println("""
 | 
			
		||||
                ------------------------------------------
 | 
			
		||||
                -----------------后台管理系统---------------
 | 
			
		||||
                ------------------------------------------
 | 
			
		||||
                """);
 | 
			
		||||
        Chose();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //选项一
 | 
			
		||||
    private static void Chose() throws IllegalAccessException {
 | 
			
		||||
        System.out.println("""
 | 
			
		||||
                请选择你要进行的功能:
 | 
			
		||||
                1.商家管理\t2.用户管理\t3.订单管理
 | 
			
		||||
                4.我是用户,我要登录\t
 | 
			
		||||
                0.退出
 | 
			
		||||
                """);
 | 
			
		||||
 | 
			
		||||
        switch (Toolset.option()) {
 | 
			
		||||
            case 0 -> System.exit(0);
 | 
			
		||||
            //商家
 | 
			
		||||
            case 1 -> {
 | 
			
		||||
                System.out.println("""
 | 
			
		||||
                        请选择你想使用的功能:
 | 
			
		||||
                        1.新建商家\t\t2.删除商家\t\t3.更新信息
 | 
			
		||||
                        4.查询所有商家\t5.根据ID查询商家
 | 
			
		||||
                        0.退出
 | 
			
		||||
                        """);
 | 
			
		||||
                MerchantsView merchants_view = new MerchantsView();
 | 
			
		||||
                merchants_view.merchantsDB(Toolset.option());
 | 
			
		||||
                Chose();
 | 
			
		||||
            }
 | 
			
		||||
            //用户
 | 
			
		||||
            case 2 -> {
 | 
			
		||||
                System.out.println("""
 | 
			
		||||
                        请选择你想使用的功能:
 | 
			
		||||
                        1.新建用户\t\t2.删除用户\t\t3.更新信息
 | 
			
		||||
                        4.查询所有用户\t5.根据ID查询用户
 | 
			
		||||
                        0.退出
 | 
			
		||||
                        """);
 | 
			
		||||
                UserView user_view = new UserView();
 | 
			
		||||
                user_view.userDB(Toolset.option());
 | 
			
		||||
                Chose();
 | 
			
		||||
            }
 | 
			
		||||
            //订单
 | 
			
		||||
            case 3 -> {
 | 
			
		||||
                System.out.println("""
 | 
			
		||||
                        请选择你想使用的功能:
 | 
			
		||||
                        1.删除订单\t\t2.更新订单
 | 
			
		||||
                        3.查询所有订单\t4.根据ID查询订单
 | 
			
		||||
                        0.退出
 | 
			
		||||
                        """);
 | 
			
		||||
                IndentView indent_view = new IndentView();
 | 
			
		||||
                indent_view.IndentDB(Toolset.option());
 | 
			
		||||
                Chose();
 | 
			
		||||
            }
 | 
			
		||||
            //下单
 | 
			
		||||
            case 4 -> {
 | 
			
		||||
                System.out.println("""
 | 
			
		||||
                        请选择你想使用的功能:
 | 
			
		||||
                        1.点菜
 | 
			
		||||
                        0.退出
 | 
			
		||||
                        其它功能还在开发中呢,请耐心等待哦~
 | 
			
		||||
                        """);
 | 
			
		||||
                CommonUse user = new CommonUse();
 | 
			
		||||
                user.use(Toolset.option());
 | 
			
		||||
                Chose();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								JDBC/src/test/java/org/example/AppTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								JDBC/src/test/java/org/example/AppTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
package org.example;
 | 
			
		||||
 | 
			
		||||
import junit.framework.Test;
 | 
			
		||||
import junit.framework.TestCase;
 | 
			
		||||
import junit.framework.TestSuite;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Unit test for simple App.
 | 
			
		||||
 */
 | 
			
		||||
public class AppTest 
 | 
			
		||||
    extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Create the test case
 | 
			
		||||
     *
 | 
			
		||||
     * @param testName name of the test case
 | 
			
		||||
     */
 | 
			
		||||
    public AppTest( String testName )
 | 
			
		||||
    {
 | 
			
		||||
        super( testName );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return the suite of tests being tested
 | 
			
		||||
     */
 | 
			
		||||
    public static Test suite()
 | 
			
		||||
    {
 | 
			
		||||
        return new TestSuite( AppTest.class );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Rigourous Test :-)
 | 
			
		||||
     */
 | 
			
		||||
    public void testApp()
 | 
			
		||||
    {
 | 
			
		||||
        assertTrue( true );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user