天天看點

企業員工管理系統 五:控制層#私藏項目實操分享#

DeptController.java

public class DeptController {
    private DeptService deptService = new DeptServiceImpl();

    public void deleteDept(Scanner scanner) throws IOException {
        System.out.println("請輸入待删除的部門的編号:");
        int deptno = scanner.nextInt();
        boolean res = deptService.deleteDeptByDeptno(deptno);
        String tip = res ? "删除成功" : "删除失敗";
        System.out.println(tip);
    }

    public void updateDept(Scanner scanner) throws IOException {
        System.out.println("請輸入待修改的部門的編号:");
        int deptno = scanner.nextInt();
        System.out.println("請輸入待修改的部門的名稱:");
        String dname = scanner.next();
        System.out.println("請輸入待修改的部門的位址:");
        String loc = scanner.next();
        Dept dept = new Dept(deptno, dname, loc);

        boolean res = deptService.updateDept(dept);
        String tip = res ? "修改成功" : "添加成功";
        System.out.println(tip);
    }

    public void addDept(Scanner scanner) throws IOException {
        System.out.println("請輸入待增加的部門的名稱:");
        String dname = scanner.next();
        System.out.println("請輸入待增加的部門的位址:");
        String loc = scanner.next();
        int deptno = new Random().nextInt(9000) + 1000;
        Dept dept = new Dept(deptno, dname, loc);

        boolean res = deptService.addDept(dept);
        String tip = res ? "添加成功" : "添加失敗";
        System.out.println(tip);
    }

    public void listDeptByDname(Scanner scanner) throws IOException {
        System.out.println("請輸入待查找的部門的名稱:");
        String dname = scanner.next();
        Dept dept = deptService.getDeptByDname(dname);
        System.out.println(dept);
    }

    public void listDeptByDeptno(Scanner scanner) throws IOException {
        System.out.println("請輸入待查找的部門的編号:");
        int deptno = scanner.nextInt();
        Dept dept = deptService.getDeptByDeptno(deptno);
        System.out.println(dept);
    }

    public void listAllDept() throws IOException {
        List<Dept> allDept = deptService.getAllDept();
        for (Dept dept : allDept) {
            System.out.println(dept);
        }
    }
}           

EmpController.java

public class EmpController {
    private EmpService empService = new EmpServiceImpl();

    public void listAllEmp() throws IOException {
        List<Emp> empList = empService.getAllEmp();
        for(Emp emp : empList) {
            System.out.println(emp);
        }
    }

    public void getEmpByEmpno(Scanner scanner) throws IOException {
        System.out.println("請輸入待查找的員工的編号:");
        int empno = scanner.nextInt();
        Emp emp = empService.getEmpByEmpno(empno);
        System.out.println(emp);
    }

    public void getEmpByEname(Scanner scanner) throws IOException {
        System.out.println("請輸入待查找的員工的名稱:");
        String ename = scanner.next();
        Emp emp = empService.getEmpByEname(ename);
        System.out.println(emp);
    }

    public void getEmpByDeptno(Scanner scanner) throws IOException {
        System.out.println("請輸入待查找的員工的部門編号:");
        int deptno = scanner.nextInt();
        List<Emp> empList = empService.getEmpByDeptno(deptno);
        for(Emp emp : empList) {
            System.out.println(emp);
        }
    }

    public void addEmp(Scanner scanner) throws IOException {
        int empno = new Random().nextInt(1000)+9000;
        System.out.println("請輸入待添加的員工的名稱:");
        String ename = scanner.next();
        System.out.println("請輸入待添加的員工的工作:");
        String job = scanner.next();

        System.out.println("請輸入待添加的員工的入職日期,格式:yyyy-MM-dd:");
        String hiredateStr = scanner.next();
        String[] split = hiredateStr.split("-");
        LocalDate hiredate = LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));

        System.out.println("請輸入待添加的員工的上司編号:");
        int mgr = scanner.nextInt();
        System.out.println("請輸入待添加的員工的工資:");
        double sal = scanner.nextDouble();
        System.out.println("請輸入待添加的員工的獎金:");
        double comm = scanner.nextDouble();
        System.out.println("請輸入待添加的員工的部門編号:");
        int deptno = scanner.nextInt();

        Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);

        boolean flag = empService.addEmp(emp);
        String res  = flag?"添加成功":"添加失敗";
        System.out.println(res);
    }

    public void updateEmp(Scanner scanner) throws IOException {
        int empno = new Random().nextInt(1000)+9000;
        System.out.println("請輸入待添加的員工的名稱:");
        String ename = scanner.next();
        System.out.println("請輸入待添加的員工的工作:");
        String job = scanner.next();

        System.out.println("請輸入待添加的員工的入職日期,格式:yyyy-MM-dd:");
        String hiredateStr = scanner.next();
        String[] split = hiredateStr.split("-");
        LocalDate hiredate = LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));

        System.out.println("請輸入待添加的員工的上司編号:");
        int mgr = scanner.nextInt();
        System.out.println("請輸入待添加的員工的工資:");
        double sal = scanner.nextDouble();
        System.out.println("請輸入待添加的員工的獎金:");
        double comm = scanner.nextDouble();
        System.out.println("請輸入待添加的員工的部門編号:");
        int deptno = scanner.nextInt();

        Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
        boolean flag = empService.updateEmp(emp);
        String res = flag?"修改成功":"添加成功";
        System.out.println(res);
    }

    public void deletEmpByEmpno(Scanner scanner) throws IOException {
        System.out.println("請輸入待删除的員工的編号:");
        int empno = scanner.nextInt();
        boolean flag = empService.deleteEmpByEmpno(empno);
        String res = flag?"删除成功":"删除失敗";
        System.out.println(res);
    }

}           

ManagerController.java

public class ManagerController {
    private ManagerService managerService = new ManagerServiceImpl();

    public boolean login(Scanner scanner) {
        System.out.println("請輸入使用者名:");
        String name = scanner.next();
        System.out.println("請輸入用密碼:");
        String password = scanner.next();
        boolean res = managerService.login(name, password);
        return res;
    }
}