示例#1
0
 private void printInfo() {
   List<Man> mans = manDao.getAll();
   for (Man man : mans) {
     System.out.println(man.getName());
     System.out.println(phoneNumberDao.getAllNumber(man));
   }
 }
示例#2
0
  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());
    }
  }