Object-Oriented Programming with Java, part I – Exercise 101

This exercise creates a virtual library and then populates and searches for books within it.

My Code

Main

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        // test your program here
        Library Library = new Library();

        Library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007));
        Library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992));
        Library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952));
        Library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));

        for (Book book : Library.searchByTitle("CHEESE")) {
            System.out.println(book);
        }

        System.out.println("---");
        for (Book book : Library.searchByPublisher("PENGUIN  ")) {
            System.out.println(book);
        }

    }
}

Book

public class Book {
    private String title;
    private String publisher;
    private int year;
    
    public Book(String title, String publisher, int year) {
        this.title = title;
        this.publisher = publisher;
        this.year = year;
    }
    
    public String title(){
        return this.title;
    }
    
    public String publisher(){
        return this.publisher;
    }
    
    public int year(){
        return this.year;
    }
    
    public String toString(){
        return this.title +", "+ this.publisher +", "+ this.year;
    }
}

Library

import java.util.ArrayList;

import java.util.Collections;

public class Library {

    private ArrayList<Book> book = new ArrayList<Book>();

    public Library() {

    }

    public void addBook(Book newBook) {
        this.book.add(newBook);
    }

    public void printBooks() {
        for (Book books : book) {
            System.out.println(books);
        }
    }

    public ArrayList<Book> searchByTitle(String title) {
        ArrayList<Book> found = new ArrayList<Book>();
        for (Book books: book) {
            if (StringUtils.included(books.title(), title)){
                found.add(books);
            }
        }
        return found;
    }

    public ArrayList<Book> searchByPublisher(String publisher) {
        ArrayList<Book> found = new ArrayList<Book>();
        for (Book books : book) {
            if (StringUtils.included(books.publisher(), publisher)) {
                found.add(books);
            }
        }
        return found;
    }

    public ArrayList<Book> searchByYear(int year) {
        ArrayList<Book> found = new ArrayList<Book>();
        for (Book books : book) {
            if (books.year() == year) {
                found.add(books);
            }
        }
        return found;
    }

}

StringUtils

public class StringUtils {

    public static boolean included(String word, String searched) {
        word = word.toUpperCase();
        word = word.trim();
        searched = searched.toUpperCase();
        searched = searched.trim();

        if (word.length() > 0 && searched.length() > 0) {
            if (word.contains(searched)) {
                return true;
            }

        }
        return false;
    }
}

Model Code

Main

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        // test your program here
        Library Library = new Library();

        Library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007));
        Library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992));
        Library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952));
        Library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));

        for (Book book : Library.searchByTitle("CHEESE")) {
            System.out.println(book);
        }

        System.out.println("---");
        for (Book book : Library.searchByPublisher("PENGUIN  ")) {
            System.out.println(book);
        }

    }
}

Book

public class Book {

 

    private String title;

    private String publisher;

    private int year;

 

    public Book(String title, String publisher, int year) {

        this.title = title;

        this.publisher = publisher;

        this.year = year;

    }

 

    public String title() {

        return title;

    }

 

    public String publisher() {

        return publisher;

    }

 

    public int year() {

        return year;

    }

 

    @Override

    public String toString() {

        return this.title + ", " + this.publisher + ", " + this.year;

    }

}

 

Library

import java.util.ArrayList;

 

public class Library {

 

    private ArrayList<Book> books;

 

    public Library() {

        this.books = new ArrayList<Book>();

    }

 

    public void addBook(Book book) {

        this.books.add(book);

    }

 

    public void printBooks() {

        for (Book book : this.books) {

            System.out.println(book);

        }

    }

 

    public ArrayList<Book> searchByTitle(String title) {

        return searchBook(title, null, -1);

    }

 

    public ArrayList<Book> searchByPublisher(String publisher) {

        return searchBook(null, publisher, -1);

    }

 

    public ArrayList<Book> searchByYear(int year) {

        return searchBook(null, null, year);

    }

 

    // Helper method

    public ArrayList<Book> searchBook(String title, String bookName, int year) {

        ArrayList<Book> found = new ArrayList<Book>();

        for (Book book : this.books) {

            if (StringUtils.included(book.title(), title)

                    || StringUtils.included(book.publisher(), bookName)

                    || book.year() == year) {

                found.add(book);

            }

        }

 

        return found;

    }

}

 

StringUtils

public class StringUtils {

 

    public static boolean included(String word, String searched) {

        if (word == null || searched == null) {

            return false;

        }

 

        word = word.trim();

        searched = searched.trim();

 

        word = word.toUpperCase();

        searched = searched.toUpperCase();

 

        return word.contains(searched);

    }

}

Comments

Not to bad. The largest difference is in the class of StringUtils and although the model version is better, mine doesn’t look all that bad.

Leave a comment