Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class that models a book

Before asking the question I have to say that this is homework and I am only looking for ideas on how to implement a class that models a book.

A book has a title and many chapters, each chapter has a title and multiple subchapters. Every subchapter has a title and a list of paragraphs. Every paragraph can have a number and a text. I am looking to implement in OOP the following functionalities: add/remove: chapters, subchapters and paragraphs and display the book. I am having some troubles finding the right structure for the class.

This is how I'm thinking of implementing it but it seems kind of redundant and complicated. Are there simpler and more correct ways to do it?

public class Element<T> {
        int nr;
        String Title;
        ArrayList<T> x = new ArrayList<T>();

        Element() {
            nr = 0;
            Title = "";
        }

        public void removeFromElement(int index) {
            if (index <= 0 && index > x.size())
                System.out.println("The chapter doesn't exist");
            else {
                x.remove(index);
                System.out.println("Succesful deletion");
            }
        }

        public void addToElement(T elem) {
            x.add(elem);
        }
    }

    public class Paragraph {
    int nr;
    String text;
    }

    public class Subchapter extends Element<Paragraph> {
    }

    public class Chapter extends Element<Subchapter> {
    }

    public class Book extends Element<Chapter> {
    }
like image 658
J. Doe Avatar asked Nov 12 '15 13:11

J. Doe


Video Answer


2 Answers

Your first approach is pretty good but does not take into account the possibility of evolution.

What would happen if we asked you to add the possibility for paragraphs to have 0-n quotes (or anything else) ? What would happen if we asked you to add, in your book, a type of chapter which can't have subchapters ?

This will need huge changes. You should take a look at composite pattern.

Following this pattern, you'll be way more flexible as far as evolutions are concerned.

When you think OOP, you must always keep in mind that interfaces can have a huge role in the way you'll design your code. Many of theses problems have been already resolved and have a conventionnal solution (design patterns). You should take the time to learn the most common of them. It will definitly change your coding approach.

("Design pattern head first" would be a good book to read).

More over, one of the most important feature of OOP is the encapsulation. This provide a very powerful way to control class' attributes' accessibility. You MUST use it. Start by adding private or protected modifiers on your class' attributes and create the getters/setters needed to access/modify these attributes.

Another thing to take in note: You should not use the System.out.println() method to logg your code. Use a log API (log4j for instance) and exceptions.

    public void removeFromElement(int index) throws ChapterNotFoundException{
        if (index <= 0 && index > x.size()){
            throw new ChapterNotFoundException(String.format("Chapter %s does not exist", index));
        }

        x.remove(index);
        logger.info(String.format("Chapter %s has been removed", index));
    }
like image 183
Grégory Elhaimer Avatar answered Oct 02 '22 10:10

Grégory Elhaimer


This is actually not correct design. (but I like how you think about minimalize code writing)

The Book is not extended by chapter or "chapter elements". The book contains chapters and it also can contain/do something else.

Therefore the correct design is the simpliest one

public class Book{
    private String title;
    private List<Chapter> chapters;
}
//other classes would look similar

This approach is much more stable and it allows easy modification (or even replacement) in future.

This "phenomen" has also name, it is Composition over inheritance

like image 23
libik Avatar answered Oct 02 '22 10:10

libik