Cant get my icon to show up ...
CODE
import java.awt.*; // import the java awt package
import java.awt.event.*; // Import the java event package
import javax.swing.*; // Import the java swing package
import java.util.*; // Import the java utility package
import java.io.*; // Import the java input output package
import java.lang.*; // Import the java lang package
import java.text.*; // Import the java text package
import java.math.*; // Import the java math package
public class CdInvApp {
public static void main(String[] args) {
FeeQtyProduct product = null;
CdInv inventory = new CdInv();
product = new FeeQtyProduct( "Star Wars",1, 5, 13.45, "PG" );
inventory.addFeeQtyProduct(product );
product = new FeeQtyProduct( "Cars", 2, 7, 12.23, "G" );
inventory.addFeeQtyProduct(product );
product = new FeeQtyProduct( "Fantastic Four", 3, 8, 18.45, "PG");
inventory.addFeeQtyProduct(product );
product = new FeeQtyProduct( "Batman", 4, 2, 10.88, "PG-13" );
inventory.addFeeQtyProduct(product );
new CdInvGUI(inventory);
}
} // end CdInvApp
abstract class Product
{
public String productName; //name
public int productNumber; // product number
private double baseAmount; // quantity in Inv
private double basePrice; // inital price
// four-argument constructor
public Product( String name, int number, double amount, double price )
{
productName = name;
productNumber = number;
baseAmount = amount;
basePrice = price;
} // end four-argument Product constructor
// set Product Name
public void setProductName( String name )
{
productName = name;
} // end method setProductName
// return ProductName
public String getProductName()
{
return productName;
} // end method getProductName
// set ProductNumber
public void setProductNumber( int number )
{
productNumber = number;
} // end method setProductNumber
// return ProductNumber
public int getProductNumber()
{
return productNumber;
} // end method getProductNumber
// set initial price
public void setBaseAmount( double amount )
{
baseAmount = amount;
} // end method setBaseAmount
// return BaseAmount
public double getBaseAmount()
{
return baseAmount;
} // end method getBaseAmount
// set BasePrice
public void setBasePrice( double price )
{
basePrice = price; // non-negative
} // end method setBasePrice
// return BasePrice
public double getBasePrice()
{
return basePrice;
} // end method getBasePrice
// return String representation of Product object no longer needed
public String toString()
{
return String.format( "%s\nProduct#: %f\nQTY#:%.0f\nPrice: $%.2f ",
getProductName(), getProductNumber(), getBaseAmount(), getBasePrice() );
} // end method toString
public abstract double total();
public abstract double restock();
} // end abstract class Product
class FeeQtyProduct extends Product
{
private String ratingTitle;
// five-argument constructor
public FeeQtyProduct( String name, int number, double amount, double price, String title )
{
super( name, number, amount, price );
setRatingTitle( title);
} // end five-argument FeeQtyProduct constructor
public void setRatingTitle( String title )
{
ratingTitle = title;
}
public String getRatingTitle()
{
return ratingTitle;
}
// calculate total; override method total in Product
public double total()
{
return getBasePrice() * getBaseAmount();
} // end method total
// calculate earnings; override method total in QtyProduct
public double restock()
{
return getBasePrice() * 1.05;
} // end method restock
public String toString()
{
return String.format( "%s: %s\n%s: %s",
"Video", super.toString(),
"Rating", getRatingTitle());
} // end method toString
} // end class FeeQtyProduct
// create inventory list
class CdInv
{
// delare variables
public static final int INVENTORY_SIZE = 30;
private FeeQtyProduct[] items;
public int numItems;
CdInv() //sets the number of items
{
items = new FeeQtyProduct[INVENTORY_SIZE];
numItems = 0;
}
//returns number of items
public int getNumItems()
{
return numItems;
}
//returns each product
public FeeQtyProduct getFeeQtyProduct(int i)
{
return items[i];
}
//deletes item and recounts number of items
public void remove(int i)
{
items[i] = items[numItems -1];
--numItems;
}
//adds products to the inventory
public void addFeeQtyProduct( FeeQtyProduct item)
{
items[numItems] = item;
++numItems;
}
// reduces numbers of items in inventory
public void removeFeeQtyProduct(FeeQtyProduct item)
{
items[numItems] = item;
--numItems;
}
// returns the amount one item inventory is worth
public double value()
{
double sum = 0.0;
for (int i = 0; i < numItems; i++)
sum += items[i].total();
return sum;
}// end double value
} // end CdInv
// GUI for the Inventory
class CdInvGUI extends JFrame
{
private CdInv myCdInv;
public int index = 0;
// GUI elements to display information
private final JLabel itemNumberLabel = new JLabel(" Item Number:");
public JTextField itemNumberText;
private final JLabel videoLabel = new JLabel( "Video:");
private JTextField videoText;
private final JLabel ratingLabel = new JLabel("Rating:");
private JTextField ratingText;
private final JLabel priceLabel = new JLabel(" Price: $");
private JTextField priceText;
private final JLabel qtyLabel = new JLabel(" Quantity:");
private JTextField qtyText;
private final JLabel valueLabel = new JLabel(" Value:");
private JTextField valueText;
private final JLabel restockFeeLabel = new JLabel(" with Restock Fee:");
private JTextField restockFeeText;
private final JLabel totalValueLabel = new JLabel(" Inventory Total Value:");
private JTextField totalValueText;
private final JLabel searchLabel = new JLabel("Search:");
private JTextField searchText;
private JLabel iconlabel; //JLabel icon
// constructor for the GUI, in charge of creating all GUI elements
CdInvGUI(CdInv inventory)
{
final Dimension size = new Dimension(125,20);
final Dimension size2 = new Dimension(300,60);
final FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
JPanel jpanel = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2, 4));
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
// create the inventory object that will hold the product information
myCdInv = inventory;
JButton firstButton = new JButton("First");
buttonPanel.add(firstButton);
JButton previousButton = new JButton("Previous");
buttonPanel.add(previousButton);
JButton nextButton = new JButton("Next");
buttonPanel.add(nextButton);
JButton lastButton = new JButton("Last");
buttonPanel.add(lastButton);
JButton addButton = new JButton("Add");
buttonPanel.add(addButton);
JButton deleteButton = new JButton("Delete");
buttonPanel.add(deleteButton);
JButton modifyButton = new JButton("Modify");
buttonPanel.add(modifyButton);
JButton saveButton = new JButton("Save");
buttonPanel.add(saveButton);
JButton searchButton = new JButton("Search");
buttonPanel.add(searchButton);
JButton helpButton = new JButton("help");
buttonPanel.add(helpButton);
// start JPanel layout
jpanel = new JPanel(layout);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
itemNumberLabel.setPreferredSize(size);
jpanel.add(itemNumberLabel);
itemNumberText = new JTextField(3);
itemNumberText.setEditable(false);
jpanel.add(itemNumberText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
videoLabel.setPreferredSize(size);
jpanel.add(videoLabel);
videoText = new JTextField(10);
videoText.setEditable(true);
jpanel.add(videoText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
ratingLabel.setPreferredSize(size);
jpanel.add(ratingLabel);
ratingText = new JTextField(10);
ratingText.setEditable(true);
jpanel.add(ratingText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
priceLabel.setPreferredSize(size);
jpanel.add(priceLabel);
priceText = new JTextField(10);
priceText.setEditable(true);
jpanel.add(priceText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
qtyLabel.setPreferredSize(size);
jpanel.add(qtyLabel);
qtyText = new JTextField(5);
qtyText.setEditable(true);
jpanel.add(qtyText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
restockFeeLabel.setPreferredSize(size);
jpanel.add(restockFeeLabel);
restockFeeText = new JTextField(5);
restockFeeText.setEditable(false);
jpanel.add(restockFeeText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
valueLabel.setPreferredSize(size);
jpanel.add(valueLabel);
valueText = new JTextField(5);
valueText.setEditable(false);
jpanel.add(valueText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
totalValueLabel.setPreferredSize(size);
jpanel.add(totalValueLabel);
totalValueText = new JTextField(5);
totalValueText.setEditable(false);
jpanel.add(totalValueText);
centerPanel.add(jpanel);
jpanel = new JPanel(layout);
searchLabel.setPreferredSize(size);
jpanel.add(searchLabel);
searchText = new JTextField(10);
searchText.setEditable(true);
jpanel.add(searchText);
centerPanel.add(jpanel);
Icon dvd = new ImageIcon(getClass().getResource( "dvd.jpg" ));
iconlabel = new JLabel ( dvd, SwingConstants.RIGHT);
iconlabel.setVerticalTextPosition( SwingConstants.TOP);
add( iconlabel );
JFrame frame = new JFrame("Video Collection"); // JFrame container
frame.setLayout(new BorderLayout()); // set layout
frame.add(buttonPanel, BorderLayout.SOUTH); // adds buttons to frame
frame.add(centerPanel, BorderLayout.CENTER); // adds center panel to frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // terminate upon close
frame.setSize(475, 525); // set size
frame.setResizable(true);
frame.setLocationRelativeTo(null); // set location
frame.setVisible(true); // display the window
repaintGUI();
// start inner classes to add actions to buttons
firstButton.addActionListener(new ActionListener() //goes to first entry
{
public void actionPerformed(ActionEvent e)
{
index = 0;
repaintGUI();
}// end action
});// end class
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int numItems = myCdInv.getNumItems();
if (numItems != 0)
index = (--index) % numItems;
if (index < 0)
index = numItems - 1;
if (numItems == 0) // catches for last entry not needed because we add a blank entry at zero but just in case
JOptionPane.showMessageDialog(null, "No more entries.","Consult the manager.", JOptionPane.ERROR_MESSAGE);
repaintGUI();
}// end action
}); // end class
nextButton.addActionListener(new ActionListener() //goes to next entry on the list
{
public void actionPerformed(ActionEvent e)
{
int numItems = myCdInv.getNumItems();
index = (++index) % numItems; // moves index up one
repaintGUI();
}// end action
}); // end class
lastButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int numItems = myCdInv.getNumItems();
index = (numItems -1) % numItems; //goes to first entry then minus one
repaintGUI();
}// end action
}); // end clas
addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FeeQtyProduct temp = (FeeQtyProduct) myCdInv.getFeeQtyProduct(index);
int numItems = myCdInv.getNumItems() +1;
index = (numItems - 2) % numItems;
repaintGUI();
if(videoText.getText().equals("Add Video Name" )) // catches for assigning more then one blank at a time
{
JOptionPane.showMessageDialog(null, "Please complete all fields before adding more.", "Click Modify when finished", JOptionPane.ERROR_MESSAGE);
}
if(videoText.getText().equals("Add Video Name" ) !=true) // allows the adding of an entry
{
FeeQtyProduct product = new FeeQtyProduct( "Add Video Name", Integer.parseInt(itemNumberText.getText()) + 1, 0, 0.0, "Add Rating Title");
index = (numItems - 1) % numItems;
myCdInv.addFeeQtyProduct(product );
repaintGUI();
}
if (numItems == 29) // catches for going over static inventory size
{
myCdInv.removeFeeQtyProduct(temp);
JOptionPane.showMessageDialog(null, "No More Entries Allowed.", "Database is Full.", JOptionPane.ERROR_MESSAGE);
}
}// end action
}); // end class
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int numItems = myCdInv.getNumItems();
InventoryStorage record = new InventoryStorage(); //calls inventroy storage class
record.openFile();
int currentRecord = 0; // keeps track of number of cycles
do // cycles through the list adding them one at a time
{
record.addRecords();
currentRecord = currentRecord + 1;
index = (++index) % numItems;
} while (currentRecord < numItems); //ends while
record.closeFile(); //closes file
}//end action
}); // end class
modifyButton.addActionListener(new ActionListener() // saves changes to the GUI
{
public void actionPerformed(ActionEvent e)
{
if (videoText.getText().equals("")) //traps for blank entry
{
JOptionPane.showMessageDialog(null, "Field needs to be completed.","You can not have a blank field.", JOptionPane.ERROR_MESSAGE);
repaintGUI();
}
if (ratingText.getText().equals("")) //traps for blank entry
{
JOptionPane.showMessageDialog(null, "Field needs to be completed.","You can not have a blank field.", JOptionPane.ERROR_MESSAGE);
repaintGUI();
}
try // traps for letters and blank entry
{
Double.parseDouble(qtyText.getText());
Double.parseDouble(priceText.getText());
}
catch (Exception d)
{
JOptionPane.showMessageDialog(null, "Please recheck your entry.", "Only numbers may be added.", JOptionPane.ERROR_MESSAGE);
repaintGUI();
}
String name; int number; double amount, price; String title; // declares variables
name = videoText.getText();
number = Integer.parseInt(itemNumberText.getText());
amount = Double.parseDouble(qtyText.getText());
price = Double.parseDouble(priceText.getText());
title = ratingText.getText();
FeeQtyProduct modify = (FeeQtyProduct) myCdInv.getFeeQtyProduct(index);
modify.setProductNumber(number);
modify.setProductName(name);
modify.setBaseAmount(amount);
modify.setBasePrice(price);
modify.setRatingTitle(title);
repaintGUI();
}
}); // end class
deleteButton.addActionListener(new ActionListener() //allows user to delete entry and sorts product numbers to be in sequence
{
public void actionPerformed(ActionEvent e)
{
int numItems = myCdInv.getNumItems();
FeeQtyProduct temp = (FeeQtyProduct) myCdInv.getFeeQtyProduct(index);
if (numItems != 0)
{
if(Integer.parseInt(itemNumberText.getText()) != numItems)
{
myCdInv.remove(index);
repaintGUI();
int i = Integer.parseInt(itemNumberText.getText());
index = (++index) % numItems;
repaintGUI();
int j = Integer.parseInt(itemNumberText.getText());
if (i > j)
index = (-- index) % numItems;
repaintGUI();
temp = (FeeQtyProduct) myCdInv.getFeeQtyProduct(index);
temp.setProductNumber(j-1);
index = (++index) % numItems;
repaintGUI();
}// end if
if(Integer.parseInt(itemNumberText.getText()) == numItems) // uses a different method to remove entry if it is the last one prevents blank entry from being in the mix
{
myCdInv.removeFeeQtyProduct(temp);
index = (++index) % numItems;
repaintGUI();
}//end if
}// end if
if (numItems == 1) // catches for 0 items error
{
FeeQtyProduct product = new FeeQtyProduct( "Add Video Name",numItems, 0, 0.0, "Add Rating Title");
myCdInv.addFeeQtyProduct(product);
JOptionPane.showMessageDialog(null, "Deletion Complete.","Thank you.", JOptionPane.ERROR_MESSAGE);
repaintGUI();
} //end if
}// end action
}); // end class
searchButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//declare variables and gets text from GUI
String search = searchText.getText();
String name = videoText.getText();
int number = Integer.parseInt(itemNumberText.getText());
int numItems = myCdInv.getNumItems();
int currentRecord = 0;
do // gets text and checks for a match
{
index = (++index) % numItems;
repaintGUI();
name = videoText.getText();
number = Integer.parseInt(itemNumberText.getText());
search = searchText.getText();
currentRecord = (++currentRecord);
if(name.equalsIgnoreCase( search )) // stops on item
repaintGUI();
if(searchText.getText().equals("" )) //catches for no entry in the search box
{
JOptionPane.showMessageDialog(null, "Please fill in search file.","We will be happy to look it up for you.", JOptionPane.ERROR_MESSAGE);
index = (--index) % numItems;
break;
}
if(currentRecord == numItems +1)
{
JOptionPane.showMessageDialog(null, "Item not found.","Please try again", JOptionPane.ERROR_MESSAGE);
break;
}
}while(name.equalsIgnoreCase(search ) != true); //ends do while
}//end action
}); // end class
helpButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "FIRST Brings you to the first entry\nPREVIOUS Brings you to the last entry\nNEXT Brings you to the next entry\nLAST Brings you to the last entry\nADD adds a blank entry to allow a new listing see (MODIFY)\nSAVE Exports the entry's to a DAT file\nSEARCH allows you to search by product name\nMODIFY must be pressed to save changes within the GUI\nDELETE deletes the entry\nHELP you are in help\n","HELP", JOptionPane.PLAIN_MESSAGE);
repaintGUI();
}
}); // end class
} // end CdInvGUI
class InventoryStorage // creates the output file
{
private Formatter output; // object used to output text to file
public void openFile()
{
try
{
String strDirectoy ="C://data/";
// Create one directory
boolean success = (new File(strDirectoy)).mkdir();
if (success)
{
JOptionPane.showMessageDialog(null, "A directory was created in your C drive.","Thank you.", JOptionPane.PLAIN_MESSAGE);
} //end if
}//end try
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Access Denied.","Contact your Manager", JOptionPane.ERROR_MESSAGE);
}//end catch
try
{
output = new Formatter( "C:/data/inventory.dat");
} // end try
catch ( SecurityException securityException ) //catches for write access and exits program
{
JOptionPane.showMessageDialog(null, "Access Denied.","Contact your manager", JOptionPane.ERROR_MESSAGE);
System.exit( 1 );
} // end catch
catch ( FileNotFoundException filesNotFoundException ) //catches for write access and exits program
{
JOptionPane.showMessageDialog(null, "You need to create a file on your C Drive","See manager for help", JOptionPane.ERROR_MESSAGE);
System.exit( 1 );
} // end catch
JOptionPane.showMessageDialog(null, "Save successfully.","Have a Nice Day", JOptionPane.PLAIN_MESSAGE);
} // end method openFile
public void addRecords() // adds the records to the file
{
FeeQtyProduct record = (FeeQtyProduct) myCdInv.getFeeQtyProduct(index);
// rounds the output numbers to 2 decimals
BigDecimal roundPrice = new BigDecimal(Double.toString(record.restock() ));
roundPrice = roundPrice.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundValue= new BigDecimal(Double.toString(record.total()));
roundValue= roundValue.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundTotal= new BigDecimal(Double.toString(myCdInv.value()));
roundTotal = roundTotal.setScale(2, RoundingMode.HALF_UP);
if (record != null) //catches for blank record
{
output.format( "Video:%s Product#: %s QTY#:%.0f Price: $%.2f%s: %s" ,
record.getProductName(), record.getProductNumber(), record.getBaseAmount(), record.getBasePrice() ,
" Rating", record.getRatingTitle() + " with restock Fee $" + (roundPrice) + " value: $" + (roundValue) + " Inventory Total $" + (roundTotal) + "\t\n END OF LINE\t\t");
} // end if
} // end addRecords
public void closeFile() // close the file
{
if ( output != null )
output.close();
} // end method closeFile
}// end InventoryStorage class
// display GUI
public void repaintGUI()
{
FeeQtyProduct temp = (FeeQtyProduct) myCdInv.getFeeQtyProduct(index);
if(temp != null) //catches for no entries
{
itemNumberText.setText("" + temp.getProductNumber());
videoText.setText(temp.getProductName());
ratingText.setText(String.format("%s", temp.getRatingTitle()));
priceText.setText(String.format("%.2f", temp.getBasePrice()));
restockFeeText.setText(String.format("$%.2f", temp.restock()));
qtyText.setText(String.format("%.0f",temp.getBaseAmount()));
valueText.setText(String.format("$%.2f", temp.total()));
}// end if
totalValueText.setText(String.format("$%.2f", myCdInv.value()));
} // end repaintGUI
} //End InventoryGUI class