Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Need help with an array and GUI

    I'm attempting to have a GUI window open up and display an inventory of titles in the window, then using the buttons have the ability to add a new book into the arrya and display it, delete a book from the array, search and save the inventory in a .dat file. Any guidance would be appreciated..

    Thank you in advance.

    Java Code:
    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Arrays;
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class BookStore8 extends JFrame {  //begin class BookStore
    
    
    	static int bookIndex = 0;
    
    	public static JTextArea prepareDisplay(Book myBookList, JTextArea myTextArea){
    
    		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    
    		myTextArea.setText("");
    
    		myTextArea.append (myBookList.toString());
    
    		return myTextArea;
    	}
    
    
    	public static void main (String args[])  {
    
    	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    
    	Book intro = new Book("0321498054","Introduction to Programming Java - Interdisciplinary Approach", "Robert Sedgewick & Kevin Wayne", 2007, "Addison-Wesley", 83.65);
    	Book think = new Book("0139798099","Thinking in C++: Introduction to Standard C++", "Bruce Eckel", 2000, "Prentice Hall", 77.65);
    	Book build = new Book("0136091814","Building Java Programs: A Back to Basics Approach", "Stuart Reges & Marty Stepp", 2010, "Addison-Wesley", 97.02);
    	Book java  = new Book("0596007736","Java in a Nutshell", "David Flanagan", 2005, "O'Reilly Media", 44.95);
    	Book learn = new Book("1449319246","Learning Java", "Patrick Niemeyer & Daniel Leuck", 2013, "O'Reilly Media", 49.99);
    
    	final Book [] bookList = new Book[5];
    	bookList[0] = intro;
    	bookList[1] = think;
    	bookList[2] = build;
    	bookList[3] = java;
    	bookList[4] = learn;
    
    
    	final JTextArea textArea = new JTextArea(20,30);
    	textArea.setText("");
    	textArea.setEditable(false);
    
    	JPanel buttonPanel = new JPanel();
    	buttonPanel.setLayout(new GridLayout(1, 3));
    
    	JButton firstButton = new JButton ("First");
    	buttonPanel.add(firstButton);
    	firstButton.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e){
    			bookIndex = 0;
    			prepareDisplay(bookList[bookIndex], textArea);
    		}
    	});
    
    	JButton prevButton = new JButton("Previous");
    	buttonPanel.add(prevButton);
    	prevButton.addActionListener(new ActionListener() {
    	   	   			public void actionPerformed(ActionEvent e) {
    	   	   						bookIndex = 1;
    								prepareDisplay(bookList[bookIndex], textArea);
    	   	   			}
    		});
    
    	JButton nextButton = new JButton("Next");
    	buttonPanel.add(nextButton);
    	nextButton.addActionListener(new ActionListener() {
    	   	   			public void actionPerformed(ActionEvent e) {
    	   	   					bookIndex = 2;
    							prepareDisplay(bookList[bookIndex], textArea);
    	   	   			}
    		});
    
    
    	JButton lastButton = new JButton("Last");
    	buttonPanel.add(lastButton);
    	lastButton.addActionListener(new ActionListener() {
    	   	   			public void actionPerformed(ActionEvent e) {
    	   	   				bookIndex = (bookList.length - 1); //set index to the last item in the array
    	   	   				prepareDisplay(bookList[bookIndex], textArea);
    	   	   				}
    		});
    
    		JLabel logoLabel = new JLabel (new ImageIcon("DukeLogosm.jpg"));
    		JPanel logoPanel = new JPanel();
    	   	logoPanel.add(logoLabel);
    
    		JPanel centerPanel = new JPanel();
    	   centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
    	   centerPanel.add(prepareDisplay(bookList[bookIndex], textArea)); //show the first employee
    
    	double total = 0;
    
    	// for method Books Array
    	for (int i = 0; i < bookList.length; i++ ) {
    		   //textArea.append("\n\nISBN: " + bookList[i].getIsbn() + "\nTitle: " + bookList[i].getTitle() + "\nAuthor: " + bookList[i].getAuthor() + "\nYear Published: " + bookList[i].getYear() + "\nPublisher: " + bookList[i].getPublisher() + "\nList Price: " + nf.format(bookList[i].getPrice()));
    			total += bookList[i].getPrice();
    			//bookList = sortArray(bookList);
    			} // end for
    
    	textArea.append("\n\nTotal Cost of Inventory: " + nf.format(total));
    
    	//initialize the GUI window and set the parameters
    	   JFrame frame = new JFrame();
    	   	   frame.setLayout(new BorderLayout()); // set layout
    	   	   frame.add(logoPanel, BorderLayout.NORTH); //add logo
    	   	   frame.add(buttonPanel, BorderLayout.SOUTH); // add previous and next button to frame
    	   	   frame.add(centerPanel, BorderLayout.CENTER); // add center panel to frame
    	   	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   	   frame.pack();
    	   	   frame.setLocationRelativeTo(null);
    	   frame.setVisible(true);
    
    	} //end main method
    
    
    
    //Method Book array sort
    public static Book[] sortArray (Book bookList[]) {
    
    	String [] titles = new String[bookList.length]; //declares new array - titles
    
    	Book [] sortedBooks = new Book[bookList.length]; //declares new array - sortedBooks
    
    	for (int i = 0; i < bookList.length; i++){//adds all Title to titles array
    		titles[i] = bookList[i].getTitle();
    	} // end for
    		Arrays.sort(titles); //sorts titles array
    
    		for (int i = 0; i < bookList.length; i++){ //Book Array
    		     for (int j = 0; j < titles.length; j++){ //Titles Array
    		          if (bookList[i].getTitle().equalsIgnoreCase(titles[j])) {
    		                     sortedBooks[j] = bookList[i];
                        break;
    					} //end if
    		 		}//end for
    			}//end for
    
    	return sortedBooks; //returns to sortedBooks array
    
      } // end public book method
    
    
    
    
    
    
    }//end class BookStore
    
    class Book
    	{
    		private String isbn;
    		private String title;
    		private String author;
    		private int year;
    		private String publisher;
    		private double price;
    		private double total;
    
    
    	public Book ( String isbn, String title, String author, int year, String publisher, double price )
    	{
    		setIsbn (isbn);
    		setTitle (title);
    		setAuthor (author);
    		setYear (year);
    		setPublisher (publisher);
    		setPrice (price);
    	}
    
    
    
    		public String getIsbn(){
    			return isbn;
    		}
    
    		public void setIsbn (String isbn){
    			this.isbn = isbn;
    		}
    
    		public String getTitle(){
    			return title;
    		}
    
    		public void setTitle (String title){
    			this.title = title;
    		}
    
    		public String getAuthor(){
    			return author;
    		}
    
    		public void setAuthor (String author){
    			this.author = author;
    		}
    
    		public int getYear(){
    			return year;
    		}
    
    		public void setYear (int year){
    			this.year = year;
    		}
    
    		public String getPublisher(){
    			return publisher;
    		}
    
    		public void setPublisher (String publisher){
    			this.publisher = publisher;
    		}
    
    		public double getPrice(){
    			return price;
    		}
    
    		public void setPrice (double price){
    			this.price = price;
    		}
    		public String toString(){
    			NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    
    			return 	"ISBN:" + "\t" + isbn + "\n" +
    					"Title:" + "\t" + title + "\n" +
    					"Author:" + "\t" + author + "\n" +
    					"Year:" + "\t" + year + "\n" +
    					"Publisher:" + "\t" + publisher + "\n" +
    					"Price:" + "\t" + nf.format(price) + "\n";
    			}
    
    
    	} //end class Book
    Last edited by Hornetrider121; 12-14-2012 at 03:13 AM. Reason: pasted incorrect code...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with an array and GUI

    If you haven't done so, you will want to paste the entire error message and any other pertinent information regarding the error.

  3. #3
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help with an array and GUI

    Any Ideas?
    Last edited by Hornetrider121; 12-15-2012 at 12:08 AM.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 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
  •