java program for library

can't find class Book, Booklist and also an error after compilin b

Page 1 of 1

4 Replies - 2012 Views - Last Post: 09 October 2008 - 05:15 AM Rate Topic: -----

#1 collegedropout2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-October 08

java program for library

Posted 08 October 2008 - 09:36 PM

public class Book{

    private int numOfBooks=0;

	private String isbn;
	private char type;
	private String status;
	private String author;
	private String publisher;
	private String dateBorrowed;
	private String title;
	
	
	
  public Book (String i,String t, char ty, String s, String a, String p,String d){
			isbn=i;
			title=t;
			type=ty;
			status=s;
			author=a;
			publisher=p;
			d=dateBorrowed;
			
	             }

   public String getIsbn(){
  	return isbn;
  }
  
  public char getType(){
  	return type;
  }
  
 
  public String getStatus(){
  	return status;
  }
  
  public String getAuthor(){
  	return author;
  }
  
  public String getPublisher(){
  	return publisher;
  }
  
  public String getdateBorrowed(){
  	return dateBorrowed;
  }
  
  public String getTitle(){
  	return title;
  }
  
  public int getNumOfBooks(){
  	return numOfBooks;
  }
 }
public class BookManager {
  
  public static Book[]booklist = new Book [1000];
  public static book i;
  
  
  public static void addBook (book i ){
		int numOfBooks = i.getNumOfBooks();
		  Book[numOfBooks - 1] = i;
		  
		  int lo = 0;
		  int hi = numOfBooks - 1;
		  
	    for (int j = lo + 1;j<=hi;j++){
			book hold = Booklist [j];
			int k = j-1;
			
		    while ( k >=0 && hold.getNumOfBooks()<Booklist[k].getNumOfBooks()){		    
		    Booklist[k + 1] = Booklist[k];
                  --k;
			}//end while
		booklist[k+1] = hold;
		}//end for
		
	}//end addBook
  }


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 08 October 2008 - 09:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java program for library

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java program for library

Posted 08 October 2008 - 09:47 PM

So tonight is the night for this.... second post in 10 minutes: :)

And the question is ?
- your code does not compile ? (Post error messages)
- code compiles but I have a run time error (Post error message)
- program runs but does not have the expected behaviour (post actual and expected behaviour)

P.S.
Thanks Martyr for the editing
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java program for library

Posted 08 October 2008 - 09:53 PM

At first glance:

- you have an extra } that closes Book class too fast
- BookManager is an inner class... inner class cannot have static methods or static members

Put your BookManager in a seperate file: BookManager.java and it should work
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: java program for library

Posted 08 October 2008 - 10:04 PM

Well I think your curly braces are right, you just have case sensitivity issues, a few missing spaces, a syntax problem and then this while loop that is completely screwed up.

Remember that Java is a case sensitive language so you can't call a variable "Booklist" and then use it as "booklist" those are two different types of variables. Same with "Book" and "book" are two variables. You also had two classes defined together, so you can't prefix them with "public" if they are in the same file. You should get in the habit of putting a class in its own file but anyways...

Here is your code to the point of compiling. I will leave the little task for you of figuring out what you are doing with those for/while loops in the addBook method...

class Book{

    private int numOfBooks=0;

	private String isbn;
	private char type;
	private String status;
	private String author;
	private String publisher;
	private String dateBorrowed;
	private String title;
	
	
	
  public Book (String i,String t, char ty, String s, String a, String p,String d){
			isbn=i;
			title=t;
			type=ty;
			status=s;
			author=a;
			publisher=p;
			d=dateBorrowed;
			
	             }

   public String getIsbn(){
  	return isbn;
  }
  
  public char getType(){
  	return type;
  }
  
 
  public String getStatus(){
  	return status;
  }
  
  public String getAuthor(){
  	return author;
  }
  
  public String getPublisher(){
  	return publisher;
  }
  
  public String getdateBorrowed(){
  	return dateBorrowed;
  }
  
  public String getTitle(){
  	return title;
  }
  
  public int getNumOfBooks(){
  	return numOfBooks;
  }
 }
 
class BookManager {
  
  public static Book[] Booklist = new Book [1000];
  public static Book i;
  
  
  public static void addBook (Book i ){
		int numOfBooks = i.getNumOfBooks();
		Booklist[numOfBooks] = i;
		  
	    for (int j = 0;j<numOfBooks;j++){
			//Book hold = Booklist[j];
			//int k = j-1;
			
			// Not sure what this is suppose to be doing... are you trying to sort? If so, this is not right at all.
		    //while ( k >=0 && hold.getNumOfBooks()<Booklist[k].getNumOfBooks()){		    
			//	Booklist[k + 1] = Booklist[k];
            //    --k;
			//}//end while
			
			//Booklist[k+1] = hold;
		}//end for
		
	}//end addBook
  }

public class library {
	public static void main(String args[]) {
		BookManager manager = new BookManager();
		Book dabook = new Book("123-444","The Raven",'P',"Checked Out","Edgar Allen Poe","publisher","2008-10-08");
		
		manager.addBook(dabook);
	}
}



Notice the changes I made by removing the "public" from the front of your two classes, the correction of capitalization on many of your variables, the commenting and changing of your addBook method, and added a quick example of main at the end.

Enjoy!

"At DIC we be library code ninjas...we kill the enemy, but only when they are standing over at the card catalog" :snap:

This post has been edited by Martyr2: 08 October 2008 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 collegedropout2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-October 08

Re: java program for library

Posted 09 October 2008 - 05:15 AM

while ( k >=0 && hold.getNumOfBooks()<Booklist[k].getNumOfBooks()){
// Booklist[k + 1] = Booklist[k];
// --k;
//}//end while

//Booklist[k+1] = hold;
}//end for

}//end addBook
This is used in an insertinto array to add a book i think the while is used to compare strings.





View PostMartyr2, on 8 Oct, 2008 - 10:04 PM, said:

Well I think your curly braces are right, you just have case sensitivity issues, a few missing spaces, a syntax problem and then this while loop that is completely screwed up.

Remember that Java is a case sensitive language so you can't call a variable "Booklist" and then use it as "booklist" those are two different types of variables. Same with "Book" and "book" are two variables. You also had two classes defined together, so you can't prefix them with "public" if they are in the same file. You should get in the habit of putting a class in its own file but anyways...

Here is your code to the point of compiling. I will leave the little task for you of figuring out what you are doing with those for/while loops in the addBook method...

class Book{

    private int numOfBooks=0;

	private String isbn;
	private char type;
	private String status;
	private String author;
	private String publisher;
	private String dateBorrowed;
	private String title;
	
	
	
  public Book (String i,String t, char ty, String s, String a, String p,String d){
			isbn=i;
			title=t;
			type=ty;
			status=s;
			author=a;
			publisher=p;
			d=dateBorrowed;
			
	             }

   public String getIsbn(){
  	return isbn;
  }
  
  public char getType(){
  	return type;
  }
  
 
  public String getStatus(){
  	return status;
  }
  
  public String getAuthor(){
  	return author;
  }
  
  public String getPublisher(){
  	return publisher;
  }
  
  public String getdateBorrowed(){
  	return dateBorrowed;
  }
  
  public String getTitle(){
  	return title;
  }
  
  public int getNumOfBooks(){
  	return numOfBooks;
  }
 }
 
class BookManager {
  
  public static Book[] Booklist = new Book [1000];
  public static Book i;
  
  
  public static void addBook (Book i ){
		int numOfBooks = i.getNumOfBooks();
		Booklist[numOfBooks] = i;
		  
	    for (int j = 0;j<numOfBooks;j++){
			//Book hold = Booklist[j];
			//int k = j-1;
			
			// Not sure what this is suppose to be doing... are you trying to sort? If so, this is not right at all.
		    //while ( k >=0 && hold.getNumOfBooks()<Booklist[k].getNumOfBooks()){		    
			//	Booklist[k + 1] = Booklist[k];
            //    --k;
			//}//end while
			
			//Booklist[k+1] = hold;
		}//end for
		
	}//end addBook
  }

public class library {
	public static void main(String args[]) {
		BookManager manager = new BookManager();
		Book dabook = new Book("123-444","The Raven",'P',"Checked Out","Edgar Allen Poe","publisher","2008-10-08");
		
		manager.addBook(dabook);
	}
}



Notice the changes I made by removing the "public" from the front of your two classes, the correction of capitalization on many of your variables, the commenting and changing of your addBook method, and added a quick example of main at the end.

Enjoy!

"At DIC we be library code ninjas...we kill the enemy, but only when they are standing over at the card catalog" :snap:



View Postpbl, on 8 Oct, 2008 - 09:53 PM, said:

At first glance:

- you have an extra } that closes Book class too fast
- BookManager is an inner class... inner class cannot have static methods or static members

Put your BookManager in a seperate file: BookManager.java and it should work


i have bookmanager in a separate java file i jus copied and paste the both. i need help getting the array to work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1