完善DAO层,重构代码,添加了登录功能,修改了一堆bug,具体见日志

This commit is contained in:
twinkle255 2023-05-07 21:07:58 +08:00 committed by myh
parent b84218d0f9
commit ab306eff80
16 changed files with 256 additions and 140 deletions

View File

@ -1,7 +1,6 @@
# Homework
- - -
**Last Update Time : 2023-05-05**
**Last Update Time : 2023-05-07 21:06**
日志文件:`log.md`
- - -
### 运行方式:
@ -12,7 +11,13 @@ src -> main -> java -> example -> App.run()
- `userDAO`和`merchantsDAO`中`update`语句未传参
- 未测试非法数据
- 已测试NULL值
- 已测试插入数据库时,空白符和换行符的检测
### 待完成功能
- 感觉好像少了个菜品类,实体类好像还得改,服了
- 联合查询
- 是的,我发现了,这个实体关系是真的乱,现在竟然用户点菜的时候需要自己设置折扣,太离谱了,以后再改叭。。。
- 再加个菜品类上面这个问题估计就解决了
- 订单和菜品DAO层的update功能
- `updata`方法更新指定字段
- 用户密码的查询和修改
> 暂时就这样了,力不从心了,以后再改吧呜呜呜~

15
log.md
View File

@ -1,7 +1,22 @@
# Homework
*网安实训作业*
## 2023-05-08-NO.2
**重大更新!**
- 修改了超级无敌多的bug
- 新建订单无法点菜bug
- 点菜模块无法自动计算总价bug
- 还有其它一些小bug不多说了
- 增加了一部分非法值检测
- 增加了用户登录功能
- DAO层操作逻辑修改主要是新建订单部分
- 新加用户操作模块
## 2023-05-07-NO.1
- DAO层完成
- 重构代码
- DAO层方法整合

View File

@ -17,7 +17,7 @@ public class abstractDAO<T> {
ct = getConnection();
ps = ct.prepareStatement(sql);
ps.setLong(1, id);
flag = ps.executeUpdate(sql);
flag = ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {

View File

@ -2,16 +2,13 @@ package dao.specification;
import dao.indentAbstractDAO;
import entities.Indent;
import entities.Merchants;
import entities.Users;
import org.jetbrains.annotations.NotNull;
import view.IndentItemView;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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;
@ -26,19 +23,45 @@ public class indentDAO implements indentAbstractDAO {
@Override
public int insert(@NotNull Indent indent) {
int flag;
//@FIXME 这个allPrice总价怎么用SQL跨表求和啊超
String insert_sql = "insert into Indent(userID, merchantsID, allPrice, message, createDate) " +
"values(?,?,?,?,getdate())";
//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);
}
preparedStatement.setObject(1, indent.getUserID());
preparedStatement.setObject(2, indent.getMerchantsID());
preparedStatement.setFloat(3, indent.getAllPrice());
preparedStatement.setString(4, indent.getMessage());
flag = preparedStatement.executeUpdate();
flag = preparedStatement.executeUpdate(insert_sql);
//创建订单中的菜品
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);
@ -52,20 +75,6 @@ public class indentDAO implements indentAbstractDAO {
@Override
public int delete(@NotNull Indent indent) {
String delete_sql = "delete from Indent where id = ?";
// int flag;
// try {
// connection = getConnection();
// preparedStatement = connection.prepareStatement(delete_sql);
//
// preparedStatement.setLong(1, indent.getId());
//
// flag = preparedStatement.executeUpdate(delete_sql);
//
// } catch (SQLException e) {
// throw new RuntimeException(e);
// } finally {
// close(connection, preparedStatement, resultSet);
// }
return Delete
(connection, preparedStatement, resultSet, delete_sql, indent.getId());
}
@ -86,7 +95,7 @@ public class indentDAO implements indentAbstractDAO {
preparedStatement.setString(4, indent.getMessage());
preparedStatement.setLong(5, indent.getId());
flag = preparedStatement.executeUpdate(update_sql);
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
@ -102,8 +111,7 @@ public class indentDAO implements indentAbstractDAO {
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(selectAll_sql);
resultSet = preparedStatement.executeQuery(selectAll_sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
indent = new Indent();
@ -129,7 +137,7 @@ public class indentDAO implements indentAbstractDAO {
connection = getConnection();
preparedStatement = connection.prepareStatement(selectID_sql);
preparedStatement.setLong(1, id);
preparedStatement.executeQuery(selectID_sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
indent = new Indent();
@ -146,8 +154,13 @@ public class indentDAO implements indentAbstractDAO {
private void SetAttribute(@NotNull Indent indent, @NotNull ResultSet rs) throws SQLException {
indent.setId(rs.getLong("id"));
indent.setUserID((Users) rs.getObject("userID"));
indent.setMerchantsID((Merchants) rs.getObject("merchantsID"));
//结果集是 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"));

View File

@ -18,11 +18,12 @@ public class indentItemDAO implements indentItemAbstractDAO {
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 (?,?,?,?,?,null)";
"finalPrice, description, indentID) values (?,?,?,?,?,?)";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(insert_sql);
@ -30,23 +31,25 @@ public class indentItemDAO implements indentItemAbstractDAO {
preparedStatement.setString(1, IItem.getName());
preparedStatement.setFloat(2, IItem.getInitialPrice());
//折扣和折后价null值的判断
if (IItem.getDiscount().compareTo(0F) > 0 //折扣和0比较,返回1为合法值
|| !String.valueOf(IItem.getDiscount()).equals(" ")) {
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.setNull(4, Types.FLOAT);
//最终价格等于初始价格
preparedStatement.setFloat(4, IItem.getInitialPrice());
}
//描述
if (!IItem.getDescription().equals(" ")) {
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(insert_sql);
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
@ -55,23 +58,10 @@ public class indentItemDAO implements indentItemAbstractDAO {
return flag;
}
@Override
public int delete(@NotNull IndentItem IItem) {
String delete_sql = "delete from indentItem where id = ?";
// int flag;
// try {
// connection = getConnection();
// preparedStatement = connection.prepareStatement(delete_sql);
// preparedStatement.setLong(1, item.getId());
// flag = preparedStatement.executeUpdate(delete_sql);
//
// } catch (SQLException e) {
// throw new RuntimeException(e);
// } finally {
// close(connection,preparedStatement,resultSet);
// }
//
// return flag;
return Delete
(connection, preparedStatement, resultSet, delete_sql, IItem.getId());
}

View File

@ -57,19 +57,6 @@ public class merchantsDAO implements merchantsAbstractDAO {
@Override
public int delete(@NotNull Merchants merchants) {
String delete_sql = "delete from Merchants where id = ?";
// int flag = 0;
// try {
// connection = getConnection();
//
// preparedStatement = connection.prepareStatement(delete_sql);
// preparedStatement.setLong(1, merchants.getId());
//
// flag = preparedStatement.executeUpdate();
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// close(connection, preparedStatement, resultSet);
// }
return Delete
(connection, preparedStatement, resultSet, delete_sql, merchants.getId());
}
@ -92,7 +79,7 @@ public class merchantsDAO implements merchantsAbstractDAO {
preparedStatement.setString(4, merchants.getPhoneNumber());
preparedStatement.setLong(5, merchants.getId());
flag = preparedStatement.executeUpdate(update_sql);
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}

View File

@ -48,19 +48,6 @@ public class userDAO implements userAbstractDAO {
@Override
public int delete(@NotNull Users user) {
String delete_sql = "delete from Users where id=?";
// int flag;
// try {
// connection = getConnection();
//
// preparedStatement = connection.prepareStatement(delete_sql);
// preparedStatement.setLong(1, users.getId());
//
// flag = preparedStatement.executeUpdate();
// } catch (SQLException e) {
// throw new RuntimeException(e);
// } finally {
// close(connection, preparedStatement, resultSet);
// }
return Delete
(connection, preparedStatement, resultSet, delete_sql, user.getId());

View File

@ -11,7 +11,7 @@ public class Indent {
private Float allPrice;//菜品总价
Date createdDate;//订单创建时间
String message;//下单备注
private String message;//下单备注
private Users userID;//外键下单用户ID
private Merchants merchantsID;//外键餐厅ID

View File

@ -0,0 +1,48 @@
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 = ?";
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.setLong(1, id);
rs = ps.executeQuery();
if (!rs.next()) {
//TODO ID输入不正确后重新输入但是这个函数调用的太乱了有点偷懒了有机会改一下
System.out.println("查无此人");
CreateIndent();//跳转到登录选项重新输入ID
}
Scanner reader = new Scanner(System.in);
System.out.println("请输入登录密码:");
String password = reader.nextLine();
while (rs.next()) {
flag = password.equals(rs.getString("password"));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
close(conn, ps, rs);
}
return flag;
}
}

View File

@ -94,5 +94,4 @@ public class Toolset {
return fields;
}
}

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

View File

@ -7,6 +7,7 @@ import entities.IndentItem;
import util.Toolset;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;
public class IndentItemView {
@ -14,34 +15,41 @@ public class IndentItemView {
switch (chose) {
case 0 -> System.exit(0);
case 1 -> CreateIItem();
case 2 -> DeleteIItem();
case 3 -> UpdateIItem();
case 4 -> SearchIItem(true);//查询全部
case 5 -> SearchIItem(false);//查询ID
case 1 -> DeleteIItem();
case 2 -> UpdateIItem();
case 3 -> SearchIItem(true);//查询全部
case 4 -> SearchIItem(false);//查询ID
}
}
private void CreateIItem() {
public void CreateIItem(Long indentID) {
indentItemAbstractDAO DML_insert = new indentItemDAO();
Scanner reader = new Scanner(System.in);
IndentItem IItem = new IndentItem();
System.out.println("请输入:菜名|初始价格|折扣|描述 退出请输入end");
System.out.println("请输入:菜名|初始价格|折扣|描述|菜单ID");
int flag = 0;
while (!reader.hasNext("end")) {
IndentItem IItem = new IndentItem();
//菜名
IItem.setName(reader.nextLine());
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
Long indentID = reader.nextLong();
indentDAO indent = new indentDAO();
IItem.setIndentID(indent.searchID(indentID));
int flag = DML_insert.insert(IItem);
flag += DML_insert.insert(IItem);
}
System.out.println(flag + "行受影响");
}

View File

@ -5,45 +5,40 @@ import dao.specification.indentDAO;
import dao.specification.merchantsDAO;
import dao.specification.userDAO;
import entities.Indent;
import entities.IndentItem;
import entities.Users;
import util.Toolset;
import java.util.List;
import java.util.Scanner;
public class IndentView {
public void IndentDB(int chose) throws IllegalAccessException {
//TODO 这个交互有点难搞
switch (chose) {
case 0 -> System.exit(0);
case 1 -> CreateIndent();
case 2 -> DeleteIndent();
case 3 -> UpdateIndent();
case 4 -> SearchIndent(true);//查询全部
case 5 -> SearchIndent(false);//按ID查询
case 1 -> DeleteIndent();
case 2 -> System.out.println("这个功能还在修复呢,小主先看看其它功能吧~");//UpdateIndent();
case 3 -> SearchIndent(true);//查询全部
case 4 -> SearchIndent(false);//按ID查询
}
}
private void CreateIndent() {
indentAbstractDAO DML_insert = new indentDAO();
public void CreateIndent(Long userID) {
Scanner reader = new Scanner(System.in);
Indent indent = new Indent();
System.out.println("请输入下单用户id|餐厅id|总价|备注");
System.out.println("请输入下单餐厅id|备注");
//用户
Long userID = reader.nextLong();
userDAO user = new userDAO();
indent.setUserID(user.searchID(userID));
//餐厅
Long merchantID = reader.nextLong();
merchantsDAO merchant = new merchantsDAO();
indent.setMerchantsID(merchant.searchID(merchantID));
//总价
indent.setAllPrice(reader.nextFloat());
//备注
indent.setMessage(reader.nextLine());
indentAbstractDAO DML_insert = new indentDAO();
int flag = DML_insert.insert(indent);
System.out.println(flag + "行受影响");
}
@ -53,7 +48,7 @@ public class IndentView {
Scanner reader = new Scanner(System.in);
Indent indent = new Indent();
System.out.println("请输入你要删除的ID");
System.out.println("请输入你要删除的订单ID");
indent.setId(reader.nextLong());
int flag = DML_delete.delete(indent);
@ -79,4 +74,5 @@ public class IndentView {
System.out.println(Toolset.table(Indent.class, indent));
}
}
}

View File

@ -7,6 +7,7 @@ import util.Toolset;
import java.util.Scanner;
//商家管理
public class MerchantsView {
//商家数据库操作
protected void merchantsDB(int chose) throws IllegalAccessException {
@ -14,7 +15,7 @@ public class MerchantsView {
case 0 -> System.exit(0);
case 1 -> CreateMerchant();
case 2 -> DeleteMerchant();
case 3 -> UpdateMerchant();
case 3 -> System.out.println("这个功能还在修复呢,小主先看看其它功能吧~");//UpdateMerchant();
case 4 -> SearchMerchant(true);//查询所有
case 5 -> SearchMerchant(false);//查询ID
}
@ -44,7 +45,8 @@ public class MerchantsView {
System.out.println(flag + "行受影响");
}
private void UpdateMerchant() {//@FIXME update没有传值
//@FIXME update没有传值
private void UpdateMerchant() {
Merchants merchants = new Merchants();
Scanner reader = new Scanner(System.in);

View File

@ -7,6 +7,7 @@ import util.Toolset;
import java.util.Scanner;
//TODO 用户密码查找和修改
public class UserView {
//用户数据库操作
protected void userDB(int chose) throws IllegalAccessException {
@ -22,7 +23,7 @@ public class UserView {
}
}
private void CreateUser() {//@TODO 非换行输入
private void CreateUser() {//TODO 非换行输入
Scanner reader = new Scanner(System.in);
userAbstractDAO DML_insert = new userDAO();

View File

@ -10,7 +10,6 @@ public class shellUI {
-----------------后台管理系统---------------
------------------------------------------
""");
Chose();
}
@ -18,7 +17,9 @@ public class shellUI {
private static void Chose() throws IllegalAccessException {
System.out.println("""
请选择你要进行的功能:
1.商家管理\t2.用户管理\t3.订单管理\t0.退出
1.商家管理\t2.用户管理\t3.订单管理
4.我是用户我要登录\t
0.退出
""");
switch (Toolset.option()) {
@ -28,7 +29,8 @@ public class shellUI {
System.out.println("""
请选择你想使用的功能:
1.新建商家\t\t2.删除商家\t\t3.更新信息
4.查询所有商家\t5.根据ID查询商家\t0.退出
4.查询所有商家\t5.根据ID查询商家
0.退出
""");
MerchantsView merchants_view = new MerchantsView();
merchants_view.merchantsDB(Toolset.option());
@ -39,7 +41,8 @@ public class shellUI {
System.out.println("""
请选择你想使用的功能:
1.新建用户\t\t2.删除用户\t\t3.更新信息
4.查询所有用户\t5.根据ID查询用户\t0.退出
4.查询所有用户\t5.根据ID查询用户
0.退出
""");
UserView user_view = new UserView();
user_view.userDB(Toolset.option());
@ -49,20 +52,26 @@ public class shellUI {
case 3 -> {
System.out.println("""
请选择你想使用的功能:
1.新建订单\t\t2.删除订单\t\t3.更新订单
4.查询所有订单\t5.根据ID查询订单\t0.退出
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();
}
}
}
}