重构代码,DAO层全部完成

This commit is contained in:
twinkle255 2023-05-07 14:29:18 +08:00 committed by myh
parent 312c414c6b
commit b84218d0f9
18 changed files with 681 additions and 185 deletions

View File

@ -11,7 +11,8 @@ src -> main -> java -> example -> App.run()
### 待修复bug
- `userDAO`和`merchantsDAO`中`update`语句未传参
- 未测试非法数据
- 已测试NULL值
### 待完成功能
- `indentDAO`和`indentItemDAO`方法实现
- 感觉好像少了个菜品类,实体类好像还得改,服了
- 联合查询
- `updata`方法更新指定字段

7
log.md
View File

@ -1,6 +1,13 @@
# Homework
*网安实训作业*
## 2023-05-07-NO.1
- DAO层完成
- 重构代码
- DAO层方法整合
- switch case 模块方法提取重构
- NULL值检测和插入
- **我恨DDL**
## 2023-05-05-NO.2

View File

@ -1,7 +0,0 @@
package dao;
import entities.Indent;
public interface IndentAbstractDAO extends abstractDAO<Indent> {
}

View File

@ -0,0 +1,7 @@
package dao;
import entities.Indent;
public interface indentAbstractDAO extends abstractDAO<Indent> {
}

View File

@ -1,7 +1,7 @@
package dao;
import entities.indentItem;
import entities.IndentItem;
public interface indentItemAbstractDAO extends abstractDAO<indentItem> {
public interface indentItemAbstractDAO extends abstractDAO<IndentItem> {
}

View File

@ -1,5 +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(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(ct, ps, rs);
}
return flag;
}
}

View File

@ -1,34 +1,155 @@
package dao.specification;
import dao.IndentAbstractDAO;
import dao.indentAbstractDAO;
import entities.Indent;
import entities.Merchants;
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;
public class indentDAO implements IndentAbstractDAO {
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(Indent indent) {
return 0;
public int insert(@NotNull Indent indent) {
int flag;
//@FIXME 这个allPrice总价怎么用SQL跨表求和啊超
String insert_sql = "insert into Indent(userID, merchantsID, allPrice, message, createDate) " +
"values(?,?,?,?,getdate())";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(insert_sql);
preparedStatement.setObject(1, indent.getUserID());
preparedStatement.setObject(2, indent.getMerchantsID());
preparedStatement.setFloat(3, indent.getAllPrice());
preparedStatement.setString(4, indent.getMessage());
flag = preparedStatement.executeUpdate(insert_sql);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return flag;
}
@Override
public int delete(Indent indent) {
return 0;
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());
}
@Override
public int update(Indent indent) {
return 0;
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(update_sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return flag;
}
@Override
public List<Indent> search(Indent indent) {
return null;
List<Indent> list = new ArrayList<>();
String selectAll_sql = "select * from Indent";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(selectAll_sql);
resultSet = preparedStatement.executeQuery(selectAll_sql);
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) {
return null;
Indent indent = null;
String selectID_sql = "select * from Indent where id = ?";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(selectID_sql);
preparedStatement.setLong(1, id);
preparedStatement.executeQuery(selectID_sql);
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"));
indent.setUserID((Users) rs.getObject("userID"));
indent.setMerchantsID((Merchants) rs.getObject("merchantsID"));
indent.setAllPrice(rs.getFloat("allPrice"));
indent.setMessage(rs.getString("message"));
indent.setCreatedDate(rs.getDate("createDate"));
}
}

View File

@ -1,4 +1,137 @@
package dao.specification;
public class indentItemDAO {
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;
@Override
public int insert(@NotNull IndentItem IItem) {
int flag;
String insert_sql = "insert into indentItem(name, initialPrice, discount, " +
"finalPrice, description, indentID) values (?,?,?,?,?,null)";
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为合法值
|| !String.valueOf(IItem.getDiscount()).equals(" ")) {
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);
}
//描述
if (!IItem.getDescription().equals(" ")) {
preparedStatement.setString(5, IItem.getDescription());
} else {
preparedStatement.setNull(5, Types.NVARCHAR);
}
flag = preparedStatement.executeUpdate(insert_sql);
} 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 = ?";
// 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());
}
@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"));
}
}

View File

@ -1,6 +1,7 @@
package dao.specification;
import dao.merchantsAbstractDAO;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import entities.Merchants;
@ -8,6 +9,7 @@ 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;
@ -20,16 +22,27 @@ public class merchantsDAO implements merchantsAbstractDAO {
@Override
public int insert(@NotNull Merchants merchants) {
int flag;
try {
connection = getConnection();
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) {
@ -42,21 +55,23 @@ public class merchantsDAO implements merchantsAbstractDAO {
//删除商家
@Override
public int delete(Merchants merchants) {
int flag = 0;
try {
connection = getConnection();
public int delete(@NotNull Merchants merchants) {
String delete_sql = "delete from Merchants where id = ?";
preparedStatement = connection.prepareStatement(delete_sql);
preparedStatement.setLong(1, merchants.getId());
flag = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(connection, preparedStatement, resultSet);
}
return flag;
// 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());
}
//更新信息
@ -98,11 +113,7 @@ public class merchantsDAO implements merchantsAbstractDAO {
while (resultSet.next()) {
merchants = new Merchants();
merchants.setId(resultSet.getLong("id"));
merchants.setName(resultSet.getString("name"));
merchants.setPhoneNumber(resultSet.getString("phoneNumber"));
merchants.setAddress(resultSet.getString("address"));
merchants.setDescription(resultSet.getString("description"));
SetAttribute(merchants, resultSet);
list.add(merchants);
}
@ -128,11 +139,7 @@ public class merchantsDAO implements merchantsAbstractDAO {
while (resultSet.next()) {
merchants = new Merchants();
merchants.setId(resultSet.getLong("id"));
merchants.setName(resultSet.getString("name"));
merchants.setAddress(resultSet.getString("address"));
merchants.setDescription(resultSet.getString("description"));
merchants.setPhoneNumber(resultSet.getString("phoneNumber"));
SetAttribute(merchants, resultSet);
}
} catch (SQLException e) {
throw new RuntimeException(e);
@ -141,4 +148,13 @@ public class merchantsDAO implements merchantsAbstractDAO {
}
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"));
}
}

View File

@ -11,6 +11,7 @@ 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;
@ -45,22 +46,24 @@ public class userDAO implements userAbstractDAO {
//删除
@Override
public int delete(@NotNull Users users) {
int flag;
try {
connection = getConnection();
public int delete(@NotNull Users user) {
String delete_sql = "delete from Users where id=?";
preparedStatement = connection.prepareStatement(delete_sql);
preparedStatement.setLong(1, users.getId());
// 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);
// }
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return flag;
return Delete
(connection, preparedStatement, resultSet, delete_sql, user.getId());
}
//更新用户个人信息
@ -90,7 +93,7 @@ public class userDAO implements userAbstractDAO {
//查询所有用户
@Override
public List<Users> search(Users users) {
public List<Users> search(Users user) {
List<Users> list = new ArrayList<>();
String selectAll_sql = "select * from Users";
@ -101,13 +104,9 @@ public class userDAO implements userAbstractDAO {
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
users = new Users();
users.setId(resultSet.getLong("id"));
users.setName(resultSet.getString("name"));
users.setPhoneNumber(resultSet.getString("phoneNumber"));
users.setAddress(resultSet.getString("address"));
users.setPassword(resultSet.getString("password"));
list.add(users);
user = new Users();
SetAttribute(user, resultSet);
list.add(user);
}
} catch (SQLException e) {
throw new RuntimeException(e);
@ -130,10 +129,7 @@ public class userDAO implements userAbstractDAO {
while (resultSet.next()) {
user = new Users();
user.setId(resultSet.getLong("id"));
user.setName(resultSet.getString("name"));
user.setPhoneNumber(resultSet.getString("phoneNumber"));
user.setAddress(resultSet.getString("address"));
SetAttribute(user, resultSet);
}
} catch (SQLException e) {
@ -143,4 +139,11 @@ public class userDAO implements userAbstractDAO {
}
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"));
}
}

View File

@ -3,7 +3,7 @@ package entities;
import lombok.Data;
//订单中的菜品
@Data
public class indentItem {
public class IndentItem {
private Long id;//主键菜品ID
private String name;//菜品名字
private Float initialPrice;//菜品原价

View File

@ -12,15 +12,6 @@ public class Merchants {
private String description;//店铺描述
private String phoneNumber;//商家联系方式
// @Override
// public String toString() {
// return "\n商家编号" + this.id +
// "\n店铺名字" + this.name +
// "\n店铺地址" + this.address +
// "\n店铺描述" + this.description +
// "\n联系方式" + this.phoneNumber;
// }
public Merchants(String name, String address,
String description, String phoneNumber) {
this.name = name;

View File

@ -11,14 +11,6 @@ public class Users {
private String address;//家庭住址
private String password;//用户密码
// @Override
// public String toString() {
// return "\n用户编号" + this.id +
// "\n用户姓名" + this.name +
// "\n家庭地址" + this.address +
// "\n联系方式" + this.phoneNumber;
// }
public Users() {
}

View File

@ -0,0 +1,83 @@
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.Scanner;
public class IndentItemView {
public void IndentItemDB(int chose) throws IllegalAccessException {
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
}
}
private void CreateIItem() {
indentItemAbstractDAO DML_insert = new indentItemDAO();
Scanner reader = new Scanner(System.in);
IndentItem IItem = new IndentItem();
System.out.println("请输入:菜名|初始价格|折扣|描述|菜单ID");
//菜名
IItem.setName(reader.nextLine());
//初始价格
IItem.setInitialPrice(reader.nextFloat());
//折扣
IItem.setDiscount(reader.nextFloat());
//描述
IItem.setDescription(reader.nextLine());
//菜单ID
Long indentID = reader.nextLong();
indentDAO indent = new indentDAO();
IItem.setIndentID(indent.searchID(indentID));
int 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));
}
}
}

View File

@ -0,0 +1,82 @@
package view;
import dao.indentAbstractDAO;
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.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查询
}
}
private void CreateIndent() {
indentAbstractDAO DML_insert = new indentDAO();
Scanner reader = new Scanner(System.in);
Indent indent = new Indent();
System.out.println("请输入下单用户id|餐厅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());
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));
}
}
}

View File

@ -9,53 +9,65 @@ import java.util.Scanner;
public class MerchantsView {
//商家数据库操作
protected static void merchantsDB(int chose) throws IllegalAccessException {
int flag;
Merchants merchants;
Scanner reader = new Scanner(System.in);
protected void merchantsDB(int chose) throws IllegalAccessException {
switch (chose) {
case 0 -> System.exit(0);
case 1 -> {//@TODO 输入格式化 不换行
case 1 -> CreateMerchant();
case 2 -> DeleteMerchant();
case 3 -> 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 = new Merchants(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());
flag = DML_insert.insert(merchants);
Merchants merchants = new Merchants
(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());
int flag = DML_insert.insert(merchants);
System.out.println(flag + "行受影响");
}
case 2 -> {
private void DeleteMerchant() {
Merchants merchants = new Merchants();
Scanner reader = new Scanner(System.in);
merchantsAbstractDAO DML_delete = new merchantsDAO();
merchants = new Merchants();
System.out.println("请输入你要删除的ID");
merchants.setId(reader.nextLong());
flag = DML_delete.delete(merchants);
int flag = DML_delete.delete(merchants);
System.out.println(flag + "行受影响");
}
case 3 -> {//@FIXME update没有传值
merchantsAbstractDAO DML_update = new merchantsDAO();
merchants = new Merchants();
flag = DML_update.update(merchants);
System.out.println(flag + "行受影响");
}
case 4 -> {
merchantsAbstractDAO DQL_all = new merchantsDAO();
merchants = new Merchants();
//格式化输出
for (Merchants item : DQL_all.search(merchants)) {
System.out.println(Toolset.table(Merchants.class, item));
}
private void UpdateMerchant() {//@FIXME update没有传值
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 + "行受影响");
}
case 5 -> {
merchantsAbstractDAO DQL_ID = new merchantsDAO();
new Merchants();
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_ID.searchID(reader.nextLong());
merchants = DQL.searchID(reader.nextLong());
//格式化输出
System.out.println(Toolset.table(Merchants.class, merchants));
}
}
}
}

View File

@ -9,53 +9,68 @@ import java.util.Scanner;
public class UserView {
//用户数据库操作
protected static void userDB(int chose) throws IllegalAccessException {
int flag;
Users user;
Scanner reader = new Scanner(System.in);
protected void userDB(int chose) throws IllegalAccessException {
switch (chose) {
case 0 -> System.exit(0);
case 1 -> {//@TODO 非换行输入
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("请输入:名字|电话|地址|密码");
user = new Users(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());
flag = DML_insert.insert(user);
Users user = new Users
(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());
int flag = DML_insert.insert(user);
System.out.println(flag + "行受影响");
}
case 2 -> {
private void DeleteUser() {
Users user = new Users();
Scanner reader = new Scanner(System.in);
userAbstractDAO DML_delete = new userDAO();
user = new Users();
System.out.println("请输入你要删除的ID:");
user.setId(reader.nextLong());
flag = DML_delete.delete(user);
int flag = DML_delete.delete(user);
System.out.println(flag + "行受影响");
}
case 3 -> {//@FIXME update没有传值
private void UpdateUser() {//@FIXME update没有传值
Users user = new Users();
;
Scanner reader = new Scanner(System.in);
userAbstractDAO DML_update = new userDAO();
user = new Users();
flag = DML_update.update(user);
int flag = DML_update.update(user);
System.out.println(flag + "行受影响");
}
case 4 -> {
userAbstractDAO DQL_all = new userDAO();
user = new Users();
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_all.search(user)) {
for (Users item : DQL.search(user)) {
System.out.println(Toolset.table(Users.class, item));
}
}
case 5 -> {
userAbstractDAO DQL_ID = new userDAO();
new Users();
} else {
System.out.println("请输入你要查询的ID:");
user = DQL_ID.searchID(reader.nextLong());
user = DQL.searchID(reader.nextLong());
//格式化输出
System.out.println(Toolset.table(Users.class, user));
}
}
}
}

View File

@ -2,9 +2,6 @@ package view;
import util.Toolset;
import static view.MerchantsView.merchantsDB;
import static view.UserView.userDB;
public class shellUI {
public static void run() throws IllegalAccessException {
@ -13,6 +10,7 @@ public class shellUI {
-----------------后台管理系统---------------
------------------------------------------
""");
Chose();
}
@ -20,7 +18,7 @@ public class shellUI {
private static void Chose() throws IllegalAccessException {
System.out.println("""
请选择你要进行的功能:
1.商家管理\t2.用户管理\t0.退出
1.商家管理\t2.用户管理\t3.订单管理\t0.退出
""");
switch (Toolset.option()) {
@ -32,7 +30,8 @@ public class shellUI {
1.新建商家\t\t2.删除商家\t\t3.更新信息
4.查询所有商家\t5.根据ID查询商家\t0.退出
""");
merchantsDB(Toolset.option());
MerchantsView merchants_view = new MerchantsView();
merchants_view.merchantsDB(Toolset.option());
Chose();
}
//用户
@ -42,10 +41,28 @@ public class shellUI {
1.新建用户\t\t2.删除用户\t\t3.更新信息
4.查询所有用户\t5.根据ID查询用户\t0.退出
""");
userDB(Toolset.option());
UserView user_view = new UserView();
user_view.userDB(Toolset.option());
Chose();
}
//订单
case 3 -> {
System.out.println("""
请选择你想使用的功能:
1.新建订单\t\t2.删除订单\t\t3.更新订单
4.查询所有订单\t5.根据ID查询订单\t0.退出
""");
IndentView indent_view = new IndentView();
indent_view.IndentDB(Toolset.option());
Chose();
}
//订单菜品
case 4 -> {
}
}
}
}