企业员工管理系统 四:服务层

参考资料:

项目介绍
工具类
实体类
服务层
控制层
UI层

service

接口

DeptService.java

public interface DeptService {

	/**
	 *  将Dept所对应的JSON字符串baocun到相应的文件中
	 * @param dept
	 * @return 返回true表示更新,false表示添加
	 * @throws IOException
	 */
	public boolean addDept(Dept dept)throws IOException;
	
	/**
	 *   按照 deptno更新数据:参数dept的deptno如果在文件中存在就更新,否则加入
	 * @param dept
	 * @return
	 * @throws IOException
	 */
	public boolean updateDept(Dept dept) throws IOException;
	
	/**
	 *   删除参数deptno制定的部门
	 * @param deptno
	 * @return 存在并且删除成功返回;true,不存在返回false
	 * @throws IOException
	 */
	public boolean deleteDeptByDeptno(Integer deptno) throws IOException;//参数使用引用数据类型
	
	public Dept getDeptByDeptno(Integer deptno) throws IOException;
	
	public Dept getDeptByDname(String dname) throws IOException;
	
	public List<Dept> getAllDept() throws IOException;
	
}

EmpService.java

public interface EmpService {

	public boolean addEmp(Emp emp) throws IOException;

	public boolean updateEmp(Emp emp) throws IOException;

	public boolean deleteEmpByEmpno(Integer empno) throws IOException;// 参数使用引用数据类型

	public Emp getEmpByEmpno(Integer empno) throws IOException;

	public Emp getEmpByEname(String ename) throws IOException;

	public List<Emp> getEmpByDeptno(Integer deptno) throws IOException;

	public List<Emp> getAllEmp() throws IOException;
	
}

ManagerService.java

public interface ManagerService {

	public boolean login(String name,String password);
	
}

实现类

DeptServiceImpl.java

public class DeptServiceImpl implements DeptService {

	File file = new File(GlobalConst.DB_NAME + "/" + GlobalConst.DEPT_TABLE_NAME); //Dept数据所保存到的文件

	@Override
	public boolean addDept(Dept dept)  {
		try {
			FileWriter writer = new FileWriter(file, true); // true追加, false覆盖
			BufferedWriter br = new BufferedWriter(writer);
			br.write(JSON.toJSONString(dept) + "\n"); /将Dept所对应的JSON数据保存到文件中
			if (br != null) {
				br.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false; // 失败返回false
		} 
		return true; // 成功返回true
	}

	@Override
	public boolean updateDept(Dept dept) throws IOException {//存在更新,否则保存
		List<Dept> allDept = getAllDept(); //获取文件中保存的所有的部门信息
		boolean flag = false; // false表示数据不存在
		for(int i =0;i<allDept.size();i++) {
			Dept item = allDept.get(i);
			if(dept.getDeptno().intValue() == item.getDeptno().intValue() ) { //根据部门编号进行比较
				allDept.set(i, dept); //更新数据
				flag = true; //表示数据存在 
				break;
			}
		}
		if(!flag) {//不存在,添加
			allDept.add(dept);
		}
		writeDepts(allDept); //将数据集保存到文件中
		return flag;  //更新返回true;添加返回false
	}

    /**
     *将参数指定的数据集保存到文件中,每一个Dept对象所对应的JSON数据在文件中占一行
     */
	private void writeDepts(List<Dept> allDept) throws IOException {  
		FileWriter writer = new FileWriter(file); // 覆盖
		BufferedWriter br = new BufferedWriter(writer);
		for(Dept item : allDept) {
			br.write(JSON.toJSONString(item) + "\n"); // \n表示换行
		}
		if (br != null) {
			br.close();
		}
	}
	@Override
	public boolean deleteDeptByDeptno(Integer deptno) throws IOException {
		List<Dept> allDept = getAllDept();//获取文件中保存的所有的部门信息
		boolean flag = false; //待删除的部门不存在
		for(int i =0;i<allDept.size();i++) {
			if(deptno.intValue()== allDept.get(i).getDeptno().intValue()) { //比较
				allDept.remove(i);//如果存在,从数据集中移除待删除的数据
				flag = true;
				break;
			}
		}
		writeDepts(allDept);//将数据集保存到文件中
		return flag;
	}

	@Override
	public Dept getDeptByDeptno(Integer deptno) throws IOException {
		Dept res = null;
		BufferedReader br = new BufferedReader(new FileReader(file));
		String line = null;
		while((line = br.readLine()) != null) { // 读取一行数据
			Dept dept = JSON.parseObject(line,Dept.class);//将读取到的JSON对象转换为Dept对象
			if(deptno ==  dept.getDeptno().intValue()) {//比较当前读取到的对象的deptno是否为要查找的
				res = dept;
				break;
			}
		}
		if (br != null) { // 关闭资源
			br.close();
		}
		return res;
	}
	
	@Override
	public Dept getDeptByDname(String dname) throws IOException {
		Dept res = null;
		FileReader writer = new FileReader(file);
		BufferedReader br = new BufferedReader(writer);
		String line = null;
		while ((line = br.readLine()) != null) {  // 读取一行数据
			Dept dept = JSON.parseObject(line, Dept.class); //将读取到的JSON对象转换为Dept对象
			if (dname.equals(dept.getDname())) { //比较当前读取到的对象的dname是否为要查找的
				res = dept;
				break;
			}
		}
		if (br != null) { // 关闭资源
			br.close();
		}
		return res;
	}

	@Override
	public List<Dept> getAllDept() throws IOException {
		List<Dept> res = new ArrayList<Dept>();//存放从文件中读取到的Dept

		FileReader reader = new FileReader(file);
		BufferedReader br = new BufferedReader(reader);

		String line = null;
		while ((line = br.readLine()) != null) {//读取一行
			Dept temp = JSON.parseObject(line, Dept.class); //将讲到的JSON形式的数据转换为Dept对象
			res.add(temp); 
		}
		if (br != null) {
			br.close();
		}
		return res;
	}

}

EmpServiceImpl.java

public class EmpServiceImpl implements EmpService {

	private File file =new File(GlobalConst.DB_NAME+"/"+GlobalConst.EMP_TABLE_NAME);//存放所有Emp数据的文件,所有Emp以JSON数组的形式保存在文件中
	
	@Override
	public boolean addEmp(Emp emp)   {
		try {
			List<Emp> empList = getAllEmp(); //从文件中读取所有的Emp数据到数据集中
			empList.add(emp);//将要用户要添加的数据保存到数据集中
			writeEmpList(empList); //将数据集中的数据保存到文件中
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean updateEmp(Emp emp) throws IOException {//若参数指定的emp的empno在数据集中存在则更新,否则添加
		boolean flag = false;
		List<Emp> empList = getAllEmp();//从文件中读取所有的Emp数据到数据集中
		for(int i =0;i<empList.size();i++) {
			if(emp.getEmpno().intValue()==empList.get(i).getEmpno().intValue()) {//存在更新
				empList.add(i, emp);//更新
				flag = true;//更新
				break;
			} 
		}
		if(!flag) {//不存在添加
			empList.add(emp);
		}
		writeEmpList(empList);		
		return flag;
	}

	@Override
	public boolean deleteEmpByEmpno(Integer empno) throws IOException {
		List<Emp> empList = getAllEmp(); //从文件中读取所有的Emp数据到数据集中
		for(int i =0;i<empList.size();i++) {
			if(empno.intValue()==empList.get(i).getEmpno().intValue()) {//根据empno比较,看数据集中是否存在和参数一致的Emp
				empList.remove(i); //从数据集中匹配的数据删除
				writeEmpList(empList);//将数据集中的数据保存到文件中
				return true;
			}
		}
		return false;
	}

	private void writeEmpList(List<Emp> empList) throws IOException { //将数据集中的数据保存到文件中
		BufferedWriter br = new BufferedWriter(new FileWriter(file));
		br.write(JSON.toJSONString(empList));
		if(br != null) {
			br.close();
		}
	}

	@Override
	public Emp getEmpByEmpno(Integer empno) throws IOException {
		List<Emp> empList = getAllEmp(); //从文件中读取所有的Emp数据到数据集中
		for(Emp emp : empList) {
			if(empno.intValue()==emp.getEmpno().intValue()) {//判断数据集中是否存在和参数一致的Emp信息
				return emp;
			}
		}
		return null;
	}

	@Override
	public Emp getEmpByEname(String ename) throws IOException {
		List<Emp> empList = getAllEmp(); //从文件中读取所有的Emp数据到数据集中
		for(Emp emp : empList) {
			if(ename.equals(emp.getEname())) {//判断数据集中是否存在和参数一致的Emp信息
				return emp;
			}
		}
		return null;
	}

	@Override
	public List<Emp> getEmpByDeptno(Integer deptno) throws IOException { 
		List<Emp> res = new ArrayList<>();
		List<Emp> empList = getAllEmp();//从文件中读取所有的Emp数据到数据集中	
		for(Emp emp: empList) {
			if(deptno.intValue() == emp.getDeptno().intValue()) {//如果当前Emp的deptno和参数一致将其放到结果集中
				res.add(emp);
			}
		}
		return res;//将结果集返回
	}

	@Override
	public List<Emp> getAllEmp() throws IOException {//从文件中读取所有的Emp数据到数据集中
		List<Emp> empList = null;
		BufferedReader br = new BufferedReader(new FileReader(file));
		String line = null;
		if((line = br.readLine()) != null){
			empList = JSON.parseArray(line, Emp.class);
		}
		if(br != null) {
			br.close();
		}
		return empList;
	}

}

ManagerServiceImpl.java

public class ManagerServiceImpl implements ManagerService {

	@Override
	public boolean login(String name, String password) {
		File managerTable = new File(GlobalConst.DB_NAME + "/" + GlobalConst.MANAGER_TABLE_NAME);
		
		try(BufferedReader br = new BufferedReader(new FileReader(managerTable));){
			String line = null;
			while((line= br.readLine()) != null) {
				Manager manager = JSON.parseObject(line , Manager.class);
				if(name.equals(manager.getName()) && MD5Util.encode(password).equals(manager.getPassword())) {
					return true;
				}
			}
		}catch (Exception e) {
		}
		return false;
	}
}
发布了253 篇原创文章 · 获赞 666 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/lianghecai52171314/article/details/103909702
今日推荐