Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,100 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,279 people online right now. Registration is fast and FREE... Join Now!




java program for library

 
Reply to this topicStart new topic

java program for library, making 7 java files to control the database of a library

collegedropout2010
post 9 Oct, 2008 - 06:09 PM
Post #1


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 6

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.

CODE
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--;
   }
}
User is offlineProfile CardPM

Go to the top of the page

pbl
post 9 Oct, 2008 - 06:21 PM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 186 times

Dream Kudos: 75
My Contributions


Are you sure that:

CODE

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


shouldn't be

CODE

  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()
User is offlineProfile CardPM

Go to the top of the page

collegedropout2010
post 9 Oct, 2008 - 08:28 PM
Post #3


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 6

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?

CODE

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.gif
*Edited to add the code tags

This post has been edited by pbl: 9 Oct, 2008 - 08:33 PM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 9 Oct, 2008 - 08:38 PM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 186 times

Dream Kudos: 75
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

collegedropout2010
post 12 Oct, 2008 - 08:11 PM
Post #5


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 6

QUOTE(pbl @ 9 Oct, 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




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

public static void addBorrower (Borrower cool.gif{
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
User is offlineProfile CardPM

Go to the top of the page

pbl
post 12 Oct, 2008 - 09:21 PM
Post #6


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 186 times

Dream Kudos: 75
My Contributions


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:

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
}



CODE

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);
    }
}

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 09:19AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month