2017-05-01 11 views
0

これは単純なライブラリプロジェクトです。 キーワードまたはジャンルに基づいて検索するようにユーザーに依頼することによって、データベースからデータをロードする必要があります。 私は2つのクラスを持っています。それらの1つはブッククラスです。:私は方法で、正しい方法Collection.sort()とのCompareTo()で呼び出したい秒1でJavaでSQLを使用してInterface Comparable、Collection.sort()を実装する方法は?

package library; 

import java.sql.Date; 

public class Book implements Comparable<Book> { 

String title; 
String author; 
Date date; 
String ISBN; 
String format; 
String publisher; 
float price; 
String[] keywords; 
String[] inputArray; 
String input; 

public Book(String title_param, String author_param, java.util.Date date_param, String ISBN_param, 
     String format_param, String publisher_param, float price_param, String keywords_param) { 

    title = title_param; 
    author = author_param; 
    date = (Date) date_param; 
    ISBN = ISBN_param; 
    format = format_param; 
    publisher = publisher_param; 
    price = price_param; 
    keywords = keywords_param.split(","); 

} 

public void setUserInput(String userIn) { 
    input = userIn; 
} 

private int getRelevance(String userInput) { 
    inputArray = userInput.split(","); 
    int num = 0; 

    for (int i = 0; i != keywords.length; i++) { 
     String in = inputArray[i]; 

     for (int l = 0; l != keywords.length; l++) { 
      if (in.equals(keywords[l])) 
       num++; 
     } 
    } 

    return num; 
} 

public int compareTo(Book o) { 
    if (this.getRelevance(input) > o.getRelevance(input)) { 
     return 1; 
    } else if (this.getRelevance(input) < o.getRelevance(input)) { 
     return -1; 
    } 

    return 0; 
} 
} 

は、それはこれらのキーワードのうちの少なくとも1つを含有冊を示すこと。しかし、入力から最も多くのキーワードを持つ本を上に表示する必要があります。 コレクションと比較部分 は現在動作していません。

package library; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Date; 
import java.util.Scanner; 

public class LibrarySearch { 
    static ArrayList<Book> books = new ArrayList<Book>(); 
    ArrayList<LibrarySearch> genres = new ArrayList<LibrarySearch>(); 
    static ArrayList<LibrarySearch> keywords = new ArrayList<LibrarySearch>(); 

    public static void main(String[] args) { 
     load_data(); 
    } 

    private static void load_data() { 
     Collections.sort(books, new Comparator<Book>() { 
      @Override 
      public int compare(Book first, Book second) { 
       if (first.compareTo(second) == 1) { 
        return 1; 
       } else if (first.compareTo(second) == -1) { 
        return -1; 
       } 
       return 0; 

      } 
     }); 

     Connection connection = null; 
     Statement statement = null; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "123456"); 
      statement = connection.createStatement(); 
      System.out.println("Choose to search by keywords or genres"); 
      Scanner scanner = new Scanner(System.in); 
      String input = scanner.nextLine(); 

      if (input.equals("keywords")) { 
       System.out.println("Enter your keywords: "); 
       String[] keyWordsInput = scanner.nextLine().split(","); 
       ResultSet result = null; 

       for (int i = 0; i != keyWordsInput.length; i++) { 
        result = statement 
          .executeQuery(" SELECT * FROM book WHERE keywords LIKE '%" + keyWordsInput[i] + "%'"); 
       } 

       while (result.next()) { 
        int id = result.getInt("id"); 
        String title = result.getString("title"); 
        String author = result.getString("author"); 
        Date date = result.getDate("date"); 
        String ISBN = result.getString("ISBN"); 
        String format = result.getString("format"); 
        String publisher = result.getString("publisher"); 
        float price = result.getFloat("price"); 
        String keywords = result.getString("keywords"); 

        System.out.println("ID = " + id); 
        System.out.println("TITLE = " + title); 
        System.out.println("AUTHOR = " + author); 
        System.out.println("DATE = " + date); 
        System.out.println("ISBN = " + ISBN); 
        System.out.println("FORMAT = " + format); 
        System.out.println("PUBLISHER = " + publisher); 
        System.out.println("PRICE = " + price); 
        System.out.println("KEYWORDS = " + keywords); 
        System.out.println("___________________________________________________________________________"); 

        if (title.equals("I,Robot")) { 
         Book new_book = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book); 
        } 
        if (title.equals("Catch-22")) { 
         Book new_book1 = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book1); 
        } 
        if (title.equals("Pride and Prejudice")) { 
         Book new_book2 = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book2); 
        } 
        if (title.equals("Gone with the Wind")) { 
         Book new_book3 = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book3); 
        } 

       } 
       result.close(); 
       statement.close(); 
       connection.close(); 

      } else if (input.equals("genres")) { 
       System.out.println("Enter your genres" + ": "); 

       String genresInput = scanner.nextLine(); 
       ResultSet result = statement.executeQuery(
         " SELECT * FROM books_genres JOIN book ON (book.id = books_genres.book_id) JOIN genre ON (genre.id = books_genres.genre_id) WHERE name LIKE '%" 
           + genresInput + "%' "); 
       while (result.next()) { 
        int id = result.getInt("id"); 
        String name = result.getString("name"); 

        int book_id = result.getInt("book_id"); 
        int genre_id = result.getInt("genre_id"); 

        int id1 = result.getInt("id"); 
        String title = result.getString("title"); 
        String author = result.getString("author"); 
        Date date = result.getDate("date"); 
        String ISBN = result.getString("ISBN"); 
        String format = result.getString("format"); 
        String publisher = result.getString("publisher"); 
        float price = result.getFloat("price"); 
        String keywords = result.getString("keywords"); 

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 

        System.out.println("Book ID = " + id1); 
        System.out.println("TITLE = " + title); 
        System.out.println("AUTHOR = " + author); 
        System.out.println("DATE = " + date); 
        System.out.println("ISBN = " + ISBN); 
        System.out.println("FORMAT = " + format); 
        System.out.println("PUBLISHER = " + publisher); 
        System.out.println("PRICE = " + price); 
        System.out.println("KEYWORDS = " + keywords); 

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 

        System.out.println("Genre ID = " + id); 
        System.out.println("Genre Name = " + name); 

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 

        System.out.println("Book ID = " + book_id); 
        System.out.println("Genre ID = " + genre_id); 

       } 

       result.close(); 
       statement.close(); 
       connection.close(); 
      } 

      else { 
       System.out.println("Sorry, wrong command"); 
      } 

     } catch (SQLException ex) { 
      System.out.println("No successful connection"); 
      System.out.println("SQLException: " + ex.getMessage()); 
      System.out.println("SQLState: " + ex.getSQLState()); 
     } 

     catch (ClassNotFoundException x_not_found) { 
      System.out.println("Class not found"); 
     } 
    } 

} 
+0

あなたの 'Book'が実装しているので、あなたは' Comparable 'の実装方法を知っています。あなたの質問は何ですか?何が問題ですか?もっと冗長にしてください。それ以外の場合はお手伝いできません。 – Turing85

+0

それは動作していない、私は推測する正しい方法でそれを呼び出すことができませんでした。ありがとうございます – Geya

+0

"それは動作していません。それは何ですか?私が言ったように、もっと冗長にしてください。どの部分が機能していますか?どの部分が機能していないのですか?あなたの質問を見ると、概念的な問題か、データベース接続の問題か、並べ替えの問題か、それらのいくつか、またはすべてが問題であるかどうかを判断できません。 [MCVE](https://stackoverflow.com/help/mcve)も高く評価されます。 – Turing85

答えて

0

まず第一に、getRelevanceは()メソッドがintを返すために、私はこのようなのcompareToを書くことを示唆している:については

Collections.sort(books, new Comparator<Book>() { 
    @Override 
    public int compare(Book first, Book second) {    
     return first.compareTo(second); 
    } 
}); 

は同様
public int compareTo(Book o) { 
    return this.getRelevance(input) - o.getRelevance(input); 
} 

、コンパレータはこのようになります。あなたは最初に空のリストを並べ替えるように見え、次にそれを検索結果に入力します。ソート部分をload_data()メソッドの最後に移動することをお勧めします。

0

1つの問題:inputArraykeywords.length回を繰り返していますが、これはおそらく正しくありません。それがあなたの問題かどうかはわかりません。キーワード配列が入力配列よりもはるかに短い場合です。

大きな問題は、inputArrayのever要素のキーワードに対する繰り返しです。あなたのキーワードをHashSetに入れ、代わりにcontainsをテストしてください。

関連する問題