I have two classes.CyberLibraray and CyberBook
These are the requirements:

Design a CyberLibrary class which houses CyberBooks. Because of local laws, the CyberLibrary
class has a limitation: the maximum number of books is 200.
The CyberLibrary has the following constructors:
Constructor #1: the default - Creates a CyberLibrary with the name: "BuffaloCyberLibrary"
which holds 150 CyberBooks.
Constructor #2: A constructor with two parameters: a String which is the name of the newly
created CyberLibrary and an int parameter which is the maximum number of CyberBooks that the
CyberLibrary can hold. If the number of books exceeds the maximum allowed, a default of 100 is
created . An error flag is set for later retrieval of this error.
The CyberLibrary has the following mutator methods:
Method #1: boolean addBook(CyberBook) - adds the CyberBook to the CyberLibrary
collection (finds the first free available space on the "bookshelf").
returns: a) true if book can be shelved
b) false if the CyberLibrary is full
The CyberLibrary has the following accessor methods:
Method #1: CyberBook Checkout( ), this method is used to checkout a random book
returns: a) any available book removed from the Library's bookshelf
b) null, if there are no more books available
Method #2: CyberBook findBookTitle( String title ), finds a CyberBook by title
returns: a) a CyberBook that matches title (and removed from the bookshelf)
b) null, if the book in not in the library
Method #2: CyberBook findBookISBN ( int ISBN ) - finds a CyberBook by ISBN.
returns: a) a CyberBook that matches the ISBN (and removed from the bookshelf)
b) null, if the book in not in the library
Method #3: boolean returnError( ), returns the value of the error flag
Method #4: int getFreeSpace( ), returns the number of free spaces on the library's bookshelf
Create a main( ) to test your program. The main ( ) should create a CyberLibrary named
"CyberButler" with space for 125 CyberBooks. Then create and add three CyberBooks with the
following titles and ISBNs [ {Java1, 1234}, { Java2, 3423}, {AdvJava1, 1298} ]. Test the methods:
findBookTitle( ) and findBookISBN( ) to see if they work properly (as an example, find the Java2
book). Print appropriate messages to the screen indicating the success/failure. Also try other tests to
see if all your methods work properly.
You must write a complete class, including constructors, all instance variables, and methods (as
detailed above). Use the appropriate access/visibility types, data types, and program logic.
This is what I have so far.

package cyberlibrary;


/**
 *
 * @author JoeLaptop
 */
public class CyberLibrary
{

   private String LibraryName;
   private static int MaxBooks;
   
   public CyberLibrary()
    {
        LibraryName = "BufaloCyberLibrary";
        MaxBooks = 150;
    }
   public CyberLibrary(String NewName,int NewMax)
    {
       System.out.println("Number " + MaxBooks);
       LibraryName = NewName;
        if(NewMax > 200)
           MaxBooks = 100;
           
        else
           MaxBooks = NewMax;
        System.out.println("Number " + MaxBooks);
    }
   public boolean addBook(String title,int isbn){
       if(MaxBooks >= 200)return false;
       else
       MaxBooks = MaxBooks + 1;
       CyberBook book1 = new CyberBook(title,isbn);

       System.out.println("Book " + book1.getTitle( ));
       System.out.println("Number " + MaxBooks);
       System.out.println(LibraryName);
       return true;
   }
   //CyberBook Checkout(){
     //  if(MaxBooks != 0)
       //    return CyberLibrary.this;
   //}
   public CyberBook findBookTitle(String title){
       Object found;

       CyberBook book1 = new CyberBook();
        found = book1.getTitle();
       System.out.println("Book Title1 " + found);
       


       return (CyberBook) found;
}

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        CyberLibrary CyberName1 = new CyberLibrary("CyberButler", 125);        
        CyberName1.addBook("Java1",1234);      
        CyberName1.addBook("Java2",3423);
        CyberName1.addBook("AdvJava1",1298);

        System.out.println("Book Title2 " + CyberName1.findBookTitle("Java1"));
        

   }// main
}// CyberLibrary

And

package cyberlibrary;

public class CyberBook  {

	private String Title;
	private int ISBN;
        
        public CyberBook() {

	}
	// constructor
	public CyberBook( String title, int isbn ) {
		Title=title;
		ISBN=isbn;
            }

    	public String getTitle() {
                return Title;
	}

	public int getISBN( ) {
		return ISBN;
	}
}

I need help with getting a return value for:
public CyberBook findBookTitle(String title)
It complies but it returns a null.
Here is my output:
run:
Number 0
Number 125
Book Java1
Number 126
CyberButler
Book Java2
Number 127
CyberButler
Book AdvJava1
Number 128
CyberButler
Book Title1 null
Book Title2 null
BUILD SUCCESSFUL (total time: 0 seconds)

This is a work in progress so some of the code is for troubleshooting.
Any help would be appreciated, I have already spent over 48 hrs on this, thanks.
I am using NetBeans 6.9.1 for editing.

Recommended Answers

All 4 Replies

First off you are not saving any of the CyberBooks you create in the method addBook(). You need a data structure to keep track of the added books, otherwise the findBookTitle() will be impossible to implement.

There is also a few lines of code in there that I dont really understand the logic behind, but your main focus now should be on fixing a data structure to hold your CyberBook objects.

I think I have done that with this revised code(used an arraylist).One problem now is I cannot get it to print my arraylist. And I still cannot get
public CyberBook findBookTitle(String title)
to work.

import java.util.ArrayList;
import java.util.List;

public class CyberLibrary
{
   private List<CyberBook> books = new ArrayList<CyberBook>();
   private List<CyberBook> bookid = new ArrayList<CyberBook>();
   private static String LibraryName;
   private static int MaxBooks;
   public static final int MaxNumber = 200;
  
   public CyberLibrary()// Constructor #1
    {
        LibraryName = "BufaloCyberLibrary";
        MaxBooks = 150;
    }
   public CyberLibrary(String NewName,int NewMax)//Constructor #2
    {
       System.out.println("Number " + MaxBooks);
       CyberLibrary.LibraryName = NewName;
        if(NewMax > MaxNumber)
           CyberLibrary.MaxBooks = 100;
           
        else
           CyberLibrary.MaxBooks = NewMax;
        System.out.println("Number " + MaxBooks);
    }
   
   public boolean addBook(String title,int isbn)//Mutator Method#1
   {

       if(MaxBooks >= MaxNumber)return false;
       else
       MaxBooks = MaxBooks + 1;
       
       CyberBook book1 = new CyberBook(title,isbn);
       books.add(book1);
      // CyberBook ID1 = new CyberBook(isbn);
      // books.add(ID1);
             
       System.out.println("Book " + book1.getTitle( ));
       System.out.println("Number " + MaxBooks);
       System.out.println(LibraryName);
       return true;
   }
 /**  public CyberBook Checkout()//Accessor Method #1
   {
           if(MaxBooks != 0)
               
          return CyberLibrary.getFreeSpace();
   */
   public CyberBook findBookTitle(String title)//Accessor Method #2
    {
       if( books.contains(title) ){
          System.out.println("ArrayList contains 1 as value");        
        }else{        
          System.out.println("ArrayList does not contain 1 as value");        
        }

       System.out.println("Book Title1 " + books);
       return null;
    }
   public CyberBook findBookISBN(int ISBN)//Accessor Method #2
    {
       if( books.contains(title) ){
          System.out.println("ArrayList contains 1 as value");        
        }else{        
          System.out.println("ArrayList does not contain 1 as value");        
        }

       System.out.println("Book Title1 " + books);
       return null;
    }
   public static int getFreeSpace()//Accessor Method #4
   {
       return MaxNumber - MaxBooks;
   }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        CyberLibrary CyberName1 = new CyberLibrary("CyberButler", 125);
        
        CyberName1.addBook("Java1",1234);
        CyberName1.addBook("Java2",3423);
        CyberName1.addBook("AdvJava1",1298);

        //CyberBook book1 = new CyberBook();

        //System.out.println("Book1 " + book1.getTitle( ));
        System.out.println("Number1 " + MaxBooks);
        CyberName1.findBookTitle("Java1");
        System.out.println("Shelves left for "+ LibraryName + " is "
               + getFreeSpace());

   }// main
}// CyberLibrary

And

public class CyberBook  {

	public String Title;
	public int ISBN;
        
       	// constructor
	public CyberBook( String title, int isbn ) {
		
                this.Title=title;
		this.ISBN=isbn;
                //System.out.println("CBook1 " + Title + " " + ISBN);
            }

    	public String getTitle() {
            //System.out.println("CBook2 " + Title + " " + ISBN);
            
                return Title;
	}

	public int getISBN( ) {
		return ISBN;
	}
}

Well, In your findBookTitle you are checking if the ArrayList contains the String name. But your ArrayList consists of CyberBook objects so of course its not going to find a match.

Could you please provide an example of how to search the Arraylist?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.