Java Programming Issue

I am trying to create an easy way to organize some objects in terms of which classes to create. Say I'm trying to track books. A book may fall under different genres and subgenres. I want to be able to recognize a book as one book, and yet it falls into these different categories. I have a genre that tracks all subgenres and a subgenre class that has all the books in it. I want the book to know the whole genre and the subgenres that fall under it. I also want to track some statistics (reviews, comments, the number of times I read, etc.) Depending on the genre and subgenre, and then be able to combine them to get numbers for the entire book. Thus, the user can select a book and know each genre / subgenre to which the book belongs, and compile statistics about this book for each category

What are some ideas on how I can do this?

My idea was for each book to define a BookGroup class, and BookGroup contain a genre and subgenre along with any relevant information for this category (assuming that subgenres can belong to only one genre). Then in the class of the book I would keep a set of book groups, which includes the book. I can add statistics from all different groups of books. The only thing I don’t like about this is that I feel that BookGroup should contain Books, and not vice versa.

Any other ideas?

Thank!

Edit: All of you guys gave really good advice. I think for simplicity I could do something like this now:

class Book
{
     Genre myGenre;
     SubGenre mySubGenre;
     String myTitle;
}

class Library
{
     Map<String,Set<Book>> allBooks = new HashMap<String,Set<Book>>();
     //where allBooks contains a mapping from book title, to all of the book objects which actually represent the same book but may contain different information related to their specific genre/subgenre
}
+3
source share
5 answers

, , :

public class Book  
{  
String name;  
List<Review> reviews;  
Set<Genre> genres;  
public Book(String name, Set<Genre> genres){}  
}

  public class Genre  
{  
String name;  
Set<Book> books;  
public Genre(String name, Set<Book> books){}  
}  

, , , DAO , , CRUD . , , " ", .

+1

, . . , , .

, , , , Java.

+1

, BookGenre = {scifi, novel,...} . Book Book , scifi .. (. . EnumMap > ); , .

+1

, . , , - : "BookGroup" , (/), . , , "BookGenreStatistics", , , .

+1

, , . , changre . , , . , .

, , , , , . , . , . , , , , , .

, , , . , ? , , - , , . , . , .., , , , , , , . , , , , , , . (, , ?), , , , .

, : 1. , . 2. ( changre) changres ( ) 2. ( , , ). , , , , . 3. , . ( , , , , , )

-Done, , ( ), , .

, , , , , , changre, .., changre, changre, , , . , , , , , .

+1

Source: https://habr.com/ru/post/1774143/


All Articles