Please help me fix my code...again

I'm still trying

Page 1 of 1

5 Replies - 814 Views - Last Post: 06 August 2008 - 03:40 AM Rate Topic: -----

#1 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Please help me fix my code...again

Post icon  Posted 05 August 2008 - 04:47 AM

OK.... I only have 5 days left of this class..GRRRRR...I have been working on Part 5 of my assignment which is:

**Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
**Add a company logo to the GUI using Java graphics classes.

I have added my buttons to my GUI, but they are not functioning the way they should be, and I can't figure out how add the fictitious company logo, but have had no luck. I tried using an example from my textbook to use the graphics classes, but I just kept getting a bunch of errors.

If someone could please help me with this...I turned it in like this two days ago, but unless I get this stage right, My next, and final stage won't be right either.

THANK YOU IN ADVANCE.. :crazy:






package inventory5;


import java.awt.*;			  
import java.awt.event.*;	  
import javax.swing.*;		 // Import the java swing package		
import javax.swing.ImageIcon;

public class Inventory5 {

	// main method begins 
	public static void main(String[] args) {
	   
	   

		Movie dvd = null;
		Inventory inventory = new Inventory();		

		dvd = new Movie(2356, "Pinnochio", 25, 14.99f, "G");
		System.out.println(dvd);
		inventory.addMovie(dvd);					
		
		dvd = new Movie(5684, "Shark Tale", 3, 12.00f, "G");
		System.out.println(dvd);
		inventory.addMovie(dvd);					
		
		dvd = new Movie(5564, "Flushed Away", 15, 15.75f, "G");
		System.out.println(dvd);
		inventory.addMovie(dvd);					

		dvd = new Movie(5562, "Shrek", 7, 9.99f, "G");
		inventory.addMovie(dvd);					
		System.out.println(dvd);

	   
						  

		inventory.printInventory();
		  new InventoryGUI(inventory);												  
	} // end main

} // end Inventory5


class DVD {
	private int itemNo;
	private String title;
	private int inStock;
	private float unitPrice;

	DVD(int itemNo, String title, int inStock, float unitPrice) {
		this.itemNo	= itemNo;
		this.title	 = title;
		this.inStock   = inStock;
		this.unitPrice = unitPrice;
	  
	}

	public int getItemNo()	  { return itemNo; }
	public String getTitle()	{ return title; }
	public int getInStock()	 { return inStock; }
	public float getUnitPrice() { return unitPrice; }
   
	public float value() {
		return inStock * unitPrice;
	}

	@Override
	public String toString() {
		return String.format("itemNo=%2d   title=%-22s   inStock=%3d   price=$%7.2f   value=$%8.2f",
							  itemNo, title, inStock, unitPrice, value());
	}

} // end DVD


class Inventory {
	// Setup an array of Movies 
	private final int INVENTORY_SIZE = 4;
	private DVD[] items;
	private int numItems;
	 
	Inventory() {
		items = new Movie[INVENTORY_SIZE];
		numItems = 0;
	}

	public int getNumItems() {
		return numItems;
	}

	public DVD getDVD(int n) {
		return items[n];
	}

	// Adds a Movie to the array of Movies. Adds to first empty slot found.
	public void addMovie(DVD item) {
		items[numItems] = item;	
		++numItems;
	}

	
	public double value() {
		double sumOfInventory = 0.0;

		for (int i = 0; i < numItems; i++)
			sumOfInventory += items[i].value();
						
		return sumOfInventory;
	}

	
	public void printInventory() {
		System.out.println("\nStacey's DVD Inventory\n");
	   
		
		if (numItems <= 0) {
				   } else {
			for (int i = 0; i < numItems; i++)
				System.out.printf("%3d   %s\n", i, items[i]);
			System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
		}
	}
	
} // end Inventory


// Extends DVD class from the base class DVD
class Movie extends DVD {
	// Holds movie Rating and adds restocking fee
	private String movieRating;

	// Constructor, calls the constructor of Movie first
	public Movie(int MovieID, String itemName, int units, float itemPrice, String Rating) {
		super(MovieID, itemName, units, itemPrice);	
		// Pass on the values needed for creating the Movie class first thing
		this.movieRating = Rating;		
	}

	// To set the rating manually
	public void setRating(String Rating) {
		movieRating = Rating;
	}

	// Get the rating 
	public String getRating() {
		return movieRating;
	}

	// Overrides value() in Movie class by calling the base class version and
	// adding a 5% restocking fee on top
	@Override
	 public float value() {
		return super.value() + restockingFee();
	} 

	// Simply gets the base class's value, and figures out the 5% restocking fee only
	public float restockingFee() {
		return super.value() * 0.05f;
	}

} // end Movie


// GUI for the Inventory
// Contains an inventory of DVD's and lets the user step through them one by one
class InventoryGUI extends JFrame 
{
	// access inventory for DVD Collection
	private Inventory theInventory; 
	
	// index in the inventory of the currently displayed DVD. 
	// the index starts at 0, goes to the number of DVDs in the inventory minus 1 
	private int index = 0; 
	
	// GUI elements to display currently selected DVD information 
	private final JLabel itemNumberLabel = new JLabel("  Item Number:"); 
	private JTextField itemNumberText; 
	
	private final JLabel prodnameLabel = new JLabel("  Product Name:"); 
	private JTextField prodnameText; 
	
	private final JLabel prodpriceLabel = new JLabel("  Price:"); 
	private JTextField prodpriceText; 
	
	private final JLabel numinstockLabel = new JLabel("  Number in Stock:"); 
	private JTextField numinstockText; 
	
	private final JLabel valueLabel = new JLabel("  Value:"); 
	private JTextField valueText; 
	
	private final JLabel movieRatingLabel = new JLabel("  Rated:"); 
	private JTextField movieRatingText; 
	
	private final JLabel totalValueLabel = new JLabel("  Inventory Total Value (5% restock fee included:"); 
	private JTextField totalValueText; 
	
	
	

	private JPanel centerPanel;
	private JPanel buttonPanel;




	// constructor for the GUI, in charge of creating all GUI elements 
	InventoryGUI(Inventory inventory) {
		super("Stacey's Movie Inventory");
		final Dimension dim = new Dimension(140, 20);
		final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
		JPanel jp;

		// create the inventory object that will hold the product information 
		theInventory = inventory;		
		
	  
		centerPanel = new JPanel(); 
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

	   
		
		buttonPanel = new JPanel();
		JButton nextButton = new JButton("Next");	
		nextButton.addActionListener(new NextButtonHandler());
		buttonPanel.add(nextButton); 
		centerPanel.add(buttonPanel);
		
		buttonPanel = new JPanel();
		JButton previousButton = new JButton("Previous");	
		previousButton.addActionListener((ActionListener) new previousButtonHandler());
		buttonPanel.add(previousButton); 
		centerPanel.add(buttonPanel);
		
		buttonPanel = new JPanel();
		JButton firstButton = new JButton("First");	
		firstButton.addActionListener((ActionListener) new firstButtonHandler());
		buttonPanel.add(firstButton); 
		centerPanel.add(buttonPanel);
		
		buttonPanel = new JPanel();
		JButton lastButton = new JButton("Last");	
		lastButton.addActionListener((ActionListener) new lastButtonHandler());
		buttonPanel.add(lastButton); 
		centerPanel.add(buttonPanel);
				
		jp = new JPanel(flo);
		itemNumberLabel.setPreferredSize(dim);
		jp.add(itemNumberLabel); 
		itemNumberText = new JTextField(3); 
		itemNumberText.setEditable(false); 
		jp.add(itemNumberText); 
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		prodnameLabel.setPreferredSize(dim);
		jp.add(prodnameLabel); 
		prodnameText = new JTextField(17); 
		prodnameText.setEditable(false); 
		jp.add(prodnameText); 
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		movieRatingLabel.setPreferredSize(dim);
		jp.add(movieRatingLabel); 
		movieRatingText = new JTextField(17); 
		movieRatingText.setEditable(false); 
		jp.add(movieRatingText); 
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		prodpriceLabel.setPreferredSize(dim);
		jp.add(prodpriceLabel); 
		prodpriceText = new JTextField(17); 
		prodpriceText.setEditable(false); 
		jp.add(prodpriceText); 
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		numinstockLabel.setPreferredSize(dim);
		jp.add(numinstockLabel); 
		numinstockText = new JTextField(5); 
		numinstockText.setEditable(false); 
		jp.add(numinstockText);  
		centerPanel.add(jp);
		
		
		
		jp = new JPanel(flo);
		valueLabel.setPreferredSize(dim);
		jp.add(valueLabel); 
		valueText = new JTextField(17); 
		valueText.setEditable(false); 
		jp.add(valueText); 
		centerPanel.add(jp);
		
		// add the overall inventory information to the panel
		jp = new JPanel(flo);
		totalValueLabel.setPreferredSize(dim);
		jp.add(totalValueLabel); 
		totalValueText = new JTextField(17);
		totalValueText.setEditable(false);
		jp.add(totalValueText); 
		centerPanel.add(jp);

		// add the panel to the GUI display
		setContentPane(centerPanel);

		repaintGUI();

		setDefaultCloseOperation(EXIT_ON_CLOSE); 
		setSize(450, 360); 
		setResizable(true); 
		setLocationRelativeTo(null); 
		setVisible(true); 
	} 

	// (re)display the GUI with current product's information 
	public void repaintGUI() { 
		Movie temp = (Movie) theInventory.getDVD(index); 
		
		if (temp != null) { 
			itemNumberText.setText("" + temp.getItemNo());	 
			prodnameText.setText(temp.getTitle()); 
			prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice())); 
			movieRatingText.setText(temp.getRating()); 
			numinstockText.setText("" + temp.getInStock()); 
			valueText.setText(String.format("$%.2f", temp.value())); 
		}
		totalValueText.setText(String.format("$%.2f", theInventory.value())); 
	}

	class NextButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int numItems = theInventory.getNumItems();
			index = (++index) % numItems;
			repaintGUI();
			
		}
	}
	class previousButtonHandler implements ActionListener{
		public void actionPerformed (ActionEvent e){
			int numItems = theInventory.getNumItems();
			index = (++index) % numItems;
			repaintGUI();
		 }
	}
	class firstButtonHandler implements ActionListener{
		public void actionPerformed (ActionEvent e){
			int numItems = theInventory.getNumItems();
			index = (++index) % numItems;
			repaintGUI();
			
		}
		
	}
	class lastButtonHandler implements ActionListener{
		public void actionPerformed (ActionListener e){
			int numItems = theInventory.getNumItems();
			index = (++index) % numItems;
			repaintGUI();
		}

		public void actionPerformed(ActionEvent e) {
			throw new UnsupportedOperationException("Not supported yet.");
		}
	}
	
   
}  // End InventoryGUI class



Is This A Good Question/Topic? 0
  • +

Replies To: Please help me fix my code...again

#2 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: Please help me fix my code...again

Posted 05 August 2008 - 10:02 AM

OK... so I got some feedback from my instructor, and the only button he has concerns with is my "Last" button that is not functioning. I did that button the same as the others, I can't figure out what I need to change to make it function.

here is some of the code I tried to make the button with....

buttonPanel = new JPanel();
		JButton lastButton = new JButton("Last");	
		lastButton.addActionListener((ActionListener) new lastButtonHandler());
		buttonPanel.add(lastButton); 
		centerPanel.add(buttonPanel);


and
 class lastButtonHandler implements ActionListener{
		public void actionPerformed (ActionListener e){
			int numItems = theInventory.getNumItems();
			index = (++index) % numItems;
			repaintGUI();


the full code to my project so far is already posted in this thread. I am still trying to figure out how to do the graphic on my own but would appreciate any suggestions.

I was graded on this part of the assignment already and received 40/50 points, so as before getting help with this will not improve my grade it will just help me learn from my mistakes.
Thank you.
Was This Post Helpful? 0
  • +
  • -

#3 vik09  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 08-July 08

Re: Please help me fix my code...again

Posted 05 August 2008 - 10:18 AM

The button is not working because when you unimplement the metod form the ActionListenr interface you put the code to throw an exception:
 public void actionPerformed(ActionEvent e) {
			throw new UnsupportedOperationException("Not supported yet.");
		}

Your code for the button action has to go here. Something like this:
 public void actionPerformed (ActionEvent e){
			int numItems = theInventory.getNumItems();
			index = (++index) % numItems;
			repaintGUI();
		}

You actually put the wrong argument in the method, you put ActionListener instead of ActionEvent.

btw, Why do all the buttons do the same thing?
Was This Post Helpful? 0
  • +
  • -

#4 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: Please help me fix my code...again

Posted 05 August 2008 - 11:06 AM

View Postvik09, on 5 Aug, 2008 - 10:18 AM, said:

btw, Why do all the buttons do the same thing?



LOL...I'm trying to figure out how to make them work right, but to be completely honest I don't know. I asked my instructor for help making each button function properly, and his reply was that the only problem he saw was that my last button didn't work...WTF, right???

I have been at this since about six this morning trying to figure this out. My last day of this class is sunday so i have to get part 6 done and handed in by Midnight Sunday...then it's over. I can handle learning this, but our materials that we are given in class do not even cover the requirements of the assignment.

Thanks for pointing out my error, sometimes all it takes is a fresh set of eyes...Well back to the grindstone...any assistance is appreciated.

Have a great day.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Please help me fix my code...again

Posted 05 August 2008 - 04:55 PM

if it is supposed to show the last

class lastButtonHandler implements ActionListener{
		public void actionPerformed (ActionListener e){
			index = theInventory.getNumItems() - 1;
			repaintGUI();
		}


Was This Post Helpful? 0
  • +
  • -

#6 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: Please help me fix my code...again

Posted 06 August 2008 - 03:40 AM

View Postpbl, on 5 Aug, 2008 - 04:55 PM, said:

if it is supposed to show the last

class lastButtonHandler implements ActionListener{
		public void actionPerformed (ActionListener e){
			index = theInventory.getNumItems() - 1;
			repaintGUI();
		}




Thank You PBL...Once again you save the day........
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1