here is assignment create a book class with getter ans setters. create a driver class book_driver, and create an array or arraylist to hol at least four books.
built the book class, built the book driver, having problem successfully building the array. I have not finshed the book driver; as i am making changes to it to except user input. question I have is after looking at about 20 million post on using and arraylist none have answered my questions? i need to store 4 book made up of elements like author,title, publisher , and ISBN. How do you store all 4 strings in an object that object being the book in the arraylist?

public class Book {
	
	private  String Title;
	private  String Author;
	private  String Publisher;
	private  Integer ISBN;
	
	public Book() 
	{
			
	}
				
		public void setTitle(String title) 
	{
		Title = title;
	}
		public void setAuthor(String author) 
	{
		Author = author;
	}
		public void setPublisher(String publisher) 
	{
		Publisher = publisher;
	}	
		public void setIsbn(Integer Isbn)
	{
		ISBN =Isbn;
	}	
		public String getTitle()
	{
		return Title;
	}
		public String getAuthor()
	{
		return Author;
	}	
		public String getPublisher() 
	{
		return Publisher;
	}
		public Integer getIsbn() 
	{
		return ISBN;
			
		}}//End class
import java.util.Scanner;

public class book_driver {
	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
	    Book book=new Book();
	    String a_title;
	    String a_author;
	    String a_publisher;
	    int a_isbn;
	    
	    	    		
		System.out.print("Enter Book's Title:\n ");
		a_title=input.nextLine();
		book.setTitle(a_title);
    
	    System.out.print("Enter Book's Author:\n ");
	    a_author=input.nextLine();
	    book.setAuthor(a_author);
	    	        
	    System.out.print("Enter Book's Publisher:\n ");
	    a_publisher=input.nextLine();
	    book.setPublisher(a_publisher);
	    
	    System.out.print("Enter Book's ISBN:\n ");
	    a_isbn=input.nextInt();
	    book.setIsbn(a_isbn);
	    
	
	}}
import java.util.ArrayList;

public class Bookcase  {
	
	public ArrayList<Bookcase> mybooks=new ArrayList<Bookcase>();

	public Bookcase(String title, String author, String publisher, String string) 
	{
		
	}}//End Class

Recommended Answers

All 3 Replies

You Bookcase array should be an ArrayList of books. Each cell in the array will contain a Book. Right now your book case just contains more book cases inside, instead of books.

public class Bookcase  
{
	public ArrayList<Book> mybooks=new ArrayList<Book>();
     
	public Bookcase(String title, String author, String publisher, String isbn) 
	{
           Book newBook = new Book();
           newBook.setTitle(title);
           newBook.setAuthor(author);
           newBook.setPublisher(publisher);
           newBook.setIsbn(isbn);
	}
}//End Class

Notice that you can implement a constructor to the Book class as well

public Book(String title, String author, String publisher, String isbn)
{
    a_title = title;
    a_author = author;
    a_publisher = publisher
    a_isbn = isbn
}

And then the Bookcase code will look more friendly

public class Bookcase  
{
	public ArrayList<Book> mybooks=new ArrayList<Book>();
     
	public Bookcase(String title, String author, String publisher, String isbn) 
	{
           Book newBook = new Book(title, author, publisher, isbn);
	}
}//End Class

here is the code i came up with I am having trouble with the loop the output does increment the array but for some reson it gets no new data after the first interation just keeps adding anoth identical record to the array
example
moby dick melville Random House 123123
moby dick melville Random House 123123
moby dick melville Random House 123123
moby dick melville Random House 123123

public class Book {
	
	private static String Title;
	private static String Author;
	private static String Publisher;
	private static Integer ISBN;

	
	public Book() 
	{
			
	}
				
		public void setTitle(String title) 
		{
		Title = title;
		}
		public void setAuthor(String author) 
		{
		Author = author;
		}
		public void setPublisher(String publisher) 
		{
		Publisher = publisher;
		}	
		public void setIsbn(Integer isbn)
		{
		ISBN =isbn;
		}	
		public void displayInventory()
		{
		System.out.println(Title + " " + Author+" "+Publisher+" "+ISBN);
		}
		public String getTitle()
		{
		return Title;
		}
		public String getAuthor()
		{
		return Author;
		}	
		public String getPublisher() 
		{
		return Publisher;
		}
		public Integer getIsbn()
		{
		return ISBN;
		}
	}//End class
import java.util.ArrayList;

public class Bookcase  
{
		
	
	private static ArrayList<Book> mybooks;
	
	public Bookcase()
	{
		mybooks=new ArrayList<Book>();
    }
	public void displayInventory()
	  	{
	    for (Book newbook : mybooks)
	    {
	      newbook.displayInventory();
	    }
	}// end displayInventory
	
	public void add(Book newBook) {
		{
			mybooks.add(newBook);
		}    
	}}//End Class
import java.util.Scanner;

public class Book_driver {
	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		Bookcase mybooks=new Bookcase();
		Book newBook = new Book();
		String a_title=null;
		String a_author=null;
		String a_publisher=null;
		Integer a_isbn=null;
		String choose=null;
		
		System.out.println(" Do you want to enter a book?  yes or no "); 
		choose = input.nextLine();
		
	while (choose.equals("yes")) 
	{   
		System.out.print("Enter book's title ");
		a_title=input.nextLine();
		newBook.setTitle(a_title);
		
	    System.out.print("Enter Book's Author:\n ");
	    a_author=input.nextLine();
	    newBook.setAuthor(a_author);
	    
	    System.out.print("Enter Book's Publisher:\n ");
	    a_publisher=input.nextLine();
	    newBook.setPublisher(a_publisher);
	    
	    System.out.print("Enter Book's ISBN:\n ");
	    a_isbn=input.nextInt();
	    newBook.setIsbn(a_isbn);
	    
	    mybooks.add(newBook);
	    input.nextLine(); 
	    	    
	}
	
	    mybooks.displayInventory();
	 
	}}//End Class

Remove the static modifiers in your Book class. Also you need to pass a new Book object on each iteration, e.g. mybooks.add(new Book(a_title, a_author,a_publisher, a_isbn)); if you have a constructor to initialize these variables.

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.