OK, so here is my problem. I have created this application that inventory's books & magazines. Everything compiles fine. I prompt the user at the beginning to pick the length of the array (stored in numBooks) then when it comes time to pass the array to the GUI, I get an error that says
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at TestBooks.main(TestBooks.java:193)
My problem, I guess, is that I am not passing the entire array correctly to the GUI. Now, there is an entirely different issue of me needing to get my buttons to work, but I think I just want help right now with how to properly pass the array in to the GUI. Any help you could give would be great, and just so you know, I am not an expert here (obviously) so let's try to keep it simple if we can. Thanks so much!
Oh and one more thing... if there are some better ways to do what I have already done, I would appreciate that too.
First file titled TestBooks.java
//Inventory Program Part 4
//This Program keeps and inventory of books and magazines
//Brandon Buchanan
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.awt.event.*;
import java.applet.*;
public class TestBooks extends JFrame// define class
{
public static JButton firstButton;
public static JButton prevButton;
public static JButton nextButton;
public static JButton lastButton;
DecimalFormat decimal = new DecimalFormat("$0.00");
public TestBooks(Books book)
{
super("Inventory GUI");
Container con = getContentPane();
BoxLayout layout = new BoxLayout(con, BoxLayout.Y_AXIS);
con.setLayout(layout);
JPanel topPanel= new JPanel();
JPanel bottomPanel= new JPanel();
con.add(topPanel, BorderLayout.NORTH);
con.add(bottomPanel, BorderLayout.SOUTH);
if (book instanceof Magazine)
{
JLabel label1 = new JLabel( "Title: " + book.getBookTitle() );
add( label1 );
JLabel issueLabel = new JLabel( "Issue: " + ((Magazine)book).getMagazineIssue() );
add( issueLabel );
JLabel label2 = new JLabel( "Price: " + (decimal.format(((Magazine)book).getBookPrice())) );
add( label2 );
JLabel label3 = new JLabel( "Quantity in stock: " + book.getBookQuantity() );
add( label3 );
JLabel label4 = new JLabel( "Magazine Total: " + (decimal.format(((Magazine)book).getBookInventory())) );
add( label4 );
JLabel restockLabel = new JLabel( "Magazine Total with restocking fee: " + (decimal.format(((Magazine)book).getMagazineValue())) );
add( restockLabel );
topPanel.add(label1);
topPanel.add(issueLabel);
topPanel.add(label2);
topPanel.add(label3);
topPanel.add(label4);
topPanel.add(restockLabel);
}
else
{
JLabel label1 = new JLabel( "Title: " + book.getBookTitle() );
add( label1 );
JLabel isbnLabel = new JLabel( "ISBN: " + ((Books)book).getBookIsbn() );
add( isbnLabel );
JLabel label2 = new JLabel( "Price: " + (decimal.format(book.getBookPrice())) );
add( label2 );
JLabel label3 = new JLabel( "Quantity in stock: " + book.getBookQuantity() );
add( label3 );
JLabel label4 = new JLabel( "Book Total: " + (decimal.format(book.getBookInventory())) );
add( label4 );
topPanel.add(label1);
topPanel.add(isbnLabel);
topPanel.add(label2);
topPanel.add(label3);
topPanel.add(label4);
}
firstButton = new JButton("|<<");
prevButton = new JButton("<<");
nextButton = new JButton(">>");
lastButton = new JButton(">>|");
bottomPanel.add(firstButton);
bottomPanel.add(prevButton);
bottomPanel.add(nextButton);
bottomPanel.add(lastButton);
pack();
setVisible( true);
}
// main method begins execution of Java application
public static void main( String args[] )
{
// This explains the program
System.out.println( "\nThis program will provide an inventory of books and/or magazines." );
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// Prompt user for number of books to enter
System.out.println( "\nHow many items would you like to inventory today?" );
int numBooks = input.nextInt(); // Reads the data entered
// Create an array of Book objects according to the number that the user inputs above
Books[] bookArray = new Books[numBooks];
// set the total inventory value
double bookTotal = 0.00;
// Begin for loop & declare variable i to store array items in until total number of books has been met
for (int i = 0; i < numBooks; i++)
{
// create book objects within for loop so array is not overwritten on each itteration
Books book = new Books();
// create magazine objects within for loop so array is not overwritten on each itteration
Magazine mag = new Magazine();
// create another Scanner to obtain input from command window during for loop
Scanner in = new Scanner( System.in );
// prompt for book or magazine
System.out.println( "\nIs this item a book or magazine? (Enter 1 for Book and 2 for Magazine.)" );
int type = input.nextInt(); //
if (type == 1)
{
// Request the book title
System.out.println( "\nPlease enter book title: " );
book.setBookTitle(in.nextLine()); // Reads the data entered
// Request the isbn for the book
System.out.println( "\nPlease enter the ISBN for \"" + book.getBookTitle() + "\"" );
book.setBookIsbn(in.nextInt()); // Reads the data entered
// Request the price of the book
System.out.println( "\nPlease enter the dollar amount for \"" + book.getBookTitle() + "\"" );
book.setBookPrice(in.nextDouble()); // Reads the data entered
//Request the number of books in stock
System.out.println( "\nPlease enter the number of books currently in stock:" );
book.setBookQuantity(in.nextInt()); // Reads the data entered
System.out.println( "\nBook information entered successfully!" );
// marks the completion of 1 itteration of for loop
bookArray[i] = book;
}
else
{
// Request the magazine title
System.out.println( "\nPlease enter magazine title:" );
mag.setBookTitle(in.nextLine()); // Reads the data entered
// Request the isbn for the magazine
System.out.println( "\nPlease enter the Issue Number for \"" + mag.getBookTitle() + "\"" );
mag.setMagazineIssue(in.nextLine()); // Reads the data entered
// Request the price of the magazine
System.out.println( "\nPlease enter the dollar amount for \"" + mag.getBookTitle() + "\"" );
mag.setBookPrice(in.nextDouble()); // Reads the data entered
//Request the number of magazines in stock
System.out.println( "\nPlease enter the number of magazines currently in stock:" );
mag.setBookQuantity(in.nextInt()); // Reads the data entered
System.out.println( "\nMagazine information entered successfully!" );
// marks the completion of 1 itteration of for loop
bookArray[i] = mag;
} // end if
} // end for loop that populates the array
TestBooks myFrame = new TestBooks(bookArray[numBooks]);
myFrame.setSize( 400, 300 );
myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}// end method main
}// end class TestBooks
next file is called Books.java
//Checkpoint: Inventory Program Part 1
//This program will provide an inventory of books
//Brandon Buchanan
public class Books // define class
{
private String bookTitle;
private int bookIsbn;
private double bookPrice;
private int bookQuantity;
//This constructor takes no arguments
public Books(){
}
//This constructor takes title, isbn, price, and quantity as arguments
public Books(String title, int isbn, double price, int quantity )
{
this.bookTitle=title;
this.bookIsbn=isbn;
this.bookPrice=price;
this.bookQuantity=quantity;
}
public void setBookTitle(String title)
{
bookTitle = title;
}
public String getBookTitle()
{
return bookTitle;
}
public void setBookIsbn(int isbn)
{
bookIsbn=isbn;
}
public int getBookIsbn()
{
return bookIsbn;
}
public void setBookPrice(double price)
{
bookPrice=price;
}
public double getBookPrice()
{
return bookPrice;
}
public void setBookQuantity(int quantity)
{
bookQuantity=quantity;
}
public int getBookQuantity()
{
return bookQuantity;
}
public double getBookInventory()
{
return bookPrice * bookQuantity;
}
}
and the last file is called Magazine.java
//Checkpoint: Inventory Program Part 3
//This is a subclass of the class Books
//Brandon Buchanan
public class Magazine extends Books // define class
{
private String magazineIssue;
// This constructor takes no arguments
public Magazine(){
}
// this constructor pulls from class Books and adds book genre
public Magazine(String title, int isbn, double price, int quantity, String issue )
{
super(title, isbn, price, quantity);
this.magazineIssue=issue;
}
//
public void setMagazineIssue(String issue)
{
magazineIssue = issue;
}
//
public String getMagazineIssue()
{
return magazineIssue;
}
public double getMagazineValue()
{
return getBookInventory() * 1.05;
}
}
Again, thanks so much for any help you can give!