0

I'm really need help with this one. Im totally lost. I have to crete a class that models a Library. It lets the user add, print, borrow and return books to the Library. I guess my problem is that I really do not understand arrays. I needs to create a array of books in the Lib class. I have to ref the books to a generated ID number in a Book Class. I have code all over the place and I have now confused myself. Can anyone take a look at this and please guide me in the correct direction.

Thanks.

Attachments
import javax.swing.*;
import java.util.*;

public class Library
{
//instance variable;
private static double numOfBooks;
private static double maxBooks;//the most amount of books the Lib can have
private static boolean inLib = true;
private static boolean addNew;//true if book added
static String outputStr;

static Book1[] Books;//Declares array
//for(int i =0; i<Books.length; i++)
//No Arg Constructor for Library class
public Library()
{
//initalise instance variables
   numOfBooks = 0;
    Books = new Book1[10];
}
//Constructor for Library class
public Library(double num)
{
    numOfBooks = num;
  //  Books = bk;
   //do i need to add Library of books
   
}
public boolean isInLib()//Checks to see if books in Lib
{
return inLib;//returns true if book is in the Library already
}

public static boolean addNew()//adds new book to Lib
{
if((!inLib) && (numOfBooks < maxBooks))
{
  Books = new Book1[10];//adds book to Lib?????
  numOfBooks++;
  return addNew = true;
}
else
return addNew = false;//returns fales if condition of both ifs is not meet.
}

public void printLib()//Prints entire LIB
{
//outputStr = a.toString() + "\n" +
  //      b.toString()+ "\n" +
    // c.toString();
        JOptionPane.showMessageDialog(null, Books, "LIBRARY BOOKS",JOptionPane.PLAIN_MESSAGE);
}

public void printBorrowed()//prints only borrowed books
{
    JOptionPane.showMessageDialog(null,Books, "Borrowed Books", JOptionPane.PLAIN_MESSAGE);
    
}

private void findBook(String id)//searches for a book by ID number
{
    }
  
public boolean borrowBook(String id)
{
if(!inLib)
return inLib = false;
else
if(Book1.status)//not borrowed
return inLib = false;
else 
Book1.status = false;
return inLib = true;
}


public static boolean returnBook(String ID)
{
if (!inLib)
return inLib = false;
else
if (Book1.status)
return inLib = false;
else 
return inLib = true;
}
//public double getId()
//{
//return id;
//}

//checks for book equality
//public boolean equals(Library myLibrary)
//{
//eturn (numOfBooks.equalsIgnoreCase (myLibrary.getId()) && isbn==myBook.getISBN() && title==myBook.getTitle());

//}


}//end Library class
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Infarction
0

First question I have is why are you using doubles when you should probably be using ints?

Second question would be why is everything static? Static members belong to the class rather than to a specific object (instance of the class). The way you have it now, all the libraries would have the same books. So make those un-static.

In your second contructor (the one taking num as a parameter), what does num represent? Your code suggests that it's the number of books in the library, but I'd guess that it should be the max number of books for the library. If my guess is correct, your empty constructor should probably call the second constructor with a default value.

inLib doesn't seem like it should be in the Library class. Do you have a separate Book class? I'm assuming you do, since you try to create an array of Book1. The structure here seems a little confused. You probably want a function like public boolean isInLib(Book b) which checks if the specified book is in the library (this'll require some way to compare Book objects). The static variable inLib probably is not what you really want, since you should be checking if a specific book is in the library or not.

Similarly with the variable addNew, it's probably not needed. The method addNew() will always return true or false; however, the method should probably take a Book as a parameter as well (e.g. public boolean addNew(Book b)). And you should add the book in the next unused index rather than at index 10. Coincidentally, this is really easy to find since you have numOfBooks.

the print functions don't look like they're done yet.

findBook() should again take a book as a parameter, and you'll have to iterate over the Book array to see if the parameter matches something in the array.

borrowBook() again should have a book as a parameter. And for the Book1.status, you might want to rename status to borrowed so that it's more apparent what the variable is for.

You have lots of return statements of the form return someVariable = something;. This is bad form in any language, but I'm pretty sure it's not even valid in Java. For future reference, you should set a variable, then return it's value on the next line typically. In this case, as mentioned, you probably don't need these variables.

Some of this probably won't make sense at first, so post back and we'll see where we're at. ;)

[edit:] I'm about to head to bed though, so I'll check back tomorrow when I get some free time...

Edited by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.