// gather the necessary information to add a publisher
 private void addPublisher() {
   try {
     Connection conn = getConnection();
     try {
       PublisherDAO pubDAO = new PublisherDAO(conn);
       Publisher toAdd = new Publisher();
       System.out.println("What is the Publisher's name?");
       String pubName = getInputString();
       System.out.println("What is the Publisher's address?");
       String pubAddress = getInputString();
       System.out.println("What is the Publisher's phone number?");
       String pubPhone = getInputString();
       toAdd.setPublisherName(pubName);
       toAdd.setPublisherAddress(pubAddress);
       toAdd.setPublisherPhone(pubPhone);
       pubDAO.create(toAdd);
       conn.commit();
       conn.close();
     } catch (Exception e) {
       conn.rollback();
       conn.close();
       e.printStackTrace();
     }
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
  private void editPublisher(Publisher publisher) {
    try {
      Connection conn = getConnection();
      System.out.println("Enter Publisher's new name:");
      String name = getInputString();
      System.out.println("Enter Publisher's new address:");
      String address = getInputString();
      System.out.println("Enter Publisher's new phone:");
      String phone = getInputString();
      try {
        PublisherDAO pubDAO = new PublisherDAO(conn);
        publisher.setPublisherName(name);
        publisher.setPublisherAddress(address);
        publisher.setPublisherPhone(phone);
        pubDAO.update(publisher);
        conn.commit();
        conn.close();
      } catch (Exception e) {
        conn.rollback();
        conn.close();
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      System.err.println("Error while connecting to database");
      e.printStackTrace();
    }
  }
  private void AddPublisher() throws SQLException {
    boolean exit_Add = false;

    String name, Address, phone;
    Set<Publisher> s = as.listpublisher3();
    Publisher p = new Publisher();
    do {

      System.out.println("Enter the Publisher Name:");
      name = sc.nextLine();
      System.out.println("Enter the Publisher Address:");
      Address = sc.nextLine();
      System.out.println("Enter the Publisher Phone:");
      phone = sc.nextLine();
      p.setPublisherName(name);
      p.setPublisherAddress(Address);
      p.setPublisherPhone(phone);
      if (name != null && name.length() > 0 && name.length() <= 45) {
        if (!s.contains(p)) {
          try {
            as.createPublisher(p);
            System.out.println("Operation successful!");
            exit_Add = true;
          } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
          }

        } else {
          System.out.println("Error: This publisher already exists.");
        }
      } else {
        System.out.println();
        System.out.println("Error:  Name cannot be empty or more than 45 characters");
      }

      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);
  }
  private void Updatepublisher2(int id) throws SQLException {
    boolean exit_Add = false;

    String name, Address, phone;
    Set<Publisher> s = as.listpublisher3();
    Publisher p = new Publisher();
    do {

      System.out.println("Enter the new Publisher Name:");
      name = sc.nextLine();
      System.out.println("Enter the new Publisher Address:");
      Address = sc.nextLine();
      System.out.println("Enter the new Publisher Phone:");
      phone = sc.nextLine();
      p.setPublisherId(id);
      p.setPublisherName(name);
      p.setPublisherAddress(Address);
      p.setPublisherPhone(phone);
      if (name != null && name.length() > 0 && name.length() <= 45) {
        if (!s.contains(p)) {
          as.updatePublisher(p);
          System.out.println("Update successful");
          exit_Add = true;
        } else {
          // If the information entered are identical to the original

          System.out.println("Update successful");
        }
      } else {
        System.out.println();
        System.out.println("Error:  Name cannot be empty or more than 45 caracter");
      }

      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);
  }
  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);
  }
 private void deleteBookPublisher(Book book) {
   Integer pubId = null;
   try {
     Connection conn = getConnection();
     BookDAO bookDAO = new BookDAO(conn);
     Publisher curPublisher = book.getPublisher();
     if (curPublisher == null) {
       curPublisher = new Publisher();
     }
     curPublisher.setPublisherId(pubId);
     book.setPublisher(curPublisher);
     bookDAO.update(book);
     conn.commit();
     conn.close();
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
  private void deletePublisher(Publisher publisher) {
    try {
      Connection conn = getConnection();
      try {
        PublisherDAO pubDAO = new PublisherDAO(conn);
        BookDAO bookDAO = new BookDAO(conn);
        List<Book> publisherBooks =
            (List<Book>)
                bookDAO.read(
                    "SELECT * FROM tbl_book WHERE pubId = ?",
                    new Object[] {publisher.getPublisherId()});
        if (publisherBooks.size() > 0) {
          ArrayList<String> answers = new ArrayList<String>();
          answers.add("No, nevermind");
          answers.add("Yes, delete this publisher");
          System.out.println(
              publisher + " has published " + publisherBooks.size() + " books in our records");
          System.out.println(
              "Are you sure you still want to delete? Some books may remain with no publisher");
          displayOptions(answers);
          int in = getInputInt(1, 2);
          if (in == 1) {
            return;
          }
          bookDAO.save(
              "UPDATE tbl_book SET pubId = ? WHERE pubId = ?",
              new Object[] {null, publisher.getPublisherId()});
        }
        pubDAO.delete(publisher);
        conn.commit();
        conn.close();
      } catch (Exception e) {
        conn.rollback();
        conn.close();
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      System.err.println("Error while connecting to database");
      e.printStackTrace();
    }
  }
 /** ******************************************************************************************* */
 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();
   }
 }
  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);
  }
  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);
  }