FileDocCategorySizeDatePackage
BookInfo.javaAPI DocExample3578Sat Apr 20 11:11:44 BST 2002ora.ch6

BookInfo

public class BookInfo extends Object
A class that represents a book listing at an online book set, including the number of reviews for the book and its sales ranking.

Fields Summary
int
id
String
isbn
String
title
int
reviews
int
ranking
int
lastReviews
int
lastRanking
Constructors Summary
public BookInfo(String isbn)

        this.isbn = isbn;
    
Methods Summary
public java.lang.StringgetIsbn()

        return isbn;
    
public intgetLastRanking()

        return lastRanking;
    
public intgetLastReviews()

        return lastReviews;
    
public intgetRanking()

        return ranking;
    
public intgetReviews()

        return reviews;
    
public java.lang.StringgetTitle()

        return title;
    
public voidsetFromInputStream(java.io.InputStream is)

        // Use an InputHelper to search the input
        InputHelper helper = new InputHelper(is);
        try {

            // Default new values to current values
            int newRanking = this.ranking;
            int newReviews = this.reviews;

            boolean found = helper.moveAfterString("buying info: ");
            if (!found) {
                return;
            }

            // Gather the title from the rest of this line
            StringBuffer titleBuffer = helper.getRestOfLine();

            // Look for the number of reviews
            found = helper.moveAfterString("Based on ");
            if (!found) {
                return;
            }

            // Gather the number of reviews from the current location
            String reviewString = helper.gatherNumber();

            // Look for the sales rank
            found = helper.moveAfterString("Sales Rank: ");
            if (!found) {
                return;
            }

            // Gather the number from the current location
            String rankingString = helper.gatherNumber();

            // Having safely found everything, set the new title
            title = titleBuffer.toString().trim();

            // Now convert the reviews and ranking to integers.
            // If they fail to convert, just leave the existing
            // values.
            try {
                newRanking = Integer.parseInt(rankingString);
            } catch (NumberFormatException ex) {
            }

            if (newRanking != ranking) {
                lastRanking = ranking;
                ranking = newRanking;
                if (lastRanking == 0) {
                    // First time, set last and current
                    // to the same value
                    lastRanking = ranking;
                }
            }

            try {
                newReviews = Integer.parseInt(reviewString);
            } catch (NumberFormatException ex) {
            }

            if (newReviews != reviews) {
                lastReviews = reviews;
                reviews = newReviews;
                if (lastReviews == 0) {
                    // First time, set last and current
                    // to the same value
                    lastReviews = reviews;
                }
            }
        } catch (IOException ex) {
        } finally {
            // Allow garbage collection
            helper.dispose();
            helper = null;
        }