Results 1 to 7 of 7
  1. #1
    datreta is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Program to manage books

    Hello.

    Been working on a simple program for managing books and I wanted to check if I'm using the right logic.

    The purpose is to insert books (name, author, isbn, year, price and area (if it's math book or algebra book...) and list all books, list books by a given author, list books by a given area and by ISB. It must delete a book by a given title and list authors who have ever written a book.

    I think I need a book class:

    PHP Code:
    public class nodebook {
        private String title;
        private int year;
        private int isbn;
        private double price;
        private nodebook nextbook;
        private nodeauthor firstauthor;
        private nodeBA firstAB;
       
        public nodebook(String t){
            title=t;
        }
    
    	public String gettitle() {
    		return title;
    	}
    
    	public void settitle(String title) {
    		this.title = title;
    	}
    
    	public nodebook getnextbook() {
    		return nextbook;
    	}
    
    	public void setnextbook(nodebook nextbook) {
    		this.nextbook = nextbook;
    	}
    
    	public nodeauthor getfirstauthor() {
    		return firstauthor;
    	}
    
    	public void setfirstauthor(nodeauthor firstauthor) {
    		this.firstauthor = firstauthor;
    	}
    
    	public nodeBA getfirstAB() {
    		return firstAB;
    	}
    
    	public void setfirstAB(nodeBA firstAB) {
    		this.firstAB = firstAB;
    	}
    
    	public int getyear() {
    		return year;
    	}
    
    	public void setyear(int year) {
    		this.year = year;
    	}
    
    	public int getIsbn() {
    		return isbn;
    	}
    
    	public void setIsbn(int isbn) {
    		this.isbn = isbn;
    	}
    
    	public double getprice() {
    		return price;
    	}
    
    	public void setprice(double price) {
    		this.price = price;
    	}
    
    }
    an author class:

    PHP Code:
    package pacote;
    
    public class nodeauthor {
        private String name;
        private nodeauthor nextauthor;
        private nodebook firstbook;
        private nodeba firstba;
       
        public nodeauthor(String n){
            name=n;
        }
    
    	public String getname() {
    		return name;
    	}
    
    	public void setname(String name) {
    		this.name = name;
    	}
    
    	public nodeauthor getnextauthor() {
    		return nextauthor;
    	}
    
    	public void setnextauthor(nodeauthor nextauthor) {
    		this.nextauthor = nextauthor;
    	}
    
    	public nodebook getfirstbook() {
    		return firstbook;
    	}
    
    	public void setfirstbook(nodebook firstbook) {
    		this.firstbook = firstbook;
    	}
    
    	public nodeba getfirstba() {
    		return firstba;
    	}
    
    	public void setfirstba(nodeba firstba) {
    		this.firstba = firstba;
    	}
        
        
    }
    a area node

    PHP Code:
    
    public class NodeArea {
    	
    	  private String area;
    
    	    public NoArea(String a){
    	        area=a;
    	    }
    
    		public String getArea() {
    			return area;
    		}
    
    		public void setArea(String area) {
    			this.area = area;
    		}
    }
    a list

    PHP Code:
    package pacote;
    
    import javax.swing.*;
    
    public class listauthors {
        private Nodeauthor firstauthor;
       
        public listauthores(){
            firstauthor=null;
        }
    
        public void insert(String name){
                Nodeauthor new = new Nodeauthor(name);
                if (firstauthor == null){
                    new.setName(aome);
                    firstauthor=new;
                }
                else {
                    if (firstauthor.getName().compareTo(name) > 0){
                        new.setnextauthor(firstauthor);
                        firstauthor = new;
                    }
                    else {
                        Nodeauthor temp = firstauthor;
                        Nodeauthor previousTemp = null;       
                        while (temp != null && temp.getName().compareTo(name) < 0){
                            previousTemp = temp;
                            temp = temp.getnextauthor();
                        }
                        new.setnextauthor(temp);
                        previousTemp.setnextauthor(new);
                    }
                }
            }
       
        public Nodeauthor search(String name){
            Nodeauthor temp = firstauthor;
            Nodeauthor posA = temp;
            if (temp!=null) {
    
                while (temp.getName().compareTo(name)!=0 && temp.getnextauthor()!= null)
                temp = temp.getnextauthor();
               
                if(temp.getName().compareTo(name)==0){   
     
                    JOptionPane.showMessageDialog(null,"The name " + name + " doesn't exist.");
                    return posA;
                }
            else
                JOptionPane.showMessageDialog(null,"The name " + name + " doesn't exist.");
                return posA;
            }
            else
                JOptionPane.showMessageDialog(null,"empty!");
                return posA;
        }
       
        public void listauthors(){
            Nodeauthor temp = firstauthor;
            String x="";
            while (temp != null){
                x=x+temp.getName()+"\n";
                temp= temp.getnameauthor();
            }
            JOptionPane.showMessageDialog(null, x);       
        }
       
        public void insertBA(NodeBA newBA, Nodeauthor posauthor){
            newBA.setnextBA(posauthor.getfirstBA());
            posauthor.setfirstBA(newBA);   
        }
       
        public void listbooksauthor(){
            Nodeauthor tempauthor = firstauthor;
            while (tempauthor != null){
                JOptionPane.showMessageDialog(null, tempauthor.getName());
                NoBA tempBA = tempauthor.getfirstBA();
                while (tempBA != null){
                    JOptionPane.showMessageDialog(null, tempBA.getoLivro().getTitle());
                    tempBA = tempbA.getProxBA();
                }
                tempauthor = tempauthor.getnextauthor();   
            }
        }
    }
    Do you think I'm on the right path? What is missing?

    Cheers!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Be careful with style. You may as well develop good habits while you're learning. Class names should be singular nouns, and should start with an upper case letter: Book, Author, Area (I prefer Topic, but that's your call), AuthorList. I would let a Book be a Book, and an Author be an Author -- forget NodeBook and its cousins.

    Make sure that your constructors are spelled the same as the class names! That's extremely important, and you've broken that rule at least twice that I can see.

    I know a lot of instructors tell their students to use different names for instance variables and constructor/getter/setter parameters. I disagree with that advice. I think there is generally one good name for a data item, and we should use that name consistently. So if a Book has a title, then call it title everywhere, and don't force users of your class to guess what t means. Of course that means you have to use
    Java Code:
                    this.title = title;
    but I don't see anything wrong with that.

    The other big thing I think you're missing is the wealth of Collections that Java offers. You seem to be reinventing the linked list, when Java has a perfectly good LinkedList class (and an ArrayList too, which might be even better for your library, as I don't see Books as intrinsically linked one to the next).

    -Gary-

  3. #3
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    gcalvin, thank you for your advices. I will keep posting my progress so that I can use your input.

    Regards.

  4. #4
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Hello again.

    I've been working on the program and I've come up with some problems. I've redone some of the code as gcalvin advised so it's a bit different now. I'll post it and on the menu class, you can see what I really want it to do.

    Menu Class

    PHP Code:
    import javax.swing.*;
    
    public class Menu {       
        public static int Choices(){
            int op;
               do {
                   String a="Menu"+ 
                               "\n1 - Insert book"+
                               "\n2 - List books by Title alphabetic order"+ 
                               "\n3 - List books by an author by Title alphabetic order"+
                               "\n4 - List books of an area by Title alphabetic order" +
                               "\n5 - List a book by ISBN"+  
                               "\n6 - Eliminate book by Title"+
                               "\n7 - List authores with written books"+
                               "\n0 - Exit";
                   op=Integer.parseInt(JOptionPane.showInputDialog(a+"\nOption? (0 to 7)"));
               }
               while ((op<0) || (op>7));
               return op;    
        }
                 
            public static void main(String[]args){
                listuthors la = new listuthores();
                Listbooks ll = new Listbooks();
                
                
                int option=Choices();
                     while (option!=0){
                         switch(option){   
                         
                         // Insert books
                         case 1:{
                             String title=JOptionPane.showInputDialog("Insert book title: ");
                             int year = Integer.parseInt(JOptionPane.showInputDialog("Inserir book year: "));
                             String isbn=JOptionPane.showInputDialog("Insert book ISBN: ");
                             int price= Integer.parseInt(JOptionPane.showInputDialog("Insert book price: "));
                             String area=JOptionPane.showInputDialog("Inserir book's Cientific Area: ");
                             String name=JOptionPane.showInputDialog("Inserir author's name: ");
                             ll.insert(title, year, isbn, price, area);
                             la.insert(name);
                             break;
                         }  
    
                         case 2:{
                             ll.list();
                             break;
                         }  
                         
     
                         case 3:{  
                             la.listauthores();                                        
                             break;
                         }    
                 
    
                         case 4:{
                             
                             break;
                         } 
                         
    
                         case 5:{
                            
                             break;
                         }  
                            
    
                         case 6:{
                             
                             break;
                         }  
                         
    
                         case 7:{
                             
                             break;
                         }  
                         
                         default: System.out.println("Not an Option.");
                         }               
                         option=Choices();             
                     }       
            }
    }

    Book Class
    PHP Code:
    public class Book {
    
    	private	String title;
    	private int year;
    	private String isbn;
    	private int price;
    	private String area;
    	private author firstauthor;
    	private book nextbook;
    	
    	public Book (String title, int year, String isbn, int price, String area){
    		this.title = title;
    		this.year = year;
    		this.isbn = isbn;
    		this.price = price;
    		this.area = area;
    	}
    
    	public String gettitle() {
    		return title;
    	}
    
    	public void settitle(String title) {
    		this.title = title;
    	}
    
    	public int getyear() {
    		return year;
    	}
    
    	public void setyear(int year) {
    		this.year = year;
    	}
    
    	public String getisbn() {
    		return isbn;
    	}
    
    	public void setisbn(String isbn) {
    		this.isbn = isbn;
    	}
    
    	public double getprice() {
    		return price;
    	}
    
    	public void setprice(int price) {
    		this.price = price;
    	}
    
    	public String getArea() {
    		return area;
    	}
    
    	public void setArea(String area) {
    		this.area = area;
    	}
    
    	public book getnextbook() {
    		return nextbook;
    	}
    
    	public void setnextbook(book nextbook) {
    		this.nextbook = nextbook;
    	}
    		
    }

    Author Class
    PHP Code:
    public class Author {
    	
    	private String name;
    	private author nextauthor;
    	private book firstbook;
    	
    	public Author (String name){
    		
    		this.name = name;		
    	}
    	
    	
    	public String getname() {
    		return name;
    	}
    
    	public void setname(String name) {
    		this.name = name;
    	}
    	
    	public author getnextauthor() {
    		return nextauthor;
    	}
    
    	public void setnextauthor(author nextauthor) {
    		this.nextauthor = nextauthor;
    	}
    	
    }

    ListBooks Class

    PHP Code:
    import javax.swing.*;
    
    public class Listbooks {
        private book firstbook;
        
        public Listbooks(){
            firstbook = null;
        }
        
        public void insert(String title, int year, String isbn, int price, String area){
            book newbook = new book(title, year, isbn, price, area);
            if (firstbook == null){
                newbook.settitle(title);
                firstbook=newbook;
            }
            else {
                if (firstbook.gettitle().compareTo(title) < 0){
                    newbook.setnextbook(firstbook);
                    firstbook = newbook;
                }
                else {
                    book temp = firstbook;
                    book previoustemp = null;        
                    while (temp != null && temp.gettitle().compareTo(title) < 0){
                        previoustemp = temp;
                        temp = temp.getnextbook();
                    }
                    newbook.setnextbook(temp);
                    previousTemp.setnextbook(newbook);
                }
            }
        }
        
        public book search(String title){
            book temp = firstbook;
            book posB = temp;
            if (temp!=null) {
                JOptionPane.showMessageDialog(null,temp.gettitle().compareTo(title));
                while (temp.gettitle().compareTo(title)!=0 && temp.getnextbook()!= null)
                temp = temp.getnextbook();
                
                if(temp.gettitle().compareTo(title)==0){    
                    JOptionPane.showMessageDialog(null,temp.gettitle());
                    JOptionPane.showMessageDialog(null,"The title " + title + " exists.");
                    return posB;
                }
            else
                JOptionPane.showMessageDialog(null,"The title " + title + " doesn't exist.");
                return posB;
            }
            else
                JOptionPane.showMessageDialog(null,"Empty!");
            return posB;
        }
        
        public void list(){
            book temp = firstbook;
            String x="";
            while (temp != null){
                x=x+"Title: "+temp.gettitle()+"\n year: "+temp.getyear()+"\n ISBN: "+temp.getIsbn()+"\n price: "+temp.getprice()+"\n Cientific Area: "+temp.getArea()+"\n";
                temp= temp.getnextbook();
            }
            JOptionPane.showMessageDialog(null, x);    
        }
        
    }

    ListAuthors Class

    PHP Code:
    import javax.swing.*;
    
    public class Listauthors {
        private author firstauthor;
        
        public Listauthores(){
            firstauthor=null;
        }
    
        public void insert(String name){
                author newauthor = new author(name);
                if (firstauthor == null){
                    newauthor.setname(name);
                    firstauthor=newauthor;
                }
                else {
                    if (firstauthor.getname().compareTo(name) > 0){
                        newauthor.setnextauthor(firstauthor);
                        firstauthor = newauthor;
                    }
                    else {
                        author temp = firstauthor;
                        author previoustemp = null;        
                        while (temp != null && temp.getname().compareTo(name) < 0){
                            previoustemp = temp;
                            temp = temp.getnextauthor();
                        }
                        newauthor.setnextauthor(temp);
                        previoustemp.setnextauthor(newauthor);
                    }
                }
            }
        
        public author search(String name){
            author temp = firstauthor;
            author posA = temp;
            if (temp!=null) {
                while (temp.getname().compareTo(name)!=0 && temp.getnextauthor()!= null)
                temp = temp.getnextauthor();
                
                if(temp.getname().compareTo(name)==0){    
                    JOptionPane.showMessageDialog(null,"The author " + name + " exists.");
                    return posA;
                }
            else
                JOptionPane.showMessageDialog(null,"The author " + name + " doesn't exist.");
                return posA;
            }
            else
                JOptionPane.showMessageDialog(null,"Empty!");
                return posA;
        }
        
        public void listauthors(){
            author temp = firstauthor;
            String x="";
            while (temp != null){
                x=x+temp.getname()+"\n";
                temp= temp.getnextauthor();
            }
            JOptionPane.showMessageDialog(null, x);        
        }
        
    }

    Ok, so as you can see, I've experimented with some code but I have some problems. For example, sometimes, when I try to insert a second book, the program crashes with the error code

    Exception in thread "main" java.lang.NullPointerException
    at pacote.ListaLivros.insere(Listbooks.java:30)
    at pacote.Menu.main(Menu.java:40)


    And I have no idea why.

    The 2nd option, to list books is working but it's not ordered by title and I don't know how to do it.

    The 3rd option orders the authors alphabetically like it should but I don't what is the difference between this and listbooks.

    The rest of the option, I think if I can get one to work, I can get the rest, except for the final two. But I'll think of that later.

    Can someone please take a look and give me some pointers?

    Thank you.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Java Code:
                    book [COLOR="Red"]previoustemp[/COLOR] = null;         
                    while (temp != null && temp.gettitle().compareTo(title) < 0){ 
                        [COLOR="red"]previoustemp[/COLOR] = temp; 
                        temp = temp.getnextbook(); 
                    } 
                    newbook.setnextbook(temp); 
                    [COLOR="red"]previousTemp[/COLOR].setnextbook(newbook);
    Take a good look.

    -Gary-

  6. #6
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Hello Gary.

    Once again, thank you for your answer.
    I wouldn't look too much into that question of upper or lower cases because although you are correct in pointing that out, it's just a case of misspelling during the translation to English so that I can put the program here so that everyone understands. :)

    It is correct on the original one because it compiles.
    I'm more worried about finding the correct way to approach the rest of the things the program has to do.

    Bye.

  7. #7
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 11-13-2009, 01:30 PM
  2. Web application to manage music
    By Thomasheen in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 01:20 AM
  3. JMX to to manage server application
    By shiva in forum Advanced Java
    Replies: 0
    Last Post: 03-26-2009, 08:47 AM
  4. How I manage Adobe eForm(pdf)
    By fastspeed33 in forum Advanced Java
    Replies: 0
    Last Post: 09-04-2008, 12:16 PM
  5. can't manage to get this
    By mollentze in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •