private PhoneNumber InputNameAndPhone(Scanner input) { PhoneNumber phoneNumber = new PhoneNumber(); System.out.println("Input name of man:"); String name = input.nextLine(); phoneNumber.setManId(manDao.getIdByName(name)); System.out.println("Input phone number(+XXX XX XXX-XX-XX):"); String phone = input.nextLine(); phoneNumber.setPhone(phone); return phoneNumber; }
private void InputLable(Scanner input, PhoneNumber phoneNumber) { String lable; System.out.println("Input lable"); lable = input.nextLine(); phoneNumber.setLable(lable); }
public void start() { try (Scanner input = new Scanner(System.in)) { System.out.println("Input *Help* to view list commands"); help(); while (true) { String command = input.nextLine(); switch (command) { case "show": { printInfo(); break; } case "exit": { return; } case "help": { help(); break; } case "addMan": { System.out.println("Input name of man:"); command = input.nextLine(); try { Man man = new Man(); man.setName(command); manDao.insert(man); } catch (RuntimeException e) { System.out.println("Man already exists"); } break; } case "delMan": { System.out.println("Input name of man:"); command = input.nextLine(); try { manDao.deleteByName(command); } catch (RuntimeException e) { System.out.println("Man already exists"); } break; } case "addPhone": { try { PhoneNumber phoneNumber = InputNameAndPhone(input); System.out.println("Add lable to this number?(Y/N)"); String check = input.nextLine(); if (check.equals("Y")) { InputLable(input, phoneNumber); } else { phoneNumber.setLable(""); } phoneNumberDao.insert(phoneNumber); } catch (NullPointerException e) { System.out.println("This name not found"); } catch (NumberFormatException e) { System.out.println(e.getMessage()); } catch (RuntimeException e) { System.out.println("Man not found"); } break; } case "delPhone": { try { PhoneNumber phoneNumber = InputNameAndPhone(input); phoneNumberDao.deleteById(phoneNumberDao.getIdByNumberAndMan(phoneNumber)); } catch (NullPointerException e) { System.out.println("This name not found"); } catch (NumberFormatException e) { System.out.println(e.getMessage()); } catch (RuntimeException e) { System.out.println("Field with the " + "entered value is not found"); } break; } case "addLable": { try { PhoneNumber phoneNumber = InputNameAndPhone(input); System.out.println("Input lable"); String lable = input.nextLine(); phoneNumber.setLable(lable); phoneNumber.setId(phoneNumberDao.getIdByNumberAndMan(phoneNumber)); phoneNumberDao.update(phoneNumber); } catch (NullPointerException e) { System.out.println("Name or number not found"); } catch (NumberFormatException e) { System.out.println(e.getMessage()); } catch (RuntimeException e) { System.out.println("Field with the " + "entered value is not found"); } break; } case "delLable": { try { PhoneNumber phoneNumber = InputNameAndPhone(input); phoneNumber.setLable("empty"); phoneNumber.setId(phoneNumberDao.getIdByNumberAndMan(phoneNumber)); phoneNumberDao.update(phoneNumber); } catch (NullPointerException e) { System.out.println("Name or number not found"); } catch (NumberFormatException e) { System.out.println(e.getMessage()); } catch (RuntimeException e) { System.out.println("Field with the " + "entered value is not found"); } break; } default: { System.err.println("Unknown command"); break; } } } } catch (Exception e) { System.err.println(e.getMessage()); } }