By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Java Inventory Program Problem!!!Please HELP!

P: 2
I cannot figure out how to add the following:
Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item.
Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
Use exception handling to create the directory and file if necessary.
Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product¿s information in the GUI.

Any help would be greatly appriciated I have been working on this for 8 hours!
Here is my code:
import java.util.Arrays;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JLabel;

class logo extends JPanel
{
public logo()
{
super(new GridLayout(1,1));
JLabel label1;
ImageIcon icon = new ImageIcon("C:/cdlogo.jpg");
label1 = new JLabel(icon);
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);
add(label1);//add label
}
}//end class logo

public class Inventory5 {


public static void main(String[] args) {

Album cd = null;
Inventory inventory = new Inventory();

cd = new Album(1, "Songs About Jane", 3, 14.99f, 2003);
System.out.println(cd);
inventory.addAlbum(cd);

cd = new Album(2, "30 Seconds to Mars", 5, 10.99f, 2005);
System.out.println(cd);
inventory.addAlbum(cd);

cd = new Album(3, "Mars Volta", 2, 13.95f, 1995);
System.out.println(cd);
inventory.addAlbum(cd);

cd = new Album(4, "The Mamas and The Papas", 9, 18.99f, 1970);
inventory.addAlbum(cd);
System.out.println(cd);

cd = new Album(5, "Idol Greatest Hits", 7, 15.98f, 2006);
System.out.println(cd);
inventory.addAlbum(cd);

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

} // end Inventory5


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

CD(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;
}

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

} // end CD


class Inventory {
// Array of CDs (set it to hold 25 items)
private final int INVENTORY_SIZE = 25;
private CD[] items;
private int numItems;

Inventory() {
items = new Album[INVENTORY_SIZE];
numItems = 0;
}

public int getNumItems() {
return numItems;
}

public CD getCD(int n) {
return items[n];
}


public void addAlbum(CD 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("\nAmanda’s DVD Inventory\n");

if (numItems <= 0) {
System.out.println("Inventory is empty at the moment.\n");
} 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



class Album extends CD {

private int albumYear;

public Album(int AlbumID, String itemName, int quantityOnHand, float itemPrice, int year) {
super(AlbumID, itemName, quantityOnHand, itemPrice);

this.albumYear = albumYear;
}


public void setYear(int year) {
albumYear = year;
}

public int getAlbumYear() {
return albumYear;
}

public float value() {
return super.value() + restockingFee();
}

public float restockingFee() {
return super.value() * 0.05f;
}

} // end Album


// GUI for inventory
class InventoryGUI extends JFrame
{
// access CD Collection inventory
private Inventory theInventory;

private int index = 0;


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 restockingFeeLabel = new JLabel(" Restocking Fee:");
private JTextField restockingFeeText;

private final JLabel totalValueLabel = new JLabel(" Inventory Total Value:");
private JTextField totalValueText;

private JPanel centerPanel;
private JPanel buttonPanel;


InventoryGUI(Inventory inventory) {
super("Amanda’s Movie Inventory");
final Dimension dim = new Dimension(140, 20);
final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
JPanel jp;

theInventory = inventory;



centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

buttonPanel = new JPanel();

JButton firstButton = new JButton("First");
firstButton.addActionListener(new FirstButtonHandler());
buttonPanel.add(firstButton);

JButton previousButton = new JButton("Previous");
previousButton.addActionListener(new PreviousButtonHandler());
buttonPanel.add(previousButton);

JButton nextButton = new JButton("Next");
nextButton.addActionListener(new NextButtonHandler());
buttonPanel.add(nextButton);

JButton lastButton = new JButton("Last");
lastButton.addActionListener(new LastButtonHandler());
buttonPanel.add(lastButton);

JButton addButton = new JButton("Add");
addButton.addActionListener(new AddButtonHandler());
buttonPanel.add(addButton);

JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new DeleteButtonHandler());
buttonPanel.add(deleteButton);

JButton modifyButton = new JButton("Modify");
modifyButton.addActionListener(new ModifyButtonHandler());
buttonPanel.add(modifyButton);

JButton saveButton = new JButton("Save");
saveButton.addActionListener(new SaveButtonHandler());
buttonPanel.add(saveButton);

JButton searchButton = new JButton("Search");
searchButton.addActionListener(new SearchButtonHandler());
buttonPanel.add(searchButton);


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);
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);
restockingFeeLabel.setPreferredSize(dim);
jp.add(restockingFeeLabel);
restockingFeeText = new JTextField(17);
restockingFeeText.setEditable(false);
jp.add(restockingFeeText);
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);

jp = new JPanel(flo);
totalValueLabel.setPreferredSize(dim);
jp.add(totalValueLabel);
totalValueText = new JTextField(17);
totalValueText.setEditable(false);
jp.add(totalValueText);
centerPanel.add(jp);

// adds panel to display
setContentPane(centerPanel);

repaintGUI();

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(420, 480);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}

public void repaintGUI() {
Album temp = (Album) theInventory.getCD(index);

if (temp != null) {
itemNumberText.setText("" + temp.getItemNo());
prodnameText.setText(temp.getTitle());
prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
restockingFeeText.setText(String.format("$%.2f", temp.restockingFee()));
numinstockText.setText("" + temp.getInStock());
valueText.setText(String.format("$%.2f", temp.value()));
}
totalValueText.setText(String.format("$%.2f", theInventory.value()));
}

class FirstButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
index = 0;
repaintGUI();
}
}

class PreviousButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
int numItems = theInventory.getNumItems();
index--;
if (index < 0)
index = numItems - 1;
repaintGUI();
}
}

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

}
}

class AddButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
int numItems = theInventory.getNumItems();
index--;
if (index > 5)
index = numItems + 6;
repaintGUI();
}
}


class SaveButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

class ModifyButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

class DeleteButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

class SearchButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

} // End InventoryGUI class

I am sorry this is my first time posting a question so if i did it wrong please let me know. I just need some examples to help me start!
Feb 4 '08 #1
Share this Question
Share on Google+
2 Replies


P: 2
Nevermind I finally got it!!
Feb 4 '08 #2

P: 3
I dont get it - can you explain?
Mar 6 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.