コード例 #1
0
  private void Updatebook() throws SQLException {
    boolean exit_Add = false;

    int id;
    Map<Integer, Book> m = as.listBooksFirstLevel2();
    Book b = new Book();
    System.out.println("\nBook ID        |       Title        |      Publisher ID");
    System.out.println("----------------------------------------------------------");
    Set<Integer> sk = new HashSet<>();
    for (Map.Entry<Integer, Book> map : m.entrySet()) {
      b = map.getValue();
      if (b.getPublisher() != null) {
        System.out.println(
            map.getKey()
                + "  \t\t "
                + b.getTitle()
                + "  \t\t "
                + b.getPublisher().getPublisherId());
      } else {
        System.out.println(map.getKey() + "  \t\t " + b.getTitle() + "  \t\t NULL");
      }
    }
    do {

      System.out.println("Enter the Book Id you want to Update:");

      try {

        id = Integer.parseInt(sc.nextLine().trim());

        if (m.containsKey(id)) {
          Updatebook2(id);
        } else {
          System.out.println("This Id does not exist.");
        }
      } catch (Exception e) {
        System.out.println("INFO:Should be an integer!");
      }

      if (!exit_Add) {
        System.out.println();
        System.out.println();
        System.out.println("Press ' q '  to return or any other key to continue");
        String option = (sc.nextLine());
        if (option.length() > 0) {
          switch (option.charAt(0)) {
            case 'q':
              exit_Add = true;
              break;

            default:
              exit_Add = false;
              break;
          }
        }
      }

    } while (!exit_Add);
  }
コード例 #2
0
  private void Updatebook2(int id) throws SQLException {
    boolean exit_Add = false;

    String title;
    int pubId;
    Map<Integer, Book> m = as.listBooksFirstLevel2();
    Set<Publisher> set = as.listpublisher3();
    Book b = new Book();
    System.out.println("\nPublisher ID | Publisher Name | Publisher Address | Publisher Phone ");
    System.out.println("---------------------------------------");
    Set<Integer> sk = new HashSet<>();
    Map<Integer, Publisher> map = new HashMap<Integer, Publisher>();
    for (Publisher s : set) {
      sk.add(s.getPublisherId());
      map.put(s.getPublisherId(), s);
      System.out.println(
          s.getPublisherId()
              + "\t "
              + s.getPublisherName()
              + "\t "
              + s.getPublisherAddress()
              + "\t "
              + s.getPublisherPhone());
    }
    do {
      System.out.println();
      System.out.println("Enter the New Publisher Id from the list above:\n");

      try {
        pubId = Integer.parseInt(sc.nextLine().trim());
        System.out.println("Enter the New Title:\n");
        title = sc.nextLine();
        if (title != null && title.length() > 0 && title.length() <= 45) {
          if (sk.contains(pubId)) { // if valid publisher ID
            b.setBookId(id);
            b.setTitle(title);

            b.setPublisher(map.get(pubId));
            as.updatebook2(b);
            System.out.println("Book updated successfully");
            exit_Add = true;
          } else {
            // If invalid publisher ID, just update the title

            b.setBookId(id);
            b.setTitle(title);
            // b.setPublisherId();
            as.updatebook(b);
            System.out.println("----------------------------------");
            System.out.println("Not a valid Publisher Id!");
            System.out.println("Only Book tilte updated ");
            exit_Add = true;
          }

        } else {
          System.out.println();
          System.out.println("Error:  Name cannot be empty");
        }
      } catch (Exception e) {
        System.out.println("INFO:Should be an integer!");
      }

      if (!exit_Add) {
        System.out.println();
        System.out.println();
        System.out.println("Press ' q '  to return or any other key to continue");
        String option = (sc.nextLine());
        if (option.length() > 0) {
          switch (option.charAt(0)) {
            case 'q':
              exit_Add = true;
              break;

            default:
              exit_Add = false;
              break;
          }
        }
      }

    } while (!exit_Add);
  }