/** ******************************************************************************************* */
 public void DeletePublisher(Publisher publisher) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (publisher == null
         || publisher.getPublisherName() == null
         || publisher.getPublisherName().length() == 0
         || publisher.getPublisherName().length() > 45
         || publisher.getPublisherAddress() == null
         || publisher.getPublisherAddress().length() == 0
         || publisher.getPublisherAddress().length() > 45
         || publisher.getPublisherPhone() == null
         || publisher.getPublisherPhone().length() == 0
         || publisher.getPublisherPhone().length() > 45) {
       throw new Exception("The Publisher cannot be null");
     } else {
       PublisherDAO pdao = new PublisherDAO(conn);
       pdao.delete(publisher);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
Exemplo n.º 2
0
  private void Deletepublisher() throws SQLException {
    boolean exit_Add = false;

    int id;
    Set<Publisher> set = as.listpublisher3();
    System.out.println("\nPublisher ID | Publisher Name | Publisher Address | Publisher Phone ");
    System.out.println("----------------------------------------------------------------------");
    Set<Integer> sk = new HashSet<Integer>();
    for (Publisher s : set) {
      sk.add(s.getPublisherId());
      System.out.println(
          s.getPublisherId()
              + "\t      "
              + s.getPublisherName()
              + "\t      "
              + s.getPublisherAddress()
              + "\t          "
              + s.getPublisherPhone());
    }
    do {
      System.out.println();
      System.out.println("Enter the Publisher Id you want to delete:");

      try {

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

        if (sk.contains(id)) {
          Publisher p = new Publisher();
          p.setPublisherId(id);
          as.deletePublisher(p);
          System.out.println("Deletion successful!");
        } else {
          System.out.println("This Id does not exist exists.");
        }
      } 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);
  }
Exemplo n.º 3
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);
  }
Exemplo n.º 4
0
  private void Addbook() throws Exception {
    boolean exit_Add = false;

    String name;
    int pubId;
    Set<Publisher> set = as.listpublisher3();
    Book b = new Book();
    Publisher p;

    System.out.println("\nPublisher ID | Publisher Name | Publisher Address | Publisher Phone ");
    System.out.println("----------------------------------------------------------------------");
    Set<Integer> sk = new HashSet<Integer>();
    Map<Integer, Publisher> m =
        new HashMap<Integer, Publisher>(); // used to retrieve later one publisher

    for (Publisher s : set) {
      sk.add(s.getPublisherId());
      m.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 Publisher Id from the list above:");

      try {
        pubId = Integer.parseInt(sc.nextLine().trim());
        System.out.println("Enter the Title of the book:");
        name = sc.nextLine();

        if (name != null && name.length() > 0 && name.length() <= 45) {
          if (sk.contains(pubId)) {
            b.setTitle(name);
            b.setPublisher(m.get(pubId));
            as.createBook(b);
            System.out.println("Book added successfuly");
            exit_Add = true;
          } else {
            //					b.setBookTitle(name);
            //				   	dao.Insertbook(b);
            exit_Add = false;
            System.out.println("Error: This publisher does not exist!!");
          }
        } else {
          System.out.println();
          System.out.println("Error: Title cannot be empty or more than 45 characters");
        }
      } catch (SQLException e) {
        System.out.println(e);
        e.printStackTrace();
      }

      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);
  }