12 Replies - 777 Views - Last Post: 01 March 2011 - 03:13 AM Rate Topic: -----

#1 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

LibraryManager class homework help

Posted 28 February 2011 - 02:05 AM

Hey guys, I have tried going at this alone but this looks to be the first assignment this semester that has me completely stumped. First of all, a bit of an explanation of what the program is supposed to do: it is an application to be ran on a console and its to manage a collection of books in a library. My first problem with this program is that i am supposed to add a feature in which each book in the library is supposed to have a unique identifier, which is a libraryID#. Now, onto my problem, im not really sure how to go about doing this, I cant seem to be able to think on how to assign a unique number to each book. Also, in the LibraryManager class, there are the addBook() methods in which every time i add a book, a unique number is added to that book as well. Ill show you guys what i have so far and see if i am on the right track or not, any comments, help or questions are welcome of course.

This is the Librarybook class, this class pretty much is the book, and all the information pertaining to the book. This is where the libraryID# is supposed t be assigned to each book:
/** A class that represents a single book. For every book it maintains
 *  title, author, number of pages and ISBN number.
 */
class LibraryBook
{
    // The fields.
    private String author;
    private String title;
    private int pages;
    private String isbn;
    private int libraryID = 1001;
    private boolean borrowStatus;
    private int timesBorrowed;

    public LibraryBook(String bookAuthor, String bookTitle, int bookpages, String isbn) {
        this.libraryID = libraryID;
        this.borrowStatus = borrowStatus;
        author = bookAuthor;
        title = bookTitle;
        pages = bookpages;
        this.isbn = formatISBN(isbn);
    }
    
    public LibraryBook(String bookAuthor, String bookTitle, int bookpages) {
        this.libraryID = libraryID;
        this.borrowStatus = borrowStatus;
        author = bookAuthor;
        title = bookTitle;
        pages = bookpages;
        this.isbn = "";
    }


**code omitted for post length purposes, does not pertain to what im trying to solve**
    public void assignLibraryID()
    {
        libraryID++;
    }
    
    public int getLibraryID()
    {
        return libraryID;
    }


Now, this is the LibraryManager class which is what can add, borrow, return, and show statistics on the library. This is where i assume the libraryID# could be assigned in the addBook() methods, which is what i was trying to do:
/**
 * This class will add/remove, find, and loan books. It will also provide 
 * a list of all books in the collection and of all books on loan.
 */
public class LibraryManager
{
    private ArrayList<LibraryBook> books = new ArrayList<LibraryBook>();

    /**
     * Constructor for objects of class LibraryManager
     */
    public LibraryManager()
    {
        
    }

    /**
     * Add a book
     * 
     * @param book   a book object from the LibraryBook class
     * @return the library ID# of the newly added book
     */
    public int addBook(LibraryBook book)
    {
        books.add(book);
        return book.getLibraryID();
    }
    
    /**
     * Add a book 
     * 
     * @param isbn this is to be put as the String of an isbn
     * @return the library ID# of the newly added book
     */
    public int addBook(String author, String title, int pages, String isbn)
    {
        books.add(new LibraryBook(author,title,pages,isbn));
        int libID = LibraryBook.libraryID;
        return libID;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: LibraryManager class homework help

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: LibraryManager class homework help

Posted 28 February 2011 - 03:43 AM

This is a perfect use for a static variable. Each time you make a new LibraryBook object, set the current id and then increase the static variable.

class LibraryBook
{
    private static int id = 0;

    // The fields.
    ...
    private int bookID;

    public LibraryBook(String bookAuthor, String bookTitle, int bookpages, String isbn) {
        ...
        bookID = id++;
    }


Was This Post Helpful? 1
  • +
  • -

#3 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: LibraryManager class homework help

Posted 28 February 2011 - 11:57 AM

View PostDogstopper, on 28 February 2011 - 03:43 AM, said:

This is a perfect use for a static variable. Each time you make a new LibraryBook object, set the current id and then increase the static variable.

class LibraryBook
{
    private static int id = 0;

    // The fields.
    ...
    private int bookID;

    public LibraryBook(String bookAuthor, String bookTitle, int bookpages, String isbn) {
        ...
        bookID = id++;
    }


Would that mean then that every time a book is created it would have a unique id (for example, the id's need to start at the number 1001, every time I create a book, would that number go up and be stored in that book to be able to identify it?)? Wouldn't this just reset the count?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: LibraryManager class homework help

Posted 28 February 2011 - 03:56 PM

Nope. A static variable is object-independent. Once created, it is the SAME value for all objects. The FIRST value is 0, but every time a new object is made, the value increases by 1. Since it's object-independent, all objects can access the same value.
Was This Post Helpful? 2
  • +
  • -

#5 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: LibraryManager class homework help

Posted 28 February 2011 - 04:34 PM

Read my blog post regarding static members if you want to read more about them. :)
Was This Post Helpful? 0
  • +
  • -

#6 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: LibraryManager class homework help

Posted 28 February 2011 - 11:55 PM

How would i go about returning the library ID of the newly added book int this method in LibraryManager then? Any ideas?

public int addBook(String author, String title, int pages, String isbn)
    {
        books.add(new LibraryBook(author,title,pages,isbn));
        return ;
    }


BTW, thank you guys so much for th ehelp and for the explanation of how static variables work.

This post has been edited by halopower67: 28 February 2011 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: LibraryManager class homework help

Posted 01 March 2011 - 12:32 AM

View Posthalopower67, on 28 February 2011 - 11:55 PM, said:

How would i go about returning the library ID of the newly added book int this method in LibraryManager then? Any ideas?

public int addBook(String author, String title, int pages, String isbn)
    {
        books.add(new LibraryBook(author,title,pages,isbn));
        return ;
    }


BTW, thank you guys so much for th ehelp and for the explanation of how static variables work.


Do you guys think, this would be a viable solution? or would i have problems in the future with this method?
public int addBook(String author, String title, int pages, String isbn)
    {
        LibraryBook book = new LibraryBook(author,title,pages,isbn);
        books.add(book);
        return book.getLibraryID();
    }

This post has been edited by halopower67: 01 March 2011 - 12:33 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: LibraryManager class homework help

Posted 01 March 2011 - 02:36 AM

If you make the static variable non-private, you could actually access it like so:
return LibraryBook.id;


Because it is class-specific and not object-specific, you can do that.
Was This Post Helpful? 0
  • +
  • -

#9 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: LibraryManager class homework help

Posted 01 March 2011 - 02:40 AM

View PostDogstopper, on 01 March 2011 - 02:36 AM, said:

If you make the static variable non-private, you could actually access it like so:
return LibraryBook.id;


Because it is class-specific and not object-specific, you can do that.

Hmm interesting.. That could simplify the code a bit.
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: LibraryManager class homework help

Posted 01 March 2011 - 02:51 AM

Also, with your method, it could count unnecessarily because you'd be making new objects just to get the value, but by making new objects, you actually increase the value.
Was This Post Helpful? 0
  • +
  • -

#11 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: LibraryManager class homework help

Posted 01 March 2011 - 02:54 AM

View PostDogstopper, on 01 March 2011 - 02:51 AM, said:

Also, with your method, it could count unnecessarily because you'd be making new objects just to get the value, but by making new objects, you actually increase the value.

And could making the static variable public be a solution for this? Because wouldn't that mean that I font have to create a new object every time?
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: LibraryManager class homework help

Posted 01 March 2011 - 03:10 AM

Or:
public int addBook(String author, String title, int pages, String isbn) {
	return addBook(new LibraryBook(author,title,pages,isbn));
}



Isn't operator overloading fun?

I'd actually approach this somewhat differently. You have two kinds of book objects going on here: LibraryBook and just plain ole Book. There are a few attributes only belonging to the library copy...

e.g.
class Book {
	private String author, title, isbn;
	private int pages;

	public Book(String author, String title, int pages, String isbn) {
		// this.libraryID = libraryID;
		// this actually didn't do anything, btw
		// this.borrowStatus = borrowStatus;
		this.author = author;
		this.title = title;
		this.pages = pages;
		// this.isbn = (isbn==null || "".equals(isbn)) ? "" : formatISBN(isbn);
	}
    
	public Book(String author, String title, int pages) { this(author, title, pages, null); }
	
	public Book(Book book) { this(book.author, book.title, book.pages, book.isbn); }
	//...
}


public class LibraryManager {
	public class LibraryBook extends Book { 
		private int libraryID;
		private boolean borrowStatus;
		private int timesBorrowed;
		
		protected LibraryBook(int libraryID, Book book) {
			super(book);
			this.libraryID = libraryID;
		}
		
		public int getLibraryID() { return libraryID; }
	}
	
	private List<LibraryBook> books;
	
	public LibraryManager() {
		books = new ArrayList<LibraryBook>();
	}

	public int addBook(Book book) {
		int libraryID = books.size();
		books.add(new LibraryBook(libraryID, book));
		return libraryID;
	}
    
	public int addBook(String author, String title, int pages, String isbn) {
		return addBook(new Book(author,title,pages,isbn));
	}

}



Now the LibraryManager controls the id based on what it has. That id is also an index to its internal list, so book lookup is trivial. There are a few places you can go from here. However, by giving the manager control of the id, you needn't use static at all. If you haven't covered static, then this is probably more what the assignment had in mind.

If you implement equals in Book, then if you are given the same one twice, you can find it in you internal list and just increment the number of copies the library has...

LibraryBook needn't actually be a child of book. I'd prefer it be a wrapper with maybe a public book interface? Yeah, I use manager and factory patterns a lot. :P

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: LibraryManager class homework help

Posted 01 March 2011 - 03:13 AM

Great idea there baavgai. Gotta love managers and factory patterns.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1