展示用戶模塊的所有代碼:公共枚舉、值對象、DAO 接口與實現、邏輯層接口與實現、工廠類、Swing 界面面板、主程序啟動類。
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) { ... }
}
public class UserModel implements Serializable {
private String uuid, name, pwd; private int type;
// getter/setter、equals/hashCode(uuid)、toString(含類型中文名)
}
public class UserQueryModel extends UserModel { }
// 接口
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(); }
}
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(); }
}
ListPanel:JList 展示用戶,按鈕跳轉新增。AddPanel:輸入 uuid、姓名、密碼、確認密碼、下拉選類型,校驗後調用邏輯層新增。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));
}
}
getByCondition 尚未實現。後續將完成圖書、進貨、銷售、庫存四大模塊,敬請期待。