Hi friends,
I m trying to write a program in Java to manipulate my personal collection of books.
Below is the code I have writen till now. If you have some ideas?

package Books;
import java.util.*;

class Book{
    private int isbn;
    private String title;
    private static int count=0;
    
    public Book(){
    	count ++;
    	}
	public void readBook(){
	
	int isbn=0;
	Scanner input = new Scanner(System.in);
    String temp;
    	
    	Book book = new Book();	
	    System.out.print("ISBN: ");
		temp=input.nextLine();
		book.setIsbn(Integer.parseInt(temp));
	    
	    System.out.print("title: ");
	    String title = input.nextLine();
	    book.setTitle(title);
    }
    public int getCount(){
    	return count;
    }
    public void incrCount(){
    	this.count++;
    }
    public int getIsbn() {
        return isbn;
    }

    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }   
}
package Books;
import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException, NumberFormatException{
        
        Scanner input = new Scanner(System.in);
        Book book = new Book();
        int choice;
        Book[] books = new Book[10];
        int currentIndex;
        String temp;
        
       
	    
        while(true){
        do{
            System.out.println("Library System");
            System.out.println("       1. add book");
            System.out.println("       2. display books");
            System.out.println("       3. save to file");
            System.out.println("       4. load from file");
            System.out.println("       0. exit");
            System.out.print("choice: ");
            temp = input.nextLine();
            choice = Integer.parseInt(temp);
        }while ((choice < 0) || (choice > 4));
        
        switch (choice){
            
            case 1: //read isbn, title
            		currentIndex=book.getCount();
            		book.readBook();
                    //add current book to the array
                    books[currentIndex+1] = book;
                    book.incrCount();
                    break;
            case 2: //display array of books
            		currentIndex=book.getCount();
                    System.out.println("ISBN         Title   ");
                    for(int i=0; i< currentIndex; i++){
                    System.out.println(books[i].getIsbn()+"  "+books[i].getTitle()+"  ");
                    }
                    break;
            case 3: break;
            case 4: break;
            case 0: return ;
            
        }
        }//end while true
       }
}

Recommended Answers

All 3 Replies

i have some similar code for a cd collection. make 1 class for the collection, and another for the disk (in your cae a book)

your code looks good so far, just try and get all the basic repetitive parts done first. constructors, mutators, acessors

and then work on the more advanced methods

commented: Haven't seen you around in awhile +8

An idea? Use JDBC to store book information then retrieve it later.

Files are ok, but it might be easier to query necessary information than try to make a dozen different methods for obtaining books in certain order, or certain books period.

before I start reading that code ... is there something that doesn't work? and what are you trying to acchieve? my idea of buying a new pair of socks is still an idea I got, but I kinda doubht it'll help your project much further

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.