service
接口
DeptService.java
public interface DeptService {
public boolean addDept(Dept dept)throws IOException;
public boolean updateDept(Dept dept) 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);
@Override
public boolean addDept(Dept dept) {
try {
FileWriter writer = new FileWriter(file, true);
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;
}
return true;
}
@Override
public boolean updateDept(Dept dept) throws IOException {
List<Dept> allDept = getAllDept();
boolean flag = 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;
}
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");
}
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);
if(deptno == dept.getDeptno().intValue()) {
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);
if (dname.equals(dept.getDname())) {
res = dept;
break;
}
}
if (br != null) {
br.close();
}
return res;
}
@Override
public List<Dept> getAllDept() throws IOException {
List<Dept> res = new ArrayList<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);
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);
@Override
public boolean addEmp(Emp emp) {
try {
List<Emp> empList = getAllEmp();
empList.add(emp);
writeEmpList(empList);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean updateEmp(Emp emp) throws IOException {
boolean flag = false;
List<Emp> empList = getAllEmp();
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();
for(int i =0;i<empList.size();i++) {
if(empno.intValue()==empList.get(i).getEmpno().intValue()) {
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();
for(Emp emp : empList) {
if(empno.intValue()==emp.getEmpno().intValue()) {
return emp;
}
}
return null;
}
@Override
public Emp getEmpByEname(String ename) throws IOException {
List<Emp> empList = getAllEmp();
for(Emp emp : empList) {
if(ename.equals(emp.getEname())) {
return emp;
}
}
return null;
}
@Override
public List<Emp> getEmpByDeptno(Integer deptno) throws IOException {
List<Emp> res = new ArrayList<>();
List<Emp> empList = getAllEmp();
for(Emp emp: empList) {
if(deptno.intValue() == emp.getDeptno().intValue()) {
res.add(emp);
}
}
return res;
}
@Override
public List<Emp> getAllEmp() throws IOException {
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;
}
}