首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 設計模塊實戰:書店管理系統單機版(二)用戶模塊完整代碼

     編輯:Java 相關知識

本篇內容

展示用戶模塊的所有代碼:公共枚舉、值對象、DAO 接口與實現、邏輯層接口與實現、工廠類、Swing 界面面板、主程序啟動類。

核心代碼彙總

1. 公共枚舉 UserTypeEnum

public enum UserTypeEnum {
    ADMIN(1, "超級管理員"), BOOK(2, "圖書管理員"),
    IN(3, "進貨管理員"), OUT(4, "銷售管理員"), STOCK(5, "庫存管理員");
    private final int type; private final String name;
    UserTypeEnum(int type, String name) { this.type = type; this.name = name; }
    public int getType() { return type; }
    public String getName() { return name; }
    public static String getNameByType(int type) { ... }
    public static int getTypeByName(String name) { ... }
}

2. 值對象 UserModel / UserQueryModel

public class UserModel implements Serializable {
    private String uuid, name, pwd; private int type;
    // getter/setter、equals/hashCode(uuid)、toString(含類型中文名)
}
public class UserQueryModel extends UserModel { }

3. DAO 層

// 接口
public interface UserDao {
    boolean create(UserModel user);
    boolean delete(String uuid);
    boolean update(UserModel user);
    List<UserModel> getAll();
    List<UserModel> getByCondition(UserQueryModel uqm);
    UserModel getSingle(String uuid);
}

// 實現(基於文件序列化)
public class UserDaoSerImpl implements UserDao {
    private static final String FILE = "User.txt";
    // 所有方法調用 FileIoUtil 讀寫 List,按 uuid 判重、增刪改查
}

// 工廠
public class UserDaoFactory {
    public static UserDao getUserDao() { return new UserDaoSerImpl(); }
}

4. 邏輯層

public interface UserEbi { /* 同 Dao 方法 */ }
public class UserEbo implements UserEbi {
    private UserDao dao = UserDaoFactory.getUserDao();
    // 直接委派 dao
}
public class UserEbiFactory {
    public static UserEbi getUserEbi() { return new UserEbo(); }
}

5. 界面層

  • ListPanel:JList 展示用戶,按鈕跳轉新增。
  • AddPanel:輸入 uuid、姓名、密碼、確認密碼、下拉選類型,校驗後調用邏輯層新增。

6. 主程序 BookStore

public class BookStore extends JFrame {
    public BookStore() {
        initComponents();
        setContentPane(new ListPanel(this));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new BookStore().setVisible(true));
    }
}

已知問題與優化方向

  • 新增時未校驗必填字段,可能存入空值。
  • UUID 由用戶手輸,後續改為系統生成(UUID.randomUUID())。
  • 查詢功能 getByCondition 尚未實現。
  • 密碼明文存儲,生產需加密(BCrypt)。

系列計劃

後續將完成圖書、進貨、銷售、庫存四大模塊,敬請期待。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved