Quantcast
Channel: Book program with arrayList - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 6

Answer by Mathieu Guindon for Book program with arrayList

$
0
0

I have never written a line of java code, but I'll give it a shot:

public Library(ArrayList<Book> other) {    if (other == null) {        throw new NullPointerException("null pointer");    } else        allBook = other;}

The else here is redundant after the guard clause, this method could be written like this:

public Library(ArrayList<Book> other) {    if (other == null) {        throw new NullPointerException("null pointer");    }    allBook = other;}

Library.toString() (spoiler):

The Book class'toString method could do a little more than just concatenating bookTitle+bookAuthor. Maybe something like "Title: "+ bookTitle +" | Author: "+ bookAuthor would look much better.


In Book.equals(), I think the method should actually throw an exception if o isn't a book, but again I'm not very used to duck-typed code so maybe not... but then if an object has a bookTitle and a bookAuthor property then why shouldn't it be treated as if it were a Book? It looks like a duck, quacks like a duck, walks like a duck, ...it's a duck! (or maybe I'm mixing up java with javascript here) -- point is, if Book.equals() handles the case where o isn't a Book, then I don't see why Book.compareTo() shouldn't do the same... or vice-versa!


Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>