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

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;
	}
}
  • 17
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
1、 创建类实现基本对象和他们关系的管理,包括学生、教学班、课程、成绩、教师等。使用集合框架存储所有对象。学生至少包含学号、姓名、性别等信息。教学班至少包含教师、课程名字、总人数、教学班号、开课学期等信息。课程至少包含课程编号、课程名字等信息。教师至少包含教师编号、姓名等信息。 2、随机生成学生,数量不少于100。一个教学班有一个教师上一门课程,教学班的学生数量不少于20。课程数量不少于3门。教师数量不少于6个。一门课至少有两个老师上课。每个学生选择至少选择3门课程。一个学生在一个教学班上一门课,考试后取得一个成绩。一门课的成绩构成有4部分构成,包括平时成绩、期中考试、实验成绩和期末考试成绩,然后计算出总成绩。成绩随机生成,均为整数。 3、分阶段模拟教学过程。例如执行一个命令,可以生成一个教学班的所有学生的平时成绩。第一步,生成初始化数据,包括教师,学生、课程,教学班等;第二步,学生选课,随机进行,为每门课程的教学班安排学生。第三步,获得平时成绩,获得期中成绩,获得实验成绩,获得期末成绩,最后计算总成绩。 4、能够显示一个教学班级的学生,可以根据学号排序,可以根据成绩排序。可以通过名字查询成绩,可以按照各科成绩和总成绩进行排名显示,可以统计各课程学生成绩的分数段分布。 5、可以实现自己的扩展功能。注意类和类之间的关系。充分利用继承,多态等特性,使用上抽象类,接口,泛型,内部类等设计元素
1、创建类实现基本对象和他们关系的管理,包括学生、教学班、课程、成绩、教师等。使用集合框架存储所有对象。学生至少包含学号、姓名、性别等信息。教学班至少包含教师、课程名字、总人数、教学班号、开课学期等信息。课程至少包含课程编号、课程名字等信息。教师至少包含教师编号、姓名等信息。 2、随机生成学生,数量不少于100。一个教学班有一个教师上一门课程,教学班的学生数量不少于20。课程数量不少于3门。教师数量不少于6个。一门课至少有两个老师上课。每个学生选择至少选择3门课程。一个学生在一个教学班上一门课,考试后取得一个成绩。一门课的成绩构成有4部分构成,包括平时成绩、期中考试、实验成绩和期末考试成绩,然后计算出总成绩。成绩随机生成,均为整数。 3、分阶段模拟教学过程。例如执行一个命令,可以生成一个教学班的所有学生的平时成绩。第一步,生成初始化数据,包括教师,学生、课程,教学班等;第二步,学生选课,随机进行,为每门课程的教学班安排学生。第三步,获得平时成绩,获得期中成绩,获得实验成绩,获得期末成绩,最后计算总成绩。 4、能够显示一个教学班级的学生,可以根据学号排序,可以根据成绩排序。可以通过名字查询成绩,可以按照各科成绩和总成绩进行排名显示,可以统计各课程学生成绩的分数段分布。 5、可以实现自己的扩展功能。注意类和类之间的关系。充分利用继承,多态等特性,使用上抽象类,接口,泛型,内部类等设计元素。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值