java program for library

making 7 java files to control the database of a library

Page 1 of 1

5 Replies - 7150 Views - Last Post: 12 October 2008 - 10:21 PM 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 09 October 2008 - 07:09 PM

A library is maintainin the database of books. so far i made the book and bookmanager java files separately but in compiling the bookmanager i am experiencing trouble in the addBook insertion sort. Can i get some help to fix this problem. the public class book and public class bookmanager are two java files.

public class BookManager {
  
  private static final int Max = 1000;
  private static Book[]bookList= new Book[Max];
  
  
  public static void addBook (Book i){
		int NumBooks = Book.GetNumBooks();
		  bookList[NumBooks - 1] = i;
		  
		  int lo = 0;
		  int hi = NumBooks - 1;
		  
		for (int j = lo + 1;j<=hi;j++){ 
			Book hold = bookList [j];
			int k = j-1;
			
		   while( k >=0 && hold.isbn.compareToIgnoreCase(bookList[k].isbn)<0){			
			bookList[k + 1] = bookList[k];
				  --k;
			}
		bookList[k+1] = hold;
		}//end for
		
	}//end addBook
	
	
  }


public class Book{

	public static int NumBooks=0;

	public 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;
			dateBorrowed=d;
			
				 }

   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 GetNumBooks(){
	  return NumBooks;
  }
  public void setNumBooks(int NumBooks){
		NumBooks--;
   }
 }


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: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: java program for library

Posted 09 October 2008 - 07:21 PM

Are you sure that:

  public static void addBook (Book i){
		int NumBooks = Book.GetNumBooks();
		  bookList[NumBooks - 1] = i;



shouldn't be

  public static void addBook (Book i){
		int NumBooks = i.GetNumBooks();
		  bookList[NumBooks - 1] = i;



Book is the name of the class but "i" is the actual Book object from which you want to GetNumBooks()
Was This Post Helpful? 1
  • +
  • -

#3 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 - 09:28 PM

Why when i compile and run does an error saying "Exception in thread "main" java.lang.NoSuchMethodError:main - is there some java files are missing because I still have some java files to construct or is it something incorrect I am doing?

public class Book{

	public static int NumBooks=0;

	public String isbn;
	private String title;
	private char type;
	private String status;
	private String author;
	private String publisher;
	private String dateBorrowed;
	
	
	
	
  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;
			dateBorrowed=d;
			
				 }

   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 GetNumBooks(){
	  return NumBooks;
  }
  public void setNumBooks(int NumBooks){
		NumBooks--;
   }
   
	public String toString(){
   	String str;
   	str = "\nIsbn:" +isbn+ "\nTitle:" +title+ "\nType:" +type+ "\nStatus:" +status+ "\nAuthor:" +author+ "\nPublisher:" +publisher+ "\nDate Borrowed" +dateBorrowed+"\n";
   	return str;
   }//end String
 }


public class BookManager {  
  private static final int Max = 1000;
  private static Book[]bookList= new Book[Max];		
		
		public static void addBook (Book i){
		int NumBooks = i.GetNumBooks();
		  bookList[NumBooks - 1] = i;
		  
		  int lo = 0;
		  int hi = NumBooks - 1;
		  
		for (int j = lo + 1;j<=hi;j++){ 
			Book hold = bookList [j];
			int k = j-1;
			
		   while( k >=0 && hold.isbn.compareToIgnoreCase(bookList[k].isbn)<0){			
			bookList[k + 1] = bookList[k];
				  --k;
			}
		bookList[k+1] = hold;
		}//end for
		
	}//end addBook
	
	
	 public static Book getBook (Book i){
	 int lo = 0;
	 int hi = bookList.length-1;
 
	 while(lo <= hi){
	 int mid = (lo + hi)/2;
	 int s = mid;
	 
	 if (s==0){
		return bookList[mid];
	 }//end if
	 if(s<0){
		mid = hi - 1;
	 }
	 else{
	 mid = lo + 1;
		 }
	}//end while

	 return null; 
   }//end GetBook
  
  public static void dropBook (Book i){
		int j;
		for (j=0;j<i.GetNumBooks();j++){
			if (bookList[j].GetIsbn().equals(i)){
				i.setNumBooks(j);	 
			 break;
			 } 
			 if (j== i.GetNumBooks()){
			 
			System.out.println ("Book does not exist.");	 
		}
		
		for (int k=j;k<i.GetNumBooks();k++){
			bookList[k]=bookList[k+1];
		}//end for		

	}//end for
   }// end dropBook  
  
  }



Please post your code like this
:code:
*Edited to add the code tags

This post has been edited by pbl: 09 October 2008 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: java program for library

Posted 09 October 2008 - 09:38 PM

When you do at the console prompt:

>java Abc

java tries to execute the method:

public static void main(String[] arg)

in Abc.class compiled from Abc.java

there is obviously to main() method in your code neither in Book neither in BookManager
I don't know which one should drive the other one but you need an entry point to start with somewhere
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 12 October 2008 - 09:11 PM

View Postpbl, on 9 Oct, 2008 - 09:38 PM, said:

When you do at the console prompt:

>java Abc

java tries to execute the method:

public static void main(String[] arg)

in Abc.class compiled from Abc.java

there is obviously to main() method in your code neither in Book neither in BookManager
I don't know which one should drive the other one but you need an entry point to start with somewhere




public class BorrowerManager {
private static final int Max = 1000;
private static Borrower[]borrowerList= new Borrower[Max];

public static void addBorrower (Borrower B){
int NumBorrowers = b.GetID();
borrowerList[NumBorrowers - 1] = b;

int lo = 0;
int hi = NumBorrowers - 1;

for (int j = lo + 1;j<=hi;j++){
Borrower hold = borrowerList [j];
int k = j-1;

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

}//end addBorrower
}


Not sure if this is correct but i need a BorrowManager class.java, Loan.java and Library.java files to run the program but i'm stuck and don't know where to go further. This suppose to be a Java program for a library database but some problems encountered. Code assistance needed. The rest of the code is in earlier posts here
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: java program for library

Posted 12 October 2008 - 10:21 PM

Few designs flaws... you will have to decide how to proceed

I assume status describes if a book is borrowed or not ... and there is only one status for each book
as you have only 1 borrowedDate.

Your BookManager is keeping only one entry for every book... now if you have 15 Robinson Crusoe how eill you distinguish between the one borrowed and the one not borrowed ?

When you add a book, you increment the count of the book you already have but your object Book pass as parameter is lost. So your BookManager would neeed a 2 dimensional array: [1000][10] to hold up to 10 copies of 1000 different books... not a really good design

Your getBook() method is useless... you receive a Book as parameter and you return the same book..

You do not need a Library class that is what it is your BookManager
You do not need a BorrowManager, the BookManager can do that

Honestly, forget about storing all the book with the same ISBN in the same slot just append them to your list. You can deal with that problem later.

You will need a

public int NumBooks=0;

in your BookManager to keep how many slots are used in your array of Books

Here is a modified version of your code... for simplicity I just kept: ISBN, Title and if borrowed

I add the main method in BookManager to test the code:

public class Book{

	public String isbn;
	private String title;
	private boolean borrowed;

	public Book (String isb, String tit){
		isbn=isb;
		title=tit;
		borrowed = false;			// new surely not borrowed
	}

	public String GetIsbn(){
		return isbn;
	}
	public boolean isBorrowed(){
		return borrowed;
	}
	public void setBorrowed(boolean status) {
		borrowed = status;
	}

	public String GetTitle(){
		return title;
	}

	public String toString(){
		String str;
		str = "Isbn:" +isbn+ " Title:" +title+ " Borrowed:" + borrowed;
		return str;
	}//end String
}




public class BookManager {  
	
	private static final int Max = 1000;
	private Book[]bookList= new Book[Max];		
	public  int NumBooks=0;

	// add a book
	public void addBook (Book i){
		bookList[NumBooks++] = i;
	}//end addBook

	// borrow a book
	public Book borrowBook(String isbn) {
		for(int i = 0; i < NumBooks; i++) {
			if(bookList[i].GetIsbn().equalsIgnoreCase(isbn))
			{
				if(bookList[i].isBorrowed() == false) {
					bookList[i].setBorrowed(true);
					return bookList[i];
				}
			}
		}
		// book not found or all borrowed
		return null;
	}

	// return a book
	public void returnBook(Book b) {
		for(int i = 0; i < NumBooks; i++) {
			if(bookList[i] == b)
			{
				bookList[i].setBorrowed(false);
				break;
			}
		}
	}
	
	// list all books
	public String toString() {
		String str = "";
		for(int i = 0; i < NumBooks; i++) {
			str = str + "\n" + (i+1) + ") " + bookList[i].toString();
		}
		return str;
	}

	public static void main(String[] arg) {
		BookManager bm = new BookManager();
		Book book1 = new Book("1234-5678", "My Life");
		Book book2 = new Book("1111-2222", "My wife");
		Book book3 = new Book("3333-4444", "My Dog");
		
		bm.addBook(book1);
		bm.addBook(book2);
		bm.addBook(book3);
		
		System.out.println("Library listing: " + bm);
		
		// try to borrow a book
		Book b = bm.borrowBook("1111-2222");
		System.out.println("Book borrowed: " + b);
		
		System.out.println("Updated book list: " + bm);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1