public static Department getDepOrCreate(String departmentName) { Department dp = departmentDAO.getByName(departmentName); if (dp == null) { dp = new Department(departmentName); departmentDAO.save(dp); } return dp; }
public static void main(String[] args) { LOGGER.info(info); Department nullDepartment = departmentDAO.getByName("with out department"); if (nullDepartment == null) { nullDepartment = new Department("with out department"); departmentDAO.save(nullDepartment); } Department currentDepartment = null; try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")))) { while (true) { String line = reader.readLine(); if (line != null) { if (line.equals("exit")) { break; } else if (line.startsWith("dep")) { String departmentName = line.substring(4, line.length()); if (isNameOk(departmentName)) { currentDepartment = getDepOrCreate(departmentName); } } else if (line.startsWith("work")) { String workerName = line.substring(5, line.length()); if (isNameOk(workerName)) { Worker newWorker = getWorkOrCreate(currentDepartment, nullDepartment, workerName); } } else if (line.startsWith("del_dep")) { String departmentName = line.substring(8, line.length()); if (isNameOk(departmentName)) { deleteDepartmentAndRelocateWorkersToNullDep(departmentName, nullDepartment); } } else if (line.startsWith("del_work_dep")) { String workerName = line.substring(13, line.length()); if (isNameOk(workerName)) { deleteWorkerFromHisDepartmentAndRelocateHimToNullDep(workerName, nullDepartment); } } else if (line.startsWith("del_work")) { String workerName = line.substring(9, line.length()); if (isNameOk(workerName)) { deleteWorkerFromAnywhere(workerName); } } else { repeatInfoMessage(); } } } } catch (IOException e) { LOGGER.error("readLine exception", e); } LOGGER.info(workerDAO.getAll().toString()); LOGGER.info(departmentDAO.getAll().toString()); }