• 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.
So this is the code I have:
package inventoryprogram3;
/**
*
* @author Ana
*/
public class InventoryProgram3 {
//main method begins execution of application
public static void main(String args []) {
Inventory invent = new Inventory(); //creates an array
Movie dvd;
dvd = new Movie("Drama", "Fear and Loathing in Las Vegas", 1001, 16.95, 15); //item 1
invent.add(dvd);
dvd = new Movie("Animation/Comedy", "The Nine Lives of Fritz the Cat", 1002, 23.95, 2); //item 2
invent.add(dvd);
dvd = new Movie("Crime/Thriller", "Silence of the Lambs", 1003, 16.95, 10); //item 3
invent.add(dvd);
dvd = new Movie("Drama", "Sling Blade", 1004, 16.95, 5); //item 4
invent.add(dvd);
invent.display();
} //end main
} // end class InventoryProgram3
package inventoryprogram3;
/**
*
* @author Ana
*/
class DVD { //priavte variable containers
private int dvdNumber;
private String dvdTitle;
private int dvdStock;
private double dvdPrice;
public DVD( String title, int item, double price, int stock) { //four argument constructor
dvdNumber = item;
dvdTitle = title;
dvdStock = stock;
dvdPrice = price;
} //end four argument constructor
// set DVD Item number
public void setDvdNumber(int item) {
dvdNumber = item;
} //end method set Dvd Item number
//return DVD Item number
public int getDvdNumber() {
return dvdNumber;
} //end method get Dvd Item number
//set DVD Title
public void setDvdTitle(String title) {
dvdTitle = title;
} //end method set Dvd Title
//return Dvd Title
public String getDvdTitle() {
return dvdTitle;
} //end method get Dvd Title
public void setDvdStock(int stock) {
dvdStock = stock;
} //end method set Dvd Stock
//return dvd Stock
public int getDvdStock() {
return dvdStock;
} //end method get Dvd Stock
public void setDvdPrice(double price) {
dvdPrice = price;
} //end method setdvdPrice
//return DVD Price
public double getDvdPrice() {
return dvdPrice;
} //end method get Dvd Price
//calculate inventory value
public double value() {
return dvdPrice * dvdStock;
} //end method value
public String toString() { //display
return String.format("ITEM #: %3d\n TITLE: %-20s\n UNITS IN STOCK: %3d\n PRICE: $%6.2f\n VALUE OF INVENTORY: $%7.2f\n",
dvdNumber, dvdTitle, dvdStock, dvdPrice, value());
} // end display
// Simply gets the base class's value, and figures out the 5% restocking fee only
public double restockingFee() {
return value() * 0.05f;
}
} //end class DVD
class Movie extends DVD { //subclass
private String movieGenre; //private variable container
public Movie(String genre, String title, int item, double price, int stock) { //5 argument constructor
super(title, item, price, stock);
movieGenre = genre;
} //end five argument constructor
public String getMovieGenre() {
return movieGenre;
}
// Overrides value() in Movie class by calling the base class version and
// adding a 5% restocking fee on top
public double value() {
return super.value() + restockingFee();
}
// Simply gets the base class's value, and figures out the 5% restocking fee only
public double restockingFee() {
return super.value() * 0.05f;
}
public String toString() { //add unique feature to string
String s = String.format("\n GENRE: %-12s\n", movieGenre);
s = s + " " + super.toString();
return s;
} //end method
} // end class Movie
package inventoryprogram3;
/**
*
* @author Ana
*/
class Inventory { //private variable containers
private DVD[] dvds;
private int count;
Inventory() {
dvds = new DVD[10];
count = 0;
}
public void add(DVD dvd) {
dvds[count] = dvd;
++count;
sort();
}
public double entireValue() { //calculate entirevalue
double value = 0;
for (int i = 0; i < count; i++) {
value = value + dvds[i].value();
}
return value;
} // end method entire value
public void sort() {
for (int index = 1; index < count; index++) {
DVD key = dvds[index];
int position = index;
// Shift larger values to the right
while (position > 0 && key.getDvdTitle().compareTo(dvds[position-1].getDvdTitle()) < 0) {
dvds[position] = dvds[position-1];
position--;
} //end while
dvds[position] = key;
} // end for
} //end method sort
public Movie get(int i) {
return get(i);
}
public int size() {
return size();
}
public void display() { //display
System.out.println("\nTHERE ARE CURRENTLY " + count + " TITLES IN INVENTORY\n");
for (int i = 0; i < count; i++)
System.out.printf("%3d %s\n", i, dvds[i]);
System.out.printf("\nTOTAL VALUE: $%.2f\n\n", entireValue());
System.out.println("note: value of inventory and total value fields include 5% restocking fee.");
} //display
} // end class Inventory
package inventoryprogram3;
/**
*
* @author Ana
*/
public class InventoryGUI extends javax.swing.JFrame {
private Inventory inv;
private int currentDisplay = 0;
/** Creates new form InventoryGUI */
public InventoryGUI() {
initComponents();
}
public void init() {
Movie p1 = new Movie("Drama", "Fear and Loathing in Las Vegas", 1001, 16.95, 15);
Movie p2 = new Movie("Animation/Comedy", "The Nine Lives of Fritz the Cat", 1002, 23.95, 2);
Movie p3 = new Movie("Crime/Thriller", "Silence of the Lambs", 1003, 16.95, 10);
Movie p4 = new Movie("Drama", "Sling Blade", 1004, 16.95, 5);
inv = new Inventory();
inv.add(p1);
inv.add(p2);
inv.add(p3);
inv.add(p4);
inv.sort();
// Output
for (int i = 0; i < inv.size(); i++) {
System.out.println("GENRE: " + inv.get(i).getMovieGenre());
System.out.println("ITEM #: " + inv.get(i).getDvdNumber());
System.out.println("NAME: " + inv.get(i).getDvdTitle());
System.out.println("UNITS IN STOCK: " + inv.get(i).getDvdStock());
System.out.println("PRICE: $" + String.format("%.2f",inv.get(i).getDvdPrice()));
System.out.println("VALUE OF INVENTORY: $" + String.format("%.2f",inv.get(i).value()));
System.out.println("RESTOCKING FEE: $" + String.format("%.2f",inv.get(i).restockingFee()));
System.out.println();
}
//total
System.out.println("TOTAL VALUE: $" + String.format("%.2f",inv.entireValue()));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setLineWrap(true);
jTextArea1.setRows(5);
jTextArea1.setText("Welcome to the DVD Inventory Program!");
jScrollPane1.setViewportView(jTextArea1);
jButton2.setText("Previous");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Next");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Last");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("Exit");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton1.setText("First");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Ana\\Desktop\\bulkdiscstack.jpg")); // NOI18N
jLabel1.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 338, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addGap(14, 14, 14)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 374, Short.MAX_VALUE)
.addComponent(jButton5)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton5))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton4)
.addComponent(jButton2)
.addComponent(jButton3))))
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); //exit program
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//action performed by clicking first button
currentDisplay = 0;// go to the beginning
displayProduct();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//action performed by clicking previous button
if (currentDisplay > 0) {
currentDisplay--;
}
else {
currentDisplay = inv.size() - 1;
}
displayProduct();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//action performed by clicking next button
if (currentDisplay < inv.size()-1) {
currentDisplay++;
} //advance to the end
else {
currentDisplay = 0;
}
displayProduct();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//action performed by clicking last button
currentDisplay = inv.size()-1;
displayProduct();
}
// display
public void displayProduct() {
jTextArea1.setText("Product Details:\n");
jTextArea1.append("GENRE: " + inv.get(currentDisplay).getMovieGenre() + "\n");
jTextArea1.append("ITEM #: " + inv.get(currentDisplay).getDvdNumber() + "\n");
jTextArea1.append("TITLE: " + inv.get(currentDisplay).getDvdTitle() + "\n");
jTextArea1.append("UNITS IN STOCK: " + inv.get(currentDisplay).getDvdStock() + "\n");
jTextArea1.append("PRICE: $" + String.format("%.2f",inv.get(currentDisplay).getDvdPrice()) + "\n");
jTextArea1.append("VALUE OF INVENTORY: $" + String.format("%.2f",inv.get(currentDisplay).value()) + "\n");
jTextArea1.append("RESTOCKING FEE: $" + String.format("%.2f",inv.get(currentDisplay).restockingFee()) + "\n\n");
jTextArea1.append("TOTAL VALUE: $" + String.format("%.2f",inv.entireValue()));
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InventoryGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
Now that that's out of the way, these are my errors upon clicking the "FIRST" button:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.displayProduct(InventoryGUI.java:223)
at inventoryprogram3.InventoryGUI.jButton1ActionPerformed(InventoryGUI.java:180)
at inventoryprogram3.InventoryGUI.access$400(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$5.actionPerformed(InventoryGUI.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
and the "PREVIOUS" button:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.jButton2ActionPerformed(InventoryGUI.java:191)
at inventoryprogram3.InventoryGUI.access$000(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$1.actionPerformed(InventoryGUI.java:91)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
and the "NEXT" button:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.jButton3ActionPerformed(InventoryGUI.java:200)
at inventoryprogram3.InventoryGUI.access$100(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$2.actionPerformed(InventoryGUI.java:98)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
and finally, the "LAST" button:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.jButton4ActionPerformed(InventoryGUI.java:213)
at inventoryprogram3.InventoryGUI.access$200(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$3.actionPerformed(InventoryGUI.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
again, i apologize for the lengthy post. i hope i did it right.

New Topic/Question
Reply
MultiQuote









|