This article shares the second chapter of the graduation design of Java Bookstore System for your reference. The specific content is as follows
1. User Management (user.txt)
Field names and order
Note: The type is int type, which is used to represent the type of the operator.
1 - Denoted as admin, all operations can be performed
2--Denoted as a person who can operate the book module
3 - Expressed as a person who can operate the purchase module
4 - Expressed as a person who can operate sales modules
5 - Expressed as a person who can operate inventory modules
Type uses enumeration implementation
package cn.hncu.bookStore.user.common;public enum UserTypeEnum { AdMIN(1,"Super Administrator"),BOOK(2,"Librarian"),IN(3,"Purchase Administrator"),OUT(4,"Sales Administrator"),STOCK(5,"Inventory Administrator"); private final int type; private final String name; UserTypeEnum(int type,String name){//Default private this.name =name; this.type=type; } public int getType() { return type; } public String getName() { return name; } public static int getTypeByName(String name){ for(UserTypeEnum utm:UserTypeEnum.values()){ if(utm.getName().equals(name.trim())){ return utm.getType(); } } throw new IllegalArgumentException("No user type corresponding to this /""+name+"/");//Illegal parameter exception} public static String getNameByType(int type){ for(UserTypeEnum utm:UserTypeEnum.values()){ if(utm.getType()==type){ return utm.getName(); } } throw new IllegalArgumentException("No user type corresponding to this /""+type+"/");//Illegal parameter exception}}package cn.hncu.bookStore.user.vo;import java.io.Serializable;import cn.hncu.bookStore.user.common.UserTypeEnum;/** * *@author<a href="mailto:[email protected]">xzm</a> */public class UserModel implements Serializable{ private static final long serialVersionUID = 1L; private String uuid,name,pwd;//user number, user name, user password private int type;//user type public UserModel() { } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserModel other = (UserModel) obj; if (uuid == null) { if (other.uuid != null) return false; } else if (!uuid.equals(other.uuid)) return false; return true; } @Override public String toString() { return uuid + "," + name +"," + UserTypeEnum.getNameByType(type); }}
package cn.hncu.bookStore.user.vo;public class UserQueryModel extends UserModel{ private static final long serialVersionUID = 1L;}Dao Layer:
package cn.hncu.bookStore.user.dao.dao;import java.util.List;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;public interface UserDAo { /** * Register a new user. If the user exists, it cannot be created * @param user * User to be created * @return * Return true if the creation is successful, otherwise it will return false */ public boolean create(UserModel user); /** * Delete a user. If the user does not exist, it will fail * @param uuid * The uuid of the user to be deleted * @return * Return true if the deletion is successful, otherwise false */ public boolean delete(String uuid); /** * Update user information. If the user does not exist, then it cannot be updated * @param user * User to be updated * If the update is successful, then false */ public boolean update(UserModel user); /** * Query the data of a user* @param uuid * User number of the information to be query* @return * If the user exists, return the user object of the specified uuid, otherwise return null */ public UserModel getSingle(String uuid); /** * According to the conditions of the query value object constraint, return all user objects that meet the user* @param user * Query value object* @return * If there is a user that meets the conditions of the query value object constraint, return the user object collection, otherwise return an empty set*/ public List<UserModel> getByCondition(UserQueryModel user); /** * Get all user objects in the file* @return * Return all user objects in the file*/ public List<UserModel> getAll();} package cn.hncu.bookStore.user.dao.impl;import java.util.ArrayList;import java.util.List;import cn.hncu.bookStore.user.dao.dao.UserDAo;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;import cn.hncu.bookStore.utils.FileIOUtil;public class UserDAOFileImpl implements UserDAo { private final static String FILE_NAME="a.txt"; @Override public boolean create(UserModel user) { if(user==null){//If the user information to be registered is null, it cannot be registered, and returns false return false; } List<UserModel> list=getAll();//Get all user objects that already exist in the file for(UserModel u:list){//Travel if(u.getUuid().equals(user.getUuid())){//If this user already exists, it cannot be registered return false; } } //After the above traversal, it means that user does not exist, you can register list.add(user); return FileIOUtil.writeToFile(list, FILE_NAME); } @Override public boolean delete(String uuid) { List<UserModel> list=getAll(); for(int i=0;i<list.size();i++){//Tranquility UserModel u=list.get(i); if(u.getUuid().equals(uuid)){ list.remove(i);//Delete return FileIOUtil.writeToFile(list, FILE_NAME); } } return false; } @Override public boolean update(UserModel user) { List<UserModel> list=getAll(); for(int i=0;i<list.size();i++){ UserModel u=list.get(i); if(u.getUuid().equals(user.getUuid())){ list.set(i, user);//Reset the user numbered to user.getUuid() return FileIOUtil.writeToFile(list, FILE_NAME); } } return false; } @Override public UserModel getSingle(String uuid) { List<UserModel> list=getAll(); for(UserModel u:list){ if(u.getUuid().equals(uuid)){ return u; } } return null; } @Override public List<UserModel> getByCondition(UserQueryModel user) { List<UserModel> list=getAll(); List<UserModel> reslist=new ArrayList<UserModel>(); for(UserModel u:list){ if(user.getUuid()!=null && user.getUuid().trim().length()>0){ if(!user.getUuid().trim().equals(u.getUuid())){ continue; } } if(user.getName()!=null && user.getName().trim().length()>0){ if(u.getName().indexOf(user.getName())==-1){ continue; } } if(user.getType()>0){ if(u.getType()!=user.getType()){ continue; } } reslist.add(u); } return reslist; } @Override public List<UserModel> getAll() { return FileIOUtil.readFromFile(FILE_NAME); }} package cn.hncu.bookStore.user.dao.factory;import cn.hncu.bookStore.user.dao.dao.UserDAo;import cn.hncu.bookStore.user.dao.impl.UserDAOFileImpl;public class UserDAOFactory { private UserDAOFactory(){ } public static UserDAo getUserDAo(){ return new UserDAOFileImpl(); }}Business logic layer:
package cn.hncu.bookStore.user.business.ebi;import java.util.List;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;public interface UserEbi { public boolean create(UserModel user); public boolean delete(String uuid); public boolean update(UserModel user); public UserModel getSingle(String uuid); public List<UserModel> getByCondition(UserQueryModel user); public List<UserModel> getAll(); public abstract List<UserModel> getAllIn(); public List<UserModel> getAllOut();} package cn.hncu.bookStore.user.business.ebo;import java.util.List;import cn.hncu.bookStore.common.uuidModelConstance;import cn.hncu.bookStore.common.uuid.dao.factory.uuidDAOFactory;import cn.hncu.bookStore.user.business.ebi.UserEbi;import cn.hncu.bookStore.user.common.UserTypeEnum;import cn.hncu.bookStore.user.dao.dao.UserDAo;import cn.hncu.bookStore.user.dao.factory.UserDAOFactory;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;public class UserEbo implements UserEbi { //Inject UserDAo dao = UserDAOFactory.getUserDAo(); @Override public boolean create(UserModel user) { String uuid=uuidDAOFactory.getUuidDAO().getNextNum(uuidModelConstance.User); user.setUuid(uuid); return dao.create(user); } @Override public boolean delete(String uuid) { return dao.delete(uuid); } @Override public boolean update(UserModel user) { return dao.update(user); } @Override public UserModel getSingle(String uuid) { return dao.getSingle(uuid); } @Override public List<UserModel> getByCondition(UserQueryModel user) { return dao.getByCondition(user); } @Override public List<UserModel> getAll() { return dao.getAll(); } @Override public List<UserModel> getAllIn() { UserQueryModel user=new UserQueryModel(); user.setType(UserTypeEnum.IN.getType()); return dao.getByCondition(user); } public List<UserModel> getAllOut() { UserQueryModel user=new UserQueryModel(); user.setType(UserTypeEnum.OUT.getType()); return dao.getByCondition(user); }} package cn.hncu.bookStore.user.business.factory;import cn.hncu.bookStore.user.business.ebi.UserEbi;import cn.hncu.bookStore.user.business.ebo.UserEbo;public class UserEbiFactory { private UserEbiFactory() { } public static UserEbi getUserEbi(){ return new UserEbo(); }} The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.