测试1

package model.service;

import model.pojo.User;
import util.IPagination;
import dao.DAOFactory;
import dao.IUserDAO;

/**
 * 对用户的相关操作
 * 单例实现
 * @author dds
 * @version 1.0
 *
 */
public class UserService {
	
/*	public static synchronized UserService getInstance() {
		if (instance == null) {
			instance = new UserService();
		}
		return instance;
	}*/
	
	//用户的DAO,通过DAO工厂获得
	private static IUserDAO dao = DAOFactory.getInstance().getUserManagerDao();
	
	//用户服务类的实例
	private static UserService instance = new UserService();
	
	private UserService() {		
	}
	
	/**
	 * 获得用户服务类的实例
	 * @return
	 */
	public static UserService getInstance() {
		return instance;
	}
	
	
	/**
	 * 检查用户是否已经存在
	 * @param userName
	 * @return	true 用户已存在
	 */
	public boolean checkUserNameExists(String userName){
		if(getUser(userName) != null){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 增加用户
	 * @param user 用户对象
	 * @throws 用户已存在
	 */
	public void addUser(User user)throws UserAlreadyExistsException {
		
		//如果用户已存在,则抛出异常
		if(checkUserNameExists(user.getUserName()) == true){
			throw new UserAlreadyExistsException();		
		}else{
			dao.addUser(user);
		}		
	}
	
	/**
	 * 根据用户名获得用户对象
	 * @param name 用户名
	 * @return User对象
	 */
	public User getUser(String name){
		return dao.getUser(name);
	}
	
	/**
	 * 根据用户ID获得用户
	 * @param userId 用户代码
	 * @return user对象
	 */
	public User getUser(int userId) {
		return dao.getUser(userId); 
	}
	
	/**
	 * 分页查询用户
	 * @param pageNo  第几页
	 * @param pageSize 每页显示多少条
	 * 
	 * @return IPaination的实现类对象,封装了返回的对象集合
	 */
	public  IPagination getAllUser(int pageNo, int pageSize) {
		return null;
	} 
	
	/**
	 * 根据用户ID删除用户
	 * @param userId 用户ID
	 */
	public void delUserById(String userId) {
	}
	
	/**
	 * 根据用户名删除用户
	 * @param userNmae
	 */
	public void delUserByName(String userNmae){
		
	}
	
	/**
	 * 更新用户
	 * @param user user对象
	 */
	public void updateUser(User user) {
		
	}
	
	/**
	 * 用户登录
	 * @param userName 用户名
	 * @param password 密码
	 * @return 登陆成功的User对象
	 * @throws UserNotFoundException 用户未找到异常
	 * @throws PasswordErrorException 密码错误异常
	 */
	public User login(String userName, String password) 
		throws UserNotFoundException, PasswordErrorException{
		User user = getUser(userName);
		if (user == null)
			throw new UserNotFoundException();
		if (!user.getPassword().equals(password))
			throw new PasswordErrorException();
		return user;
	}
	
	/**
	 * 修改密码
	 * @param userId 用户代码
	 * @param password 密码
	 */
	public void modifyPassword(String userId, String password) {
		
	}
	
	
}



package model.service;

import model.pojo.User;
import util.IPagination;
import dao.DAOFactory;
import dao.IUserDAO;

/**
 * 对用户的相关操作
 * 单例实现
 * @author dds
 * @version 1.0
 *
 */
public class UserService {
	
/*	public static synchronized UserService getInstance() {
		if (instance == null) {
			instance = new UserService();
		}
		return instance;
	}*/
	
	//用户的DAO,通过DAO工厂获得
	private static IUserDAO dao = DAOFactory.getInstance().getUserManagerDao();
	
	//用户服务类的实例
	private static UserService instance = new UserService();
	
	private UserService() {		
	}
	
	/**
	 * 获得用户服务类的实例
	 * @return
	 */
	public static UserService getInstance() {
		return instance;
	}
	
	
	/**
	 * 检查用户是否已经存在
	 * @param userName
	 * @return	true 用户已存在
	 */
	public boolean checkUserNameExists(String userName){
		if(getUser(userName) != null){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 增加用户
	 * @param user 用户对象
	 * @throws 用户已存在
	 */
	public void addUser(User user)throws UserAlreadyExistsException {
		
		//如果用户已存在,则抛出异常
		if(checkUserNameExists(user.getUserName()) == true){
			throw new UserAlreadyExistsException();		
		}else{
			dao.addUser(user);
		}		
	}
	
	/**
	 * 根据用户名获得用户对象
	 * @param name 用户名
	 * @return User对象
	 */
	public User getUser(String name){
		return dao.getUser(name);
	}
	
	/**
	 * 根据用户ID获得用户
	 * @param userId 用户代码
	 * @return user对象
	 */
	public User getUser(int userId) {
		return dao.getUser(userId); 
	}
	
	/**
	 * 分页查询用户
	 * @param pageNo  第几页
	 * @param pageSize 每页显示多少条
	 * 
	 * @return IPaination的实现类对象,封装了返回的对象集合
	 */
	public  IPagination getAllUser(int pageNo, int pageSize) {
		return null;
	} 
	
	/**
	 * 根据用户ID删除用户
	 * @param userId 用户ID
	 */
	public void delUserById(String userId) {
	}
	
	/**
	 * 根据用户名删除用户
	 * @param userNmae
	 */
	public void delUserByName(String userNmae){
		
	}
	
	/**
	 * 更新用户
	 * @param user user对象
	 */
	public void updateUser(User user) {
		
	}
	
	/**
	 * 用户登录
	 * @param userName 用户名
	 * @param password 密码
	 * @return 登陆成功的User对象
	 * @throws UserNotFoundException 用户未找到异常
	 * @throws PasswordErrorException 密码错误异常
	 */
	public User login(String userName, String password) 
		throws UserNotFoundException, PasswordErrorException{
		User user = getUser(userName);
		if (user == null)
			throw new UserNotFoundException();
		if (!user.getPassword().equals(password))
			throw new PasswordErrorException();
		return user;
	}
	
	/**
	 * 修改密码
	 * @param userId 用户代码
	 * @param password 密码
	 */
	public void modifyPassword(String userId, String password) {
		
	}
	
	
}



 

 

你可能感兴趣的:(DAO)