Okay, so I've been working on this Inventory Program non-stop all day long. I've made massive changes to it several times and feel like I'm banging my head against the wall. And finally, I managed to make it do everything I need it to do like cycle through an inventory one item at a time, add new entries, alter existing entries, deleting entries, and saving changes. However, the one thing, and perhaps the most important thing it's supposed to do, it doesn't do! Which is display the inventory information. And I could have sworn I included code that specifically did that. >.<
I'm using netbeans, and it is giving me absolutely zero error reports or feedback of any kind. It brings up the program's GUI and displays the buttons/commands or otherwise functional part of the display. But the other half of the display is blank, which is where product information should be. I need help, bad. If anyone could point me in the right direction to make the GUI display everything, I'd be very grateful. The code is below. Feel free to copy/paste into your own IDE to run it yourself to see what I'm talking about.
Main Class:
package videogameinventory;
/**
*
* @author Lunar Savage
*/
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class VideoGameInventory extends JFrame {
protected int currentItem;
private JTextArea output;
private JButton next, previous, first, last, save, search;
private JButton add, modify, delete;
// array to store products
protected static ArrayList<ExtendedGameClass> list = new ArrayList<ExtendedGameClass>();
public VideoGameInventory() {
super("Inventory GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // quit if the window is closed
// set up the data
initData();
// set up the GUI itself
JPanel mainpanel = new JPanel();
setContentPane(mainpanel);
mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
// output area
output = new JTextArea(10,60);
output.setEditable(false);
mainpanel.add(output);
// button area
JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
// "first" button
first = new JButton("First");
currentItem = 0;
first.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
VideoGameInventory.this.currentItem = 0;
VideoGameInventory.this.displayGameClass();
}
});
buttons.add(first);
// "previous" button
previous = new JButton("Previous");
currentItem = 0;
previous.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
VideoGameInventory.this.currentItem--;
// gone too far, so go to the end...
if (VideoGameInventory.this.currentItem < 0) VideoGameInventory.this.currentItem = VideoGameInventory.this.list.size()-1;
VideoGameInventory.this.displayGameClass();
}
});
buttons.add(previous);
// "next" button
next = new JButton("Next");
currentItem = 0;
next.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
VideoGameInventory.this.currentItem++;
// gone to far, so go back to the beginning
if (VideoGameInventory.this.currentItem >= VideoGameInventory.this.list.size()) VideoGameInventory.this.currentItem = 0;
VideoGameInventory.this.displayGameClass();
}
});
buttons.add(next);
// "last" button
last = new JButton("Last");
currentItem = 0;
last.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
VideoGameInventory.this.currentItem = VideoGameInventory.this.list.size()-1;
VideoGameInventory.this.displayGameClass();
}
});
buttons.add(last);
// second button area
JPanel buttons2 = new JPanel();
buttons2.setLayout(new BoxLayout(buttons2,BoxLayout.X_AXIS));
// "save" button
save = new JButton("Save");
save.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
save();
}
});
buttons2.add(save);
// "search" button
search = new JButton("Search");
search.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
search();
}
});
buttons2.add(search);
// "add" button
add = new JButton("Add");
add.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
// get values and set them
String name = JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the item's name");
int units = Integer.parseInt(JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the units in stock"));
double price = Double.parseDouble(JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the price of each item"));
String genre = JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the genre");
//pad name
while (name.length() < 20) {
name += " ";
}
int number = 0;
//get the highest number
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getNumber() > number)
number = list.get(i).getNumber();
}
// make the object
ExtendedGameClass p = new ExtendedGameClass(number+1,name,units,price,genre);
list.add(p);
currentItem = list.size()-1;
// show it
displayGameClass();
}
});
buttons2.add(add);
// "modify" button
modify = new JButton("Modify");
modify.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
// get new values and set them
GameClass p = list.get(currentItem);
p.setNumber(Integer.parseInt(JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the item number",p.getNumber())));
p.setName(JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the item's name",p.getName()));
p.setUnits(Integer.parseInt(JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the units in stock",p.getUnits())));
p.setPrice(Double.parseDouble(JOptionPane.showInputDialog(VideoGameInventory.this,"Enter the price of each item",p.getPrice())));
// show it
displayGameClass();
}
});
buttons2.add(modify);
// "delete" button
delete = new JButton("Delete");
delete.addActionListener(new ActionListener() { // for button clicks
public void actionPerformed(ActionEvent e) {
try {
list.remove(currentItem);
} catch (Exception ex) {} // if there was nothing to begin with
currentItem--;
if (currentItem < 0) currentItem = 0;
displayGameClass();
}
});
buttons2.add(delete);
mainpanel.add(buttons);
mainpanel.add(buttons2);
// show the logo graphics
CompanyLogo g = new CompanyLogo();
mainpanel.add(g);
displayGameClass(); // show the first product
}
private void initData() {
ExtendedGameClass p1 = new ExtendedGameClass(1, "Fallout ", 12, 9.99, "RPG");
ExtendedGameClass p2 = new ExtendedGameClass(2, "Gears of War ", 20, 12.99, "TPS ");
ExtendedGameClass p3 = new ExtendedGameClass(3, "Portal 2 ", 10, 14.99, "Puzzle");
ExtendedGameClass p4 = new ExtendedGameClass(4, "Galaga ", 3, 17.50, "Arcade");
// put them in the array
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
// sort it
list = bubbleSort(list);
}
protected void displayGameClass() {
try {
displayGameClass(list.get(currentItem),output);
} catch (Exception ex) {
displayGameClass(null,output); // if the list is empty
}
}
// display one product in the output area
protected void displayGameClass(ExtendedGameClass p, JTextArea j) {
j.setText(""); // clear anything that's there
if (p == null) return;
// output the products using proper formatting, with a header row
// the columns are spaced using tabs to look nice
j.append("Number\tName\t\tUnits\tPrice\tFee\tVal\tGenre\n");
j.append(String.format("%03d\t%s\t%03d\t$%.2f\t$%.2f\t$%.2f\t%s\n",
p.getNumber(),p.getName(),p.getUnits(),p.getPrice(),p.fee(),p.calculateInventory()));
// total value...
j.append(String.format("Total value (with the fee): $%.2f\n",totalValue()));
}
// total value of everything in the array
public static double totalValue() {
double totalval = 0.0;
for (int i = 0; i < list.size(); i++) {
totalval += list.get(i).calculateInventory();
}
return totalval;
}
// bubble sort
public static ArrayList<ExtendedGameClass> bubbleSort(ArrayList<ExtendedGameClass> list) {
int n = list.size();
for (int search = 1; search < n; search++) {
for (int i = 0; i < n-search; i++) {
if (list.get(i).getName().compareToIgnoreCase(list.get(i+1).getName()) > 0) {
// swap
ExtendedGameClass temp = list.get(i);
list.set(i,list.get(i+1));
list.set(i+1,temp);
}
}
}
return list;
}
// call save the first time with true, so that we make the dir, if it's not there
public void save() {
save(true);
}
// save it to C:\data\inventory.dat
public void save(boolean saveagain) {
try { // try saving it!
BufferedWriter w = new BufferedWriter(new FileWriter("c:\\data\\inventory.dat"));
for (int i = 0; i < list.size(); i++) {
ExtendedGameClass GCD = list.get(i);
w.write( "Item number: " + GCD.getNumber() + "\n");
w.write( "Item name: " + GCD.getName() + "\n");
w.write( "Items in stock: " + GCD.getUnits() + "\n");
w.write( "Genre: " + GCD.getgenre() + "\n");
w.write( "Price: $" + GCD.getPrice() + "\n");
//w.write( "Fee: $" + GCD.getfee() + "\n");
w.write( "Value (including the fee): $" + GCD.calculateInventory() + "\n");
w.newLine();
}
// total value of it
w.write( "Total fee: $" + (totalValue() - totalValue()/1.05) + "\n");
w.write( "Total value (including the fee): $" + totalValue() + "\n");
w.close();
} catch (Exception ex) {
if (saveagain) {
new File("c:\\data\\").mkdir(); // make it if it wasn't there!
save(false); // if it doesn't work now, we have a big problem!
}
}
}
// search by name
public void search() {
boolean found = false;
String name = JOptionPane.showInputDialog(this,"Enter the name to search for");
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().trim().equals(name)) {
currentItem = i;
displayGameClass();
found = true;
}
}
if (!found) {
JOptionPane.showMessageDialog(VideoGameInventory.this,"Not found"); // do nothing
}
}
public static void main(String[] args) {
// setup and run the gui
VideoGameInventory GCD = new VideoGameInventory();
GCD.pack();
GCD.setVisible(true);
}
}
Game Class:
package videogameinventory;
/**
*
* @author Lunar Savage
*/
public class GameClass {
private int number;
private String name;
private int units;
private double price;
// constructor
public GameClass(int number, String name, int units, double price)
{
this.number = number;
this.name = name;
this.units = units;
this.price = price;
}
// value of this product
public double calculateInventory()
{
return price*units;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getUnits()
{
return units;
}
public void setUnits(int units)
{
this.units = units;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
}
Extended Game Class:
package videogameinventory;
/**
*
* @author Lunar Savage
*/
public class ExtendedGameClass extends GameClass {
private String genre; //extra field
double fee=0.05;
// constructor
public ExtendedGameClass(int number, String name, int units, double price, String genre)
{
super(number, name, units, price);
this.genre = genre;
}
// value of this including a 5% fee
public double calculateInventory()
{
return super.getPrice()*super.getUnits() + fee();
}
// compute the fee
public double fee()
{
return super.getPrice()*super.getUnits()*0.05;
}
public String getgenre()
{
return genre;
}
public void setgenre(String genre)
{
this.genre = genre;
}
}
Company Logo Class:
package videogameinventory;
/**
*
* @author Lunar Savage
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
// graphics, draws company logo
public class CompanyLogo extends JPanel
{
public CompanyLogo()
{
super();
setPreferredSize(new Dimension(150,100));
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillOval(15,10,125,60);
g.setColor(Color.black);
g.drawOval(15,10,125,60);
g.drawString("Games R Us", 45, 45);
}
}
If anyone bothers to look at this, I thank you in advance for your patience and time for helping one very frustrated person.