Java

JDBC-Controller

NellKiM 2024. 10. 25. 21:31
public class CRUDController {

	static Scanner sc = new Scanner(System.in);
	static EmpService empservice = new EmpService();

	public static void main(String[] args) {
		boolean isStart = false;
		while (!isStart) {
			menu();
			int job_select = Integer.parseInt(sc.nextLine());
			switch (job_select) {
			case 1 -> {
				f_select();
			}
			case 2 -> {
				f_insert();
			}
			case 3 -> {
				f_update();
			}
			case 4 -> {
				f_delete();
			}
			case 5 -> {
				f_selectByIdService();
			}
			case 6->{
				f_selectByEmpFourService();
			}

			case 7 -> {
				f_selectByDeptService();
			}
			case 8 -> {
				f_selectByJobService();
			}
			case 9 -> {
				f_selectByEmpSalService();
			}
			case 10 -> {
				isStart = true;
			}

			default -> {
				System.out.println("작업선택 오류");
			}

			}
		}
		sc.close();
		System.out.println("====프로그램 종료======");
	}

	private static void f_update() {

		int result = empservice.updateService(f_makeEmp());
		EmpView.display(result + "건 수정");
	}

	private static EmpDTO f_makeEmp() {
		System.out.print("직원번호>>");
		int emp_id = Integer.parseInt(sc.nextLine());

		System.out.println("first_name >>");
		String fname = sc.nextLine();

		System.out.println("Last_name");
		String lname = sc.nextLine();

		System.out.println("이메일");
		String email = sc.nextLine();

		System.out.println("전화번호");
		String phone_number = sc.nextLine();

		System.out.println("hire_date");
		String hdate = sc.nextLine();
		Date hire_date = DateUtil.convertSqlDate(DateUtil.convertDate(hdate));

		System.out.println("job_id");
		String job_id = sc.nextLine();

		System.out.println("salary");
		Double salary = Double.parseDouble(sc.nextLine());

		System.out.println("commission");
		double commission = Double.parseDouble(sc.nextLine());

		System.out.println("mgr_id");
		int mid = Integer.parseInt(sc.nextLine());

		System.out.println("dept_id");
		int dept_id = Integer.parseInt(sc.nextLine());

		EmpDTO emp = new EmpDTO();
		emp.setCommission_pct(commission);
		emp.setDepartment_id(dept_id);
		emp.setEmail(email);
		emp.setEmployee_id(emp_id);
		emp.setFirst_name(fname);
		emp.setLast_name(lname);
		emp.setHire_date(hire_date);
		emp.setJob_id(job_id);
		emp.setManager_id(mid);
		emp.setPhone_number(phone_number);
		emp.setSalary(salary);

		return emp;
	}

	private static void f_insert() {

		empservice.insertService(f_makeEmp());
	}

	private static void f_select() {

		List<EmpDTO> empList = empservice.SelectAllService();
		EmpView.display(empList);

	}

	private static void f_selectByIdService() {
		System.out.println("직원번호>>");
		int empid = Integer.parseInt(sc.nextLine());

		EmpDTO emp = empservice.selectByIdService(empid);
		EmpView.display(emp);

	}

	private static void f_delete() {
		System.out.print("삭제할 직원번호>>");
		int emp_id = Integer.parseInt(sc.nextLine());
		int result = empservice.deleteService(emp_id);
		EmpView.display(result + "건 삭제됨");

	}

	private static void menu() {

		System.out.println("===================================");
		System.out.println("1.조회 2.입력 3.수정 4. 삭제 5.상세보기6.종료");
		System.out.println("===================================");
		System.out.print("작업선택>>>>>>>>>>>>>>");

	}

	private static void f_selectByDeptService() {
		System.out.println("부서번호>>");
		int deptid = Integer.parseInt(sc.nextLine());

		List<EmpDTO> deptList = empservice.selectByDeptId(deptid);
		EmpView.display(deptList);

	}

	private static void f_selectByJobService() {
		System.out.println("직업번호>>");
		String jobid = sc.nextLine();

		List<EmpDTO> jobEmpList = empservice.selectByJobId(jobid);
		EmpView.display(jobEmpList);

	}

	private static void f_selectByEmpSalService() {
		System.out.println("급여조회>>");
		double salary = Integer.parseInt(sc.nextLine());

		List<EmpDTO> deptList = empservice.selectByEmpSalId(salary);
		EmpView.display(deptList);

	}

	private static void f_selectByEmpFourService() {
		// 부서,직책 ,급여,입사일 조건으로 조회
		System.out.println("조회dept)id");
		int deptid = Integer.parseInt(sc.nextLine());

		System.out.println("조회 job_id");
		String job_id = sc.nextLine();

		System.out.println("급여>>");
		double salary = Integer.parseInt(sc.nextLine());

		System.out.print("입사일 hire_date(yyyy-MM-dd) 이상>>");
		String hdate = sc.nextLine();
		Date hire_date = DateUtil.convertSqlDate(
				            DateUtil.convertDate(hdate));

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("department_id", deptid);
		map.put("job_id", job_id);
		map.put("salary", salary);
		map.put("hire_date", hire_date);

		List<EmpDTO> deptList = empservice.selectByFour(map);
		EmpView.display(deptList);

	}

}