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..
CODE
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